~ubuntu-branches/ubuntu/quantal/muse/quantal

« back to all changes in this revision

Viewing changes to awl/floatentry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-08-12 11:16:41 UTC
  • mto: (1.1.9) (10.1.6 sid)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: james.westby@ubuntu.com-20110812111641-72iatqb9jomjejko
ImportĀ upstreamĀ versionĀ 2.0~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//=============================================================================
 
2
//  Awl
 
3
//  Audio Widget Library
 
4
//  $Id:$
 
5
//
 
6
//  Copyright (C) 2002-2006 by Werner Schweer and others
 
7
//
 
8
//  This program is free software; you can redistribute it and/or modify
 
9
//  it under the terms of the GNU General Public License version 2.
 
10
//
 
11
//  This program is distributed in the hope that it will be useful,
 
12
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//  GNU General Public License for more details.
 
15
//
 
16
//  You should have received a copy of the GNU General Public License
 
17
//  along with this program; if not, write to the Free Software
 
18
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
//=============================================================================
 
20
 
 
21
#include "floatentry.h"
 
22
#include "fastlog.h"
 
23
 
 
24
#include <QLineEdit>
 
25
#include <QMouseEvent>
 
26
#include <QTimer>
 
27
 
 
28
#define TIMER1    400
 
29
#define TIMER2    200
 
30
#define TIMEC     7
 
31
#define TIMER3    100
 
32
#define TIMEC2    20
 
33
#define TIMER4    50
 
