KParts

statusbarextension.cpp
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2003 Daniel Molkentin <molkentin@kde.org>
4 SPDX-FileCopyrightText: 2003 David Faure <faure@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "statusbarextension.h"
10
11#include "kparts_logging.h"
12
13#include "guiactivateevent.h"
14#include "readonlypart.h"
15
16#include <KMainWindow>
17
18#include <QObject>
19#include <QPointer>
20#include <QStatusBar>
21
22using namespace KParts;
23
24///////////////////////////////////////////////////////////////////
25// Helper Classes
26///////////////////////////////////////////////////////////////////
27
28class KParts::StatusBarItem
29{
30public:
31 StatusBarItem() // for QValueList
32 : m_widget(nullptr)
33 , m_visible(false)
34 {
35 }
36 StatusBarItem(QWidget *widget, int stretch, bool permanent)
37 : m_widget(widget)
38 , m_stretch(stretch)
39 , m_permanent(permanent)
40 , m_visible(false)
41 {
42 }
43
44 QWidget *widget() const
45 {
46 return m_widget;
47 }
48
49 void ensureItemShown(QStatusBar *sb)
50 {
51 if (m_widget && !m_visible) {
52 if (m_permanent) {
53 sb->addPermanentWidget(m_widget, m_stretch);
54 } else {
55 sb->addWidget(m_widget, m_stretch);
56 }
57 m_visible = true;
58 m_widget->show();
59 }
60 }
61 void ensureItemHidden(QStatusBar *sb)
62 {
63 if (m_widget && m_visible) {
64 sb->removeWidget(m_widget);
65 m_visible = false;
66 m_widget->hide();
67 }
68 }
69
70private:
71 QPointer<QWidget> m_widget;
72 int m_stretch;
73 bool m_permanent;
74 bool m_visible; // true when the item has been added to the statusbar
75};
76
77class KParts::StatusBarExtensionPrivate
78{
79public:
80 StatusBarExtensionPrivate(StatusBarExtension *qq)
81 : q(qq)
82 , m_statusBar(nullptr)
83 , m_activated(true)
84 {
85 }
86
88 QList<StatusBarItem> m_statusBarItems; // Our statusbar items
89 QStatusBar *m_statusBar;
90 bool m_activated;
91};
92
93///////////////////////////////////////////////////////////////////
94
95StatusBarExtension::StatusBarExtension(KParts::Part *parent)
96 : QObject(parent)
97 , d(new StatusBarExtensionPrivate(this))
98{
99 parent->installEventFilter(this);
100}
101
102StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent)
103 : QObject(parent)
104 , d(new StatusBarExtensionPrivate(this))
105{
106 parent->installEventFilter(this);
107}
108
109StatusBarExtension::~StatusBarExtension()
110{
111 QStatusBar *sb = d->m_statusBar;
112 for (int i = d->m_statusBarItems.count() - 1; i >= 0; --i) {
113 if (d->m_statusBarItems[i].widget()) {
114 if (sb) {
115 d->m_statusBarItems[i].ensureItemHidden(sb);
116 }
117 d->m_statusBarItems[i].widget()->deleteLater();
118 }
119 }
120}
121
126
128{
129 if (!GUIActivateEvent::test(ev) || !::qobject_cast<KParts::Part *>(watched)) {
130 return QObject::eventFilter(watched, ev);
131 }
132
133 QStatusBar *sb = statusBar();
134 if (!sb) {
135 return QObject::eventFilter(watched, ev);
136 }
137
138 GUIActivateEvent *gae = static_cast<GUIActivateEvent *>(ev);
139 d->m_activated = gae->activated();
140
141 if (d->m_activated) {
142 for (auto &item : d->m_statusBarItems) {
143 item.ensureItemShown(sb);
144 }
145 } else {
146 for (auto &item : d->m_statusBarItems) {
147 item.ensureItemHidden(sb);
148 }
149 }
150
151 return false;
152}
153
155{
156 if (!d->m_statusBar) {
158 QWidget *w = part ? part->widget() : nullptr;
160 if (mw) {
161 d->m_statusBar = mw->statusBar();
162 }
163 }
164 return d->m_statusBar;
165}
166
168{
169 d->m_statusBar = status;
170}
171
172void StatusBarExtension::addStatusBarItem(QWidget *widget, int stretch, bool permanent)
173{
174 d->m_statusBarItems.append(StatusBarItem(widget, stretch, permanent));
175 StatusBarItem &it = d->m_statusBarItems.last();
176 QStatusBar *sb = statusBar();
177 if (sb && d->m_activated) {
178 it.ensureItemShown(sb);
179 }
180}
181
183{
184 QStatusBar *sb = statusBar();
185 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin();
186 for (; it != d->m_statusBarItems.end(); ++it) {
187 if ((*it).widget() == widget) {
188 if (sb) {
189 (*it).ensureItemHidden(sb);
190 }
191 d->m_statusBarItems.erase(it);
192 return;
193 }
194 }
195
196 qCWarning(KPARTSLOG) << "StatusBarExtension::removeStatusBarItem. Widget not found :" << widget;
197}
198
199#include "moc_statusbarextension.cpp"
This event is sent to a Part when its GUI has been activated or deactivated.
Base class for parts.
Definition part.h:57
virtual QWidget * widget()
Definition part.cpp:61
Base class for any "viewer" part.
An extension for KParts that allows more sophisticated statusbar handling.
bool eventFilter(QObject *watched, QEvent *ev) override
void setStatusBar(QStatusBar *status)
This allows the hosting application to set a particular QStatusBar for this part.
void removeStatusBarItem(QWidget *widget)
Remove a widget from the statusbar for this part.
static StatusBarExtension * childObject(QObject *obj)
Queries obj for a child object which inherits from this StatusBarExtension class.
void addStatusBarItem(QWidget *widget, int stretch, bool permanent)
This adds a widget to the statusbar for this part.
Q_SCRIPTABLE CaptureState status()
The KParts namespace,.
QStatusBar * statusBar() const const
virtual bool eventFilter(QObject *watched, QEvent *event)
T findChild(const QString &name, Qt::FindChildOptions options) const const
void installEventFilter(QObject *filterObj)
QObject * parent() const const
T qobject_cast(QObject *object)
void addPermanentWidget(QWidget *widget, int stretch)
void addWidget(QWidget *widget, int stretch)
void removeWidget(QWidget *widget)
FindDirectChildrenOnly
QWidget * topLevelWidget() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.