20#include <ttdetoolbarbutton.h>
24#include <tqpopupmenu.h>
27#include <tqdrawutil.h>
30#include <tdeversion.h>
31#include <tdeapplication.h>
32#include <kiconeffect.h>
33#include <kiconloader.h>
34#include <tdeglobalsettings.h>
37#define POPUP_DELAY 500
39class TTDEToolBarButton::TTDEToolBarButtonPrivate
42 TTDEToolBarButtonPrivate()
44 m_iconMode = TK::IconOnly;
50 ignoreNextMousePress =
false;
53 m_iconName = TQString();
54 m_arrowPressed =
false;
58 m_disabledIconName = TQString();
59 m_defaultIconName = TQString();
61 m_instance = TDEGlobal::instance();
64 ~TTDEToolBarButtonPrivate()
76 bool ignoreNextMousePress;
80 TQString m_disabledIconName;
81 TQString m_defaultIconName;
83 TK::IconMode m_iconMode;
85 TQTimer *m_delayTimer;
88 TDEInstance *m_instance;
91TTDEToolBarButton::TTDEToolBarButton(
const TQString& icon,
const TQString& txt,
92 TQWidget* parent,
const char* name,
93 TDEInstance *instance )
94: TQToolButton(parent,
name)
96 d =
new TTDEToolBarButtonPrivate;
98 d->m_instance = instance;
100 setFocusPolicy( TQWidget::NoFocus );
102 connect(
this, TQ_SIGNAL(clicked()), TQ_SLOT(slotClicked()) );
103 connect(
this, TQ_SIGNAL(pressed()), TQ_SLOT(slotPressed()) );
104 connect(
this, TQ_SIGNAL(released()), TQ_SLOT(slotReleased()) );
106 installEventFilter(
this);
112TTDEToolBarButton::TTDEToolBarButton(
const TQPixmap& pixmap,
const TQString& txt, TQWidget* parent,
const char* name )
113: TQToolButton(parent,
name )
115 d =
new TTDEToolBarButtonPrivate;
118 setFocusPolicy( TQWidget::NoFocus );
120 connect(
this, TQ_SIGNAL(clicked()), TQ_SLOT(slotClicked()) );
121 connect(
this, TQ_SIGNAL(pressed()), TQ_SLOT(slotPressed()) );
122 connect(
this, TQ_SIGNAL(released()), TQ_SLOT(slotReleased()) );
124 installEventFilter(
this);
130TTDEToolBarButton::~TTDEToolBarButton()
135TQString TTDEToolBarButton::text()
140void TTDEToolBarButton::modeChange()
142 TQToolTip::add(
this,d->m_text);
149 w += pixmap()->width();
150 h = TQMAX(h,pixmap()->height()+2*border);
151 if (d->m_iconMode == TK::IconAndText && !d->m_text.isEmpty())
155 if ((d->m_iconMode==TK::IconAndText||d->m_iconMode==TK::TextOnly) && !d->m_text.isEmpty())
157 TQFont tmp_font = TDEGlobalSettings::toolBarFont();
158 TQFontMetrics fm(tmp_font);
160 h = TQMAX(h,fm.lineSpacing()+2*border);
161 w += fm.width(d->m_text);
164 if (d->m_popup && !d->m_isToggle)
168 setMinimumSize(size);
173void TTDEToolBarButton::setEnabled(
bool enabled )
175 if (isEnabled()==enabled)
178 TQToolButton::setPixmap( (enabled ? defaultPixmap : disabledPixmap) );
179 TQToolButton::setEnabled( enabled );
182void TTDEToolBarButton::setText(
const TQString& text)
188void TTDEToolBarButton::setIcon(
const TQString& icon )
190 d->m_iconName = icon;
193 setPixmap( BarIcon(icon, iconSize, TDEIcon::ActiveState, d->m_instance),
false );
194 setDisabledPixmap( BarIcon(icon, iconSize, TDEIcon::DisabledState, d->m_instance) );
195 setDefaultPixmap( BarIcon(icon, iconSize, TDEIcon::DefaultState, d->m_instance) );
198void TTDEToolBarButton::setDisabledIcon(
const TQString &icon )
200 d->m_disabledIconName = icon;
202 setDisabledPixmap( BarIcon(icon, iconSize, TDEIcon::DisabledState, d->m_instance) );
205void TTDEToolBarButton::setDefaultIcon(
const TQString &icon )
207 d->m_defaultIconName = icon;
209 setDefaultPixmap( BarIcon(icon, iconSize, TDEIcon::DefaultState, d->m_instance) );
212TQPixmap TTDEToolBarButton::getActivePixmap()
const
217void TTDEToolBarButton::setPixmap(
const TQPixmap &pixmap )
219 setPixmap( pixmap,
true );
222void TTDEToolBarButton::setPixmap(
const TQPixmap &pixmap,
bool generate )
224 activePixmap = pixmap;
229 makeDisabledPixmap();
233 if (defaultPixmap.isNull())
234 defaultPixmap = activePixmap;
235 if (disabledPixmap.isNull())
236 disabledPixmap = activePixmap;
239 TQToolButton::setPixmap( isEnabled() ? defaultPixmap : disabledPixmap );
242void TTDEToolBarButton::setDefaultPixmap(
const TQPixmap &pixmap )
244 defaultPixmap = pixmap;
245 TQToolButton::setPixmap( isEnabled() ? defaultPixmap : disabledPixmap );
248void TTDEToolBarButton::setDisabledPixmap(
const TQPixmap &pixmap )
250 disabledPixmap = pixmap;
251 TQToolButton::setPixmap( isEnabled() ? defaultPixmap : disabledPixmap );
254void TTDEToolBarButton::setPopup(TQPopupMenu *p)
257 d->m_popup->setFont(TDEGlobalSettings::toolBarFont());
258 p->installEventFilter(
this);
263TQPopupMenu *TTDEToolBarButton::popup()
268void TTDEToolBarButton::setDelayedPopup (TQPopupMenu *p,
bool toggle )
273 if (!d->m_delayTimer) {
274 d->m_delayTimer =
new TQTimer(
this);
275 connect(d->m_delayTimer, TQ_SIGNAL(timeout()),
this, TQ_SLOT(slotDelayTimeout()));
281void TTDEToolBarButton::setRaised(
bool f)
287void TTDEToolBarButton::setAutoRaised(
bool f)
292void TTDEToolBarButton::leaveEvent(TQEvent *)
294 if (!d->m_isToggle && !(d->m_popup && d->m_popup->isVisible()) ) {
295 TQToolButton::setPixmap(isEnabled() ? defaultPixmap : disabledPixmap);
301void TTDEToolBarButton::enterEvent(TQEvent *)
303 if (!d->m_isToggle) {
305 TQToolButton::setPixmap(activePixmap);
309 TQToolButton::setPixmap(disabledPixmap);
315bool TTDEToolBarButton::eventFilter(TQObject *o, TQEvent *ev)
318 if (ev->type() == TQEvent::MouseButtonPress && d->m_popup && d->m_isPopup ) {
319 if (!d->m_isToggle) {
320 d->m_arrowPressed = arrowPressed( mapFromGlobal(TQCursor::pos()) );
322 d->m_delayTimer->start(POPUP_DELAY);
326 if ( o == d->m_popup) {
335 if ( !geometry().contains(parentWidget()->mapFromGlobal(TQCursor::pos())) )
339 case TQEvent::MouseButtonPress: {
340 d->m_arrowPressed = arrowPressed( mapFromGlobal(TQCursor::pos()) );
341 d->ignoreNextMousePress = d->m_arrowPressed;
351void TTDEToolBarButton::drawButton( TQPainter* p )
353#define DRAW_PIXMAP_AND_TEXT \
356 style().drawItem( p, TQRect( x, 0, pixmap()->width(), height() ), AlignCenter, colorGroup(), isEnabled(), pixmap(), TQString() );\
357 if (d->m_iconMode==TK::IconAndText && !d->m_text.isEmpty()) {\
358 x += pixmap()->width() + 3;\
361 if ((d->m_iconMode==TK::IconAndText||d->m_iconMode==TK::TextOnly) && !d->m_text.isEmpty()) {\
362 TQFontMetrics fm(TDEGlobalSettings::toolBarFont());\
363 style().drawItem( p, TQRect( x, 0, fm.width(d->m_text), height() ), AlignCenter, colorGroup(), isEnabled(), 0, d->m_text );\
366 const char* arrow[] = {
374 TQPixmap arrow_pix(arrow);
375 bool f = d->m_isOn || isDown();
377 if (d->m_popup && !d->m_isToggle)
381 TQStyle::SFlags flags = TQStyle::Style_Default;
382 if (isEnabled()) flags |= TQStyle::Style_Enabled;
383 if (isOn()) flags |= TQStyle::Style_On;
384 if (d->m_isRaised) flags |= TQStyle::Style_Raised;
385 if (hasFocus()) flags |= TQStyle::Style_HasFocus;
387 style().drawComplexControl( TQStyle::CC_ToolButton, p,
this, TQRect( 0, 0, width()-12, height() ), colorGroup(), flags, TQStyle::SC_ToolButton );
388 style().drawComplexControl( TQStyle::CC_ToolButton, p,
this, TQRect( width()-13, 0, 13, height() ), colorGroup(), flags, TQStyle::SC_ToolButton );
389 style().drawItem( p, TQRect( width()-13, 0, 13, height() ), AlignCenter, colorGroup(), isEnabled(), &arrow_pix, TQString() );
391 qDrawShadeLine( p, width()-12, 0, width()-12, height(), colorGroup(),
true );
394 style().drawControl( TQStyle::CE_PushButton, p,
this, TQRect( 0, 0, width(), height() ), isEnabled() ? colorGroup() : palette().disabled(), f );
397 p->drawPixmap(width()-11+z,(height()-4)/2+z ,arrow_pix);
400 style().drawControl( TQStyle::CE_PushButton, p,
this, TQRect( 0, 0, width(), height() ), isEnabled() ? colorGroup() : palette().disabled(), f );
405void TTDEToolBarButton::paletteChange(
const TQPalette &)
407 makeDisabledPixmap();
409 TQToolButton::setPixmap( disabledPixmap );
411 TQToolButton::setPixmap( defaultPixmap );
415void TTDEToolBarButton::makeDefaultPixmap()
417 if (activePixmap.isNull())
420 TDEIconEffect effect;
421 defaultPixmap = effect.apply(activePixmap, TDEIcon::Toolbar, TDEIcon::DefaultState);
424void TTDEToolBarButton::makeDisabledPixmap()
426 if (activePixmap.isNull())
429 TDEIconEffect effect;
430 disabledPixmap = effect.apply(activePixmap, TDEIcon::Toolbar, TDEIcon::DisabledState);
433TQSize TTDEToolBarButton::sizeHint()
const
435 return minimumSize();
438TQSize TTDEToolBarButton::minimumSizeHint()
const
440 return minimumSize();
443void TTDEToolBarButton::showMenu()
445 TQPoint p ( mapToGlobal( TQPoint( 0, 0 ) ) );
446 const int deskHeight = TDEGlobalSettings::desktopGeometry(
this).height();
447 if ( p.y() + height() + d->m_popup->height() > deskHeight )
448 p.setY( p.y() - d->m_popup->height() );
450 p.setY( p.y() + height( ));
452 d->m_popup->popup(p);
455void TTDEToolBarButton::slotDelayTimeout()
457 d->m_delayTimer->stop();
461void TTDEToolBarButton::slotClicked()
463 if ( d->ignoreNextMousePress ) {
464 d->ignoreNextMousePress=
false;
468 if (d->m_popup && !d->m_isPopup)
471 emit buttonClicked();
474void TTDEToolBarButton::slotPressed()
476 if ( d->ignoreNextMousePress )
480 if (!d->m_isPopup || d->m_isPopup && d->m_arrowPressed)
484 emit buttonPressed();
486 d->ignoreNextMousePress =
false;
489void TTDEToolBarButton::slotReleased()
491 if (d->m_popup && d->m_isPopup)
492 d->m_delayTimer->stop();
494 emit buttonReleased();
497void TTDEToolBarButton::slotToggled()
499 emit buttonToggled();
502void TTDEToolBarButton::on(
bool flag)
508void TTDEToolBarButton::toggle()
513void TTDEToolBarButton::setToggle(
bool flag)
515 d->m_isToggle = flag;
517 connect(
this, TQ_SIGNAL(toggled(
bool)),
this, TQ_SLOT(slotToggled()));
519 disconnect(
this, TQ_SIGNAL(toggled(
bool)),
this, TQ_SLOT(slotToggled()));
525void TTDEToolBarButton::setIconMode( TK::IconMode m )
532#include <ttdetoolbarbutton.moc>
const char * name(StdAction id)