34
 
 
35
namespace Awl {
 
36
 
 
37
//---------------------------------------------------------
 
38
//   FloatEntry
 
39
//---------------------------------------------------------
 
40
 
 
41
FloatEntry::FloatEntry(QWidget* parent)
 
42
   : QLineEdit(parent)
 
43
      {
 
44
      _id        = 0;
 
45
      _minValue  = 0.0;
 
46
      _maxValue  = 1.0;
 
47
      _log       = false;
 
48
      evx        = 1.0;
 
49
      _precision = 3;
 
50
      timer      = new QTimer(this);
 
51
      connect(timer, SIGNAL(timeout()), SLOT(repeat()));
 
52
      _value = 0.0f;
 
53
      connect(this, SIGNAL(returnPressed()), SLOT(endEdit()));
 
54
      setCursor(QCursor(Qt::ArrowCursor));
 
55
      updateValue();
 
56
      }
 
57
 
 
58
//---------------------------------------------------------
 
59
//   setString
 
60
//---------------------------------------------------------
 
61
 
 
62
bool FloatEntry::setString(double v)
 
63
      {
 
64
      QString s;
 
65
//      if (v < _minValue || v > _maxValue) {
 
66
      if (v < _minValue) {
 
67
            if (!_specialText.isEmpty())
 
68
                  setText(_specialText);
 
69
            return true;
 
70
            }
 
71
      s.setNum(v, 'f', _precision);
 
72
      if (!_suffix.isEmpty()) {
 
73
            // s += " ";
 
74
            s += _suffix;
 
75
            }
 
76
      setText(s);
 
77
      return false;
 
78
      }
 
79
 
 
80
//---------------------------------------------------------
 
81
//   setSValue
 
82
//---------------------------------------------------------
 
83
 
 
84
void FloatEntry::setSValue(const QString& s)
 
85
      {
 
86
      bool ok;
 
87
      double v = s.toFloat(&ok);
 
88
      if (ok && (v != _value)) {
 
89
            if (v < _minValue)
 
90
                  v = _minValue;
 
91
            if (v > _maxValue)
 
92
                  v = _maxValue;
 
93
            _value = v;
 
94
            updateValue();
 
95
            valueChange();
 
96
            }
 
97
      }
 
98
 
 
99
//---------------------------------------------------------
 
100
//   valueChange
 
101
//---------------------------------------------------------
 
102
 
 
103
void FloatEntry::valueChange()
 
104
      {
 
105
      emit valueChanged(value(), _id);
 
106
      }
 
107
 
 
108
//---------------------------------------------------------
 
109
//   incValue
 
110
//---------------------------------------------------------
 
111
 
 
112
void FloatEntry::incValue(double)
 
113
      {
 
114
      if (_value + 1.0 < _maxValue) {
 
115
            _value = _value + 1.0;
 
116
            updateValue();
 
117
            valueChange();
 
118
            }
 
119
      }
 
120
 
 
121
//---------------------------------------------------------
 
122
//   decValue
 
123
//---------------------------------------------------------
 
124
 
 
125
void FloatEntry::decValue(double)
 
126
      {
 
127
      if (_value - 1.0 > _minValue) {
 
128
            _value = _value - 1.0;
 
129
            updateValue();
 
130
            valueChange();
 
131
            }
 
132
      }
 
133
 
 
134
//---------------------------------------------------------
 
135
//   setPrecision
 
136
//---------------------------------------------------------
 
137
 
 
138
void FloatEntry::setPrecision(int v)
 
139
      {
 
140
      _precision = v;
 
141
      setString(_value);
 
142
      }
 
143
 
 
144
//---------------------------------------------------------
 
145
//   sizeHint
 
146
//---------------------------------------------------------
 
147
 
 
148
QSize FloatEntry::sizeHint() const
 
149
      {
 
150
      QFontMetrics fm = fontMetrics();
 
151
      int h           = fm.height() + 4;
 
152
      int n = _precision + 3;
 
153
      int w = fm.width(QString("-0.")) + fm.width('0') * n + 6;
 
154
      return QSize(w, h);
 
155
      }
 
156
 
 
157
//---------------------------------------------------------
 
158
//   endEdit
 
159
//---------------------------------------------------------
 
160
 
 
161
void FloatEntry::endEdit()
 
162
      {
 
163
      if (QLineEdit::isModified())
 
164
            setSValue(text());
 
165
      clearFocus();
 
166
      }
 
167
 
 
168
//---------------------------------------------------------
 
169
//   mousePressEvent
 
170
//---------------------------------------------------------
 
171
 
 
172
void FloatEntry::mousePressEvent(QMouseEvent* event)
 
173
      {
 
174
      button = event->button();
 
175
      starty = event->y();
 
176
      evx    = double(event->x());
 
177
      timecount = 0;
 
178
      repeat();
 
179
      timer->start(TIMER1);
 
180
      }
 
181
 
 
182
//---------------------------------------------------------
 
183
//   wheelEvent
 
184
//---------------------------------------------------------
 
185
 
 
186
void FloatEntry::wheelEvent(QWheelEvent* event)
 
187
      {
 
188
      int delta = event->delta();
 
189
 
 
190
      if (delta < 0)
 
191
            decValue(-1.0);
 
192
      else if (delta > 0)
 
193
            incValue(1.0);
 
194
      }
 
195
 
 
196
//---------------------------------------------------------
 
197
//   repeat
 
198
//---------------------------------------------------------
 
199
 
 
200
void FloatEntry::repeat()
 
201
      {
 
202
      if (timecount == 1) {
 
203
           ++timecount;
 
204
            timer->stop();
 
205
            timer->start(TIMER2);
 
206
            return;
 
207
            }
 
208
      ++timecount;
 
209
      if (timecount == TIMEC) {
 
210
            timer->stop();
 
211
            timer->start(TIMER3);
 
212
            }
 
213
      if (timecount == TIMEC2) {
 
214
            timer->stop();
 
215
            timer->start(TIMER4);
 
216
            }
 
217
 
 
218
      switch (button) {
 
219
            case Qt::LeftButton:
 
220
                  return;
 
221
            case Qt::MidButton:
 
222
                  decValue(evx);
 
223
                  break;
 
224
            case Qt::RightButton:
 
225
                  incValue(evx);
 
226
                  break;
 
227
            default:
 
228
                  break;
 
229
            }
 
230
      }
 
231
 
 
232
//---------------------------------------------------------
 
233
//   mouseReleaseEvent
 
234
//---------------------------------------------------------
 
235
 
 
236
void FloatEntry::mouseReleaseEvent(QMouseEvent*)
 
237
      {
 
238
      button = Qt::NoButton;
 
239
      timer->stop();
 
240
      }
 
241
 
 
242
//---------------------------------------------------------
 
243
//   mouseMoveEvent
 
244
//---------------------------------------------------------
 
245
 
 
246
void FloatEntry::mouseMoveEvent(QMouseEvent*)
 
247
      {
 
248
      switch (button) {
 
249
            case Qt::LeftButton:
 
250
                  break;
 
251
            case Qt::MidButton:
 
252
                  break;
 
253
            case Qt::RightButton:
 
254
                  break;
 
255
            default:
 
256
                  break;
 
257
            }
 
258
      }
 
259
 
 
260
//---------------------------------------------------------
 
261
//   mouseDoubleClickEvent
 
262
//---------------------------------------------------------
 
263
 
 
264
void FloatEntry::mouseDoubleClickEvent(QMouseEvent* event)
 
265
      {
 
266
      if (event->button() != Qt::LeftButton) {
 
267
            mousePressEvent(event);
 
268
            return;
 
269
            }
 
270
      setFocus();
 
271
      QLineEdit::setFrame(true);
 
272
      update();
 
273
      }
 
274
 
 
275
//---------------------------------------------------------
 
276
//   setValue
 
277
//---------------------------------------------------------
 
278
 
 
279
void FloatEntry::setValue(double val)
 
280
      {
 
281
      if (_log) {
 
282
            if (val == 0.0f)
 
283
                  _value = _minValue;
 
284
            else
 
285
                  _value = fast_log10(val) * 20.0f;
 
286
            }
 
287
      else
 
288
            _value = val;
 
289
      updateValue();
 
290
      }
 
291
 
 
292
//---------------------------------------------------------
 
293
//   updateValue
 
294
//---------------------------------------------------------
 
295
 
 
296
void FloatEntry::updateValue()
 
297
      {
 
298
      if (setString(_value)) {
 
299
            // value is out of range:
 
300
            if (_value > _maxValue)
 
301
                  _value = _maxValue;
 
302
            else if (_value < _minValue)
 
303
                  _value = _minValue;
 
304
            }
 
305
      }
 
306
 
 
307
//---------------------------------------------------------
 
308
//   value
 
309
//---------------------------------------------------------
 
310
 
 
311
double FloatEntry::value() const
 
312
      {
 
313
      double rv;
 
314
      if (_log)
 
315
            rv = pow(10.0, _value * 0.05f);
 
316
      else
 
317
            rv = _value;
 
318
      return rv;
 
319
      }
 
320
}
 
321