statusbarextension.cpp
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 <tqvaluelist.h> 00024 #include <tqobjectlist.h> 00025 00026 #include <kstatusbar.h> 00027 #include <tdemainwindow.h> 00028 #include <kdebug.h> 00029 #include <tdelibs_export.h> 00030 #include <tdeparts/part.h> 00031 #include <tdeparts/event.h> 00032 00033 using namespace KParts; 00034 00036 // Helper Classes 00038 00039 class KParts::StatusBarItem { 00040 public: 00041 StatusBarItem() // for QValueList 00042 : m_widget(0), m_visible(false) 00043 {} 00044 StatusBarItem( TQWidget * widget, int stretch, bool permanent ) 00045 : m_widget(widget), m_stretch(stretch), m_permanent(permanent), m_visible(false) 00046 {} 00047 00048 TQWidget * widget() const { return m_widget; } 00049 00050 void ensureItemShown( KStatusBar * sb ) 00051 { 00052 if ( !m_visible ) 00053 { 00054 sb->addWidget( m_widget, m_stretch, m_permanent ); 00055 m_visible = true; 00056 m_widget->show(); 00057 } 00058 } 00059 void ensureItemHidden( KStatusBar * sb ) 00060 { 00061 if ( m_visible ) 00062 { 00063 sb->removeWidget( m_widget ); 00064 m_visible = false; 00065 m_widget->hide(); 00066 } 00067 } 00068 private: 00069 TQWidget * m_widget; 00070 int m_stretch; 00071 bool m_permanent; 00072 bool m_visible; // true when the item has been added to the statusbar 00073 }; 00074 00076 00077 00078 StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent, const char* name) 00079 : TQObject(parent, name), m_statusBar(0), d(0) 00080 { 00081 parent->installEventFilter(this); 00082 } 00083 00084 StatusBarExtension::~StatusBarExtension() 00085 { 00086 } 00087 00088 00089 StatusBarExtension *StatusBarExtension::childObject( TQObject *obj ) 00090 { 00091 if ( !obj || obj->childrenListObject().isEmpty() ) 00092 return 0L; 00093 00094 // we try to do it on our own, in hope that we are faster than 00095 // queryList, which looks kind of big :-) 00096 const TQObjectList children = obj->childrenListObject(); 00097 TQObjectListIt it( children ); 00098 for (; it.current(); ++it ) 00099 if ( it.current()->inherits( "KParts::StatusBarExtension" ) ) 00100 return static_cast<KParts::StatusBarExtension *>( it.current() ); 00101 00102 return 0L; 00103 } 00104 00105 bool StatusBarExtension::eventFilter(TQObject * watched, TQEvent* ev) 00106 { 00107 if ( !GUIActivateEvent::test( ev ) || 00108 !watched->inherits("KParts::ReadOnlyPart") ) 00109 return TQObject::eventFilter(watched, ev); 00110 00111 KStatusBar * sb = statusBar(); 00112 if ( !sb ) 00113 return TQObject::eventFilter(watched, ev); 00114 00115 GUIActivateEvent *gae = static_cast<GUIActivateEvent*>(ev); 00116 00117 if ( gae->activated() ) 00118 { 00119 TQValueListIterator<StatusBarItem> it = m_statusBarItems.begin(); 00120 for ( ; it != m_statusBarItems.end() ; ++it ) 00121 (*it).ensureItemShown( sb ); 00122 } 00123 else 00124 { 00125 TQValueListIterator<StatusBarItem> it = m_statusBarItems.begin(); 00126 for ( ; it != m_statusBarItems.end() ; ++it ) 00127 (*it).ensureItemHidden( sb ); 00128 } 00129 00130 return false; 00131 00132 } 00133 00134 KStatusBar * StatusBarExtension::statusBar() const 00135 { 00136 if ( !m_statusBar ) { 00137 TQWidget* w = static_cast<KParts::ReadOnlyPart*>(parent())->widget(); 00138 TDEMainWindow* mw = tqt_dynamic_cast<TDEMainWindow *>( w->topLevelWidget() ); 00139 if ( mw ) 00140 m_statusBar = mw->statusBar(); 00141 } 00142 return m_statusBar; 00143 } 00144 00145 void StatusBarExtension::setStatusBar( KStatusBar* status ) 00146 { 00147 m_statusBar = status; 00148 } 00149 00150 void StatusBarExtension::addStatusBarItem( TQWidget * widget, int stretch, bool permanent ) 00151 { 00152 m_statusBarItems.append( StatusBarItem( widget, stretch, permanent ) ); 00153 TQValueListIterator<StatusBarItem> it = m_statusBarItems.fromLast(); 00154 KStatusBar * sb = statusBar(); 00155 Q_ASSERT(sb); 00156 if (sb) 00157 (*it).ensureItemShown( sb ); 00158 } 00159 00160 void StatusBarExtension::removeStatusBarItem( TQWidget * widget ) 00161 { 00162 KStatusBar * sb = statusBar(); 00163 TQValueListIterator<StatusBarItem> it = m_statusBarItems.begin(); 00164 for ( ; it != m_statusBarItems.end() ; ++it ) 00165 if ( (*it).widget() == widget ) 00166 { 00167 if ( sb ) 00168 (*it).ensureItemHidden( sb ); 00169 m_statusBarItems.remove( it ); 00170 break; 00171 } 00172 if ( it == m_statusBarItems.end() ) 00173 kdWarning(1000) << "StatusBarExtension::removeStatusBarItem. Widget not found : " << widget << endl; 00174 } 00175 00176 #include "statusbarextension.moc" 00177 00178 // vim: ts=2 sw=2 et