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