1/* This file is part of the KDE project
2 Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org>
3 Copyright (C) 2003 David Faure <faure@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19 */
20
21#include "statusbarextension.h"
22
23#include <QtCore/QObject>
24
25#include <kstatusbar.h>
26#include <kmainwindow.h>
27#include <kdebug.h>
28#include <kglobal.h>
29#include <kparts/part.h>
30#include <kparts/event.h>
31
32using namespace KParts;
33
34///////////////////////////////////////////////////////////////////
35// Helper Classes
36///////////////////////////////////////////////////////////////////
37
38class KParts::StatusBarItem {
39 public:
40 StatusBarItem() // for QValueList
41 : m_widget(0), m_visible(false)
42 {}
43 StatusBarItem( QWidget * widget, int stretch, bool permanent )
44 : m_widget(widget), m_stretch(stretch), m_permanent(permanent), m_visible(false)
45 {}
46
47 QWidget * widget() const { return m_widget; }
48
49 void ensureItemShown( KStatusBar * sb )
50 {
51 if ( m_widget && !m_visible )
52 {
53 if ( m_permanent )
54 sb->addPermanentWidget( m_widget, m_stretch );
55 else
56 sb->addWidget( m_widget, m_stretch );
57 m_visible = true;
58 m_widget->show();
59 }
60 }
61 void ensureItemHidden( KStatusBar * sb )
62 {
63 if ( m_widget && m_visible )
64 {
65 sb->removeWidget( m_widget );
66 m_visible = false;
67 m_widget->hide();
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
77class KParts::StatusBarExtensionPrivate
78{
79public:
80 StatusBarExtensionPrivate(StatusBarExtension *q): q(q),
81 m_statusBar(0),
82 m_activated(true) {}
83
84 StatusBarExtension *q;
85 QList<StatusBarItem> m_statusBarItems; // Our statusbar items
86 KStatusBar* m_statusBar;
87 bool m_activated;
88};
89
90///////////////////////////////////////////////////////////////////
91
92
93StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent)
94 : QObject(parent), d(new StatusBarExtensionPrivate(this))
95{
96 parent->installEventFilter(this);
97}
98
99StatusBarExtension::~StatusBarExtension()
100{
101 KStatusBar * sb = d->m_statusBar;
102 for ( int i = d->m_statusBarItems.count () - 1; i >= 0 ; --i ) {
103 if ( d->m_statusBarItems[i].widget() ) {
104 if ( sb ) {
105 d->m_statusBarItems[i].ensureItemHidden( sb );
106 }
107 d->m_statusBarItems[i].widget()->deleteLater();
108 }
109 }
110
111 delete d;
112}
113
114StatusBarExtension *StatusBarExtension::childObject( QObject *obj )
115{
116 return KGlobal::findDirectChild<KParts::StatusBarExtension*>(obj);
117}
118
119bool StatusBarExtension::eventFilter(QObject * watched, QEvent* ev)
120{
121 if ( !GUIActivateEvent::test( ev ) ||
122 !::qobject_cast<KParts::ReadOnlyPart *>(watched) )
123 return QObject::eventFilter(watched, ev);
124
125 KStatusBar * sb = statusBar();
126 if ( !sb )
127 return QObject::eventFilter(watched, ev);
128
129 GUIActivateEvent *gae = static_cast<GUIActivateEvent*>(ev);
130 d->m_activated = gae->activated();
131
132 if ( d->m_activated )
133 {
134 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin();
135 for ( ; it != d->m_statusBarItems.end() ; ++it )
136 (*it).ensureItemShown( sb );
137 }
138 else
139 {
140 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin();
141 for ( ; it != d->m_statusBarItems.end() ; ++it )
142 (*it).ensureItemHidden( sb );
143 }
144
145 return false;
146
147}
148
149KStatusBar * StatusBarExtension::statusBar() const
150{
151 if ( !d->m_statusBar ) {
152 KParts::ReadOnlyPart* part = qobject_cast<KParts::ReadOnlyPart*>(parent());
153 QWidget* w = part ? part->widget() : 0;
154 KMainWindow* mw = w ? qobject_cast<KMainWindow *>( w->topLevelWidget() ) : 0;
155 if ( mw )
156 d->m_statusBar = mw->statusBar();
157 }
158 return d->m_statusBar;
159}
160
161void StatusBarExtension::setStatusBar( KStatusBar* status )
162{
163 d->m_statusBar = status;
164}
165
166void StatusBarExtension::addStatusBarItem( QWidget * widget, int stretch, bool permanent )
167{
168 d->m_statusBarItems.append( StatusBarItem( widget, stretch, permanent ) );
169 StatusBarItem& it = d->m_statusBarItems.last();
170 KStatusBar * sb = statusBar();
171 if (sb && d->m_activated)
172 it.ensureItemShown( sb );
173}
174
175void StatusBarExtension::removeStatusBarItem( QWidget * widget )
176{
177 KStatusBar * sb = statusBar();
178 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin();
179 for ( ; it != d->m_statusBarItems.end() ; ++it )
180 if ( (*it).widget() == widget )
181 {
182 if ( sb )
183 (*it).ensureItemHidden( sb );
184 d->m_statusBarItems.erase( it );
185 return;
186 }
187
188 kWarning(1000) << "StatusBarExtension::removeStatusBarItem. Widget not found : " << widget;
189}
190
191#include "statusbarextension.moc"
192
193// vim: ts=2 sw=2 et
194