fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kparts / statusbarextension.cpp
blob0b35987eeb5f3511243072515f8cd5fe87890a8e
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>
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.
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.
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.
21 #include "statusbarextension.h"
23 #include <QtCore/QObject>
25 #include <kstatusbar.h>
26 #include <kmainwindow.h>
27 #include <kdebug.h>
28 #include <kparts/part.h>
29 #include <kparts/event.h>
31 using namespace KParts;
33 ///////////////////////////////////////////////////////////////////
34 // Helper Classes
35 ///////////////////////////////////////////////////////////////////
37 class KParts::StatusBarItem {
38 public:
39 StatusBarItem() // for QValueList
40 : m_widget(0), m_visible(false)
42 StatusBarItem( QWidget * widget, int stretch, bool permanent )
43 : m_widget(widget), m_stretch(stretch), m_permanent(permanent), m_visible(false)
46 QWidget * widget() const { return m_widget; }
48 void ensureItemShown( KStatusBar * sb )
50 if ( !m_visible )
52 if ( m_permanent )
53 sb->addPermanentWidget( m_widget, m_stretch );
54 else
55 sb->addWidget( m_widget, m_stretch );
56 m_visible = true;
57 m_widget->show();
60 void ensureItemHidden( KStatusBar * sb )
62 if ( m_visible )
64 sb->removeWidget( m_widget );
65 m_visible = false;
66 m_widget->hide();
69 private:
70 QWidget * m_widget;
71 int m_stretch;
72 bool m_permanent;
73 bool m_visible; // true when the item has been added to the statusbar
76 class KParts::StatusBarExtensionPrivate
78 public:
79 StatusBarExtensionPrivate(StatusBarExtension *q): q(q),
80 m_statusBar(0) {}
82 StatusBarExtension *q;
83 QList<StatusBarItem> m_statusBarItems; // Our statusbar items
84 KStatusBar* m_statusBar;
87 ///////////////////////////////////////////////////////////////////
90 StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent)
91 : QObject(parent), d(new StatusBarExtensionPrivate(this))
93 parent->installEventFilter(this);
96 StatusBarExtension::~StatusBarExtension()
98 delete d;
102 StatusBarExtension *StatusBarExtension::childObject( QObject *obj )
104 if ( !obj )
105 return 0;
107 // we try to do it on our own, in hope that we are faster than
108 // queryList, which looks kind of big :-)
109 const QObjectList &children = obj->children();
110 QObjectList::ConstIterator it = children.begin();
111 for (; it != children.end(); ++it ) {
112 KParts::StatusBarExtension* ext = ::qobject_cast<KParts::StatusBarExtension *>( *it );
113 if ( ext )
114 return ext;
117 return 0;
120 bool StatusBarExtension::eventFilter(QObject * watched, QEvent* ev)
122 if ( !GUIActivateEvent::test( ev ) ||
123 !::qobject_cast<KParts::ReadOnlyPart *>(watched) )
124 return QObject::eventFilter(watched, ev);
126 KStatusBar * sb = statusBar();
127 if ( !sb )
128 return QObject::eventFilter(watched, ev);
130 GUIActivateEvent *gae = static_cast<GUIActivateEvent*>(ev);
132 if ( gae->activated() )
134 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin();
135 for ( ; it != d->m_statusBarItems.end() ; ++it )
136 (*it).ensureItemShown( sb );
138 else
140 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin();
141 for ( ; it != d->m_statusBarItems.end() ; ++it )
142 (*it).ensureItemHidden( sb );
145 return false;
149 KStatusBar * StatusBarExtension::statusBar() const
151 if ( !d->m_statusBar ) {
152 QWidget* w = static_cast<KParts::ReadOnlyPart*>(parent())->widget();
153 KMainWindow* mw = dynamic_cast<KMainWindow *>( w->topLevelWidget() );
154 if ( mw )
155 d->m_statusBar = mw->statusBar();
157 return d->m_statusBar;
160 void StatusBarExtension::setStatusBar( KStatusBar* status )
162 d->m_statusBar = status;
165 void StatusBarExtension::addStatusBarItem( QWidget * widget, int stretch, bool permanent )
167 d->m_statusBarItems.append( StatusBarItem( widget, stretch, permanent ) );
168 StatusBarItem& it = d->m_statusBarItems.last();
169 KStatusBar * sb = statusBar();
170 if (sb)
171 it.ensureItemShown( sb );
174 void StatusBarExtension::removeStatusBarItem( QWidget * widget )
176 KStatusBar * sb = statusBar();
177 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin();
178 for ( ; it != d->m_statusBarItems.end() ; ++it )
179 if ( (*it).widget() == widget )
181 if ( sb )
182 (*it).ensureItemHidden( sb );
183 d->m_statusBarItems.erase( it );
184 break;
186 if ( it == d->m_statusBarItems.end() )
187 kWarning(1000) << "StatusBarExtension::removeStatusBarItem. Widget not found : " << widget;
190 #include "statusbarextension.moc"
192 // vim: ts=2 sw=2 et