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(widget: m_widget, stretch: m_stretch);
54 } else {
55 sb->addWidget(widget: m_widget, stretch: 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(widget: 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
87 StatusBarExtension *q;
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(filterObj: this);
100}
101
102StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent)
103 : QObject(parent)
104 , d(new StatusBarExtensionPrivate(this))
105{
106 parent->installEventFilter(filterObj: 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
122StatusBarExtension *StatusBarExtension::childObject(QObject *obj)
123{
124 return obj->findChild<KParts::StatusBarExtension *>(aName: QString(), options: Qt::FindDirectChildrenOnly);
125}
126
127bool StatusBarExtension::eventFilter(QObject *watched, QEvent *ev)
128{
129 if (!GUIActivateEvent::test(event: ev) || !::qobject_cast<KParts::Part *>(object: watched)) {
130 return QObject::eventFilter(watched, event: ev);
131 }
132
133 QStatusBar *sb = statusBar();
134 if (!sb) {
135 return QObject::eventFilter(watched, event: 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
154QStatusBar *StatusBarExtension::statusBar() const
155{
156 if (!d->m_statusBar) {
157 KParts::Part *part = qobject_cast<KParts::Part *>(object: parent());
158 QWidget *w = part ? part->widget() : nullptr;
159 KMainWindow *mw = w ? qobject_cast<KMainWindow *>(object: w->topLevelWidget()) : nullptr;
160 if (mw) {
161 d->m_statusBar = mw->statusBar();
162 }
163 }
164 return d->m_statusBar;
165}
166
167void StatusBarExtension::setStatusBar(QStatusBar *status)
168{
169 d->m_statusBar = status;
170}
171
172void StatusBarExtension::addStatusBarItem(QWidget *widget, int stretch, bool permanent)
173{
174 d->m_statusBarItems.append(t: 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
182void StatusBarExtension::removeStatusBarItem(QWidget *widget)
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(pos: it);
192 return;
193 }
194 }
195
196 qCWarning(KPARTSLOG) << "StatusBarExtension::removeStatusBarItem. Widget not found :" << widget;
197}
198
199#include "moc_statusbarextension.cpp"
200

source code of kparts/src/statusbarextension.cpp