20#include <ttdetoolbarbutton.h>
24#include <tqpopupmenu.h>
27#include <tqdrawutil.h>
30#include <tdeapplication.h>
31#include <kiconeffect.h>
32#include <kiconloader.h>
33#include <tdeglobalsettings.h>
36#define POPUP_DELAY 500
38class TTDEToolBarButton::TTDEToolBarButtonPrivate
41 TTDEToolBarButtonPrivate()
43 m_iconMode = TK::IconOnly;
49 ignoreNextMousePress =
false;
52 m_iconName = TQString();
53 m_arrowPressed =
false;
57 m_disabledIconName = TQString();
58 m_defaultIconName = TQString();
60 m_instance = TDEGlobal::instance();
63 ~TTDEToolBarButtonPrivate()
75 bool ignoreNextMousePress;
79 TQString m_disabledIconName;
80 TQString m_defaultIconName;
82 TK::IconMode m_iconMode;
84 TQTimer *m_delayTimer;
87 TDEInstance *m_instance;
90TTDEToolBarButton::TTDEToolBarButton(
const TQString& icon,
const TQString& txt,
91 TQWidget* parent,
const char* name,
92 TDEInstance *instance )
93: TQToolButton(parent,
name)
95 d =
new TTDEToolBarButtonPrivate;
97 d->m_instance = instance;
99 setFocusPolicy( TQWidget::NoFocus );
101 connect(
this, TQ_SIGNAL(clicked()), TQ_SLOT(slotClicked()) );
102 connect(
this, TQ_SIGNAL(pressed()), TQ_SLOT(slotPressed()) );
103 connect(
this, TQ_SIGNAL(released()), TQ_SLOT(slotReleased()) );
105 installEventFilter(
this);
111TTDEToolBarButton::TTDEToolBarButton(
const TQPixmap& pixmap,
const TQString& txt, TQWidget* parent,
const char* name )
112: TQToolButton(parent,
name )
114 d =
new TTDEToolBarButtonPrivate;
117 setFocusPolicy( TQWidget::NoFocus );
119 connect(
this, TQ_SIGNAL(clicked()), TQ_SLOT(slotClicked()) );
120 connect(
this, TQ_SIGNAL(pressed()), TQ_SLOT(slotPressed()) );
121 connect(
this, TQ_SIGNAL(released()), TQ_SLOT(slotReleased()) );
123 installEventFilter(
this);
129TTDEToolBarButton::~TTDEToolBarButton()
134TQString TTDEToolBarButton::text()
139void TTDEToolBarButton::modeChange()
141 TQToolTip::add(
this,d->m_text);
148 w += pixmap()->width();
149 h = TQMAX(h,pixmap()->height()+2*border);
150 if (d->m_iconMode == TK::IconAndText && !d->m_text.isEmpty())
154 if ((d->m_iconMode==TK::IconAndText||d->m_iconMode==TK::TextOnly) && !d->m_text.isEmpty())
156 TQFont tmp_font = TDEGlobalSettings::toolBarFont();
157 TQFontMetrics fm(tmp_font);
159 h = TQMAX(h,fm.lineSpacing()+2*border);
160 w += fm.width(d->m_text);
163 if (d->m_popup && !d->m_isToggle)
167 setMinimumSize(size);
172void TTDEToolBarButton::setEnabled(
bool enabled )
174 if (isEnabled()==enabled)
177 TQToolButton::setPixmap( (enabled ? defaultPixmap : disabledPixmap) );
178 TQToolButton::setEnabled( enabled );
181void TTDEToolBarButton::setText(
const TQString& text)
187void TTDEToolBarButton::setIcon(
const TQString& icon )
189 d->m_iconName = icon;
192 setPixmap( BarIcon(icon, iconSize, TDEIcon::ActiveState, d->m_instance),
false );
193 setDisabledPixmap( BarIcon(icon, iconSize, TDEIcon::DisabledState, d->m_instance) );
194 setDefaultPixmap( BarIcon(icon, iconSize, TDEIcon::DefaultState, d->m_instance) );
197void TTDEToolBarButton::setDisabledIcon(
const TQString &icon )
199 d->m_disabledIconName = icon;
201 setDisabledPixmap( BarIcon(icon, iconSize, TDEIcon::DisabledState, d->m_instance) );
204void TTDEToolBarButton::setDefaultIcon(
const TQString &icon )
206 d->m_defaultIconName = icon;
208 setDefaultPixmap( BarIcon(icon, iconSize, TDEIcon::DefaultState, d->m_instance) );
211TQPixmap TTDEToolBarButton::getActivePixmap()
const
216void TTDEToolBarButton::setPixmap(
const TQPixmap &pixmap )
218 setPixmap( pixmap,
true );
221void TTDEToolBarButton::setPixmap(
const TQPixmap &pixmap,
bool generate )
223 activePixmap = pixmap;
228 makeDisabledPixmap();
232 if (defaultPixmap.isNull())
233 defaultPixmap = activePixmap;
234 if (disabledPixmap.isNull())
235 disabledPixmap = activePixmap;
238 TQToolButton::setPixmap( isEnabled() ? defaultPixmap : disabledPixmap );
241void TTDEToolBarButton::setDefaultPixmap(
const TQPixmap &pixmap )
243 defaultPixmap = pixmap;
244 TQToolButton::setPixmap( isEnabled() ? defaultPixmap : disabledPixmap );
247void TTDEToolBarButton::setDisabledPixmap(
const TQPixmap &pixmap )
249 disabledPixmap = pixmap;
250 TQToolButton::setPixmap( isEnabled() ? defaultPixmap : disabledPixmap );
253void TTDEToolBarButton::setPopup(TQPopupMenu *p)
256 d->m_popup->setFont(TDEGlobalSettings::toolBarFont());
257 p->installEventFilter(
this);
262TQPopupMenu *TTDEToolBarButton::popup()
267void TTDEToolBarButton::setDelayedPopup (TQPopupMenu *p,
bool toggle )
272 if (!d->m_delayTimer) {
273 d->m_delayTimer =
new TQTimer(
this);
274 connect(d->m_delayTimer, TQ_SIGNAL(timeout()),
this, TQ_SLOT(slotDelayTimeout()));
280void TTDEToolBarButton::setRaised(
bool f)
286void TTDEToolBarButton::setAutoRaised(
bool f)
291void TTDEToolBarButton::leaveEvent(TQEvent *)
293 if (!d->m_isToggle && !(d->m_popup && d->m_popup->isVisible()) ) {
294 TQToolButton::setPixmap(isEnabled() ? defaultPixmap : disabledPixmap);
300void TTDEToolBarButton::enterEvent(TQEvent *)
302 if (!d->m_isToggle) {
304 TQToolButton::setPixmap(activePixmap);
308 TQToolButton::setPixmap(disabledPixmap);
314bool TTDEToolBarButton::eventFilter(TQObject *o, TQEvent *ev)
317 if (ev->type() == TQEvent::MouseButtonPress && d->m_popup && d->m_isPopup ) {
318 if (!d->m_isToggle) {
319 d->m_arrowPressed = arrowPressed( mapFromGlobal(TQCursor::pos()) );
321 d->m_delayTimer->start(POPUP_DELAY);
325 if ( o == d->m_popup) {
334 if ( !geometry().contains(parentWidget()->mapFromGlobal(TQCursor::pos())) )
338 case TQEvent::MouseButtonPress: {
339 d->m_arrowPressed = arrowPressed( mapFromGlobal(TQCursor::pos()) );
340 d->ignoreNextMousePress = d->m_arrowPressed;
350void TTDEToolBarButton::drawButton( TQPainter* p )
352#define DRAW_PIXMAP_AND_TEXT \
355 style().drawItem( p, TQRect( x, 0, pixmap()->width(), height() ), AlignCenter, colorGroup(), isEnabled(), pixmap(), TQString() );\
356 if (d->m_iconMode==TK::IconAndText && !d->m_text.isEmpty()) {\
357 x += pixmap()->width() + 3;\
360 if ((d->m_iconMode==TK::IconAndText||d->m_iconMode==TK::TextOnly) && !d->m_text.isEmpty()) {\
361 TQFontMetrics fm(TDEGlobalSettings::toolBarFont());\
362 style().drawItem( p, TQRect( x, 0, fm.width(d->m_text), height() ), AlignCenter, colorGroup(), isEnabled(), 0, d->m_text );\
365 const char* arrow[] = {
373 TQPixmap arrow_pix(arrow);
374 bool f = d->m_isOn || isDown();
376 if (d->m_popup && !d->m_isToggle)
380 TQStyle::SFlags flags = TQStyle::Style_Default;
381 if (isEnabled()) flags |= TQStyle::Style_Enabled;
382 if (isOn()) flags |= TQStyle::Style_On;
383 if (d->m_isRaised) flags |= TQStyle::Style_Raised;
384 if (hasFocus()) flags |= TQStyle::Style_HasFocus;
386 style().drawComplexControl( TQStyle::CC_ToolButton, p,
this, TQRect( 0, 0, width()-12, height() ), colorGroup(), flags, TQStyle::SC_ToolButton );
387 style().drawComplexControl( TQStyle::CC_ToolButton, p,
this, TQRect( width()-13, 0, 13, height() ), colorGroup(), flags, TQStyle::SC_ToolButton );
388 style().drawItem( p, TQRect( width()-13, 0, 13, height() ), AlignCenter, colorGroup(), isEnabled(), &arrow_pix, TQString() );
390 qDrawShadeLine( p, width()-12, 0, width()-12, height(), colorGroup(),
true );
393 style().drawControl( TQStyle::CE_PushButton, p,
this, TQRect( 0, 0, width(), height() ), isEnabled() ? colorGroup() : palette().disabled(), f );
396 p->drawPixmap(width()-11+z,(height()-4)/2+z ,arrow_pix);
399 style().drawControl( TQStyle::CE_PushButton, p,
this, TQRect( 0, 0, width(), height() ), isEnabled() ? colorGroup() : palette().disabled(), f );
404void TTDEToolBarButton::paletteChange(
const TQPalette &)
406 makeDisabledPixmap();
408 TQToolButton::setPixmap( disabledPixmap );
410 TQToolButton::setPixmap( defaultPixmap );
414void TTDEToolBarButton::makeDefaultPixmap()
416 if (activePixmap.isNull())
419 TDEIconEffect effect;
420 defaultPixmap = effect.apply(activePixmap, TDEIcon::Toolbar, TDEIcon::DefaultState);
423void TTDEToolBarButton::makeDisabledPixmap()
425 if (activePixmap.isNull())
428 TDEIconEffect effect;
429 disabledPixmap = effect.apply(activePixmap, TDEIcon::Toolbar, TDEIcon::DisabledState);
432TQSize TTDEToolBarButton::sizeHint()
const
434 return minimumSize();
437TQSize TTDEToolBarButton::minimumSizeHint()
const
439 return minimumSize();
442void TTDEToolBarButton::showMenu()
444 TQPoint p ( mapToGlobal( TQPoint( 0, 0 ) ) );
445 const int deskHeight = TDEGlobalSettings::desktopGeometry(
this).height();
446 if ( p.y() + height() + d->m_popup->height() > deskHeight )
447 p.setY( p.y() - d->m_popup->height() );
449 p.setY( p.y() + height( ));
451 d->m_popup->popup(p);
454void TTDEToolBarButton::slotDelayTimeout()
456 d->m_delayTimer->stop();
460void TTDEToolBarButton::slotClicked()
462 if ( d->ignoreNextMousePress ) {
463 d->ignoreNextMousePress=
false;
467 if (d->m_popup && !d->m_isPopup)
470 emit buttonClicked();
473void TTDEToolBarButton::slotPressed()
475 if ( d->ignoreNextMousePress )
479 if (!d->m_isPopup || d->m_isPopup && d->m_arrowPressed)
483 emit buttonPressed();
485 d->ignoreNextMousePress =
false;
488void TTDEToolBarButton::slotReleased()
490 if (d->m_popup && d->m_isPopup)
491 d->m_delayTimer->stop();
493 emit buttonReleased();
496void TTDEToolBarButton::slotToggled()
498 emit buttonToggled();
501void TTDEToolBarButton::on(
bool flag)
507void TTDEToolBarButton::toggle()
512void TTDEToolBarButton::setToggle(
bool flag)
514 d->m_isToggle = flag;
516 connect(
this, TQ_SIGNAL(toggled(
bool)),
this, TQ_SLOT(slotToggled()));
518 disconnect(
this, TQ_SIGNAL(toggled(
bool)),
this, TQ_SLOT(slotToggled()));
524void TTDEToolBarButton::setIconMode( TK::IconMode m )
531#include <ttdetoolbarbutton.moc>
const char * name(StdAction id)