File indexing completed on 2024-04-28 05:48:19

0001 /**
0002  * \file
0003  *
0004  * \brief Class \c kate::CloseConfirmDialog (implementation)
0005  *
0006  * SPDX-FileCopyrightText: 2012 Alex Turbov <i.zaufi@gmail.com>
0007  *
0008  * \date Sun Jun 24 16:29:13 MSK 2012 -- Initial design
0009  */
0010 /*
0011  * KateCloseExceptPlugin is free software: you can redistribute it and/or modify it
0012  * under the terms of the GNU General Public License as published by the
0013  * Free Software Foundation, either version 3 of the License, or
0014  * (at your option) any later version.
0015  *
0016  * KateCloseExceptPlugin is distributed in the hope that it will be useful, but
0017  * WITHOUT ANY WARRANTY; without even the implied warranty of
0018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0019  * See the GNU General Public License for more details.
0020  *
0021  * You should have received a copy of the GNU General Public License along
0022  * with this program.  If not, see <http://www.gnu.org/licenses/>.
0023  */
0024 
0025 // Project specific includes
0026 #include "close_confirm_dialog.h"
0027 
0028 // Standard includes
0029 #include <KConfig>
0030 #include <KConfigGroup>
0031 #include <KLocalizedString> /// \todo Where is \c i18n() defined?
0032 #include <KSharedConfig>
0033 #include <KWindowConfig>
0034 #include <QHeaderView>
0035 #include <QPushButton>
0036 #include <QStyle>
0037 
0038 #include <cassert>
0039 
0040 namespace kate
0041 {
0042 namespace
0043 {
0044 class KateDocItem : public QTreeWidgetItem
0045 {
0046 public:
0047     KateDocItem(KTextEditor::Document *doc, QTreeWidget *tw)
0048         : QTreeWidgetItem(tw)
0049         , document(doc)
0050     {
0051         setText(0, doc->documentName());
0052         setText(1, doc->url().toString());
0053         setCheckState(0, Qt::Checked);
0054     }
0055     KTextEditor::Document *document;
0056 };
0057 } // anonymous namespace
0058 
0059 CloseConfirmDialog::CloseConfirmDialog(QList<KTextEditor::Document *> &docs, KToggleAction *show_confirmation_action, QWidget *const parent)
0060     : QDialog(parent)
0061     , m_docs(docs)
0062 {
0063     assert("Documents container expected to be non empty" && !docs.isEmpty());
0064     setupUi(this);
0065 
0066     setWindowTitle(i18nc("@title:window", "Close files confirmation"));
0067     setModal(true);
0068     buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
0069 
0070     icon->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-warning")).pixmap(style()->pixelMetric(QStyle::PM_LargeIconSize, nullptr, this)));
0071 
0072     text->setText(i18nc("@label:listbox", "You are about to close the following documents:"));
0073 
0074     QStringList headers;
0075     headers << i18nc("@title:column", "Document") << i18nc("@title:column", "Location");
0076     m_docs_tree->setHeaderLabels(headers);
0077     m_docs_tree->setSelectionMode(QAbstractItemView::SingleSelection);
0078     m_docs_tree->setRootIsDecorated(false);
0079 
0080     for (auto &doc : qAsConst(m_docs)) {
0081         new KateDocItem(doc, m_docs_tree);
0082     }
0083     m_docs_tree->header()->setStretchLastSection(false);
0084     m_docs_tree->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
0085     m_docs_tree->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
0086 
0087     m_dont_ask_again->setText(i18nc("option:check", "Do not ask again"));
0088     // NOTE If we are here, it means that 'Show Confirmation' action is enabled,
0089     // so not needed to read config...
0090     assert("Sanity check" && show_confirmation_action->isChecked());
0091     m_dont_ask_again->setCheckState(Qt::Unchecked);
0092     connect(m_dont_ask_again, &QCheckBox::toggled, show_confirmation_action, &KToggleAction::toggle);
0093 
0094     // Update documents list according checkboxes
0095     connect(this, &CloseConfirmDialog::accepted, this, &CloseConfirmDialog::updateDocsList);
0096 
0097     KConfigGroup gcg(KSharedConfig::openConfig(), QStringLiteral("kate-close-except-like-CloseConfirmationDialog"));
0098     KWindowConfig::restoreWindowSize(windowHandle(), gcg); // restore dialog geometry from config
0099 }
0100 
0101 CloseConfirmDialog::~CloseConfirmDialog()
0102 {
0103     KConfigGroup gcg(KSharedConfig::openConfig(), QStringLiteral("kate-close-except-like-CloseConfirmationDialog"));
0104     KWindowConfig::saveWindowSize(windowHandle(), gcg); // write dialog geometry to config
0105     gcg.sync();
0106 }
0107 
0108 /**
0109  * Going to remove unchecked files from the given documents list
0110  */
0111 void CloseConfirmDialog::updateDocsList()
0112 {
0113     for (QTreeWidgetItemIterator it(m_docs_tree, QTreeWidgetItemIterator::NotChecked); *it; ++it) {
0114         KateDocItem *item = static_cast<KateDocItem *>(*it);
0115         m_docs.removeAll(item->document);
0116         qDebug() << "do not close the file " << item->document->url().toString();
0117     }
0118 }
0119 
0120 } // namespace kate
0121 
0122 #include "moc_close_confirm_dialog.cpp"