Better label "Do not save"->"Discard". (Matches Qt dialog on closing a document.)
[supercollider.git] / editors / sc-ide / widgets / documents_dialog.cpp
blobd02e4c859d83524188289ceb9241a467646d4dae
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "documents_dialog.hpp"
22 #include "main_window.hpp"
23 #include "../core/main.hpp"
25 #include <QVBoxLayout>
26 #include <QHBoxLayout>
27 #include <QDialogButtonBox>
28 #include <QPushButton>
29 #include <QHeaderView>
30 #include <QDir>
31 #include <QApplication>
33 namespace ScIDE {
35 DocumentsDialog::DocumentsDialog( Mode mode, QWidget * parent ):
36 QDialog(parent)
38 init(mode);
41 DocumentsDialog::DocumentsDialog(const QList<Document*> & docs, Mode mode, QWidget *parent):
42 QDialog(parent)
44 init(mode, docs);
47 void DocumentsDialog::init( Mode mode, const QList<Document*> &docs )
49 DocumentManager *mng = Main::documentManager();
50 connect(mng, SIGNAL(changedExternally(Document*)), this, SLOT(onDocumentChangedExternally(Document*)));
52 mMode = mode;
54 mLabel = new QLabel;
55 mLabel->setWordWrap(true);
57 mDocTree = new QTreeWidget;
58 mDocTree->setRootIsDecorated(false);
60 QStringList headerLabels;
61 headerLabels << tr("Document") << tr("Path", "File path.");
62 if (mode == ExternalChange)
63 headerLabels << tr("Status");
64 mDocTree->setHeaderLabels(headerLabels);
66 mDocTree->header()->setStretchLastSection(false);
67 mDocTree->header()->setResizeMode(1, QHeaderView::Stretch);
68 if (mode == ExternalChange)
69 mDocTree->header()->setResizeMode(2, QHeaderView::ResizeToContents);
71 switch (mode)
73 case ExternalChange:
74 setWindowTitle(tr("Externally Changed Documents"));
75 mLabel->setText(tr("The following documents have changed externally.\n\n"
76 "Apply the desired actions to selected documents, until the list is empty."));
77 break;
78 case Quit:
79 setWindowTitle(tr("Unsaved Documents"));
80 mLabel->setText(tr("The following documents have unsaved changes.\n\n"
81 "Apply desired actions to selected documents, until the list is empty."));
82 break;
85 foreach(Document *doc, docs)
86 addDocument(doc);
88 QDialogButtonBox *dialogBtnBox = new QDialogButtonBox();
89 QPushButton *btn;
90 QPushButton *defaultBtn;
91 if (mode == ExternalChange) {
92 defaultBtn = btn = dialogBtnBox->addButton(tr("&Reload"), QDialogButtonBox::ActionRole);
93 btn->setIcon( QIcon::fromTheme("view-refresh") );
94 connect(btn, SIGNAL(clicked()), this, SLOT(reloadSelected()));
96 btn = dialogBtnBox->addButton(tr("Over&write"), QDialogButtonBox::ActionRole);
97 btn->setIcon( QIcon::fromTheme("document-save") );
98 connect(btn, SIGNAL(clicked()), this, SLOT(saveSelected()));
100 btn = dialogBtnBox->addButton(tr("&Ignore"), QDialogButtonBox::AcceptRole);
101 btn->setIcon( QIcon::fromTheme("window-close") );
102 connect(btn, SIGNAL(clicked()), this, SLOT(ignoreSelected()));
104 btn = dialogBtnBox->addButton(tr("&Close"), QDialogButtonBox::AcceptRole);
105 btn->setIcon( QIcon::fromTheme("window-close") );
106 connect(btn, SIGNAL(clicked()), this, SLOT(closeSelected()));
108 else {
109 defaultBtn = btn = dialogBtnBox->addButton(tr("&Save"), QDialogButtonBox::ActionRole);
110 btn->setIcon( QIcon::fromTheme("document-save") );
111 connect(btn, SIGNAL(clicked()), this, SLOT(saveSelected()));
113 btn = dialogBtnBox->addButton(tr("&Discard"), QDialogButtonBox::ActionRole);
114 btn->setIcon( QIcon::fromTheme("window-close") );
115 connect(btn, SIGNAL(clicked()), this, SLOT(ignoreSelected()));
117 btn = dialogBtnBox->addButton(tr("&Cancel"), QDialogButtonBox::RejectRole);
118 btn->setIcon( QIcon::fromTheme("window-close") );
119 connect(btn, SIGNAL(clicked()), this, SLOT(reject()));
122 QPushButton *selectAllBtn = new QPushButton(tr("Select &All"));
123 connect(selectAllBtn, SIGNAL(clicked()), this, SLOT(selectAll()));
125 QPushButton *selectNoneBtn = new QPushButton(tr("Select N&one"));
126 connect(selectNoneBtn, SIGNAL(clicked()), this, SLOT(selectNone()));
128 QLabel *iconLabel = new QLabel;
129 iconLabel->setPixmap( QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(48,48) );
130 iconLabel->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
132 QHBoxLayout *lblBox = new QHBoxLayout();
133 lblBox->addWidget( iconLabel );
134 lblBox->addWidget( mLabel );
136 QHBoxLayout *selectionBox = new QHBoxLayout();
137 selectionBox->addWidget(selectAllBtn);
138 selectionBox->addWidget(selectNoneBtn);
140 QVBoxLayout *vbox = new QVBoxLayout();
141 vbox->addLayout( lblBox );
142 vbox->addWidget( mDocTree );
143 vbox->addLayout( selectionBox );
144 vbox->addWidget( dialogBtnBox );
146 setLayout(vbox);
148 defaultBtn->setDefault(true);
149 defaultBtn->setFocus(Qt::OtherFocusReason);
151 resize(500,300);
154 void DocumentsDialog::addDocument( Document *doc )
156 Item *item = new Item( doc );
157 mDocTree->addTopLevelItem(item);
160 void DocumentsDialog::selectAll()
162 int c = count();
163 for (int i = 0; i < c; ++i)
164 item(i)->setChecked(true);
167 void DocumentsDialog::selectNone()
169 int c = count();
170 for (int i = 0; i < c; ++i)
171 item(i)->setChecked(false);
174 void DocumentsDialog::saveSelected()
176 int i = 0;
177 while (i < count())
179 Item *itm = item(i);
180 if ( itm->isChecked() )
182 if ( !MainWindow::save(itm->document()) )
183 return;
184 delete itm;
186 else
187 ++i;
189 if (!count())
190 accept();
193 void DocumentsDialog::reloadSelected()
195 DocumentManager *mng = Main::documentManager();
197 int i = 0;
198 while (i < count())
200 Item *itm = item(i);
201 if ( itm->isChecked() )
203 if ( !mng->reload(itm->document()) )
204 return;
205 delete itm;
207 else
208 ++i;
210 if (!count())
211 accept();
214 void DocumentsDialog::ignoreSelected()
216 int i = 0;
217 while (i < count())
219 Item *itm = item(i);
220 if ( itm->isChecked() )
221 delete itm;
222 else
223 ++i;
225 if (!count())
226 accept();
229 void DocumentsDialog::closeSelected()
231 DocumentManager *mng = Main::documentManager();
233 int i = 0;
234 while (i < count())
236 Item *itm = item(i);
237 if ( itm->isChecked() )
239 mng->close(itm->document());
240 delete itm;
242 else
243 ++i;
245 if (!count())
246 accept();
249 void DocumentsDialog::onDocumentChangedExternally( Document *doc )
251 int c = count();
252 for (int i = 0; i < c; ++i)
254 Item *itm = item(i);
255 if (itm->document() == doc) {
256 itm->update();
257 return;
261 addDocument(doc);
264 DocumentsDialog::Item::Item(Document *doc):
265 mDoc(doc)
267 update();
270 void DocumentsDialog::Item::update()
272 setTitle(mDoc->title());
273 setPath(mDoc->filePath());
274 if (!mDoc->filePath().isEmpty() && !QFile::exists(mDoc->filePath())) {
275 setStatus("Removed");
276 setChecked(false);
278 else {
279 setStatus("Modified");
280 setChecked(true);