KParts
statusbarextension.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org> 00003 Copyright (C) 2003 David Faure <faure@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "statusbarextension.h" 00022 00023 #include <QtCore/QObject> 00024 00025 #include <kstatusbar.h> 00026 #include <kmainwindow.h> 00027 #include <kdebug.h> 00028 #include <kparts/part.h> 00029 #include <kparts/event.h> 00030 00031 using namespace KParts; 00032 00034 // Helper Classes 00036 00037 class KParts::StatusBarItem { 00038 public: 00039 StatusBarItem() // for QValueList 00040 : m_widget(0), m_visible(false) 00041 {} 00042 StatusBarItem( QWidget * widget, int stretch, bool permanent ) 00043 : m_widget(widget), m_stretch(stretch), m_permanent(permanent), m_visible(false) 00044 {} 00045 00046 QWidget * widget() const { return m_widget; } 00047 00048 void ensureItemShown( KStatusBar * sb ) 00049 { 00050 if ( m_widget && !m_visible ) 00051 { 00052 if ( m_permanent ) 00053 sb->addPermanentWidget( m_widget, m_stretch ); 00054 else 00055 sb->addWidget( m_widget, m_stretch ); 00056 m_visible = true; 00057 m_widget->show(); 00058 } 00059 } 00060 void ensureItemHidden( KStatusBar * sb ) 00061 { 00062 if ( m_widget && m_visible ) 00063 { 00064 sb->removeWidget( m_widget ); 00065 m_visible = false; 00066 m_widget->hide(); 00067 } 00068 } 00069 private: 00070 QPointer<QWidget> m_widget; 00071 int m_stretch; 00072 bool m_permanent; 00073 bool m_visible; // true when the item has been added to the statusbar 00074 }; 00075 00076 class KParts::StatusBarExtensionPrivate 00077 { 00078 public: 00079 StatusBarExtensionPrivate(StatusBarExtension *q): q(q), 00080 m_statusBar(0) {} 00081 00082 StatusBarExtension *q; 00083 QList<StatusBarItem> m_statusBarItems; // Our statusbar items 00084 KStatusBar* m_statusBar; 00085 }; 00086 00088 00089 00090 StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent) 00091 : QObject(parent), d(new StatusBarExtensionPrivate(this)) 00092 { 00093 parent->installEventFilter(this); 00094 } 00095 00096 StatusBarExtension::~StatusBarExtension() 00097 { 00098 delete d; 00099 } 00100 00101 00102 StatusBarExtension *StatusBarExtension::childObject( QObject *obj ) 00103 { 00104 if ( !obj ) 00105 return 0; 00106 00107 // we try to do it on our own, in hope that we are faster than 00108 // queryList, which looks kind of big :-) 00109 const QObjectList &children = obj->children(); 00110 QObjectList::ConstIterator it = children.begin(); 00111 for (; it != children.end(); ++it ) { 00112 KParts::StatusBarExtension* ext = ::qobject_cast<KParts::StatusBarExtension *>( *it ); 00113 if ( ext ) 00114 return ext; 00115 } 00116 00117 return 0; 00118 } 00119 00120 bool StatusBarExtension::eventFilter(QObject * watched, QEvent* ev) 00121 { 00122 if ( !GUIActivateEvent::test( ev ) || 00123 !::qobject_cast<KParts::ReadOnlyPart *>(watched) ) 00124 return QObject::eventFilter(watched, ev); 00125 00126 KStatusBar * sb = statusBar(); 00127 if ( !sb ) 00128 return QObject::eventFilter(watched, ev); 00129 00130 GUIActivateEvent *gae = static_cast<GUIActivateEvent*>(ev); 00131 00132 if ( gae->activated() ) 00133 { 00134 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin(); 00135 for ( ; it != d->m_statusBarItems.end() ; ++it ) 00136 (*it).ensureItemShown( sb ); 00137 } 00138 else 00139 { 00140 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin(); 00141 for ( ; it != d->m_statusBarItems.end() ; ++it ) 00142 (*it).ensureItemHidden( sb ); 00143 } 00144 00145 return false; 00146 00147 } 00148 00149 KStatusBar * StatusBarExtension::statusBar() const 00150 { 00151 if ( !d->m_statusBar ) { 00152 QWidget* w = static_cast<KParts::ReadOnlyPart*>(parent())->widget(); 00153 KMainWindow* mw = w ? dynamic_cast<KMainWindow *>( w->topLevelWidget() ) : 0; 00154 if ( mw ) 00155 d->m_statusBar = mw->statusBar(); 00156 } 00157 return d->m_statusBar; 00158 } 00159 00160 void StatusBarExtension::setStatusBar( KStatusBar* status ) 00161 { 00162 d->m_statusBar = status; 00163 } 00164 00165 void StatusBarExtension::addStatusBarItem( QWidget * widget, int stretch, bool permanent ) 00166 { 00167 d->m_statusBarItems.append( StatusBarItem( widget, stretch, permanent ) ); 00168 StatusBarItem& it = d->m_statusBarItems.last(); 00169 KStatusBar * sb = statusBar(); 00170 if (sb) 00171 it.ensureItemShown( sb ); 00172 } 00173 00174 void StatusBarExtension::removeStatusBarItem( QWidget * widget ) 00175 { 00176 KStatusBar * sb = statusBar(); 00177 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin(); 00178 for ( ; it != d->m_statusBarItems.end() ; ++it ) 00179 if ( (*it).widget() == widget ) 00180 { 00181 if ( sb ) 00182 (*it).ensureItemHidden( sb ); 00183 d->m_statusBarItems.erase( it ); 00184 break; 00185 } 00186 if ( it == d->m_statusBarItems.end() ) 00187 kWarning(1000) << "StatusBarExtension::removeStatusBarItem. Widget not found : " << widget; 00188 } 00189 00190 #include "statusbarextension.moc" 00191 00192 // vim: ts=2 sw=2 et
KDE 4.6 API Reference