KParts

mainwindow.cpp
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 
27 using namespace KParts;
28 
29 namespace KParts
30 {
31 class MainWindowPrivate
32 {
33 public:
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 
50  : KXmlGuiWindow(parent, f)
51  , d(new MainWindowPrivate())
52 {
54 }
55 
56 MainWindow::~MainWindow() = default;
57 
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(d->m_activePart, &ev);
78 
79  factory->removeClient(d->m_activePart);
80 
81  disconnect(d->m_activePart.data(), &Part::setWindowCaption, this, static_cast<void (MainWindow::*)(const QString &)>(&MainWindow::setCaption));
82  disconnect(d->m_activePart.data(), &Part::setStatusBarText, this, &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(part, &Part::setWindowCaption, this, static_cast<void (MainWindow::*)(const QString &)>(&MainWindow::setCaption));
94  }
96 
97  factory->addClient(part);
98 
99  GUIActivateEvent ev(true);
100  QApplication::sendEvent(part, &ev);
101  }
102 
103  d->m_activePart = part;
104 }
105 
107 {
108  statusBar()->showMessage(text);
109 }
110 
111 void 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 
120  QAction *helpContentsAction = d->m_helpMenu->action(KHelpMenu::menuHelpContents);
121  QAction *whatsThisAction = d->m_helpMenu->action(KHelpMenu::menuWhatsThis);
122  QAction *reportBugAction = d->m_helpMenu->action(KHelpMenu::menuReportBug);
123  QAction *switchLanguageAction = d->m_helpMenu->action(KHelpMenu::menuSwitchLanguage);
124  QAction *aboutAppAction = d->m_helpMenu->action(KHelpMenu::menuAboutApp);
125  QAction *aboutKdeAction = d->m_helpMenu->action(KHelpMenu::menuAboutKDE);
126  QAction *donateAction = d->m_helpMenu->action(KHelpMenu::menuDonate);
127 
128  if (helpContentsAction) {
129  actions->addAction(helpContentsAction->objectName(), helpContentsAction);
130  }
131  if (whatsThisAction) {
132  actions->addAction(whatsThisAction->objectName(), whatsThisAction);
133  }
134  if (reportBugAction) {
135  actions->addAction(reportBugAction->objectName(), reportBugAction);
136  }
137  if (switchLanguageAction) {
138  actions->addAction(switchLanguageAction->objectName(), switchLanguageAction);
139  }
140  if (aboutAppAction) {
141  actions->addAction(aboutAppAction->objectName(), aboutAppAction);
142  }
143  if (aboutKdeAction) {
144  actions->addAction(aboutKdeAction->objectName(), aboutKdeAction);
145  }
146  if (donateAction) {
147  actions->addAction(donateAction->objectName(), donateAction);
148  }
149  }
150 
151  QString f = xmlFile();
153  if (!f.isEmpty()) {
154  setXMLFile(f, true);
155  } else {
156  QString auto_file(componentName() + QLatin1String("ui.rc"));
157  setXMLFile(auto_file, true);
158  }
159 
160  GUIActivateEvent ev(true);
161  QApplication::sendEvent(this, &ev);
162 
163  guiFactory()->addClient(this);
164 
166  } else {
167  GUIActivateEvent ev(false);
168  QApplication::sendEvent(this, &ev);
169 
170  guiFactory()->removeClient(this);
171  }
172 }
173 
175 {
176  d->m_manageWindowTitle = enabled;
177 }
178 
180 {
181  createGUI(d->m_activePart);
183  applyMainWindowSettings(cg);
184 }
185 
186 void KParts::MainWindow::configureToolbars()
187 {
188  // No difference with base class anymore.
190 }
191 
192 #include "moc_mainwindow.cpp"
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:49
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:106
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:26
void showMessage(const QString &message, int timeout)
typedef WindowFlags
void saveNewToolbarConfig() override
Rebuilds the GUI after KEditToolBar changed the toolbar layout.
Definition: mainwindow.cpp:179
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:56
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:174
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:58
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 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-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:48:58 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.