File indexing completed on 2022-09-27 16:29:17

0001 /**
0002  * SPDX-FileCopyrightText: (C) 2003 Sébastien Laoût <slaout@linux62.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "mainwindow.h"
0008 
0009 #include <QAction>
0010 #include <QApplication>
0011 #include <QDesktopWidget>
0012 #include <QLocale>
0013 #include <QStatusBar>
0014 #include <QtGui/QMoveEvent>
0015 #include <QtGui/QResizeEvent>
0016 
0017 #include <KAboutData>
0018 #include <KActionCollection>
0019 #include <KConfig>
0020 #include <KConfigGroup>
0021 #include <KEditToolBar>
0022 #include <KLocalizedString>
0023 #include <KMessageBox>
0024 #include <KShortcutsDialog>
0025 #include <KToggleAction>
0026 
0027 #include "basketstatusbar.h"
0028 #include "bnpview.h"
0029 #include "global.h"
0030 #include "settings.h"
0031 
0032 /** Container */
0033 
0034 MainWindow::MainWindow(QWidget *parent)
0035     : KXmlGuiWindow(parent)
0036     , m_settings(nullptr)
0037     , m_quit(false)
0038 {
0039     BasketStatusBar *bar = new BasketStatusBar(statusBar());
0040     m_baskets = new BNPView(this, "BNPViewApp", this, actionCollection(), bar);
0041     setCentralWidget(m_baskets);
0042 
0043     setupActions();
0044     statusBar()->show();
0045     statusBar()->setSizeGripEnabled(true);
0046 
0047     setAutoSaveSettings(/*groupName=*/QString::fromLatin1("MainWindow"), /*saveWindowSize=*//*FIXME:false:Why was it false??*/ true);
0048 
0049     //  m_actShowToolbar->setChecked(   toolBar()->isVisible()   );
0050     m_actShowStatusbar->setChecked(statusBar()->isVisible());
0051     connect(m_baskets, &BNPView::setWindowCaption, this, &MainWindow::setWindowTitle);
0052 
0053     //  InlineEditors::instance()->richTextToolBar();
0054     setStandardToolBarMenuEnabled(true);
0055 
0056     createGUI("basketui.rc");
0057     KConfigGroup group = KSharedConfig::openConfig()->group(autoSaveGroup());
0058     applyMainWindowSettings(group);
0059 }
0060 
0061 MainWindow::~MainWindow()
0062 {
0063     KConfigGroup group = KSharedConfig::openConfig()->group(autoSaveGroup());
0064     saveMainWindowSettings(group);
0065     delete m_settings;
0066     delete m_baskets;
0067 }
0068 
0069 void MainWindow::setupActions()
0070 {
0071     actQuit = KStandardAction::quit(this, SLOT(quit()), actionCollection());
0072     QAction *a = nullptr;
0073     a = actionCollection()->addAction("minimizeRestore", this, SLOT(minimizeRestore()));
0074     a->setText(i18n("Minimize"));
0075     a->setIcon(QIcon::fromTheme(QString()));
0076     a->setShortcut(0);
0077 
0078     /** Settings : ************************************************************/
0079     //  m_actShowToolbar   = KStandardAction::showToolbar(   this, SLOT(toggleToolBar()),   actionCollection());
0080     m_actShowStatusbar = KStandardAction::showStatusbar(this, SLOT(toggleStatusBar()), actionCollection());
0081 
0082     //  m_actShowToolbar->setCheckedState( KGuiItem(i18n("Hide &Toolbar")) );
0083 
0084     (void)KStandardAction::keyBindings(this, SLOT(showShortcutsSettingsDialog()), actionCollection());
0085 
0086     (void)KStandardAction::configureToolbars(this, SLOT(configureToolbars()), actionCollection());
0087 
0088     // QAction *actCfgNotifs = KStandardAction::configureNotifications(this, SLOT(configureNotifications()), actionCollection() );
0089     // actCfgNotifs->setEnabled(false); // Not yet implemented !
0090 
0091     actAppConfig = KStandardAction::preferences(this, SLOT(showSettingsDialog()), actionCollection());
0092 }
0093 
0094 /*void MainWindow::toggleToolBar()
0095 {
0096     if (toolBar()->isVisible())
0097         toolBar()->hide();
0098     else
0099         toolBar()->show();
0100 
0101     saveMainWindowSettings( KSharedConfig::openConfig(), autoSaveGroup() );
0102 }*/
0103 
0104 void MainWindow::toggleStatusBar()
0105 {
0106     if (statusBar()->isVisible())
0107         statusBar()->hide();
0108     else
0109         statusBar()->show();
0110 
0111     KConfigGroup group = KSharedConfig::openConfig()->group(autoSaveGroup());
0112     saveMainWindowSettings(group);
0113 }
0114 
0115 void MainWindow::configureToolbars()
0116 {
0117     KConfigGroup group = KSharedConfig::openConfig()->group(autoSaveGroup());
0118     saveMainWindowSettings(group);
0119 
0120     KEditToolBar dlg(actionCollection());
0121     connect(&dlg, &KEditToolBar::newToolBarConfig, this, &MainWindow::slotNewToolbarConfig);
0122     dlg.exec();
0123 }
0124 
0125 void MainWindow::configureNotifications()
0126 {
0127     // TODO
0128     // KNotifyDialog *dialog = new KNotifyDialog(this, "KNotifyDialog", false);
0129     // dialog->show();
0130 }
0131 
0132 void MainWindow::slotNewToolbarConfig() // This is called when OK or Apply is clicked
0133 {
0134     // ...if you use any action list, use plugActionList on each here...
0135     createGUI("basketui.rc"); // TODO: Reconnect tags menu aboutToShow() ??
0136     // TODO: Does this do anything?
0137     plugActionList(QString::fromLatin1("go_baskets_list"), actBasketsList);
0138     KConfigGroup group = KSharedConfig::openConfig()->group(autoSaveGroup());
0139     applyMainWindowSettings(group);
0140 }
0141 
0142 void MainWindow::showSettingsDialog()
0143 {
0144     if (!m_settings)
0145         m_settings = new SettingsDialog (qApp->activeWindow());
0146 
0147     if (Global::activeMainWindow()) {
0148         m_settings->exec();
0149         return;
0150     }
0151 
0152     m_settings->show();
0153 }
0154 
0155 void MainWindow::showShortcutsSettingsDialog()
0156 {
0157     KShortcutsDialog::configure(actionCollection());
0158     //.setWindowTitle(..)
0159     // actionCollection()->writeSettings();
0160 }
0161 
0162 void MainWindow::ensurePolished()
0163 {
0164     bool shouldSave = false;
0165 
0166     // If position and size has never been set, set nice ones:
0167     //  - Set size to sizeHint()
0168     //  - Keep the window manager placing the window where it want and save this
0169     if (Settings::mainWindowSize().isEmpty()) {
0170         //      qDebug() << "Main Window Position: Initial Set in show()";
0171         int defaultWidth = qApp->desktop()->width() * 5 / 6;
0172         int defaultHeight = qApp->desktop()->height() * 5 / 6;
0173         resize(defaultWidth, defaultHeight); // sizeHint() is bad (too small) and we want the user to have a good default area size
0174         shouldSave = true;
0175     } else {
0176         //      qDebug() << "Main Window Position: Recall in show(x="
0177         //                << Settings::mainWindowPosition().x() << ", y=" << Settings::mainWindowPosition().y()
0178         //                << ", width=" << Settings::mainWindowSize().width() << ", height=" << Settings::mainWindowSize().height()
0179         //                << ")";
0180         // move(Settings::mainWindowPosition());
0181         // resize(Settings::mainWindowSize());
0182     }
0183 
0184     KXmlGuiWindow::ensurePolished();
0185 
0186     if (shouldSave) {
0187         //      qDebug() << "Main Window Position: Save size and position in show(x="
0188         //                << pos().x() << ", y=" << pos().y()
0189         //                << ", width=" << size().width() << ", height=" << size().height()
0190         //                << ")";
0191         Settings::setMainWindowPosition(pos());
0192         Settings::setMainWindowSize(size());
0193         Settings::saveConfig();
0194     }
0195 }
0196 
0197 void MainWindow::resizeEvent(QResizeEvent *event)
0198 {
0199     //  qDebug() << "Main Window Position: Save size in resizeEvent(width=" << size().width() << ", height=" << size().height() << ") ; isMaximized="
0200     //            << (isMaximized() ? "true" : "false");
0201     Settings::setMainWindowSize(size());
0202     Settings::saveConfig();
0203 
0204     // Added to make it work (previous lines do not work):
0205     // saveMainWindowSettings( KSharedConfig::openConfig(), autoSaveGroup() );
0206     KXmlGuiWindow::resizeEvent(event);
0207 }
0208 
0209 void MainWindow::moveEvent(QMoveEvent *event)
0210 {
0211     //  qDebug() << "Main Window Position: Save position in moveEvent(x=" << pos().x() << ", y=" << pos().y() << ")";
0212     Settings::setMainWindowPosition(pos());
0213     Settings::saveConfig();
0214 
0215     // Added to make it work (previous lines do not work):
0216     // saveMainWindowSettings( KSharedConfig::openConfig(), autoSaveGroup() );
0217     KXmlGuiWindow::moveEvent(event);
0218 }
0219 
0220 bool MainWindow::queryExit()
0221 {
0222     hide();
0223     return true;
0224 }
0225 
0226 void MainWindow::quit()
0227 {
0228     m_quit = true;
0229     close();
0230 }
0231 
0232 bool MainWindow::queryClose()
0233 {
0234     /*  if (m_shuttingDown) // Set in askForQuit(): we don't have to ask again
0235         return true;*/
0236 
0237     if (qApp->isSavingSession()) {
0238         Settings::saveConfig();
0239         return true;
0240     }
0241 
0242     return askForQuit();
0243 }
0244 
0245 bool MainWindow::askForQuit()
0246 {
0247     QString message = i18n("<p>Do you really want to quit %1?</p>", QGuiApplication::applicationDisplayName());
0248 
0249     int really = KMessageBox::warningContinueCancel(this, message, i18n("Quit Confirm"), KStandardGuiItem::quit(), KStandardGuiItem::cancel(), "confirmQuitAsking");
0250 
0251     if (really == KMessageBox::Cancel) {
0252         m_quit = false;
0253         return false;
0254     }
0255 
0256     return true;
0257 }
0258 
0259 void MainWindow::minimizeRestore()
0260 {
0261     if (this->windowState() != Qt::WindowMinimized) {
0262         this->setWindowState(Qt::WindowMinimized);
0263     } else {
0264         this->setWindowState(Qt::WindowActive);
0265     }
0266 }