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 00089 KStatusBar::~KStatusBar () 00090 { 00091 delete d; 00092 } 00093 00094 void KStatusBar::insertItem( const QString& text, int id, int stretch) 00095 { 00096 if ( d->items[id] ) { 00097 kDebug() << "KStatusBar::insertItem: item id " << id << " already exists."; 00098 } 00099 00100 KSqueezedTextLabel *l = new KSqueezedTextLabel( text, this ); 00101 l->installEventFilter( this ); 00102 l->setFixedHeight( fontMetrics().height() + 2 ); 00103 l->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter ); 00104 d->items.insert( id, l ); 00105 addPermanentWidget( l, stretch ); 00106 l->show(); 00107 } 00108 00109 void KStatusBar::insertFixedItem( const QString& text, int id ) 00110 { 00111 insertItem( text, id, 0 ); 00112 setItemFixed( id ); 00113 } 00114 00115 00116 void KStatusBar::insertPermanentItem( const QString& text, int id, int stretch) 00117 { 00118 if (d->items[id]) { 00119 kDebug() << "KStatusBar::insertPermanentItem: item id " << id << " already exists."; 00120 } 00121 00122 QLabel *l = new QLabel( text, this ); 00123 l->installEventFilter( this ); 00124 l->setFixedHeight( fontMetrics().height() + 2 ); 00125 l->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter ); 00126 d->items.insert( id, l ); 00127 addPermanentWidget( l, stretch ); 00128 l->show(); 00129 } 00130 00131 void KStatusBar::insertPermanentFixedItem( const QString& text, int id ) 00132 { 00133 insertPermanentItem( text, id, 0 ); 00134 setItemFixed( id ); 00135 } 00136 00137 void KStatusBar::removeItem (int id) 00138 { 00139 if ( d->items.contains( id ) ) { 00140 QLabel *label = d->items[id]; 00141 removeWidget( label ); 00142 d->items.remove( id ); 00143 delete label; 00144 } else { 00145 kDebug() << "KStatusBar::removeItem: bad item id: " << id; 00146 } 00147 } 00148 00149 bool KStatusBar::hasItem( int id ) const 00150 { 00151 return d->items.contains(id); 00152 } 00153 00154 QString KStatusBar::itemText( int id ) const 00155 { 00156 if ( !hasItem( id ) ) { 00157 return QString(); 00158 } 00159 00160 return d->items[id]->text(); 00161 } 00162 00163 void KStatusBar::changeItem( const QString& text, int id ) 00164 { 00165 QLabel *label = d->items[id]; 00166 KSqueezedTextLabel *squeezed = qobject_cast<KSqueezedTextLabel*>( label ); 00167 00168 if ( squeezed ) { 00169 squeezed->setText( text ); 00170 } else if ( label ) { 00171 label->setText( text ); 00172 if ( label->minimumWidth () != label->maximumWidth () ) { 00173 reformat(); 00174 } 00175 } else { 00176 kDebug() << "KStatusBar::changeItem: bad item id: " << id; 00177 } 00178 } 00179 00180 void KStatusBar::setItemAlignment (int id, Qt::Alignment alignment) 00181 { 00182 QLabel *label = qobject_cast<QLabel*>( d->items[id] ); 00183 if ( label ) { 00184 label->setAlignment( alignment ); 00185 } else { 00186 kDebug() << "KStatusBar::setItemAlignment: bad item id: " << id; 00187 } 00188 } 00189 00190 void KStatusBar::setItemFixed(int id, int w) 00191 { 00192 QLabel *label = qobject_cast<QLabel*>(d->items[id]); 00193 if ( label ) { 00194 if ( w == -1 ) { 00195 w = fontMetrics().boundingRect(label->text()).width()+3; 00196 } 00197 00198 label->setFixedWidth(w); 00199 } else { 00200 kDebug() << "KStatusBar::setItemFixed: bad item id: " << id; 00201 } 00202 } 00203 00204 #include "kstatusbar.moc" 00205
KDE 4.6 API Reference