00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <config.h>
00025 #include <string.h>
00026
00027 #include "ktoolbarbutton.h"
00028 #include "ktoolbar.h"
00029
00030 #include <qstyle.h>
00031 #include <qimage.h>
00032 #include <qtimer.h>
00033 #include <qdrawutil.h>
00034 #include <qtooltip.h>
00035 #include <qbitmap.h>
00036 #include <qpopupmenu.h>
00037 #include <qcursor.h>
00038 #include <qpainter.h>
00039 #include <qlayout.h>
00040
00041 #include <kapplication.h>
00042 #include <kdebug.h>
00043 #include <kglobal.h>
00044 #include <kglobalsettings.h>
00045 #include <kiconeffect.h>
00046 #include <kiconloader.h>
00047
00048
00049 #include <kmainwindow.h>
00050
00051 template class QIntDict<KToolBarButton>;
00052
00053 class KToolBarButtonPrivate
00054 {
00055 public:
00056 KToolBarButtonPrivate()
00057 {
00058 m_buttonDown = false;
00059
00060 m_noStyle = false;
00061 m_isSeparator = false;
00062 m_isRadio = false;
00063 m_highlight = false;
00064 m_isRaised = false;
00065 m_isActive = false;
00066
00067 m_iconName = QString::null;
00068 m_iconText = KToolBar::IconOnly;
00069 m_iconSize = 0;
00070
00071 m_parent = 0;
00072 m_instance = KGlobal::instance();
00073 }
00074 ~KToolBarButtonPrivate()
00075 {
00076 }
00077
00078 int m_id;
00079 bool m_buttonDown : 1;
00080 bool m_noStyle: 1;
00081 bool m_isSeparator: 1;
00082 bool m_isRadio: 1;
00083 bool m_highlight: 1;
00084 bool m_isRaised: 1;
00085 bool m_isActive: 1;
00086
00087 QString m_iconName;
00088
00089 KToolBar *m_parent;
00090 KToolBar::IconText m_iconText;
00091 int m_iconSize;
00092 QSize size;
00093
00094 QPoint m_mousePressPos;
00095
00096 KInstance *m_instance;
00097 };
00098
00099
00100 KToolBarButton::KToolBarButton( QWidget *_parent, const char *_name )
00101 : QToolButton( _parent , _name)
00102 {
00103 d = new KToolBarButtonPrivate;
00104
00105 resize(6,6);
00106 hide();
00107 d->m_isSeparator = true;
00108 }
00109
00110 KToolBarButton::KToolBarButton( const QString& _icon, int _id,
00111 QWidget *_parent, const char *_name,
00112 const QString &_txt, KInstance *_instance )
00113 : QToolButton( _parent, _name ), d( 0 )
00114 {
00115 d = new KToolBarButtonPrivate;
00116
00117 d->m_id = _id;
00118 QToolButton::setTextLabel(_txt);
00119 d->m_instance = _instance;
00120
00121 d->m_parent = dynamic_cast<KToolBar*>(_parent);
00122 if (d->m_parent) {
00123 connect(d->m_parent, SIGNAL( modechange() ),
00124 this, SLOT( modeChange() ));
00125 }
00126
00127 setFocusPolicy( NoFocus );
00128
00129
00130 connect(this, SIGNAL( clicked() ),
00131 this, SLOT( slotClicked() ) );
00132 connect(this, SIGNAL( pressed() ),
00133 this, SLOT( slotPressed() ) );
00134 connect(this, SIGNAL( released() ),
00135 this, SLOT( slotReleased() ) );
00136 installEventFilter(this);
00137
00138 d->m_iconName = _icon;
00139
00140
00141 modeChange();
00142 }
00143
00144 KToolBarButton::KToolBarButton( const QPixmap& pixmap, int _id,
00145 QWidget *_parent, const char *name,
00146 const QString& txt)
00147 : QToolButton( _parent, name ), d( 0 )
00148 {
00149 d = new KToolBarButtonPrivate;
00150
00151 d->m_id = _id;
00152 QToolButton::setTextLabel(txt);
00153
00154 d->m_parent = dynamic_cast<KToolBar*>(_parent);
00155 if (d->m_parent) {
00156 connect(d->m_parent, SIGNAL( modechange() ),
00157 this, SLOT( modeChange() ));
00158 }
00159
00160 setFocusPolicy( NoFocus );
00161
00162
00163 connect(this, SIGNAL( clicked() ),
00164 this, SLOT( slotClicked() ));
00165 connect(this, SIGNAL( pressed() ),
00166 this, SLOT( slotPressed() ));
00167 connect(this, SIGNAL( released() ),
00168 this, SLOT( slotReleased() ));
00169 installEventFilter(this);
00170
00171
00172 setIconSet( QIconSet( pixmap ));
00173 modeChange();
00174 }
00175
00176 KToolBarButton::~KToolBarButton()
00177 {
00178 delete d; d = 0;
00179 }
00180
00181 void KToolBarButton::modeChange()
00182 {
00183 QSize mysize;
00184
00185
00186 if (d->m_parent) {
00187 d->m_highlight = d->m_parent->highlight();
00188 d->m_iconText = d->m_parent->iconText();
00189
00190 d->m_iconSize = d->m_parent->iconSize();
00191 }
00192 if (!d->m_iconName.isNull())
00193 setIcon(d->m_iconName);
00194
00195
00196 int pix_width = d->m_iconSize;
00197 if ( d->m_iconSize == 0 ) {
00198 if (d->m_parent && !strcmp(d->m_parent->name(), "mainToolBar"))
00199 pix_width = IconSize( KIcon::MainToolbar );
00200 else
00201 pix_width = IconSize( KIcon::Toolbar );
00202 }
00203 int pix_height = pix_width;
00204
00205 int text_height = 0;
00206 int text_width = 0;
00207
00208 QToolTip::remove(this);
00209 if (d->m_iconText != KToolBar::IconOnly)
00210 {
00211
00212 QFont tmp_font = KGlobalSettings::toolBarFont();
00213
00214
00215 QFontMetrics fm(tmp_font);
00216
00217 text_height = fm.lineSpacing();
00218 text_width = fm.width(textLabel());
00219
00220
00221 }
00222 else
00223 {
00224 QToolTip::add(this, textLabel());
00225 }
00226
00227 switch (d->m_iconText)
00228 {
00229 case KToolBar::IconOnly:
00230 mysize = QSize(pix_width, pix_height);
00231 break;
00232
00233 case KToolBar::IconTextRight:
00234 mysize = QSize(pix_width + text_width + 4, pix_height);
00235 break;
00236
00237 case KToolBar::TextOnly:
00238 mysize = QSize(text_width + 4, text_height);
00239 break;
00240
00241 case KToolBar::IconTextBottom:
00242 mysize = QSize((text_width + 4 > pix_width) ? text_width + 4 : pix_width, pix_height + text_height);
00243 break;
00244
00245 default:
00246 break;
00247 }
00248
00249 mysize = style().sizeFromContents(QStyle::CT_ToolButton, this, mysize).
00250 expandedTo(QApplication::globalStrut());
00251
00252
00253 if (mysize.height() > mysize.width())
00254 mysize.setWidth(mysize.height());
00255
00256 d->size = mysize;
00257 updateGeometry();
00258 }
00259
00260 void KToolBarButton::setTextLabel( const QString& text, bool tipToo)
00261 {
00262 if (text.isNull())
00263 return;
00264
00265 QString txt(text);
00266 if (txt.endsWith(QString::fromLatin1("...")))
00267 txt.truncate(txt.length() - 3);
00268
00269 QToolButton::setTextLabel(txt, tipToo);
00270 update();
00271 }
00272
00273 void KToolBarButton::setText( const QString& text)
00274 {
00275 setTextLabel(text, true);
00276 modeChange();
00277 }
00278
00279 void KToolBarButton::setIcon( const QString &icon )
00280 {
00281 d->m_iconName = icon;
00282 if (d->m_parent)
00283 d->m_iconSize = d->m_parent->iconSize();
00284
00285 if (d->m_parent && !strcmp(d->m_parent->name(), "mainToolBar"))
00286 QToolButton::setIconSet( d->m_instance->iconLoader()->loadIconSet(
00287 d->m_iconName, KIcon::MainToolbar, d->m_iconSize ));
00288 else
00289 QToolButton::setIconSet( d->m_instance->iconLoader()->loadIconSet(
00290 d->m_iconName, KIcon::Toolbar, d->m_iconSize ));
00291 }
00292
00293 void KToolBarButton::setIconSet( const QIconSet &iconset )
00294 {
00295 QToolButton::setIconSet( iconset );
00296 }
00297
00298
00299 void KToolBarButton::setPixmap( const QPixmap &pixmap )
00300 {
00301 if( pixmap.isNull())
00302 {
00303 QToolButton::setPixmap( pixmap );
00304 return;
00305 }
00306 QIconSet set = iconSet();
00307 set.setPixmap( pixmap, QIconSet::Automatic, QIconSet::Active );
00308 QToolButton::setIconSet( set );
00309 }
00310
00311 void KToolBarButton::setDefaultPixmap( const QPixmap &pixmap )
00312 {
00313 QIconSet set = iconSet();
00314 set.setPixmap( pixmap, QIconSet::Automatic, QIconSet::Normal );
00315 QToolButton::setIconSet( set );
00316 }
00317
00318 void KToolBarButton::setDisabledPixmap( const QPixmap &pixmap )
00319 {
00320 QIconSet set = iconSet();
00321 set.setPixmap( pixmap, QIconSet::Automatic, QIconSet::Disabled );
00322 QToolButton::setIconSet( set );
00323 }
00324
00325 void KToolBarButton::setDefaultIcon( const QString& icon )
00326 {
00327 QIconSet set = iconSet();
00328 QPixmap pm;
00329 if (d->m_parent && !strcmp(d->m_parent->name(), "mainToolBar"))
00330 pm = d->m_instance->iconLoader()->loadIcon( icon, KIcon::MainToolbar,
00331 d->m_iconSize );
00332 else
00333 pm = d->m_instance->iconLoader()->loadIcon( icon, KIcon::Toolbar,
00334 d->m_iconSize );
00335 set.setPixmap( pm, QIconSet::Automatic, QIconSet::Normal );
00336 QToolButton::setIconSet( set );
00337 }
00338
00339 void KToolBarButton::setDisabledIcon( const QString& icon )
00340 {
00341 QIconSet set = iconSet();
00342 QPixmap pm;
00343 if (d->m_parent && !strcmp(d->m_parent->name(), "mainToolBar"))
00344 pm = d->m_instance->iconLoader()->loadIcon( icon, KIcon::MainToolbar,
00345 d->m_iconSize );
00346 else
00347 pm = d->m_instance->iconLoader()->loadIcon( icon, KIcon::Toolbar,
00348 d->m_iconSize );
00349 set.setPixmap( pm, QIconSet::Automatic, QIconSet::Disabled );
00350 QToolButton::setIconSet( set );
00351 }
00352
00353 QPopupMenu *KToolBarButton::popup()
00354 {
00355
00356
00357 return QToolButton::popup();
00358 }
00359
00360 void KToolBarButton::setPopup(QPopupMenu *p, bool)
00361 {
00362 QToolButton::setPopup(p);
00363 QToolButton::setPopupDelay(-1);
00364 }
00365
00366
00367 void KToolBarButton::setDelayedPopup (QPopupMenu *p, bool)
00368 {
00369 QToolButton::setPopup(p);
00370 QToolButton::setPopupDelay(QApplication::startDragTime());
00371 }
00372
00373 void KToolBarButton::leaveEvent(QEvent *)
00374 {
00375 if( d->m_isRaised || d->m_isActive )
00376 {
00377 d->m_isRaised = false;
00378 d->m_isActive = false;
00379 repaint(false);
00380 }
00381
00382 emit highlighted(d->m_id, false);
00383 }
00384
00385 void KToolBarButton::enterEvent(QEvent *)
00386 {
00387 if (d->m_highlight)
00388 {
00389 if (isEnabled())
00390 {
00391 d->m_isActive = true;
00392 if (!isToggleButton())
00393 d->m_isRaised = true;
00394 }
00395 else
00396 {
00397 d->m_isRaised = false;
00398 d->m_isActive = false;
00399 }
00400
00401 repaint(false);
00402 }
00403 emit highlighted(d->m_id, true);
00404 }
00405
00406 bool KToolBarButton::eventFilter(QObject *o, QEvent *ev)
00407 {
00408 if ((KToolBarButton *)o == this)
00409 {
00410
00411
00412
00413 if (QToolButton::popup())
00414 {
00415 if (ev->type() == QEvent::MouseButtonPress)
00416 {
00417 QMouseEvent* mev = static_cast<QMouseEvent*>(ev);
00418 d->m_mousePressPos = mev->pos();
00419 }
00420 else if (ev->type() == QEvent::MouseMove)
00421 {
00422 QMouseEvent* mev = static_cast<QMouseEvent*>(ev);
00423 if ((mev->pos() - d->m_mousePressPos).manhattanLength()
00424 > KGlobalSettings::dndEventDelay())
00425 {
00426 openPopup();
00427 return true;
00428 }
00429 }
00430 }
00431
00432 if (d->m_isRadio &&
00433 (ev->type() == QEvent::MouseButtonPress ||
00434 ev->type() == QEvent::MouseButtonRelease ||
00435 ev->type() == QEvent::MouseButtonDblClick) && isOn())
00436 return true;
00437
00438
00439 if (ev->type() == QEvent::MouseButtonDblClick)
00440 {
00441 emit doubleClicked(d->m_id);
00442 return false;
00443 }
00444 }
00445
00446 return QToolButton::eventFilter(o, ev);
00447 }
00448
00449 void KToolBarButton::mousePressEvent( QMouseEvent * e )
00450 {
00451 d->m_buttonDown = true;
00452
00453 if ( e->button() == MidButton )
00454 {
00455
00456 QMouseEvent ev( QEvent::MouseButtonPress, e->pos(), e->globalPos(), LeftButton, e->state() );
00457 QToolButton::mousePressEvent(&ev);
00458 return;
00459 }
00460 QToolButton::mousePressEvent(e);
00461 }
00462
00463 void KToolBarButton::mouseReleaseEvent( QMouseEvent * e )
00464 {
00465 Qt::ButtonState state = Qt::ButtonState(e->button() | (e->state() & KeyButtonMask));
00466 if ( e->button() == MidButton )
00467 {
00468 QMouseEvent ev( QEvent::MouseButtonRelease, e->pos(), e->globalPos(), LeftButton, e->state() );
00469 QToolButton::mouseReleaseEvent(&ev);
00470 }
00471 else
00472 QToolButton::mouseReleaseEvent(e);
00473
00474 if ( !d->m_buttonDown )
00475 return;
00476 d->m_buttonDown = false;
00477
00478 if ( hitButton( e->pos() ) )
00479 emit buttonClicked( d->m_id, state );
00480 }
00481
00482 void KToolBarButton::drawButton( QPainter *_painter )
00483 {
00484 QStyle::SFlags flags = QStyle::Style_Default;
00485 QStyle::SCFlags active = QStyle::SC_None;
00486
00487 if (isDown()) {
00488 flags |= QStyle::Style_Down;
00489 active |= QStyle::SC_ToolButton;
00490 }
00491 if (isEnabled()) flags |= QStyle::Style_Enabled;
00492 if (isOn()) flags |= QStyle::Style_On;
00493 if (isEnabled() && hasMouse()) flags |= QStyle::Style_Raised;
00494 if (hasFocus()) flags |= QStyle::Style_HasFocus;
00495
00496
00497 style().drawComplexControl(QStyle::CC_ToolButton, _painter, this, rect(),
00498 colorGroup(), flags, QStyle::SC_ToolButton, active, QStyleOption());
00499
00500 int dx, dy;
00501 QFont tmp_font(KGlobalSettings::toolBarFont());
00502 QFontMetrics fm(tmp_font);
00503 QRect textRect;
00504 int textFlags = 0;
00505
00506 if (d->m_iconText == KToolBar::IconOnly)
00507 {
00508 QPixmap pixmap = iconSet().pixmap( QIconSet::Automatic,
00509 isEnabled() ? (d->m_isActive ? QIconSet::Active : QIconSet::Normal) :
00510 QIconSet::Disabled,
00511 isOn() ? QIconSet::On : QIconSet::Off );
00512 if( !pixmap.isNull())
00513 {
00514 dx = ( width() - pixmap.width() ) / 2;
00515 dy = ( height() - pixmap.height() ) / 2;
00516 if ( isDown() && style().styleHint(QStyle::SH_GUIStyle) == WindowsStyle )
00517 {
00518 ++dx;
00519 ++dy;
00520 }
00521 _painter->drawPixmap( dx, dy, pixmap );
00522 }
00523 }
00524 else if (d->m_iconText == KToolBar::IconTextRight)
00525 {
00526 QPixmap pixmap = iconSet().pixmap( QIconSet::Automatic,
00527 isEnabled() ? (d->m_isActive ? QIconSet::Active : QIconSet::Normal) :
00528 QIconSet::Disabled,
00529 isOn() ? QIconSet::On : QIconSet::Off );
00530 if( !pixmap.isNull())
00531 {
00532 dx = 4;
00533 dy = ( height() - pixmap.height() ) / 2;
00534 if ( isDown() && style().styleHint(QStyle::SH_GUIStyle) == WindowsStyle )
00535 {
00536 ++dx;
00537 ++dy;
00538 }
00539 _painter->drawPixmap( dx, dy, pixmap );
00540 }
00541
00542 if (!textLabel().isNull())
00543 {
00544 textFlags = AlignVCenter|AlignLeft;
00545 if (!pixmap.isNull())
00546 dx = 4 + pixmap.width() + 2;
00547 else
00548 dx = 4;
00549 dy = 0;
00550 if ( isDown() && style().styleHint(QStyle::SH_GUIStyle) == WindowsStyle )
00551 {
00552 ++dx;
00553 ++dy;
00554 }
00555 textRect = QRect(dx, dy, width()-dx, height());
00556 }
00557 }
00558 else if (d->m_iconText == KToolBar::TextOnly)
00559 {
00560 if (!textLabel().isNull())
00561 {
00562 textFlags = AlignVCenter|AlignLeft;
00563 dx = (width() - fm.width(textLabel())) / 2;
00564 dy = (height() - fm.lineSpacing()) / 2;
00565 if ( isDown() && style().styleHint(QStyle::SH_GUIStyle) == WindowsStyle )
00566 {
00567 ++dx;
00568 ++dy;
00569 }
00570 textRect = QRect( dx, dy, fm.width(textLabel()), fm.lineSpacing() );
00571 }
00572 }
00573 else if (d->m_iconText == KToolBar::IconTextBottom)
00574 {
00575 QPixmap pixmap = iconSet().pixmap( QIconSet::Automatic,
00576 isEnabled() ? (d->m_isActive ? QIconSet::Active : QIconSet::Normal) :
00577 QIconSet::Disabled,
00578 isOn() ? QIconSet::On : QIconSet::Off );
00579 if( !pixmap.isNull())
00580 {
00581 dx = (width() - pixmap.width()) / 2;
00582 dy = (height() - fm.lineSpacing() - pixmap.height()) / 2;
00583 if ( isDown() && style().styleHint(QStyle::SH_GUIStyle) == WindowsStyle )
00584 {
00585 ++dx;
00586 ++dy;
00587 }
00588 _painter->drawPixmap( dx, dy, pixmap );
00589 }
00590
00591 if (!textLabel().isNull())
00592 {
00593 textFlags = AlignBottom|AlignHCenter;
00594 dx = (width() - fm.width(textLabel())) / 2;
00595 dy = height() - fm.lineSpacing() - 4;
00596
00597 if ( isDown() && style().styleHint(QStyle::SH_GUIStyle) == WindowsStyle )
00598 {
00599 ++dx;
00600 ++dy;
00601 }
00602 textRect = QRect( dx, dy, fm.width(textLabel()), fm.lineSpacing() );
00603 }
00604 }
00605
00606
00607 if (!textLabel().isNull() && !textRect.isNull())
00608 {
00609 _painter->setFont(KGlobalSettings::toolBarFont());
00610 if (!isEnabled())
00611 _painter->setPen(palette().disabled().dark());
00612 else if(d->m_isRaised)
00613 _painter->setPen(KGlobalSettings::toolBarHighlightColor());
00614 else
00615 _painter->setPen( colorGroup().buttonText() );
00616 _painter->drawText(textRect, textFlags, textLabel());
00617 }
00618
00619 if (QToolButton::popup())
00620 {
00621 QStyle::SFlags arrowFlags = QStyle::Style_Default;
00622
00623 if (isDown()) arrowFlags |= QStyle::Style_Down;
00624 if (isEnabled()) arrowFlags |= QStyle::Style_Enabled;
00625
00626 style().drawPrimitive(QStyle::PE_ArrowDown, _painter,
00627 QRect(width()-7, height()-7, 7, 7), colorGroup(),
00628 arrowFlags, QStyleOption() );
00629 }
00630 }
00631
00632 void KToolBarButton::paletteChange(const QPalette &)
00633 {
00634 if(!d->m_isSeparator)
00635 {
00636 modeChange();
00637 repaint(false);
00638 }
00639 }
00640
00641 bool KToolBarButton::event(QEvent *e)
00642 {
00643 if (e->type() == QEvent::ParentFontChange || e->type() == QEvent::ApplicationFontChange)
00644 {
00645
00646 if (d->m_iconText != KToolBar::IconOnly)
00647 modeChange();
00648 return true;
00649 }
00650
00651 return QToolButton::event(e);
00652 }
00653
00654
00655 void KToolBarButton::showMenu()
00656 {
00657
00658
00659 }
00660
00661 void KToolBarButton::slotDelayTimeout()
00662 {
00663
00664
00665 }
00666
00667 void KToolBarButton::slotClicked()
00668 {
00669 emit clicked( d->m_id );
00670
00671
00672 if ( d->m_parent && !d->m_parent->rect().contains( geometry().center() ) ) {
00673 ButtonState state = KApplication::keyboardMouseState();
00674 if ( ( state & MouseButtonMask ) == NoButton )
00675 state = ButtonState( LeftButton | state );
00676 emit buttonClicked( d->m_id, state );
00677 }
00678 }
00679
00680 void KToolBarButton::slotPressed()
00681 {
00682 emit pressed( d->m_id );
00683 }
00684
00685 void KToolBarButton::slotReleased()
00686 {
00687 emit released( d->m_id );
00688 }
00689
00690 void KToolBarButton::slotToggled()
00691 {
00692 emit toggled( d->m_id );
00693 }
00694
00695 void KToolBarButton::setNoStyle(bool no_style)
00696 {
00697 d->m_noStyle = no_style;
00698
00699 modeChange();
00700 d->m_iconText = KToolBar::IconTextRight;
00701 repaint(false);
00702 }
00703
00704 void KToolBarButton::setRadio (bool f)
00705 {
00706 if ( d )
00707 d->m_isRadio = f;
00708 }
00709
00710 void KToolBarButton::on(bool flag)
00711 {
00712 if(isToggleButton())
00713 setOn(flag);
00714 else
00715 {
00716 setDown(flag);
00717 leaveEvent((QEvent *) 0);
00718 }
00719 repaint();
00720 }
00721
00722 void KToolBarButton::toggle()
00723 {
00724 setOn(!isOn());
00725 repaint();
00726 }
00727
00728 void KToolBarButton::setToggle(bool flag)
00729 {
00730 setToggleButton(flag);
00731 if (flag)
00732 connect(this, SIGNAL(toggled(bool)), this, SLOT(slotToggled()));
00733 else
00734 disconnect(this, SIGNAL(toggled(bool)), this, SLOT(slotToggled()));
00735 }
00736
00737 QSize KToolBarButton::sizeHint() const
00738 {
00739 return d->size;
00740 }
00741
00742 QSize KToolBarButton::minimumSizeHint() const
00743 {
00744 return d->size;
00745 }
00746
00747 QSize KToolBarButton::minimumSize() const
00748 {
00749 return d->size;
00750 }
00751
00752 bool KToolBarButton::isRaised() const
00753 {
00754 return d->m_isRaised;
00755 }
00756
00757 bool KToolBarButton::isActive() const
00758 {
00759 return d->m_isActive;
00760 }
00761
00762 int KToolBarButton::iconTextMode() const
00763 {
00764 return static_cast<int>( d->m_iconText );
00765 }
00766
00767 int KToolBarButton::id() const
00768 {
00769 return d->m_id;
00770 }
00771
00772
00773 KToolBarButtonList::KToolBarButtonList()
00774 {
00775 setAutoDelete(false);
00776 }
00777
00778 void KToolBarButton::virtual_hook( int, void* )
00779 { }
00780
00781 #include "ktoolbarbutton.moc"