summaryrefslogtreecommitdiff
path: root/muse2/muse/widgets/spinbox.cpp
blob: aa536809b22160b79f936a410d5b9a4de435200e (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//=========================================================
//  MusE
//  Linux Music Editor
//    $Id: spinbox.cpp,v 1.1.2.3 2009/07/09 18:27:11 terminator356 Exp $
//    (C) Copyright 2001 Werner Schweer (ws@seh.de)
//=========================================================

#include <QEvent>
#include <QKeyEvent>
#include <QMouseEvent>

#include "spinbox.h"

//---------------------------------------------------------
//   SpinBox
//---------------------------------------------------------

SpinBox::SpinBox(QWidget* parent)
   : QSpinBox(parent)
{
  _clearFocus = true;
}

SpinBox::SpinBox(int minValue, int maxValue, int step, QWidget* parent)
   : QSpinBox(parent)
{
  setRange(minValue, maxValue);
  setSingleStep(step);
  _clearFocus = true;
  upEnabled = StepUpEnabled;
  downEnabled = StepDownEnabled;
}

QAbstractSpinBox::StepEnabled SpinBox::stepEnabled() const
{
  return upEnabled | downEnabled;
}

void SpinBox::setStepEnabled(bool up, bool down)
{
  upEnabled = up ? StepUpEnabled : StepNone;
  downEnabled = down ? StepDownEnabled : StepNone;
}

int SpinBox::arrowWidth() const
{
  QStyleOptionSpinBox styleOpt;
  styleOpt.initFrom(this);
  QRect upArrowRect = QApplication::style()->subControlRect(QStyle::CC_SpinBox, &styleOpt, QStyle::SC_SpinBoxUp, this);
  return upArrowRect.width();
}

void SpinBox::setEditor(QLineEdit* ed)
{
  setLineEdit(ed);
}

void SpinBox::mousePressEvent ( QMouseEvent * event )
{
  // FIXME: I couldn't find a way to access the arrow buttons directly. Hence I am using a QRect::contains method.
  // Unfortunately this is not 100% accurate with the Oxygen style; one needs to push to the right hand side of the 
  // buttons. But it works perfect with the QtCurve style - Orcan
  QStyleOptionSpinBox styleOpt;
  styleOpt.initFrom(this);
  QRect upArrowRect = QApplication::style()->subControlRect(QStyle::CC_SpinBox, &styleOpt, QStyle::SC_SpinBoxUp, this);
  QRect downArrowRect = QApplication::style()->subControlRect(QStyle::CC_SpinBox, &styleOpt, QStyle::SC_SpinBoxDown, this);
 
  if (upArrowRect.contains(event->pos()))
    emit(stepUpPressed());
  else if (downArrowRect.contains(event->pos()))
    emit(stepDownPressed());
  QSpinBox::mousePressEvent(event);
}


bool SpinBox::eventFilter(QObject* o, QEvent* ev)
{
    // if (o != (QObject*)editor()) ddskrjo can't find editor()
    //    return QSpinBox::eventFilter(o,ev);
    
    bool retval = FALSE; 
    if(ev->type() == QEvent::KeyPress) 
    {
        QKeyEvent* k = (QKeyEvent*)ev;
        if(k->key() == Qt::Key_Up || k->key() == Qt::Key_Down) 
        {
          // stepUp/stepDown will be called. Set this now.
          _clearFocus = false;
        }  
        else if (k->key() == Qt::Key_Enter || k->key() == Qt::Key_Return) 
        {
          // With this line, two enter presses after an edit will clear focus.
          // Without, just one enter press clears the focus.
          //if(!editor()->isModified())
          {  
            clearFocus();
            return TRUE;
          }
        }
   }
   else
   if(ev->type() == QEvent::MouseButtonDblClick) 
   {
     emit doubleClicked();
     return TRUE;
   }
   
   retval = QSpinBox::eventFilter(o, ev);
     
   return retval;
}

void SpinBox::stepUp()
{
  QSpinBox::stepUp();
  if(_clearFocus)
    clearFocus();
  else  
    _clearFocus = true;
}

void SpinBox::stepDown()
{
  QSpinBox::stepDown();
  if(_clearFocus)
    clearFocus();
  else  
    _clearFocus = true;
}