KParts

statusbarextension.cpp
1 /*
2  This file is part of the KDE project
3  SPDX-FileCopyrightText: 2003 Daniel Molkentin <[email protected]>
4  SPDX-FileCopyrightText: 2003 David Faure <[email protected]>
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 
22 using namespace KParts;
23 
24 ///////////////////////////////////////////////////////////////////
25 // Helper Classes
26 ///////////////////////////////////////////////////////////////////
27 
28 class KParts::StatusBarItem
29 {
30 public:
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 
70 private:
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 
77 class KParts::StatusBarExtensionPrivate
78 {
79 public:
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 
95 StatusBarExtension::StatusBarExtension(KParts::Part *parent)
96  : QObject(parent)
97  , d(new StatusBarExtensionPrivate(this))
98 {
99  parent->installEventFilter(this);
100 }
101 
102 StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent)
103  : QObject(parent)
104  , d(new StatusBarExtensionPrivate(this))
105 {
106  parent->installEventFilter(this);
107 }
108 
109 StatusBarExtension::~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 
123 {
125 }
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) {
157  KParts::Part *part = qobject_cast<KParts::Part *>(parent());
158  QWidget *w = part ? part->widget() : nullptr;
159  KMainWindow *mw = w ? qobject_cast<KMainWindow *>(w->topLevelWidget()) : 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 
172 void 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 }
void removeStatusBarItem(QWidget *widget)
Remove a widget from the statusbar for this part.
QWidget * topLevelWidget() const const
An extension for KParts that allows more sophisticated statusbar handling.
bool eventFilter(QObject *watched, QEvent *ev) override
This event is sent to a Part when its GUI has been activated or deactivated. This is related to PartA...
void removeWidget(QWidget *widget)
void addWidget(QWidget *widget, int stretch)
void addStatusBarItem(QWidget *widget, int stretch, bool permanent)
This adds a widget to the statusbar for this part.
virtual bool eventFilter(QObject *watched, QEvent *event)
static StatusBarExtension * childObject(QObject *obj)
Queries obj for a child object which inherits from this BrowserExtension class.
void installEventFilter(QObject *filterObj)
void setStatusBar(QStatusBar *status)
This allows the hosting application to set a particular QStatusBar for this part.
FindDirectChildrenOnly
void addPermanentWidget(QWidget *widget, int stretch)
QStatusBar * statusBar() const const
T findChild(const QString &name, Qt::FindChildOptions options) const const
Base class for parts.
Definition: part.h:62
Base class for any "viewer" part.
Definition: readonlypart.h:51
virtual QWidget * widget()
Definition: part.cpp:75
QStatusBar * statusBar() const
QObject * parent() const const
The KParts namespace,.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 03:59:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.