File indexing completed on 2024-05-26 05:27:12

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "selectiontypedialog.h"
0008 #include "gui/widgets/selectiontypetreewidget.h"
0009 
0010 #include <KConfigGroup>
0011 #include <KLocalizedString>
0012 #include <KSharedConfig>
0013 #include <KWindowConfig>
0014 #include <QCheckBox>
0015 #include <QDialogButtonBox>
0016 #include <QHBoxLayout>
0017 #include <QPushButton>
0018 #include <QVBoxLayout>
0019 #include <QWindow>
0020 namespace
0021 {
0022 static const char myConfigSelectionTypeDialogDialog[] = "SelectionTypeDialog";
0023 }
0024 SelectionTypeDialog::SelectionTypeDialog(bool backupData, QWidget *parent)
0025     : QDialog(parent)
0026     , mSelectionTreeWidget(new SelectionTypeTreeWidget(backupData, this))
0027     , mUseTemplateByDefault(new QCheckBox(i18n("Use this template by default"), this))
0028     , mSaveTemplate(new QPushButton(i18n("Save as Template..."), this))
0029     , mLoadTemplate(new QPushButton(i18n("Load Template..."), this))
0030 {
0031     setWindowTitle(i18nc("@title:window", "Select Type"));
0032     setModal(true);
0033 
0034     auto topLayout = new QVBoxLayout(this);
0035 
0036     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0037     buttonBox->setObjectName(QLatin1StringView("buttonbox"));
0038 
0039     auto okButton = buttonBox->button(QDialogButtonBox::Ok);
0040     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0041     connect(buttonBox, &QDialogButtonBox::accepted, this, &SelectionTypeDialog::accept);
0042     connect(buttonBox, &QDialogButtonBox::rejected, this, &SelectionTypeDialog::reject);
0043     okButton->setDefault(true);
0044 
0045     mSelectionTreeWidget->setObjectName(QLatin1StringView("mSelectionTreeWidget"));
0046     topLayout->addWidget(mSelectionTreeWidget);
0047 
0048     mUseTemplateByDefault->setObjectName(QLatin1StringView("mUseTemplateByDefault"));
0049     topLayout->addWidget(mUseTemplateByDefault);
0050 
0051     auto hbox = new QHBoxLayout;
0052     auto selectAll = new QPushButton(i18n("Select All"), this);
0053     selectAll->setObjectName(QLatin1StringView("selectAll"));
0054     connect(selectAll, &QPushButton::clicked, this, &SelectionTypeDialog::slotSelectAll);
0055     hbox->addWidget(selectAll);
0056 
0057     auto unselectAll = new QPushButton(i18n("Unselect All"), this);
0058     unselectAll->setObjectName(QLatin1StringView("unselectAll"));
0059     connect(unselectAll, &QPushButton::clicked, this, &SelectionTypeDialog::slotUnselectAll);
0060     hbox->addWidget(unselectAll);
0061 
0062     mSaveTemplate->setObjectName(QLatin1StringView("mSaveTemplate"));
0063     connect(mSaveTemplate, &QPushButton::clicked, this, &SelectionTypeDialog::slotSaveAsTemplate);
0064     hbox->addWidget(mSaveTemplate);
0065 
0066     mLoadTemplate->setObjectName(QLatin1StringView("mLoadTemplate"));
0067     connect(mLoadTemplate, &QPushButton::clicked, this, &SelectionTypeDialog::slotLoadTemplate);
0068     hbox->addWidget(mLoadTemplate);
0069 
0070     topLayout->addLayout(hbox);
0071     topLayout->addWidget(buttonBox);
0072     readConfig();
0073 }
0074 
0075 SelectionTypeDialog::~SelectionTypeDialog()
0076 {
0077     saveDefaultTemplate();
0078     writeConfig();
0079 }
0080 
0081 void SelectionTypeDialog::readConfig()
0082 {
0083     create(); // ensure a window is created
0084     windowHandle()->resize(QSize(600, 400));
0085     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigSelectionTypeDialogDialog));
0086     KWindowConfig::restoreWindowSize(windowHandle(), group);
0087     resize(windowHandle()->size()); // workaround for QTBUG-40584
0088     loadDefaultTemplate();
0089 }
0090 
0091 void SelectionTypeDialog::writeConfig()
0092 {
0093     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigSelectionTypeDialogDialog));
0094     KWindowConfig::saveWindowSize(windowHandle(), group);
0095 }
0096 
0097 QMap<Utils::AppsType, Utils::importExportParameters> SelectionTypeDialog::storedType() const
0098 {
0099     return mSelectionTreeWidget->storedType();
0100 }
0101 
0102 QString SelectionTypeDialog::exportedFileInfo() const
0103 {
0104     return mSelectionTreeWidget->exportedFileInfo();
0105 }
0106 
0107 void SelectionTypeDialog::removeNotSelectedItems()
0108 {
0109     mSelectionTreeWidget->removeNotSelectedItems();
0110     mSaveTemplate->hide();
0111     mLoadTemplate->hide();
0112     mUseTemplateByDefault->hide();
0113 }
0114 
0115 void SelectionTypeDialog::loadTemplate(const QString &fileName)
0116 {
0117     if (!fileName.isEmpty()) {
0118         mSelectionTreeWidget->loadTemplate(fileName);
0119     }
0120 }
0121 
0122 void SelectionTypeDialog::slotSelectAll()
0123 {
0124     mSelectionTreeWidget->selectAllItems();
0125 }
0126 
0127 void SelectionTypeDialog::slotUnselectAll()
0128 {
0129     mSelectionTreeWidget->unSelectAllItems();
0130 }
0131 
0132 void SelectionTypeDialog::slotSaveAsTemplate()
0133 {
0134     mSelectionTreeWidget->saveAsTemplate();
0135 }
0136 
0137 void SelectionTypeDialog::slotLoadTemplate()
0138 {
0139     mSelectionTreeWidget->loadTemplate();
0140 }
0141 
0142 void SelectionTypeDialog::saveDefaultTemplate()
0143 {
0144     if (mUseTemplateByDefault->isChecked()) {
0145         mSelectionTreeWidget->saveAsDefaultTemplate();
0146     }
0147 }
0148 
0149 void SelectionTypeDialog::loadDefaultTemplate()
0150 {
0151     mSelectionTreeWidget->loadDefaultTemplate();
0152 }
0153 
0154 #include "moc_selectiontypedialog.cpp"