summaryrefslogtreecommitdiff
path: root/muse2/muse/widgets/spinboxFP.cpp
blob: 89a41770859309421eef567137b1690c6cc382ea (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//=========================================================
//  MusE
//  Linux Music Editor
//    $Id: spinboxFP.cpp,v 1.1.1.1 2003/10/27 18:55:03 wschweer Exp $
//    (C) Copyright 2001 Werner Schweer (ws@seh.de)
//
//  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; version 2 of
//  the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//=========================================================

#include <stdio.h>
#include <cmath>

//#include <QtGui>
//#include <QDoubleValidator>
//#include <QLineEdit>

#include "spinboxFP.h"

namespace MusEGui {

//---------------------------------------------------------
//   SpinBoxFP
//---------------------------------------------------------

SpinBoxFP::SpinBoxFP(QWidget* parent)
   //: QSpinBox(parent)
   : QDoubleSpinBox(parent)
      {
        //validator = new QDoubleValidator(this);
        //lineEdit()->setValidator(validator = new QDoubleValidator(this));
        //validator->setNotation(QDoubleValidator::StandardNotation);
        
        //_decimals = 0;
        setDecimals(0);
        
        connect(this, SIGNAL(valueChanged(double)), SLOT(valueChange(double)));
      }

SpinBoxFP::SpinBoxFP(int minValue, int maxValue, int step, QWidget* parent)
//SpinBoxFP::SpinBoxFP(double minValue, double maxValue, double step, QWidget* parent)
   //: QSpinBox(parent)
   : QDoubleSpinBox(parent)
      {
        //validator = new QDoubleValidator(this);
        //lineEdit()->setValidator(validator = new QDoubleValidator(this));
        //validator->setNotation(QDoubleValidator::StandardNotation);
        
        //_decimals = 0;
        QDoubleSpinBox::setDecimals(0);
        
        setRange(minValue, maxValue);
        setSingleStep(step);
        
        connect(this, SIGNAL(valueChanged(double)), SLOT(valueChange(double)));
      }

//---------------------------------------------------------
//   valueChange
//---------------------------------------------------------

void SpinBoxFP::valueChange(double)
{
        double div = exp10(decimals());
        emit valueChanged(int(value() * div));
}

//---------------------------------------------------------
//   setValue
//---------------------------------------------------------

void SpinBoxFP::setValue(int val)
      {
        double div = exp10(decimals());
        QDoubleSpinBox::setValue(double(val) /  div );
      }

//---------------------------------------------------------
//   intValue
//---------------------------------------------------------

int SpinBoxFP::intValue()
      {
        double div = exp10(decimals());
        return int(value() * div);
      }

//---------------------------------------------------------
//   setDecimals
//---------------------------------------------------------

void SpinBoxFP::setDecimals(int val)
      {
      //_decimals = val;
      
      //updateDisplay();
      //interpretText();  // TODO: Check - is this what we need? Will send out signals?
      //setValue(value());    // Try this. "setValue() will emit valueChanged() if the new value is different from the old one."
      
      QDoubleSpinBox::setDecimals(val);
      double step = 1.0 / exp10(val);
      setSingleStep(step);
      }

/*
//---------------------------------------------------------
//   validate
//---------------------------------------------------------

QValidator::State SpinBoxFP::validate(QString& input, int& pos) const
{
  // Must set these dynamically as settings may have changed.
  validator->setRange(minimum(), maximum(), _decimals);
  
  QValidator::State s = validator->validate(input, pos);
  return s;
}

//---------------------------------------------------------
//   mapValueToText
//---------------------------------------------------------

QString SpinBoxFP::textFromValue(int value) const
      {
      if (_decimals) {
            QString s;
            int div = int(exp10(_decimals));
//            printf("val %d, prec %d, div %d\n", value, _precision, div);
            
            s.sprintf("%d.%0*d", value/div, _decimals, value%div);
            //s.sprintf("%0*f", value, _decimals);
            
            return s;
            }
      return QSpinBox::textFromValue(value);
      }

//---------------------------------------------------------
//   mapTextToValue
//---------------------------------------------------------

int SpinBoxFP::valueFromText(const QString& text) const
      {
      //QString qs = cleanText();
      if (_decimals) {
            //const char* s = qs.toLatin1();
            //const char* s = cleanText().toAscii().data();
            
            //int a, b;
            bool ok;
            double f = text.toDouble(&ok);
            
            //int n = sscanf(s, "%d.%d", &a, &b);
            //int n = sscanf(s, "%f", &f);
            
            //if (n != 2) {
            //if (n != 1) {
            if (!ok) {
            
                  // *ok = false;
                  //return 0;
                  // TODO: Check - Hmm, no OK parameter. Why return 0? Let's try: 
                  // Keep returning the current value until something valid comes in...
                  return value();
                  }
            
            //int div = int(exp10(_decimals));
            double div = int(exp10(_decimals));
            
            //return a * div + b;
            return (f * div);
            
            }
      return QSpinBox::valueFromText(text);
      }

*/

} // namespace MusEGui