File indexing completed on 2024-05-12 07:51:58

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