File indexing completed on 2024-04-28 15:29:21

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
0004     SPDX-FileCopyrightText: 1999 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "mainwindow.h"
0010 
0011 #include "guiactivateevent.h"
0012 #include "part.h"
0013 #include "plugin.h"
0014 
0015 #include <KAboutData>
0016 #include <KActionCollection>
0017 #include <KConfigGroup>
0018 #include <KEditToolBar>
0019 #include <KHelpMenu>
0020 #include <KSharedConfig>
0021 #include <KXMLGUIFactory>
0022 
0023 #include <QAction>
0024 #include <QApplication>
0025 #include <QPointer>
0026 #include <QStatusBar>
0027 
0028 using namespace KParts;
0029 
0030 namespace KParts
0031 {
0032 class MainWindowPrivate
0033 {
0034 public:
0035     MainWindowPrivate()
0036         : m_activePart(nullptr)
0037     {
0038     }
0039     ~MainWindowPrivate()
0040     {
0041     }
0042 
0043     QPointer<Part> m_activePart;
0044     bool m_bShellGUIActivated = false;
0045     KHelpMenu *m_helpMenu = nullptr;
0046     bool m_manageWindowTitle = true;
0047 };
0048 }
0049 
0050 MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags f)
0051     : KXmlGuiWindow(parent, f)
0052     , d(new MainWindowPrivate())
0053 {
0054     PartBase::setPartObject(this);
0055 }
0056 
0057 MainWindow::~MainWindow() = default;
0058 
0059 void MainWindow::createGUI(Part *part)
0060 {
0061 #if 0
0062     // qDebug() << "part=" << part
0063             << (part ? part->metaObject()->className() : "")
0064             << (part ? part->objectName() : "");
0065 #endif
0066     KXMLGUIFactory *factory = guiFactory();
0067 
0068     Q_ASSERT(factory);
0069 
0070     if (d->m_activePart) {
0071 #if 0
0072         // qDebug() << "deactivating GUI for" << d->m_activePart
0073                 << d->m_activePart->metaObject()->className()
0074                 << d->m_activePart->objectName();
0075 #endif
0076 
0077         GUIActivateEvent ev(false);
0078         QApplication::sendEvent(d->m_activePart, &ev);
0079 
0080         factory->removeClient(d->m_activePart);
0081 
0082         disconnect(d->m_activePart.data(), &Part::setWindowCaption, this, static_cast<void (MainWindow::*)(const QString &)>(&MainWindow::setCaption));
0083         disconnect(d->m_activePart.data(), &Part::setStatusBarText, this, &MainWindow::slotSetStatusBarText);
0084     }
0085 
0086     if (!d->m_bShellGUIActivated) {
0087 #if KPARTS_BUILD_DEPRECATED_SINCE(5, 90)
0088         loadPlugins(this, this, KAboutData::applicationData().componentName());
0089 #endif
0090         createShellGUI();
0091         d->m_bShellGUIActivated = true;
0092     }
0093 
0094     if (part) {
0095         // do this before sending the activate event
0096         if (d->m_manageWindowTitle) {
0097             connect(part, &Part::setWindowCaption, this, static_cast<void (MainWindow::*)(const QString &)>(&MainWindow::setCaption));
0098         }
0099         connect(part, &Part::setStatusBarText, this, &MainWindow::slotSetStatusBarText);
0100 
0101         factory->addClient(part);
0102 
0103         GUIActivateEvent ev(true);
0104         QApplication::sendEvent(part, &ev);
0105     }
0106 
0107     d->m_activePart = part;
0108 }
0109 
0110 void MainWindow::slotSetStatusBarText(const QString &text)
0111 {
0112     statusBar()->showMessage(text);
0113 }
0114 
0115 void MainWindow::createShellGUI(bool create)
0116 {
0117     Q_ASSERT(d->m_bShellGUIActivated != create);
0118     d->m_bShellGUIActivated = create;
0119     if (create) {
0120         if (isHelpMenuEnabled() && !d->m_helpMenu) {
0121             d->m_helpMenu = new KHelpMenu(this, KAboutData::applicationData(), true);
0122 
0123             KActionCollection *actions = actionCollection();
0124             QAction *helpContentsAction = d->m_helpMenu->action(KHelpMenu::menuHelpContents);
0125             QAction *whatsThisAction = d->m_helpMenu->action(KHelpMenu::menuWhatsThis);
0126             QAction *reportBugAction = d->m_helpMenu->action(KHelpMenu::menuReportBug);
0127             QAction *switchLanguageAction = d->m_helpMenu->action(KHelpMenu::menuSwitchLanguage);
0128             QAction *aboutAppAction = d->m_helpMenu->action(KHelpMenu::menuAboutApp);
0129             QAction *aboutKdeAction = d->m_helpMenu->action(KHelpMenu::menuAboutKDE);
0130             QAction *donateAction = d->m_helpMenu->action(KHelpMenu::menuDonate);
0131 
0132             if (helpContentsAction) {
0133                 actions->addAction(helpContentsAction->objectName(), helpContentsAction);
0134             }
0135             if (whatsThisAction) {
0136                 actions->addAction(whatsThisAction->objectName(), whatsThisAction);
0137             }
0138             if (reportBugAction) {
0139                 actions->addAction(reportBugAction->objectName(), reportBugAction);
0140             }
0141             if (switchLanguageAction) {
0142                 actions->addAction(switchLanguageAction->objectName(), switchLanguageAction);
0143             }
0144             if (aboutAppAction) {
0145                 actions->addAction(aboutAppAction->objectName(), aboutAppAction);
0146             }
0147             if (aboutKdeAction) {
0148                 actions->addAction(aboutKdeAction->objectName(), aboutKdeAction);
0149             }
0150             if (donateAction) {
0151                 actions->addAction(donateAction->objectName(), donateAction);
0152             }
0153         }
0154 
0155         QString f = xmlFile();
0156         setXMLFile(KXMLGUIClient::standardsXmlFileLocation());
0157         if (!f.isEmpty()) {
0158             setXMLFile(f, true);
0159         } else {
0160             QString auto_file(componentName() + QLatin1String("ui.rc"));
0161             setXMLFile(auto_file, true);
0162         }
0163 
0164         GUIActivateEvent ev(true);
0165         QApplication::sendEvent(this, &ev);
0166 
0167         guiFactory()->addClient(this);
0168 
0169         checkAmbiguousShortcuts();
0170     } else {
0171         GUIActivateEvent ev(false);
0172         QApplication::sendEvent(this, &ev);
0173 
0174         guiFactory()->removeClient(this);
0175     }
0176 }
0177 
0178 void KParts::MainWindow::setWindowTitleHandling(bool enabled)
0179 {
0180     d->m_manageWindowTitle = enabled;
0181 }
0182 
0183 void KParts::MainWindow::saveNewToolbarConfig()
0184 {
0185     createGUI(d->m_activePart);
0186     KConfigGroup cg(KSharedConfig::openConfig(), QString());
0187     applyMainWindowSettings(cg);
0188 }
0189 
0190 void KParts::MainWindow::configureToolbars()
0191 {
0192     // No difference with base class anymore.
0193     KXmlGuiWindow::configureToolbars();
0194 }
0195 
0196 #include "moc_mainwindow.cpp"