1 | /** |
2 | * \file |
3 | * |
4 | * \brief Class \c kate::CloseConfirmDialog (implementation) |
5 | * |
6 | * Copyright (C) 2012 Alex Turbov <i.zaufi@gmail.com> |
7 | * |
8 | * \date Sun Jun 24 16:29:13 MSK 2012 -- Initial design |
9 | */ |
10 | /* |
11 | * KateCloseExceptPlugin is free software: you can redistribute it and/or modify it |
12 | * under the terms of the GNU General Public License as published by the |
13 | * Free Software Foundation, either version 3 of the License, or |
14 | * (at your option) any later version. |
15 | * |
16 | * KateCloseExceptPlugin is distributed in the hope that it will be useful, but |
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | * See the GNU General Public License for more details. |
20 | * |
21 | * You should have received a copy of the GNU General Public License along |
22 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
23 | */ |
24 | |
25 | // Project specific includes |
26 | #include "close_confirm_dialog.h" |
27 | |
28 | // Standard includes |
29 | #include <KConfig> |
30 | #include <KLocalizedString> /// \todo Where is \c i18n() defiend? |
31 | #include <KVBox> |
32 | #include <QtGui/QLabel> |
33 | #include <QtGui/QHeaderView> |
34 | #include <cassert> |
35 | |
36 | namespace kate { namespace { |
37 | |
38 | class KateDocItem : public QTreeWidgetItem |
39 | { |
40 | public: |
41 | KateDocItem(KTextEditor::Document* doc, QTreeWidget* tw) |
42 | : QTreeWidgetItem(tw) |
43 | , document(doc) |
44 | { |
45 | setText(0, doc->documentName()); |
46 | setText(1, doc->url().prettyUrl()); |
47 | setCheckState(0, Qt::Checked); |
48 | } |
49 | KTextEditor::Document* document; |
50 | }; |
51 | } // anonymous namespace |
52 | |
53 | CloseConfirmDialog::CloseConfirmDialog( |
54 | QList<KTextEditor::Document*>& docs |
55 | , KToggleAction* show_confirmation_action |
56 | , QWidget* const parent |
57 | ) |
58 | : KDialog(parent) |
59 | , m_docs(docs) |
60 | { |
61 | assert("Documents container expected to be non empty" && !docs.isEmpty()); |
62 | |
63 | setCaption(i18nc("@title:window" , "Close files confirmation" )); |
64 | setButtons(Ok | Cancel); |
65 | setModal(true); |
66 | setDefaultButton(KDialog::Ok); |
67 | |
68 | KVBox* w = new KVBox(this); |
69 | setMainWidget(w); |
70 | w->setSpacing(KDialog::spacingHint()); |
71 | |
72 | KHBox* lo1 = new KHBox(w); |
73 | |
74 | // dialog text |
75 | QLabel* icon = new QLabel(lo1); |
76 | icon->setPixmap(DesktopIcon("dialog-warning" )); |
77 | |
78 | QLabel* t = new QLabel( |
79 | i18nc("@label:listbox" , "You are about to close the following documents:" ) |
80 | , lo1 |
81 | ); |
82 | lo1->setStretchFactor(t, 1000); |
83 | |
84 | // document list |
85 | m_docs_tree = new QTreeWidget(w); |
86 | QStringList ; |
87 | headers << i18nc("@title:column" , "Document" ) << i18nc("@title:column" , "Location" ); |
88 | m_docs_tree->setHeaderLabels(headers); |
89 | m_docs_tree->setSelectionMode(QAbstractItemView::SingleSelection); |
90 | m_docs_tree->setRootIsDecorated(false); |
91 | |
92 | for (int i = 0; i < m_docs.size(); i++) |
93 | { |
94 | new KateDocItem(m_docs[i], m_docs_tree); |
95 | } |
96 | m_docs_tree->header()->setStretchLastSection(false); |
97 | m_docs_tree->header()->setResizeMode(0, QHeaderView::ResizeToContents); |
98 | m_docs_tree->header()->setResizeMode(1, QHeaderView::ResizeToContents); |
99 | |
100 | m_dont_ask_again = new QCheckBox(i18nc("option:check" , "Do not ask again" ), w); |
101 | // NOTE If we are here, it means that 'Show Confirmation' action is enabled, |
102 | // so not needed to read config... |
103 | assert("Sanity check" && show_confirmation_action->isChecked()); |
104 | m_dont_ask_again->setCheckState(Qt::Unchecked); |
105 | connect(m_dont_ask_again, SIGNAL(toggled(bool)), show_confirmation_action, SLOT(toggle())); |
106 | |
107 | // Update documents list according checkboxes |
108 | connect(this, SIGNAL(accepted()), this, SLOT(updateDocsList())); |
109 | |
110 | KConfigGroup gcg(KGlobal::config(), "CloseConfirmationDialog" ); |
111 | restoreDialogSize(gcg); // restore dialog geometry from config |
112 | } |
113 | |
114 | CloseConfirmDialog::~CloseConfirmDialog() |
115 | { |
116 | KConfigGroup gcg(KGlobal::config(), "CloseConfirmationDialog" ); |
117 | saveDialogSize(gcg); // write dialog geometry to config |
118 | gcg.sync(); |
119 | } |
120 | |
121 | /** |
122 | * Going to remove unchecked files from the given documents list |
123 | */ |
124 | void CloseConfirmDialog::updateDocsList() |
125 | { |
126 | for ( |
127 | QTreeWidgetItemIterator it(m_docs_tree, QTreeWidgetItemIterator::NotChecked) |
128 | ; *it |
129 | ; ++it |
130 | ) |
131 | { |
132 | KateDocItem* item = static_cast<KateDocItem*>(*it); |
133 | m_docs.removeAll(item->document); |
134 | kDebug() << "do not close the file " << item->document->url().prettyUrl(); |
135 | } |
136 | } |
137 | |
138 | } // namespace kate |
139 | |