kparts Library API Documentation

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., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include "statusbarextension.h"
00022 
00023 #include <qvaluelist.h>
00024 
00025 #include <kstatusbar.h>
00026 #include <kmainwindow.h>
00027 
00028 #include <kparts/part.h>
00029 #include <kparts/event.h>
00030 
00031 #include <kdebug.h>
00032 #include <qobjectlist.h>
00033 
00034 using namespace KParts;
00035 
00037 // Helper Classes
00039 
00040 class KParts::StatusBarItem {
00041   public:
00042     StatusBarItem() // for QValueList
00043       : m_widget(0), m_visible(false)
00044       {}
00045     StatusBarItem( QWidget * widget, int stretch, bool permanent )
00046       : m_widget(widget), m_stretch(stretch), m_permanent(permanent), m_visible(false)
00047       {}
00048 
00049     QWidget * widget() const { return m_widget; }
00050 
00051     void ensureItemShown( KStatusBar * sb )
00052     {
00053       if ( !m_visible )
00054       {
00055         sb->addWidget( m_widget, m_stretch, m_permanent );
00056         m_visible = true;
00057         m_widget->show();
00058       }
00059     }
00060     void ensureItemHidden( KStatusBar * sb )
00061     {
00062       if ( m_visible )
00063       {
00064         sb->removeWidget( m_widget );
00065         m_visible = false;
00066         m_widget->hide();
00067       }
00068     }
00069   private:
00070     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 
00077 
00078 
00079 StatusBarExtension::StatusBarExtension(KParts::ReadOnlyPart *parent, const char* name)
00080   : QObject(parent, name), m_statusBar(0), d(0)
00081 {
00082   parent->installEventFilter(this);
00083 }
00084 
00085 StatusBarExtension::~StatusBarExtension()
00086 {
00087 }
00088 
00089 
00090 StatusBarExtension *StatusBarExtension::childObject( QObject *obj )
00091 {
00092     if ( !obj || !obj->children() )
00093         return 0L;
00094 
00095     // we try to do it on our own, in hope that we are faster than
00096     // queryList, which looks kind of big :-)
00097     const QObjectList *children = obj->children();
00098     QObjectListIt it( *children );
00099     for (; it.current(); ++it )
00100         if ( it.current()->inherits( "KParts::StatusBarExtension" ) )
00101             return static_cast<KParts::StatusBarExtension *>( it.current() );
00102 
00103     return 0L;
00104 }
00105 
00106 bool StatusBarExtension::eventFilter(QObject * watched, QEvent* ev)
00107 {
00108   if ( !GUIActivateEvent::test( ev ) ||
00109       !watched->inherits("KParts::ReadOnlyPart")  )
00110       return QObject::eventFilter(watched, ev);
00111 
00112   KStatusBar * sb = statusBar();
00113   if ( !sb )
00114       return QObject::eventFilter(watched, ev);
00115 
00116   GUIActivateEvent *gae = static_cast<GUIActivateEvent*>(ev);
00117 
00118   if ( gae->activated() )
00119   {
00120     QValueListIterator<StatusBarItem> it = m_statusBarItems.begin();
00121     for ( ; it != m_statusBarItems.end() ; ++it )
00122       (*it).ensureItemShown( sb );
00123   }
00124   else
00125   {
00126     QValueListIterator<StatusBarItem> it = m_statusBarItems.begin();
00127     for ( ; it != m_statusBarItems.end() ; ++it )
00128       (*it).ensureItemHidden( sb );
00129   }
00130 
00131   return false;
00132 
00133 }
00134 
00135 KStatusBar * StatusBarExtension::statusBar() const
00136 {
00137   if ( !m_statusBar )  {
00138     QWidget* w = static_cast<KParts::ReadOnlyPart*>(parent())->widget();
00139     KMainWindow* mw = dynamic_cast<KMainWindow *>( w->topLevelWidget() );
00140     if ( mw )
00141       m_statusBar = mw->statusBar();
00142   }
00143   return m_statusBar;
00144 }
00145 
00146 void StatusBarExtension::setStatusBar( KStatusBar* status )
00147 {
00148   m_statusBar = status;
00149 }
00150 
00151 void StatusBarExtension::addStatusBarItem( QWidget * widget, int stretch, bool permanent )
00152 {
00153   m_statusBarItems.append( StatusBarItem( widget, stretch, permanent ) );
00154   QValueListIterator<StatusBarItem> it = m_statusBarItems.fromLast();
00155   KStatusBar * sb = statusBar();
00156   Q_ASSERT(sb);
00157   if (sb)
00158     (*it).ensureItemShown( sb );
00159 }
00160 
00161 void StatusBarExtension::removeStatusBarItem( QWidget * widget )
00162 {
00163   KStatusBar * sb = statusBar();
00164   QValueListIterator<StatusBarItem> it = m_statusBarItems.begin();
00165   for ( ; it != m_statusBarItems.end() ; ++it )
00166     if ( (*it).widget() == widget )
00167     {
00168       if ( sb )
00169         (*it).ensureItemHidden( sb );
00170       m_statusBarItems.remove( it );
00171       break;
00172     }
00173   if ( it == m_statusBarItems.end() )
00174     kdWarning(1000) << "StatusBarExtension::removeStatusBarItem. Widget not found : " << widget << endl;
00175 }
00176 
00177 #include "statusbarextension.moc"
00178 
00179 // vim: ts=2 sw=2 et
KDE Logo
This file is part of the documentation for kparts Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed May 5 07:17:14 2004 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003