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 delete d; 00100 } 00101 00102 StatusBarExtension *StatusBarExtension::childObject( QObject *obj ) 00103 { 00104 return KGlobal::findDirectChild<KParts::StatusBarExtension*>(obj); 00105 } 00106 00107 bool StatusBarExtension::eventFilter(QObject * watched, QEvent* ev) 00108 { 00109 if ( !GUIActivateEvent::test( ev ) || 00110 !::qobject_cast<KParts::ReadOnlyPart *>(watched) ) 00111 return QObject::eventFilter(watched, ev); 00112 00113 KStatusBar * sb = statusBar(); 00114 if ( !sb ) 00115 return QObject::eventFilter(watched, ev); 00116 00117 GUIActivateEvent *gae = static_cast<GUIActivateEvent*>(ev); 00118 00119 if ( gae->activated() ) 00120 { 00121 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin(); 00122 for ( ; it != d->m_statusBarItems.end() ; ++it ) 00123 (*it).ensureItemShown( sb ); 00124 } 00125 else 00126 { 00127 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin(); 00128 for ( ; it != d->m_statusBarItems.end() ; ++it ) 00129 (*it).ensureItemHidden( sb ); 00130 } 00131 00132 return false; 00133 00134 } 00135 00136 KStatusBar * StatusBarExtension::statusBar() const 00137 { 00138 if ( !d->m_statusBar ) { 00139 QWidget* w = static_cast<KParts::ReadOnlyPart*>(parent())->widget(); 00140 KMainWindow* mw = w ? dynamic_cast<KMainWindow *>( w->topLevelWidget() ) : 0; 00141 if ( mw ) 00142 d->m_statusBar = mw->statusBar(); 00143 } 00144 return d->m_statusBar; 00145 } 00146 00147 void StatusBarExtension::setStatusBar( KStatusBar* status ) 00148 { 00149 d->m_statusBar = status; 00150 } 00151 00152 void StatusBarExtension::addStatusBarItem( QWidget * widget, int stretch, bool permanent ) 00153 { 00154 d->m_statusBarItems.append( StatusBarItem( widget, stretch, permanent ) ); 00155 StatusBarItem& it = d->m_statusBarItems.last(); 00156 KStatusBar * sb = statusBar(); 00157 if (sb) 00158 it.ensureItemShown( sb ); 00159 } 00160 00161 void StatusBarExtension::removeStatusBarItem( QWidget * widget ) 00162 { 00163 KStatusBar * sb = statusBar(); 00164 QList<StatusBarItem>::iterator it = d->m_statusBarItems.begin(); 00165 for ( ; it != d->m_statusBarItems.end() ; ++it ) 00166 if ( (*it).widget() == widget ) 00167 { 00168 if ( sb ) 00169 (*it).ensureItemHidden( sb ); 00170 d->m_statusBarItems.erase( it ); 00171 break; 00172 } 00173 if ( it == d->m_statusBarItems.end() ) 00174 kWarning(1000) << "StatusBarExtension::removeStatusBarItem. Widget not found : " << widget; 00175 } 00176 00177 #include "statusbarextension.moc" 00178 00179 // vim: ts=2 sw=2 et
KDE 4.7 API Reference