• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kstatusbar.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1997 Mark Donohoe (donohoe@kde.org)
00003               (C) 1997,1998, 2000 Sven Radej (radej@kde.org)
00004               (C) 2007 Aron Boström (aron.bostrom@gmail.com)
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kstatusbar.h"
00023 
00024 #include <QtCore/QHash>
00025 #include <QtCore/QEvent>
00026 #include <QtGui/QLabel>
00027 
00028 #include <kdebug.h>
00029 #include <kglobal.h>
00030 #include <ksharedconfig.h>
00031 #include <kconfiggroup.h>
00032 
00033 #include "ksqueezedtextlabel.h"
00034 
00035 
00036 class KStatusBarPrivate
00037 {
00038 public:
00039     int id(QObject* object) const
00040     {
00041         QHash<int, QLabel*>::const_iterator it = qFind(items, object);
00042         if (it != items.constEnd())
00043             return it.key();
00044         // Not found. This happens when a subclass uses an eventFilter too,
00045         // on objects not registered here.
00046         return -1;
00047     }
00048 
00049     QHash<int, QLabel*> items;
00050 };
00051 
00052 
00053 bool KStatusBar::eventFilter(QObject* object, QEvent* event)
00054 {
00055     if ( event->type() == QEvent::MouseButtonPress ) {
00056         const int id = d->id(object);
00057         if (id > -1) {
00058             emit pressed( d->id( object ) );
00059             return true;
00060         }
00061     }
00062     else if ( event->type() == QEvent::MouseButtonRelease ) {
00063         const int id = d->id(object);
00064         if (id > -1) {
00065             emit released( d->id( object ) );
00066             return true;
00067         }
00068     }
00069     return QStatusBar::eventFilter(object, event);
00070 }
00071 
00072 KStatusBar::KStatusBar( QWidget *parent )
00073   : QStatusBar( parent ),
00074     d(new KStatusBarPrivate)
00075 {
00076     // make the size grip stuff configurable
00077     // ...but off by default (sven)
00078     // ...but on by default on OSX, else windows with a KStatusBar are not resizable at all (marijn)
00079     KSharedConfig::Ptr config = KGlobal::config();
00080     KConfigGroup group( config, QLatin1String("StatusBar style") );
00081 #ifdef Q_WS_MAC
00082     bool grip_enabled = group.readEntry(QLatin1String("SizeGripEnabled"), true);
00083 #else
00084     bool grip_enabled = group.readEntry(QLatin1String("SizeGripEnabled"), false);
00085 #endif
00086     setSizeGripEnabled(grip_enabled);
00087 
00088     setStyleSheet( "::item { border: 0px; }" );
00089 }
00090 
00091 KStatusBar::~KStatusBar ()
00092 {
00093   delete d;
00094 }
00095 
00096 void KStatusBar::insertItem( const QString& text, int id, int stretch)
00097 {
00098     if ( d->items[id] ) {
00099         kDebug() << "KStatusBar::insertItem: item id " << id << " already exists.";
00100     }
00101 
00102     KSqueezedTextLabel *l = new KSqueezedTextLabel( text, this );
00103     l->installEventFilter( this );
00104     l->setFixedHeight( fontMetrics().height() + 2 );
00105     l->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
00106     d->items.insert( id, l );
00107     addPermanentWidget( l, stretch );
00108     l->show();
00109 }
00110 
00111 void KStatusBar::insertFixedItem( const QString& text, int id )
00112 {
00113     insertItem( text, id, 0 );
00114     setItemFixed( id );
00115 }
00116 
00117 
00118 void KStatusBar::insertPermanentItem( const QString& text, int id, int stretch)
00119 {
00120     if (d->items[id]) {
00121         kDebug() << "KStatusBar::insertPermanentItem: item id " << id << " already exists.";
00122     }
00123 
00124     QLabel *l = new QLabel( text, this );
00125     l->installEventFilter( this );
00126     l->setFixedHeight( fontMetrics().height() + 2 );
00127     l->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
00128     d->items.insert( id, l );
00129     addPermanentWidget( l, stretch );
00130     l->show();
00131 }
00132 
00133 void KStatusBar::insertPermanentFixedItem( const QString& text, int id )
00134 {
00135     insertPermanentItem( text, id, 0 );
00136     setItemFixed( id );
00137 }
00138 
00139 void KStatusBar::removeItem (int id)
00140 {
00141     if ( d->items.contains( id ) ) {
00142         QLabel *label = d->items[id];
00143         removeWidget( label );
00144         d->items.remove( id );
00145         delete label;
00146     } else {
00147         kDebug() << "KStatusBar::removeItem: bad item id: " << id;
00148     }
00149 }
00150 
00151 bool KStatusBar::hasItem( int id ) const
00152 {
00153     return d->items.contains(id);
00154 }
00155 
00156 QString KStatusBar::itemText( int id ) const
00157 {
00158     if ( !hasItem( id ) ) {
00159         return QString();
00160     }
00161 
00162     return d->items[id]->text();
00163 }
00164 
00165 void KStatusBar::changeItem( const QString& text, int id )
00166 {
00167     QLabel *label = d->items[id];
00168     KSqueezedTextLabel *squeezed = qobject_cast<KSqueezedTextLabel*>( label );
00169 
00170     if ( squeezed ) {
00171         squeezed->setText( text );
00172     } else if ( label ) {
00173         label->setText( text );
00174         if ( label->minimumWidth () != label->maximumWidth () ) {
00175             reformat();
00176         }
00177     } else {
00178         kDebug() << "KStatusBar::changeItem: bad item id: " << id;
00179     }
00180 }
00181 
00182 void KStatusBar::setItemAlignment (int id, Qt::Alignment alignment)
00183 {
00184     QLabel *label = qobject_cast<QLabel*>( d->items[id] );
00185     if ( label ) {
00186         label->setAlignment( alignment );
00187     } else {
00188         kDebug() << "KStatusBar::setItemAlignment: bad item id: " << id;
00189     }
00190 }
00191 
00192 void KStatusBar::setItemFixed(int id, int w)
00193 {
00194     QLabel *label = qobject_cast<QLabel*>(d->items[id]);
00195     if ( label ) {
00196         if ( w == -1 ) {
00197             w = fontMetrics().boundingRect(label->text()).width()+3;
00198         }
00199 
00200         label->setFixedWidth(w);
00201     } else {
00202         kDebug() << "KStatusBar::setItemFixed: bad item id: " << id;
00203     }
00204 }
00205 
00206 #include "kstatusbar.moc"
00207 

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal