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 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