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>
28 #include <kparts/part.h>
29 #include <kparts/event.h>
31 using namespace KParts
;
33 ///////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////
37 class KParts::StatusBarItem
{
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
)
53 sb
->addPermanentWidget( m_widget
, m_stretch
);
55 sb
->addWidget( m_widget
, m_stretch
);
60 void ensureItemHidden( KStatusBar
* sb
)
64 sb
->removeWidget( m_widget
);
73 bool m_visible
; // true when the item has been added to the statusbar
76 class KParts::StatusBarExtensionPrivate
79 StatusBarExtensionPrivate(StatusBarExtension
*q
): q(q
),
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()
102 StatusBarExtension
*StatusBarExtension::childObject( QObject
*obj
)
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
);
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();
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
);
140 QList
<StatusBarItem
>::iterator it
= d
->m_statusBarItems
.begin();
141 for ( ; it
!= d
->m_statusBarItems
.end() ; ++it
)
142 (*it
).ensureItemHidden( sb
);
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() );
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();
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
)
182 (*it
).ensureItemHidden( sb
);
183 d
->m_statusBarItems
.erase( it
);
186 if ( it
== d
->m_statusBarItems
.end() )
187 kWarning(1000) << "StatusBarExtension::removeStatusBarItem. Widget not found : " << widget
;
190 #include "statusbarextension.moc"