File indexing completed on 2024-04-28 17:06:09

0001 /*
0002     SPDX-FileCopyrightText: 2000 Shie Erlich <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000 Rafi Yanai <krusader@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "konfigurator.h"
0010 #include "../Dialogs/krdialogs.h"
0011 #include "../icon.h"
0012 #include "../krglobal.h"
0013 
0014 // QtGui
0015 #include <QPixmap>
0016 #include <QResizeEvent>
0017 
0018 #include <KConfigCore/KSharedConfig>
0019 #include <KConfigWidgets/KHelpClient>
0020 #include <KI18n/KLocalizedString>
0021 #include <KWidgetsAddons/KMessageBox>
0022 
0023 #include "../GUI/kfnkeys.h"
0024 #include "../defaults.h"
0025 #include "../krusaderview.h"
0026 
0027 // the frames
0028 #include "kgadvanced.h"
0029 #include "kgarchives.h"
0030 #include "kgcolors.h"
0031 #include "kgdependencies.h"
0032 #include "kggeneral.h"
0033 #include "kgpanel.h"
0034 #include "kgprotocols.h"
0035 #include "kgstartup.h"
0036 #include "kguseractions.h"
0037 
0038 Konfigurator::Konfigurator(bool f, int startPage)
0039     : KPageDialog((QWidget *)nullptr)
0040     , firstTime(f)
0041     , internalCall(false)
0042     , sizeX(-1)
0043     , sizeY(-1)
0044 {
0045     setWindowTitle(i18n("Konfigurator - Creating Your Own Krusader"));
0046     setWindowModality(Qt::ApplicationModal);
0047     setFaceType(KPageDialog::List);
0048 
0049     setStandardButtons(QDialogButtonBox::Help | QDialogButtonBox::RestoreDefaults | QDialogButtonBox::Close | QDialogButtonBox::Apply
0050                        | QDialogButtonBox::Reset);
0051     button(QDialogButtonBox::Apply)->setDefault(true);
0052 
0053     connect(button(QDialogButtonBox::Close), &QPushButton::clicked, this, &Konfigurator::slotClose);
0054     connect(button(QDialogButtonBox::Help), &QPushButton::clicked, this, &Konfigurator::slotShowHelp);
0055     connect(button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &Konfigurator::slotRestoreDefaults);
0056     connect(button(QDialogButtonBox::Reset), &QPushButton::clicked, this, &Konfigurator::slotReset);
0057     connect(button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &Konfigurator::slotApply);
0058     connect(this, &Konfigurator::currentPageChanged, this, &Konfigurator::slotPageSwitch);
0059     connect(&restoreTimer, &QTimer::timeout, this, &Konfigurator::slotRestorePage);
0060 
0061     createLayout(startPage);
0062 
0063     KConfigGroup group(krConfig, "Konfigurator");
0064     int sx = group.readEntry("Window Width", -1);
0065     int sy = group.readEntry("Window Height", -1);
0066 
0067     if (sx != -1 && sy != -1)
0068         resize(sx, sy);
0069     else
0070         resize(900, 680);
0071 
0072     if (group.readEntry("Window Maximized", false))
0073         showMaximized();
0074     else
0075         show();
0076 }
0077 
0078 void Konfigurator::resizeEvent(QResizeEvent *e)
0079 {
0080     if (!isMaximized()) {
0081         sizeX = e->size().width();
0082         sizeY = e->size().height();
0083     }
0084     QDialog::resizeEvent(e);
0085 }
0086 
0087 void Konfigurator::closeDialog()
0088 {
0089     KConfigGroup group(krConfig, "Konfigurator");
0090 
0091     group.writeEntry("Window Width", sizeX);
0092     group.writeEntry("Window Height", sizeY);
0093     group.writeEntry("Window Maximized", isMaximized());
0094 }
0095 
0096 void Konfigurator::reject()
0097 {
0098     closeDialog();
0099     QDialog::reject();
0100 }
0101 
0102 void Konfigurator::newPage(KonfiguratorPage *page, const QString &name, const QString &desc, const QIcon &icon)
0103 {
0104     auto *item = new KPageWidgetItem(page, name);
0105     item->setIcon(icon);
0106     item->setHeader(desc);
0107     addPage(item);
0108 
0109     kgPages.append(item);
0110     connect(page, &KonfiguratorPage::sigChanged, this, &Konfigurator::slotApplyEnable);
0111 }
0112 
0113 void Konfigurator::createLayout(int startPage)
0114 {
0115     // startup
0116     newPage(new KgStartup(firstTime, this), i18n("Startup"), i18n("Krusader's settings upon startup"), Icon("go-home"));
0117     // panel
0118     newPage(new KgPanel(firstTime, this), i18n("Panel"), i18n("Panel"), Icon("view-choose"));
0119     // colors
0120     newPage(new KgColors(firstTime, this), i18n("Colors"), i18n("Colors"), Icon("color-picker"));
0121     // general
0122     newPage(new KgGeneral(firstTime, this), i18n("General"), i18n("Basic Operations"), Icon("system-run"));
0123     // advanced
0124     newPage(new KgAdvanced(firstTime, this), i18n("Advanced"), i18n("Be sure you know what you are doing."), Icon("dialog-messages"));
0125     // archives
0126     newPage(new KgArchives(firstTime, this), i18n("Archives"), i18n("Customize the way Krusader deals with archives"), Icon("archive-extract"));
0127     // dependencies
0128     newPage(new KgDependencies(firstTime, this), i18n("Dependencies"), i18n("Set the full path of the external applications"), Icon("debug-run"));
0129     // useractions
0130     newPage(new KgUserActions(firstTime, this), i18n("User Actions"), i18n("Configure your personal actions"), Icon("user-properties"));
0131     // protocols
0132     newPage(new KgProtocols(firstTime, this), i18n("Protocols"), i18n("Link MIMEs to protocols"), Icon("document-preview"));
0133 
0134     setCurrentPage(kgPages.at(startPage));
0135     slotApplyEnable();
0136 }
0137 
0138 void Konfigurator::closeEvent(QCloseEvent *event)
0139 {
0140     lastPage = currentPage();
0141     if (slotPageSwitch(lastPage, lastPage)) {
0142         hide();
0143         KPageDialog::closeEvent(event);
0144     } else
0145         event->ignore();
0146 }
0147 
0148 void Konfigurator::slotApplyEnable()
0149 {
0150     lastPage = currentPage();
0151     bool isChanged = (qobject_cast<KonfiguratorPage *>(lastPage->widget()))->isChanged();
0152     button(QDialogButtonBox::Apply)->setEnabled(isChanged);
0153     button(QDialogButtonBox::Reset)->setEnabled(isChanged);
0154 }
0155 
0156 bool Konfigurator::slotPageSwitch(KPageWidgetItem *current, KPageWidgetItem *before)
0157 {
0158     if (before == nullptr)
0159         return true;
0160 
0161     auto *currentPg = qobject_cast<KonfiguratorPage *>(before->widget());
0162 
0163     if (internalCall) {
0164         internalCall = false;
0165         return true;
0166     }
0167 
0168     if (currentPg->isChanged()) {
0169         int result = KMessageBox::questionYesNoCancel(nullptr,
0170                                                       i18n("The current page has been changed. Do you want to apply changes?"),
0171                                                       {},
0172                                                       KStandardGuiItem::apply(),
0173                                                       KStandardGuiItem::discard());
0174 
0175         switch (result) {
0176         case KMessageBox::No:
0177             currentPg->loadInitialValues();
0178             currentPg->apply();
0179             break;
0180         case KMessageBox::Yes:
0181             emit configChanged(currentPg->apply());
0182             break;
0183         default:
0184             restoreTimer.setSingleShot(true);
0185             restoreTimer.start(0);
0186             return false;
0187         }
0188     }
0189 
0190     button(QDialogButtonBox::Apply)->setEnabled(currentPg->isChanged());
0191     lastPage = current;
0192     return true;
0193 }
0194 
0195 void Konfigurator::slotRestorePage()
0196 {
0197     if (lastPage != currentPage()) {
0198         internalCall = true;
0199         setCurrentPage(lastPage);
0200     }
0201 }
0202 
0203 void Konfigurator::slotClose()
0204 {
0205     lastPage = currentPage();
0206     if (slotPageSwitch(lastPage, lastPage)) {
0207         reject();
0208     }
0209 }
0210 
0211 void Konfigurator::slotApply()
0212 {
0213     emit configChanged((qobject_cast<KonfiguratorPage *>(currentPage()->widget()))->apply());
0214 }
0215 
0216 void Konfigurator::slotReset()
0217 {
0218     (qobject_cast<KonfiguratorPage *>(currentPage()->widget()))->loadInitialValues();
0219 }
0220 
0221 void Konfigurator::slotRestoreDefaults()
0222 {
0223     (qobject_cast<KonfiguratorPage *>(currentPage()->widget()))->setDefaults();
0224 }
0225 
0226 void Konfigurator::slotShowHelp()
0227 {
0228     KHelpClient::invokeHelp(QStringLiteral("konfigurator"));
0229 }