1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
4 SPDX-FileCopyrightText: 1999 David Faure <faure@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "mainwindow.h"
10
11#include "guiactivateevent.h"
12#include "part.h"
13
14#include <KAboutData>
15#include <KActionCollection>
16#include <KConfigGroup>
17#include <KEditToolBar>
18#include <KHelpMenu>
19#include <KSharedConfig>
20#include <KXMLGUIFactory>
21
22#include <QAction>
23#include <QApplication>
24#include <QPointer>
25#include <QStatusBar>
26
27using namespace KParts;
28
29namespace KParts
30{
31class MainWindowPrivate
32{
33public:
34 MainWindowPrivate()
35 : m_activePart(nullptr)
36 {
37 }
38 ~MainWindowPrivate()
39 {
40 }
41
42 QPointer<Part> m_activePart;
43 bool m_bShellGUIActivated = false;
44 KHelpMenu *m_helpMenu = nullptr;
45 bool m_manageWindowTitle = true;
46};
47}
48
49MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags f)
50 : KXmlGuiWindow(parent, f)
51 , d(new MainWindowPrivate())
52{
53 PartBase::setPartObject(this);
54}
55
56MainWindow::~MainWindow() = default;
57
58void MainWindow::createGUI(Part *part)
59{
60#if 0
61 // qDebug() << "part=" << part
62 << (part ? part->metaObject()->className() : "")
63 << (part ? part->objectName() : "");
64#endif
65 KXMLGUIFactory *factory = guiFactory();
66
67 Q_ASSERT(factory);
68
69 if (d->m_activePart) {
70#if 0
71 // qDebug() << "deactivating GUI for" << d->m_activePart
72 << d->m_activePart->metaObject()->className()
73 << d->m_activePart->objectName();
74#endif
75
76 GUIActivateEvent ev(false);
77 QApplication::sendEvent(receiver: d->m_activePart, event: &ev);
78
79 factory->removeClient(client: d->m_activePart);
80
81 disconnect(sender: d->m_activePart.data(), signal: &Part::setWindowCaption, receiver: this, slot: static_cast<void (MainWindow::*)(const QString &)>(&MainWindow::setCaption));
82 disconnect(sender: d->m_activePart.data(), signal: &Part::setStatusBarText, receiver: this, slot: &MainWindow::slotSetStatusBarText);
83 }
84
85 if (!d->m_bShellGUIActivated) {
86 createShellGUI();
87 d->m_bShellGUIActivated = true;
88 }
89
90 if (part) {
91 // do this before sending the activate event
92 if (d->m_manageWindowTitle) {
93 connect(sender: part, signal: &Part::setWindowCaption, context: this, slot: static_cast<void (MainWindow::*)(const QString &)>(&MainWindow::setCaption));
94 }
95 connect(sender: part, signal: &Part::setStatusBarText, context: this, slot: &MainWindow::slotSetStatusBarText);
96
97 factory->addClient(client: part);
98
99 GUIActivateEvent ev(true);
100 QApplication::sendEvent(receiver: part, event: &ev);
101 }
102
103 d->m_activePart = part;
104}
105
106void MainWindow::slotSetStatusBarText(const QString &text)
107{
108 statusBar()->showMessage(text);
109}
110
111void MainWindow::createShellGUI(bool create)
112{
113 Q_ASSERT(d->m_bShellGUIActivated != create);
114 d->m_bShellGUIActivated = create;
115 if (create) {
116 if (isHelpMenuEnabled() && !d->m_helpMenu) {
117 d->m_helpMenu = new KHelpMenu(this, KAboutData::applicationData(), true);
118
119 KActionCollection *actions = actionCollection();
120 QAction *helpContentsAction = d->m_helpMenu->action(id: KHelpMenu::menuHelpContents);
121 QAction *whatsThisAction = d->m_helpMenu->action(id: KHelpMenu::menuWhatsThis);
122 QAction *reportBugAction = d->m_helpMenu->action(id: KHelpMenu::menuReportBug);
123 QAction *switchLanguageAction = d->m_helpMenu->action(id: KHelpMenu::menuSwitchLanguage);
124 QAction *aboutAppAction = d->m_helpMenu->action(id: KHelpMenu::menuAboutApp);
125 QAction *aboutKdeAction = d->m_helpMenu->action(id: KHelpMenu::menuAboutKDE);
126 QAction *donateAction = d->m_helpMenu->action(id: KHelpMenu::menuDonate);
127
128 if (helpContentsAction) {
129 actions->addAction(name: helpContentsAction->objectName(), action: helpContentsAction);
130 }
131 if (whatsThisAction) {
132 actions->addAction(name: whatsThisAction->objectName(), action: whatsThisAction);
133 }
134 if (reportBugAction) {
135 actions->addAction(name: reportBugAction->objectName(), action: reportBugAction);
136 }
137 if (switchLanguageAction) {
138 actions->addAction(name: switchLanguageAction->objectName(), action: switchLanguageAction);
139 }
140 if (aboutAppAction) {
141 actions->addAction(name: aboutAppAction->objectName(), action: aboutAppAction);
142 }
143 if (aboutKdeAction) {
144 actions->addAction(name: aboutKdeAction->objectName(), action: aboutKdeAction);
145 }
146 if (donateAction) {
147 actions->addAction(name: donateAction->objectName(), action: donateAction);
148 }
149 }
150
151 QString f = xmlFile();
152 setXMLFile(file: KXMLGUIClient::standardsXmlFileLocation());
153 if (!f.isEmpty()) {
154 setXMLFile(file: f, merge: true);
155 } else {
156 QString auto_file(componentName() + QLatin1String("ui.rc"));
157 setXMLFile(file: auto_file, merge: true);
158 }
159
160 GUIActivateEvent ev(true);
161 QApplication::sendEvent(receiver: this, event: &ev);
162
163 guiFactory()->addClient(client: this);
164
165 checkAmbiguousShortcuts();
166 } else {
167 GUIActivateEvent ev(false);
168 QApplication::sendEvent(receiver: this, event: &ev);
169
170 guiFactory()->removeClient(client: this);
171 }
172}
173
174void KParts::MainWindow::setWindowTitleHandling(bool enabled)
175{
176 d->m_manageWindowTitle = enabled;
177}
178
179void KParts::MainWindow::saveNewToolbarConfig()
180{
181 createGUI(part: d->m_activePart);
182 KConfigGroup cg(KSharedConfig::openConfig(), QString());
183 applyMainWindowSettings(config: cg);
184}
185
186void KParts::MainWindow::configureToolbars()
187{
188 // No difference with base class anymore.
189 KXmlGuiWindow::configureToolbars();
190}
191
192#include "moc_mainwindow.cpp"
193

source code of kparts/src/mainwindow.cpp