1 //=========================================================
2 //  MusE
3 //  Linux Music Editor
4 //    $Id: spinboxFP.cpp,v 1.1.1.1 2003/10/27 18:55:03 wschweer Exp $
5 //    (C) Copyright 2001 Werner Schweer (ws@seh.de)
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; version 2 of
10 //  the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 //=========================================================
22 
23 #include <stdio.h>
24 
25 //#include <QtWidgets>
26 //#include <QDoubleValidator>
27 //#include <QLineEdit>
28 
29 #include "muse_math.h"
30 #include "spinboxFP.h"
31 
32 namespace MusEGui {
33 
34 //---------------------------------------------------------
35 //   SpinBoxFP
36 //---------------------------------------------------------
37 
SpinBoxFP(QWidget * parent)38 SpinBoxFP::SpinBoxFP(QWidget* parent)
39    //: QSpinBox(parent)
40    : QDoubleSpinBox(parent)
41       {
42         //validator = new QDoubleValidator(this);
43         //lineEdit()->setValidator(validator = new QDoubleValidator(this));
44         //validator->setNotation(QDoubleValidator::StandardNotation);
45 
46         //_decimals = 0;
47         setDecimals(0);
48 
49         connect(this, SIGNAL(valueChanged(double)), SLOT(valueChange(double)));
50       }
51 
SpinBoxFP(int minValue,int maxValue,int step,QWidget * parent)52 SpinBoxFP::SpinBoxFP(int minValue, int maxValue, int step, QWidget* parent)
53 //SpinBoxFP::SpinBoxFP(double minValue, double maxValue, double step, QWidget* parent)
54    //: QSpinBox(parent)
55    : QDoubleSpinBox(parent)
56       {
57         //validator = new QDoubleValidator(this);
58         //lineEdit()->setValidator(validator = new QDoubleValidator(this));
59         //validator->setNotation(QDoubleValidator::StandardNotation);
60 
61         //_decimals = 0;
62         QDoubleSpinBox::setDecimals(0);
63 
64         setRange(minValue, maxValue);
65         setSingleStep(step);
66 
67         connect(this, SIGNAL(valueChanged(double)), SLOT(valueChange(double)));
68       }
69 
70 //---------------------------------------------------------
71 //   valueChange
72 //---------------------------------------------------------
73 
valueChange(double)74 void SpinBoxFP::valueChange(double)
75 {
76         double div = exp10(decimals());
77         emit valueChanged(int(value() * div));
78 }
79 
80 //---------------------------------------------------------
81 //   setValue
82 //---------------------------------------------------------
83 
setValue(int val)84 void SpinBoxFP::setValue(int val)
85       {
86         double div = exp10(decimals());
87         QDoubleSpinBox::setValue(double(val) /  div );
88       }
89 
90 //---------------------------------------------------------
91 //   intValue
92 //---------------------------------------------------------
93 
intValue()94 int SpinBoxFP::intValue()
95       {
96         double div = exp10(decimals());
97         return int(value() * div);
98       }
99 
100 //---------------------------------------------------------
101 //   setDecimals
102 //---------------------------------------------------------
103 
setDecimals(int val)104 void SpinBoxFP::setDecimals(int val)
105       {
106       //_decimals = val;
107 
108       //updateDisplay();
109       //interpretText();  // TODO: Check - is this what we need? Will send out signals?
110       //setValue(value());    // Try this. "setValue() will emit valueChanged() if the new value is different from the old one."
111 
112       QDoubleSpinBox::setDecimals(val);
113       double step = 1.0 / exp10(val);
114       setSingleStep(step);
115       }
116 
117 /*
118 //---------------------------------------------------------
119 //   validate
120 //---------------------------------------------------------
121 
122 QValidator::State SpinBoxFP::validate(QString& input, int& pos) const
123 {
124   // Must set these dynamically as settings may have changed.
125   validator->setRange(minimum(), maximum(), _decimals);
126 
127   QValidator::State s = validator->validate(input, pos);
128   return s;
129 }
130 
131 //---------------------------------------------------------
132 //   mapValueToText
133 //---------------------------------------------------------
134 
135 QString SpinBoxFP::textFromValue(int value) const
136       {
137       if (_decimals) {
138             QString s;
139             int div = int(exp10(_decimals));
140 //            printf("val %d, prec %d, div %d\n", value, _precision, div);
141 
142             s.sprintf("%d.%0*d", value/div, _decimals, value%div);
143             //s.sprintf("%0*f", value, _decimals);
144 
145             return s;
146             }
147       return QSpinBox::textFromValue(value);
148       }
149 
150 //---------------------------------------------------------
151 //   mapTextToValue
152 //---------------------------------------------------------
153 
154 int SpinBoxFP::valueFromText(const QString& text) const
155       {
156       //QString qs = cleanText();
157       if (_decimals) {
158             //const char* s = qs.toLatin1();
159             //const char* s = cleanText().toLatin1().data();
160 
161             //int a, b;
162             bool ok;
163             double f = text.toDouble(&ok);
164 
165             //int n = sscanf(s, "%d.%d", &a, &b);
166             //int n = sscanf(s, "%f", &f);
167 
168             //if (n != 2) {
169             //if (n != 1) {
170             if (!ok) {
171 
172                   // *ok = false;
173                   //return 0;
174                   // TODO: Check - Hmm, no OK parameter. Why return 0? Let's try:
175                   // Keep returning the current value until something valid comes in...
176                   return value();
177                   }
178 
179             //int div = int(exp10(_decimals));
180             double div = int(exp10(_decimals));
181 
182             //return a * div + b;
183             return (f * div);
184 
185             }
186       return QSpinBox::valueFromText(text);
187       }
188 
189 */
190 
191 } // namespace MusEGui
192