KDEUI
kstatusbar.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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)
00040 {
00041 QHash<int, QLabel*>::const_iterator it = items.constBegin();
00042 QHash<int, QLabel*>::const_iterator end = items.constEnd();
00043 while ( it != end ) {
00044 if ( object == it.value() ) {
00045 return it.key();
00046 }
00047
00048 ++it;
00049 }
00050 kDebug() << "Danger: Unable to find originating event object in object list. This shouldn't happen!";
00051 return -1;
00052 }
00053
00054 QHash<int, QLabel*> items;
00055 };
00056
00057
00058 bool KStatusBar::eventFilter(QObject* object, QEvent* event)
00059 {
00060 if ( event->type() == QEvent::MouseButtonPress ) {
00061 emit pressed( d->id( object ) );
00062 return true;
00063 }
00064 else if ( event->type() == QEvent::MouseButtonRelease ) {
00065 emit released( d->id( object ) );
00066 return true;
00067 }
00068 return false;
00069 }
00070
00071 KStatusBar::KStatusBar( QWidget *parent )
00072 : QStatusBar( parent ),
00073 d(new KStatusBarPrivate)
00074 {
00075
00076
00077
00078 KSharedConfig::Ptr config = KGlobal::config();
00079 KConfigGroup group( config, QLatin1String("StatusBar style") );
00080 #ifdef Q_WS_MAC
00081 bool grip_enabled = group.readEntry(QLatin1String("SizeGripEnabled"), true);
00082 #else
00083 bool grip_enabled = group.readEntry(QLatin1String("SizeGripEnabled"), false);
00084 #endif
00085 setSizeGripEnabled(grip_enabled);
00086
00087 setStyleSheet( "::item { border: 0px; }" );
00088 }
00089
00090 KStatusBar::~KStatusBar ()
00091 {
00092 delete d;
00093 }
00094
00095 void KStatusBar::insertItem( const QString& text, int id, int stretch)
00096 {
00097 if ( d->items[id] ) {
00098 kDebug() << "KStatusBar::insertItem: item id " << id << " already exists.";
00099 }
00100
00101 KSqueezedTextLabel *l = new KSqueezedTextLabel( text, this );
00102 l->installEventFilter( this );
00103 l->setFixedHeight( fontMetrics().height() + 2 );
00104 l->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
00105 d->items.insert( id, l );
00106 addPermanentWidget( l, stretch );
00107 l->show();
00108 }
00109
00110 void KStatusBar::insertFixedItem( const QString& text, int id )
00111 {
00112 insertItem( text, id, 0 );
00113 setItemFixed( id );
00114 }
00115
00116
00117 void KStatusBar::insertPermanentItem( const QString& text, int id, int stretch)
00118 {
00119 if (d->items[id]) {
00120 kDebug() << "KStatusBar::insertPermanentItem: item id " << id << " already exists.";
00121 }
00122
00123 QLabel *l = new QLabel( text, this );
00124 l->installEventFilter( this );
00125 l->setFixedHeight( fontMetrics().height() + 2 );
00126 l->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
00127 d->items.insert( id, l );
00128 addPermanentWidget( l, stretch );
00129 l->show();
00130 }
00131
00132 void KStatusBar::insertPermanentFixedItem( const QString& text, int id )
00133 {
00134 insertPermanentItem( text, id, 0 );
00135 setItemFixed( id );
00136 }
00137
00138 void KStatusBar::removeItem (int id)
00139 {
00140 if ( d->items.contains( id ) ) {
00141 QLabel *label = d->items[id];
00142 removeWidget( label );
00143 d->items.remove( id );
00144 delete label;
00145 } else {
00146 kDebug() << "KStatusBar::removeItem: bad item id: " << id;
00147 }
00148 }
00149
00150 bool KStatusBar::hasItem( int id ) const
00151 {
00152 return d->items.contains(id);
00153 }
00154
00155 QString KStatusBar::itemText( int id ) const
00156 {
00157 if ( !hasItem( id ) ) {
00158 return QString();
00159 }
00160
00161 return d->items[id]->text();
00162 }
00163
00164 void KStatusBar::changeItem( const QString& text, int id )
00165 {
00166 QLabel *label = d->items[id];
00167 KSqueezedTextLabel *squeezed = qobject_cast<KSqueezedTextLabel*>( label );
00168
00169 if ( squeezed ) {
00170 squeezed->setText( text );
00171 } else if ( label ) {
00172 label->setText( text );
00173 if ( label->minimumWidth () != label->maximumWidth () ) {
00174 reformat();
00175 }
00176 } else {
00177 kDebug() << "KStatusBar::changeItem: bad item id: " << id;
00178 }
00179 }
00180
00181 void KStatusBar::setItemAlignment (int id, Qt::Alignment alignment)
00182 {
00183 QLabel *label = qobject_cast<QLabel*>( d->items[id] );
00184 if ( label ) {
00185 label->setAlignment( alignment );
00186 } else {
00187 kDebug() << "KStatusBar::setItemAlignment: bad item id: " << id;
00188 }
00189 }
00190
00191 void KStatusBar::setItemFixed(int id, int w)
00192 {
00193 QLabel *label = qobject_cast<QLabel*>(d->items[id]);
00194 if ( label ) {
00195 if ( w == -1 ) {
00196 w = fontMetrics().boundingRect(label->text()).width()+3;
00197 }
00198
00199 label->setFixedWidth(w);
00200 } else {
00201 kDebug() << "KStatusBar::setItemFixed: bad item id: " << id;
00202 }
00203 }
00204
00205 #include "kstatusbar.moc"
00206