KDEUI
ktoolbarlabelaction.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 #include "ktoolbarlabelaction.h"
00020 #include "ktoolbar.h"
00021
00022 #include <QtCore/QPointer>
00023 #include <QtGui/QApplication>
00024 #include <QtGui/QLabel>
00025
00026 class KToolBarLabelAction::Private
00027 {
00028 public:
00029 QPointer<QAction> buddy;
00030 QPointer<QLabel> label;
00031 };
00032
00033 KToolBarLabelAction::KToolBarLabelAction(const QString &text, QObject *parent)
00034 : KAction(text, parent),
00035 d( new Private )
00036 {
00037 d->label = 0;
00038 }
00039
00040 KToolBarLabelAction::KToolBarLabelAction(QAction* buddy, const QString &text, QObject *parent)
00041 : KAction(text, parent),
00042 d( new Private )
00043 {
00044 setBuddy( buddy );
00045
00046 d->label = 0;
00047 }
00048
00049 KToolBarLabelAction::~KToolBarLabelAction()
00050 {
00051 delete d;
00052 }
00053
00054 void KToolBarLabelAction::setBuddy( QAction* buddy )
00055 {
00056 d->buddy = buddy;
00057
00058 QList<QLabel*> labels;
00059 foreach ( QWidget* widget, associatedWidgets() )
00060 if ( QToolBar* toolBar = qobject_cast<QToolBar*>( widget ) )
00061 if ( QLabel* label = qobject_cast<QLabel*>( toolBar->widgetForAction( this ) ) )
00062 labels.append( label );
00063
00064 foreach ( QWidget* widget, buddy->associatedWidgets() )
00065 if ( QToolBar* toolBar = qobject_cast<QToolBar*>( widget ) ) {
00066 QWidget* newBuddy = toolBar->widgetForAction( buddy );
00067 foreach ( QLabel* label, labels )
00068 label->setBuddy( newBuddy );
00069 return;
00070 }
00071 }
00072
00073 QAction* KToolBarLabelAction::buddy() const
00074 {
00075 return d->buddy;
00076 }
00077
00078 bool KToolBarLabelAction::event( QEvent *event )
00079 {
00080 if ( event->type() == QEvent::ActionChanged ) {
00081 if ( d->label && text() != d->label->text() ) {
00082 emit textChanged( text() );
00083 d->label->setText(text());
00084 }
00085 }
00086
00087 return KAction::event( event );
00088 }
00089
00090 bool KToolBarLabelAction::eventFilter( QObject *watched, QEvent *event )
00091 {
00092 if ( d->label && d->buddy && event->type() == QEvent::PolishRequest && watched == d->label) {
00093 foreach ( QWidget* widget, d->buddy->associatedWidgets() ) {
00094 if ( QToolBar* toolBar = qobject_cast<QToolBar*>( widget ) ) {
00095 QWidget* newBuddy = toolBar->widgetForAction( d->buddy );
00096 d->label->setBuddy( newBuddy );
00097 }
00098 }
00099 }
00100
00101 return KAction::eventFilter( watched, event );
00102 }
00103
00104 QWidget *KToolBarLabelAction::createWidget( QWidget* _parent )
00105 {
00106 QToolBar *parent = qobject_cast<QToolBar *>(_parent);
00107 if (!parent)
00108 return KAction::createWidget(_parent);
00109 if (!d->label) {
00110 d->label = new QLabel( parent );
00111
00116 d->label->setBackgroundRole( QPalette::Button );
00117 d->label->setAlignment( (QApplication::isRightToLeft() ? Qt::AlignRight : Qt::AlignLeft) |
00118 Qt::AlignVCenter );
00119 d->label->adjustSize();
00120 d->label->setText(text());
00121 d->label->installEventFilter( this );
00122 }
00123
00124 return d->label;
00125 }
00126
00127 #include "ktoolbarlabelaction.moc"