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>
31 #include <QApplication>
35 DocumentsDialog::DocumentsDialog( Mode mode
, QWidget
* parent
):
41 DocumentsDialog::DocumentsDialog(const QList
<Document
*> & docs
, Mode mode
, QWidget
*parent
):
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
*)));
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
);
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."));
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."));
85 foreach(Document
*doc
, docs
)
88 QDialogButtonBox
*dialogBtnBox
= new QDialogButtonBox();
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()));
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
);
148 defaultBtn
->setDefault(true);
149 defaultBtn
->setFocus(Qt::OtherFocusReason
);
154 void DocumentsDialog::addDocument( Document
*doc
)
156 Item
*item
= new Item( doc
);
157 mDocTree
->addTopLevelItem(item
);
160 void DocumentsDialog::selectAll()
163 for (int i
= 0; i
< c
; ++i
)
164 item(i
)->setChecked(true);
167 void DocumentsDialog::selectNone()
170 for (int i
= 0; i
< c
; ++i
)
171 item(i
)->setChecked(false);
174 void DocumentsDialog::saveSelected()
180 if ( itm
->isChecked() )
182 if ( !MainWindow::save(itm
->document()) )
193 void DocumentsDialog::reloadSelected()
195 DocumentManager
*mng
= Main::documentManager();
201 if ( itm
->isChecked() )
203 if ( !mng
->reload(itm
->document()) )
214 void DocumentsDialog::ignoreSelected()
220 if ( itm
->isChecked() )
229 void DocumentsDialog::closeSelected()
231 DocumentManager
*mng
= Main::documentManager();
237 if ( itm
->isChecked() )
239 mng
->close(itm
->document());
249 void DocumentsDialog::onDocumentChangedExternally( Document
*doc
)
252 for (int i
= 0; i
< c
; ++i
)
255 if (itm
->document() == doc
) {
264 DocumentsDialog::Item::Item(Document
*doc
):
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");
279 setStatus("Modified");