KParts

mainwindow.cpp
1 /*
2  This file is part of the KDE project
3  SPDX-FileCopyrightText: 1999 Simon Hausmann <[email protected]>
4  SPDX-FileCopyrightText: 1999 David Faure <[email protected]>
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 #include "plugin.h"
14 
15 #include <KAboutData>
16 #include <KActionCollection>
17 #include <KConfigGroup>
18 #include <KEditToolBar>
19 #include <KHelpMenu>
20 #include <KSharedConfig>
21 #include <KXMLGUIFactory>
22 
23 #include <QAction>
24 #include <QApplication>
25 #include <QPointer>
26 #include <QStatusBar>
27 
28 using namespace KParts;
29 
30 namespace KParts
31 {
32 class MainWindowPrivate
33 {
34 public:
35  MainWindowPrivate()
36  : m_activePart(nullptr)
37  {
38  }
39  ~MainWindowPrivate()
40  {
41  }
42 
43  QPointer<Part> m_activePart;
44  bool m_bShellGUIActivated = false;
45  KHelpMenu *m_helpMenu = nullptr;
46  bool m_manageWindowTitle = true;
47 };
48 }
49 
51  : KXmlGuiWindow(parent, f)
52  , d(new MainWindowPrivate())
53 {
55 }
56 
57 MainWindow::~MainWindow() = default;
58 
60 {
61 #if 0
62  // qDebug() << "part=" << part
63  << (part ? part->metaObject()->className() : "")
64  << (part ? part->objectName() : "");
65 #endif
66  KXMLGUIFactory *factory = guiFactory();
67 
68  Q_ASSERT(factory);
69 
70  if (d->m_activePart) {
71 #if 0
72  // qDebug() << "deactivating GUI for" << d->m_activePart
73  << d->m_activePart->metaObject()->className()
74  << d->m_activePart->objectName();
75 #endif
76 
77  GUIActivateEvent ev(false);
78  QApplication::sendEvent(d->m_activePart, &ev);
79 
80  factory->removeClient(d->m_activePart);
81 
82  disconnect(d->m_activePart.data(), &Part::setWindowCaption, this, static_cast<void (MainWindow::*)(const QString &)>(&MainWindow::setCaption));
83  disconnect(d->m_activePart.data(), &Part::setStatusBarText, this, &MainWindow::slotSetStatusBarText);
84  }
85 
86  if (!d->m_bShellGUIActivated) {
87 #if KPARTS_BUILD_DEPRECATED_SINCE(5, 90)
89 #endif
90  createShellGUI();
91  d->m_bShellGUIActivated = true;
92  }
93 
94  if (part) {
95  // do this before sending the activate event
96  if (d->m_manageWindowTitle) {
97  connect(part, &Part::setWindowCaption, this, static_cast<void (MainWindow::*)(const QString &)>(&MainWindow::setCaption));
98  }
100 
101  factory->addClient(part);
102 
103  GUIActivateEvent ev(true);
104  QApplication::sendEvent(part, &ev);
105  }
106 
107  d->m_activePart = part;
108 }
109 
111 {
112  statusBar()->showMessage(text);
113 }
114 
115 void MainWindow::createShellGUI(bool create)
116 {
117  Q_ASSERT(d->m_bShellGUIActivated != create);
118  d->m_bShellGUIActivated = create;
119  if (create) {
120  if (isHelpMenuEnabled() && !d->m_helpMenu) {
121  d->m_helpMenu = new KHelpMenu(this, KAboutData::applicationData(), true);
122 
124  QAction *helpContentsAction = d->m_helpMenu->action(KHelpMenu::menuHelpContents);
125  QAction *whatsThisAction = d->m_helpMenu->action(KHelpMenu::menuWhatsThis);
126  QAction *reportBugAction = d->m_helpMenu->action(KHelpMenu::menuReportBug);
127  QAction *switchLanguageAction = d->m_helpMenu->action(KHelpMenu::menuSwitchLanguage);
128  QAction *aboutAppAction = d->m_helpMenu->action(KHelpMenu::menuAboutApp);
129  QAction *aboutKdeAction = d->m_helpMenu->action(KHelpMenu::menuAboutKDE);
130  QAction *donateAction = d->m_helpMenu->action(KHelpMenu::menuDonate);
131 
132  if (helpContentsAction) {
133  actions->addAction(helpContentsAction->objectName(), helpContentsAction);
134  }
135  if (whatsThisAction) {
136  actions->addAction(whatsThisAction->objectName(), whatsThisAction);
137  }
138  if (reportBugAction) {
139  actions->addAction(reportBugAction->objectName(), reportBugAction);
140  }
141  if (switchLanguageAction) {
142  actions->addAction(switchLanguageAction->objectName(), switchLanguageAction);
143  }
144  if (aboutAppAction) {
145  actions->addAction(aboutAppAction->objectName(), aboutAppAction);
146  }
147  if (aboutKdeAction) {
148  actions->addAction(aboutKdeAction->objectName(), aboutKdeAction);
149  }
150  if (donateAction) {
151  actions->addAction(donateAction->objectName(), donateAction);
152  }
153  }
154 
155  QString f = xmlFile();
157  if (!f.isEmpty()) {
158  setXMLFile(f, true);
159  } else {
160  QString auto_file(componentName() + QLatin1String("ui.rc"));
161  setXMLFile(auto_file, true);
162  }
163 
164  GUIActivateEvent ev(true);
165  QApplication::sendEvent(this, &ev);
166 
167  guiFactory()->addClient(this);
168 
170  } else {
171  GUIActivateEvent ev(false);
172  QApplication::sendEvent(this, &ev);
173 
174  guiFactory()->removeClient(this);
175  }
176 }
177 
179 {
180  d->m_manageWindowTitle = enabled;
181 }
182 
184 {
185  createGUI(d->m_activePart);
187  applyMainWindowSettings(cg);
188 }
189 
190 void KParts::MainWindow::configureToolbars()
191 {
192  // No difference with base class anymore.
194 }
A KPart-aware main window, whose user interface is described in XML.
Definition: mainwindow.h:50
MainWindow(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
Constructor, same signature as KMainWindow.
Definition: mainwindow.cpp:50
QList< QAction * > actions() const const
void removeClient(KXMLGUIClient *client)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
virtual void slotSetStatusBarText(const QString &)
Called when the active part wants to change the statusbar message Reimplement if your mainwindow has ...
Definition: mainwindow.cpp:110
This event is sent to a Part when its GUI has been activated or deactivated. This is related to PartA...
void checkAmbiguousShortcuts()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void setPartObject(QObject *object)
Internal method.
Definition: partbase.cpp:28
void showMessage(const QString &message, int timeout)
typedef WindowFlags
void saveNewToolbarConfig() override
Rebuilds the GUI after KEditToolBar changed the toolbar layout.
Definition: mainwindow.cpp:183
bool sendEvent(QObject *receiver, QEvent *event)
virtual QString xmlFile() const
~MainWindow() override
Destructor.
bool isEmpty() const const
virtual void setXMLFile(const QString &file, bool merge=false, bool setXMLDoc=true)
KXMLGUIFactory * factory() const
static KAboutData applicationData()
void addClient(KXMLGUIClient *client)
virtual const QMetaObject * metaObject() const const
QStatusBar * statusBar() const const
const char * className() const const
Base class for parts.
Definition: part.h:62
virtual KActionCollection * actionCollection() const
virtual void configureToolbars()
virtual void setCaption(const QString &caption)
void setWindowTitleHandling(bool enabled)
Enable or disable the automatic setting of window titles by the part's document title.
Definition: mainwindow.cpp:178
void createGUI(KParts::Part *part)
Create the GUI (by merging the host's and the active part's) You must call this in order to see any G...
Definition: mainwindow.cpp:59
static QString standardsXmlFileLocation()
bool isHelpMenuEnabled() const
void setStatusBarText(const QString &text)
Emitted by the part, to set a text in the statusbar of the window(s) hosting this part.
void loadPlugins(QObject *parent, KXMLGUIClient *parentGUIClient, const KAboutData &aboutData)
Load the Plugins honoring the PluginLoadingMode.
Definition: partbase.cpp:82
void create(WId window, bool initializeWindow, bool destroyOldWindow)
The KParts namespace,.
void setWindowCaption(const QString &caption)
Emitted by the part, to set the caption of the window(s) hosting this part.
virtual QString componentName() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 9 2022 03:51:33 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.