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) const
00040 {
00041 QHash<int, QLabel*>::const_iterator it = qFind(items, object);
00042 if (it != items.constEnd())
00043 return it.key();
00044
00045
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
00077
00078
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