File: spin.cpp

package info (click to toggle)
kdeutils 2%3A980312-6
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 5,380 kB
  • ctags: 3,423
  • sloc: cpp: 17,658; ansic: 9,867; sh: 6,347; makefile: 2,060; awk: 378; sed: 111; perl: 108
file content (225 lines) | stat: -rw-r--r-- 5,910 bytes parent folder | download
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*

 $Id: spin.cpp,v 1.2 1997/11/09 22:59:09 wuebben Exp $

 KNotes - Notes for the KDE Project
 Copyright (C) 1997 Bernd Johannes Wuebben
 
 wuebben@math.cornell.edu
 wuebben@kde.org

 This class is a modified version of a class found in:

 qtremind - an X windows appoint reminder program.
 Copyright (C) 1997  Tom Daley

 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.

 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., 675 Mass Ave, Cambridge, MA 02139, USA.


*/


#include <qbitmap.h>
#include <qtimer.h>
#include <spin.h>

#include "up.xbm"
#include "down.xbm"


QtedSetInt::QtedSetInt(int min, int max, int start_value, int format, 
           QWidget *parent, const char *name) : 
           QWidget(parent, name), _min(min), _max(max),
           _format(format), _up_timer_p(NULL), _dn_timer_p(NULL) {
   if (_max <_min)		// make sure numbers make sense
      _max = _min;
 
   QString str;
   str.sprintf("%d", _min);	// find the max number string length 
   _length = str.length();
   str.sprintf("%d", _max);
   if ((int)str.length() > _length)
      _length = str.length();

   _upButton_p = new QPushButton(this);
   QBitmap up_bm(up_arrow_width, up_arrow_height, up_arrow_bits, TRUE);
   _upButton_p->setPixmap(up_bm);
   _upButton_p->setAutoResize(TRUE);

   _dnButton_p = new QPushButton(this);
   QBitmap dn_bm(dn_arrow_width, dn_arrow_height, dn_arrow_bits, TRUE);
   _dnButton_p->setPixmap(dn_bm);
   _dnButton_p->setAutoResize(TRUE);

   _label_p = new QLabel("Foo", this);
   _label_p->setAlignment(AlignCenter);
   _label_p->setLineWidth(2);
   _label_p->setFrameStyle(QFrame::Panel | QFrame::Sunken);
//   _label_p->setBackgroundColor(white);

   value(start_value);
   setSize();
   connect(_upButton_p, SIGNAL(pressed()), this, SLOT(upPressed()));
   connect(_upButton_p, SIGNAL(released()), this, SLOT(upReleased()));
   connect(_dnButton_p, SIGNAL(pressed()), this, SLOT(dnPressed()));
   connect(_dnButton_p, SIGNAL(released()), this, SLOT(dnReleased()));
}

//*****************************************************************************

void QtedSetInt::
setSize(void) {
  int num_wid;// mid_wid;
   int bt_sz = 13;
   int w, mid_w, label_h;
   label_h = fontMetrics().lineSpacing () + 2 * _label_p->frameWidth();

   num_wid = fontMetrics().maxWidth() * _length;
   num_wid += 2 * _label_p->frameWidth(); 

   if (num_wid > bt_sz * 2)
      w = num_wid;
   else 
      w = bt_sz * 2;
   mid_w = w / 2;

   _label_p->setGeometry(0, 0, w, label_h);
   _upButton_p->setGeometry(mid_w - bt_sz, label_h +2, bt_sz, bt_sz);
   _dnButton_p->setGeometry(mid_w, label_h +2, bt_sz, bt_sz);
   setFixedSize(w, label_h + bt_sz +2);
}

//*****************************************************************************

void QtedSetInt::
setText(void) {
   QString str;
   switch (_format) {
      case RightJustified:
         str.sprintf("%*d", _length, _value);
         break;
      case Centered:
         str.sprintf("%d", _value);
         break;
      case ZeroFilled:
         str.sprintf("%0*d", _length, _value);
         break;
   }
   _label_p->setText(str);
}

//*****************************************************************************

void QtedSetInt::
value(int the_value) {
   if (the_value < _min)
      _value = _min;
   else if (the_value > _max)
      _value = _max;
   else
      _value = the_value; 

   setText();
}

//*****************************************************************************

void QtedSetInt::
upPressed() {
   incValue();
   if (_up_timer_p == NULL) {
      _up_timer_p= new QTimer(this);
      connect(_up_timer_p, SIGNAL(timeout()), this, SLOT(upRepeat()));
   }
   if (_value == _max)
      _up_timer_p->stop();
   else
      _up_timer_p->changeInterval((int)(0.25 * 1000));
}

//*****************************************************************************

void QtedSetInt::
upReleased() {
   _up_timer_p->stop();
}

//*****************************************************************************
 
void QtedSetInt::
upRepeat() {
   incValue(); 
   if (_value == _max)
      _up_timer_p->stop();
   else
      _up_timer_p->changeInterval((int)(0.033 * 1000));
}

//*****************************************************************************

void QtedSetInt::
dnPressed() {
   decValue(); 
   if (_dn_timer_p == NULL) {
      _dn_timer_p= new QTimer(this);
      connect(_dn_timer_p, SIGNAL(timeout()), this, SLOT(dnRepeat()));
   }
   if (_value == _min)
      _dn_timer_p->stop();
   else
      _dn_timer_p->changeInterval((int)(0.25 * 1000));
}

//*****************************************************************************

void QtedSetInt::
dnReleased() {
   _dn_timer_p->stop();
}

//*****************************************************************************
 
void QtedSetInt::
dnRepeat() {
   decValue(); 
   if (_value == _min)
      _dn_timer_p->stop(); 
   else
      _dn_timer_p->changeInterval((int)(0.033 * 1000));
}

//*****************************************************************************

void QtedSetInt::
incValue(void) {
   if (_value < _max) {
      _value++;
      setText();
   }
}

//*****************************************************************************

void QtedSetInt::
decValue(void) {
   if (_value > _min) {
      _value--;
      setText();
   }
}

//*****************************************************************************

#include "spin.moc"