summaryrefslogtreecommitdiffstats
path: root/libk3b/tools/k3btoolbutton.cpp
blob: 2e3e4687e6100e5254d93b70c212b25525af813e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* 
 *
 * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
 * Copyright (C) 2006 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3btoolbutton.h"

#include <qstyle.h>
#include <qpainter.h>
#include <qevent.h>

#include <kglobalsettings.h>
#include <kapplication.h>


class K3bToolButton::Private
{
public:
  QPoint mousePressPos;
  bool instantMenu;
};


K3bToolButton::K3bToolButton( QWidget* parent )
  : QToolButton( parent )
{
  d = new Private;
  d->instantMenu = false;
  installEventFilter(this);
}


K3bToolButton::~K3bToolButton()
{
  delete d;
}


void K3bToolButton::setInstantMenu( bool b )
{
  d->instantMenu = b;
}


void K3bToolButton::drawButton( QPainter* p )
{
  QToolButton::drawButton( p );

  //
  // code below comes from ktoolbarbutton.cpp from the kdelibs sources
  // see the file for copyright information
  //
  if( QToolButton::popup() ) {
    QStyle::SFlags arrowFlags = QStyle::Style_Default;
    
    if( isDown() )
      arrowFlags |= QStyle::Style_Down;
    if( isEnabled() )
      arrowFlags |= QStyle::Style_Enabled;
    
    style().drawPrimitive(QStyle::PE_ArrowDown, p,
			  QRect(width()-7, height()-7, 7, 7), colorGroup(),
			  arrowFlags, QStyleOption() );
  }
}


bool K3bToolButton::eventFilter( QObject* o, QEvent* ev )
{
  if( dynamic_cast<K3bToolButton*>(o) == this ) {

    // Popup the menu when the left mousebutton is pressed and the mouse
    // is moved by a small distance.
    if( QToolButton::popup() ) {
      if( ev->type() == QEvent::MouseButtonPress ) {
	QMouseEvent* mev = static_cast<QMouseEvent*>(ev);

	if( d->instantMenu ) {
	  setDown(true);
	  openPopup();
	  return true;
	}
	else {
	  d->mousePressPos = mev->pos();
	}
      }
      else if( ev->type() == QEvent::MouseMove ) {
        QMouseEvent* mev = static_cast<QMouseEvent*>(ev);
        if( !d->instantMenu &&
	    ( mev->pos() - d->mousePressPos).manhattanLength() > KGlobalSettings::dndEventDelay() ) {
	  openPopup();
          return true;
        }
      }
    }
  }

  return QToolButton::eventFilter( o, ev );
}