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 <kglobal.h> 00029 #include <kparts/part.h> 00030 #include <kparts/event.h> 00031 00032 using namespace KParts; 00033 00035 // Helper Classes 00037 00038 class KParts::StatusBarItem { 00039 public: 00040 StatusBarItem() // for QValueList 00041 : m_widget(0), m_visible(false) 00042 {} 00043 StatusBarItem( QWidget * widget, int stretch, bool permanent ) 00044 : m_widget(widget), m_stretch(stretch), m_permanent(permanent), m_visible(false) 00045 {} 00046 00047 QWidget * widget() const { return m_widget; } 00048 00049 void ensureItemShown( KStatusBar * sb ) 00050 { 00051 if ( m_widget && !m_visible ) 00052 { 00053 if ( m_permanent ) 00054 sb->addPermanentWidget( m_widget, m_stretch ); 00055 else 00056 sb->addWidget( m_widget, m_stretch ); 00057 m_visible = true; 00058 m_widget->show(); 00059 } 00060 } 00061 void ensureItemHidden( KStatusBar * sb ) 00062 { 00063 if ( m_widget && m_visible ) 00064 { 00065 sb->removeWidget( m_widget ); 00066 m_visible = false; 00067 m_widget->hide(); 00068 } 00069 } 00070 private: 00071 QPointer<QWidget> m_widget; 00072 int m_stretch; 00073 bool m_permanent; 00074 bool m_visible; // true when the item has been added to the statusbar 00075 }; 00076 00077 class KParts::StatusBarExtensionPrivate 00078 { 00079 public: 00080 StatusBarExtensionPrivate(StatusBarExtension *q): q(q), 00081 m_statusBar(0) {} 00082 00083 StatusBarExtension *q; 00084 QList<StatusBarItem> m_statusBarItems; // Our statusbar items 00085 KStatusBar* m_statusBar; 00086 }; 00087 00089 00090 00091 StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent) 00092 : QObject(parent), d(new StatusBarExtensionPrivate(this)) 00093 { 00094 parent->installEventFilter(this); 00095 } 00096 00097 StatusBarExtension::~StatusBarExtension() 00098 { 00099 KStatusBar * sb = d->m_statusBar; 00100 for ( int i = d->m_statusBarItems.count () - 1; i >= 0 ; --i ) { 00101 if ( d->m_statusBarItems[i].widget() ) { 00102 if ( sb ) { 00103 d->m_statusBarItems[i].ensureItemHidden( sb ); 00104 } 00105 d->m_statusBarItems[i].widget()->deleteLater(); 00106 } 00107 } 00108 00109 delete d; 00110 } 00111 00112 StatusBarExtension *StatusBarExtension::childObject( QObject *obj ) 00113 { 00114 return KGlobal::findDirectChild<KParts::StatusBarExtension*>(obj); 00115 } 00116 00117 bool StatusBarExtension::eventFilter(QObject * watched, QEvent* ev) 00118 { 00119 if ( !GUIActivateEvent::test( ev ) || 00120 !::qobject_cast<KParts::ReadOnlyPart *>(watched) ) 00121 return QObject::eventFilter(watched, ev); 00122 00123 KStatusBar * sb = statusBar(); 00124 if ( !sb ) 00125 return QObject::eventFilter(watched, ev); 00126 00127 GUIActivateEvent *gae = static_cast<GUIActivateEvent*>(ev); 00128 00129 if ( gae->activated() ) 00130 { 00131 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin(); 00132 for ( ; it != d->m_statusBarItems.end() ; ++it ) 00133 (*it).ensureItemShown( sb ); 00134 } 00135 else 00136 { 00137 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin(); 00138 for ( ; it != d->m_statusBarItems.end() ; ++it ) 00139 (*it).ensureItemHidden( sb ); 00140 } 00141 00142 return false; 00143 00144 } 00145 00146 KStatusBar * StatusBarExtension::statusBar() const 00147 { 00148 if ( !d->m_statusBar ) { 00149 KParts::ReadOnlyPart* part = qobject_cast<KParts::ReadOnlyPart*>(parent()); 00150 QWidget* w = part ? part->widget() : 0; 00151 KMainWindow* mw = w ? qobject_cast<KMainWindow *>( w->topLevelWidget() ) : 0; 00152 if ( mw ) 00153 d->m_statusBar = mw->statusBar(); 00154 } 00155 return d->m_statusBar; 00156 } 00157 00158 void StatusBarExtension::setStatusBar( KStatusBar* status ) 00159 { 00160 d->m_statusBar = status; 00161 } 00162 00163 void StatusBarExtension::addStatusBarItem( QWidget * widget, int stretch, bool permanent ) 00164 { 00165 d->m_statusBarItems.append( StatusBarItem( widget, stretch, permanent ) ); 00166 StatusBarItem& it = d->m_statusBarItems.last(); 00167 KStatusBar * sb = statusBar(); 00168 if (sb) 00169 it.ensureItemShown( sb ); 00170 } 00171 00172 void StatusBarExtension::removeStatusBarItem( QWidget * widget ) 00173 { 00174 KStatusBar * sb = statusBar(); 00175 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin(); 00176 for ( ; it != d->m_statusBarItems.end() ; ++it ) 00177 if ( (*it).widget() == widget ) 00178 { 00179 if ( sb ) 00180 (*it).ensureItemHidden( sb ); 00181 d->m_statusBarItems.erase( it ); 00182 break; 00183 } 00184 if ( it == d->m_statusBarItems.end() ) 00185 kWarning(1000) << "StatusBarExtension::removeStatusBarItem. Widget not found : " << widget; 00186 } 00187 00188 #include "statusbarextension.moc" 00189 00190 // vim: ts=2 sw=2 et
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:37:13 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed May 2 2012 18:37:13 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.