1 | /****************************************************************************
|
---|
2 | ** $Id: qdial.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of the dial widget
|
---|
5 | **
|
---|
6 | ** Created : 979899
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the widgets module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include "qdial.h"
|
---|
39 |
|
---|
40 | #ifndef QT_NO_DIAL
|
---|
41 |
|
---|
42 | #include "qpainter.h"
|
---|
43 | #include "qpointarray.h"
|
---|
44 | #include "qcolor.h"
|
---|
45 | #include "qapplication.h"
|
---|
46 | #include "qregion.h"
|
---|
47 | #include "qbitmap.h"
|
---|
48 | #include "qstyle.h"
|
---|
49 | #if defined(QT_ACCESSIBILITY_SUPPORT)
|
---|
50 | #include "qaccessible.h"
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #include <math.h> // sin(), cos(), atan()
|
---|
54 | //### Forutsetter linking med math lib - Jfr kommentar i qpainter_x11.cpp!
|
---|
55 |
|
---|
56 | static const double m_pi = 3.14159265358979323846;
|
---|
57 | static const double rad_factor = 180.0 / m_pi;
|
---|
58 |
|
---|
59 |
|
---|
60 | class QDialPrivate
|
---|
61 | {
|
---|
62 | public:
|
---|
63 | QDialPrivate()
|
---|
64 | {
|
---|
65 | wrapping = FALSE;
|
---|
66 | tracking = TRUE;
|
---|
67 | doNotEmit = FALSE;
|
---|
68 | target = 3.7;
|
---|
69 | mousePressed = FALSE;
|
---|
70 | }
|
---|
71 |
|
---|
72 | bool wrapping;
|
---|
73 | bool tracking;
|
---|
74 | bool doNotEmit;
|
---|
75 | double target;
|
---|
76 | QRect eraseArea;
|
---|
77 | bool eraseAreaValid;
|
---|
78 | bool showNotches;
|
---|
79 | bool onlyOutside;
|
---|
80 | bool mousePressed;
|
---|
81 |
|
---|
82 | QPointArray lines;
|
---|
83 | };
|
---|
84 |
|
---|
85 |
|
---|
86 | /*!
|
---|
87 | \class QDial qdial.h
|
---|
88 |
|
---|
89 | \brief The QDial class provides a rounded range control (like a speedometer or potentiometer).
|
---|
90 |
|
---|
91 | \ingroup basic
|
---|
92 | \mainclass
|
---|
93 |
|
---|
94 | QDial is used when the user needs to control a value within a
|
---|
95 | program-definable range, and the range either wraps around
|
---|
96 | (typically, 0..359 degrees) or the dialog layout needs a square
|
---|
97 | widget.
|
---|
98 |
|
---|
99 | Both API- and UI-wise, the dial is very similar to a \link QSlider
|
---|
100 | slider. \endlink Indeed, when wrapping() is FALSE (the default)
|
---|
101 | there is no real difference between a slider and a dial. They
|
---|
102 | have the same signals, slots and member functions, all of which do
|
---|
103 | the same things. Which one you use depends only on your taste
|
---|
104 | and on the application.
|
---|
105 |
|
---|
106 | The dial initially emits valueChanged() signals continuously while
|
---|
107 | the slider is being moved; you can make it emit the signal less
|
---|
108 | often by calling setTracking(FALSE). dialMoved() is emitted
|
---|
109 | continuously even when tracking() is FALSE.
|
---|
110 |
|
---|
111 | The slider also emits dialPressed() and dialReleased() signals
|
---|
112 | when the mouse button is pressed and released. But note that the
|
---|
113 | dial's value can change without these signals being emitted; the
|
---|
114 | keyboard and wheel can be used to change the value.
|
---|
115 |
|
---|
116 | Unlike the slider, QDial attempts to draw a "nice" number of
|
---|
117 | notches rather than one per lineStep(). If possible, the number
|
---|
118 | of notches drawn is one per lineStep(), but if there aren't enough
|
---|
119 | pixels to draw every one, QDial will draw every second, third
|
---|
120 | etc., notch. notchSize() returns the number of units per notch,
|
---|
121 | hopefully a multiple of lineStep(); setNotchTarget() sets the
|
---|
122 | target distance between neighbouring notches in pixels. The
|
---|
123 | default is 3.75 pixels.
|
---|
124 |
|
---|
125 | Like the slider, the dial makes the QRangeControl functions
|
---|
126 | setValue(), addLine(), subtractLine(), addPage() and
|
---|
127 | subtractPage() available as slots.
|
---|
128 |
|
---|
129 | The dial's keyboard interface is fairly simple: The left/up and
|
---|
130 | right/down arrow keys move by lineStep(), page up and page down by
|
---|
131 | pageStep() and Home and End to minValue() and maxValue().
|
---|
132 |
|
---|
133 | <img src=qdial-m.png> <img src=qdial-w.png>
|
---|
134 |
|
---|
135 | \sa QScrollBar QSpinBox
|
---|
136 | \link guibooks.html#fowler GUI Design Handbook: Slider\endlink
|
---|
137 | */
|
---|
138 |
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 | /*!
|
---|
143 | Constructs a dial called \a name with parent \a parent. \a f is
|
---|
144 | propagated to the QWidget constructor. It has the default range of
|
---|
145 | a QRangeControl.
|
---|
146 | */
|
---|
147 |
|
---|
148 | QDial::QDial( QWidget *parent, const char *name, WFlags f )
|
---|
149 | : QWidget( parent, name, f | WNoAutoErase ), QRangeControl()
|
---|
150 | {
|
---|
151 | d = new QDialPrivate;
|
---|
152 | d->eraseAreaValid = FALSE;
|
---|
153 | d->showNotches = FALSE;
|
---|
154 | d->onlyOutside = FALSE;
|
---|
155 | setFocusPolicy( QWidget::WheelFocus );
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 | /*!
|
---|
161 | Constructs a dial called \a name with parent \a parent. The dial's
|
---|
162 | value can never be smaller than \a minValue or greater than \a
|
---|
163 | maxValue. Its page step size is \a pageStep, and its initial value
|
---|
164 | is \a value.
|
---|
165 |
|
---|
166 | \a value is forced to be within the legal range.
|
---|
167 | */
|
---|
168 |
|
---|
169 | QDial::QDial( int minValue, int maxValue, int pageStep, int value,
|
---|
170 | QWidget *parent, const char *name )
|
---|
171 | : QWidget( parent, name, WNoAutoErase ),
|
---|
172 | QRangeControl( minValue, maxValue, 1, pageStep, value )
|
---|
173 | {
|
---|
174 | d = new QDialPrivate;
|
---|
175 | d->eraseAreaValid = FALSE;
|
---|
176 | d->showNotches = FALSE;
|
---|
177 | d->onlyOutside = FALSE;
|
---|
178 | setFocusPolicy( QWidget::WheelFocus );
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*!
|
---|
182 | Destroys the dial.
|
---|
183 | */
|
---|
184 | QDial::~QDial()
|
---|
185 | {
|
---|
186 | delete d;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | void QDial::setTracking( bool enable )
|
---|
191 | {
|
---|
192 | d->tracking = enable;
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | /*!
|
---|
197 | \property QDial::tracking
|
---|
198 | \brief whether tracking is enabled
|
---|
199 |
|
---|
200 | If TRUE (the default), tracking is enabled. This means that the
|
---|
201 | arrow can be moved using the mouse; otherwise the arrow cannot be
|
---|
202 | moved with the mouse.
|
---|
203 | */
|
---|
204 |
|
---|
205 | bool QDial::tracking() const
|
---|
206 | {
|
---|
207 | return d ? d->tracking : TRUE;
|
---|
208 | }
|
---|
209 |
|
---|
210 | void QDial::setValue( int newValue )
|
---|
211 | { // ### set doNotEmit? Matthias?
|
---|
212 | QRangeControl::setValue( newValue );
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /*!
|
---|
217 | Increments the dial's value() by one lineStep().
|
---|
218 | */
|
---|
219 |
|
---|
220 | void QDial::addLine()
|
---|
221 | {
|
---|
222 | QRangeControl::addLine();
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /*!
|
---|
227 | Decrements the dial's value() by one lineStep().
|
---|
228 | */
|
---|
229 |
|
---|
230 | void QDial::subtractLine()
|
---|
231 | {
|
---|
232 | QRangeControl::subtractLine();
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | /*! \reimp */
|
---|
237 |
|
---|
238 | void QDial::resizeEvent( QResizeEvent * e )
|
---|
239 | {
|
---|
240 | d->lines.resize( 0 );
|
---|
241 | QWidget::resizeEvent( e );
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | /*!
|
---|
246 | \reimp
|
---|
247 | */
|
---|
248 |
|
---|
249 | void QDial::paintEvent( QPaintEvent * e )
|
---|
250 | {
|
---|
251 | repaintScreen( &e->rect() );
|
---|
252 | }
|
---|
253 |
|
---|
254 | /*!
|
---|
255 | Paints the dial using clip region \a cr.
|
---|
256 | */
|
---|
257 |
|
---|
258 | void QDial::repaintScreen( const QRect *cr )
|
---|
259 | {
|
---|
260 | QPainter p;
|
---|
261 | p.begin( this );
|
---|
262 |
|
---|
263 | bool resetClipping = FALSE;
|
---|
264 |
|
---|
265 | // calculate clip-region for erasing background
|
---|
266 | if ( cr ) {
|
---|
267 | p.setClipRect( *cr );
|
---|
268 | } else if ( !d->onlyOutside && d->eraseAreaValid ) {
|
---|
269 | QRegion reg = d->eraseArea;
|
---|
270 | double a;
|
---|
271 | reg = reg.subtract( calcArrow( a ) );
|
---|
272 | p.setClipRegion( reg );
|
---|
273 | resetClipping = TRUE;
|
---|
274 | }
|
---|
275 |
|
---|
276 | QRect br( calcDial() );
|
---|
277 | p.setPen( NoPen );
|
---|
278 | // if ( style() == MotifStyle )
|
---|
279 | // p.setBrush( colorGroup().brush( QColorGroup::Mid ) );
|
---|
280 | // else {
|
---|
281 | QBrush b;
|
---|
282 | if ( colorGroup().brush( QColorGroup::Light ).pixmap() )
|
---|
283 | b = QBrush( colorGroup().brush( QColorGroup::Light ) );
|
---|
284 | else
|
---|
285 | b = QBrush( colorGroup().light(), Dense4Pattern );
|
---|
286 | p.setBrush( b );
|
---|
287 | p.setBackgroundMode( OpaqueMode );
|
---|
288 | // }
|
---|
289 |
|
---|
290 | QRect te = br;
|
---|
291 | te.setWidth(te.width()+2);
|
---|
292 | te.setHeight(te.height()+2);
|
---|
293 | // erase background of dial
|
---|
294 | if ( !d->onlyOutside ) {
|
---|
295 | p.drawEllipse( te );
|
---|
296 | }
|
---|
297 |
|
---|
298 | // erase remaining space around the dial
|
---|
299 | QRegion remaining( 0, 0, width(), height() );
|
---|
300 | remaining = remaining.subtract( QRegion( te, QRegion::Ellipse ) );
|
---|
301 | if ( p.hasClipping() )
|
---|
302 | remaining = remaining.intersect( p.clipRegion() );
|
---|
303 | erase(remaining);
|
---|
304 |
|
---|
305 | if ( resetClipping ) {
|
---|
306 | if ( cr )
|
---|
307 | p.setClipRect( *cr );
|
---|
308 | else
|
---|
309 | p.setClipRect( QRect( 0, 0, width(), height() ) );
|
---|
310 | }
|
---|
311 |
|
---|
312 | // draw notches
|
---|
313 | if ( d->showNotches ) {
|
---|
314 | calcLines();
|
---|
315 | p.setPen( colorGroup().foreground() );
|
---|
316 | p.drawLineSegments( d->lines );
|
---|
317 | }
|
---|
318 |
|
---|
319 | // calculate and paint arrow
|
---|
320 | p.setPen( QPen( colorGroup().dark() ) );
|
---|
321 | p.drawArc( te, 60 * 16, 180 * 16 );
|
---|
322 | p.setPen( QPen( colorGroup().light() ) );
|
---|
323 | p.drawArc( te, 240 * 16, 180 * 16 );
|
---|
324 |
|
---|
325 | double a;
|
---|
326 | QPointArray arrow( calcArrow( a ) );
|
---|
327 | QRect ea( arrow.boundingRect() );
|
---|
328 | d->eraseArea = ea;
|
---|
329 | d->eraseAreaValid = TRUE;
|
---|
330 |
|
---|
331 | p.setPen( NoPen );
|
---|
332 | p.setBrush( colorGroup().brush( QColorGroup::Button ) );
|
---|
333 | if ( !d->onlyOutside )
|
---|
334 | p.drawPolygon( arrow );
|
---|
335 |
|
---|
336 | a = angle( QPoint( width() / 2, height() / 2 ), arrow[ 0 ] );
|
---|
337 | p.setBrush( Qt::NoBrush );
|
---|
338 |
|
---|
339 | // that's still a hack...
|
---|
340 | if ( a <= 0 || a > 200 ) {
|
---|
341 | p.setPen( colorGroup().light() );
|
---|
342 | p.drawLine( arrow[ 2 ], arrow[ 0 ] );
|
---|
343 | p.drawLine( arrow[ 1 ], arrow[ 2 ] );
|
---|
344 | p.setPen( colorGroup().dark() );
|
---|
345 | p.drawLine( arrow[ 0 ], arrow[ 1 ] );
|
---|
346 | } else if ( a > 0 && a < 45 ) {
|
---|
347 | p.setPen( colorGroup().light() );
|
---|
348 | p.drawLine( arrow[ 2 ], arrow[ 0 ] );
|
---|
349 | p.setPen( colorGroup().dark() );
|
---|
350 | p.drawLine( arrow[ 1 ], arrow[ 2 ] );
|
---|
351 | p.drawLine( arrow[ 0 ], arrow[ 1 ] );
|
---|
352 | } else if ( a >= 45 && a < 135 ) {
|
---|
353 | p.setPen( colorGroup().dark() );
|
---|
354 | p.drawLine( arrow[ 2 ], arrow[ 0 ] );
|
---|
355 | p.drawLine( arrow[ 1 ], arrow[ 2 ] );
|
---|
356 | p.setPen( colorGroup().light() );
|
---|
357 | p.drawLine( arrow[ 0 ], arrow[ 1 ] );
|
---|
358 | } else if ( a >= 135 && a < 200 ) {
|
---|
359 | p.setPen( colorGroup().dark() );
|
---|
360 | p.drawLine( arrow[ 2 ], arrow[ 0 ] );
|
---|
361 | p.setPen( colorGroup().light() );
|
---|
362 | p.drawLine( arrow[ 0 ], arrow[ 1 ] );
|
---|
363 | p.drawLine( arrow[ 1 ], arrow[ 2 ] );
|
---|
364 | }
|
---|
365 |
|
---|
366 | // draw focus rect around the dial
|
---|
367 | if ( hasFocus() ) {
|
---|
368 | p.setClipping( FALSE );
|
---|
369 | br.setWidth( br.width() + 2 );
|
---|
370 | br.setHeight( br.height() + 2 );
|
---|
371 | if ( d->showNotches ) {
|
---|
372 | int r = QMIN( width(), height() ) / 2;
|
---|
373 | br.moveBy( -r / 6, - r / 6 );
|
---|
374 | br.setWidth( br.width() + r / 3 );
|
---|
375 | br.setHeight( br.height() + r / 3 );
|
---|
376 | }
|
---|
377 | // strange, but else we get redraw errors on Windows
|
---|
378 | p.end();
|
---|
379 | p.begin( this );
|
---|
380 | p.save();
|
---|
381 | p.setPen( QPen( colorGroup().background() ) );
|
---|
382 | p.setBrush( NoBrush );
|
---|
383 | p.drawRect( br );
|
---|
384 | p.restore();
|
---|
385 | style().drawPrimitive( QStyle::PE_FocusRect, &p, br, colorGroup());
|
---|
386 | }
|
---|
387 | p.end();
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | /*!
|
---|
392 | \reimp
|
---|
393 | */
|
---|
394 |
|
---|
395 | void QDial::keyPressEvent( QKeyEvent * e )
|
---|
396 | {
|
---|
397 | switch ( e->key() ) {
|
---|
398 | case Key_Left: case Key_Down:
|
---|
399 | subtractLine();
|
---|
400 | break;
|
---|
401 | case Key_Right: case Key_Up:
|
---|
402 | addLine();
|
---|
403 | break;
|
---|
404 | case Key_Prior:
|
---|
405 | subtractPage();
|
---|
406 | break;
|
---|
407 | case Key_Next:
|
---|
408 | addPage();
|
---|
409 | break;
|
---|
410 | case Key_Home:
|
---|
411 | setValue( minValue() );
|
---|
412 | break;
|
---|
413 | case Key_End:
|
---|
414 | setValue( maxValue() );
|
---|
415 | break;
|
---|
416 | default:
|
---|
417 | e->ignore();
|
---|
418 | break;
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 | /*!
|
---|
424 | \reimp
|
---|
425 | */
|
---|
426 |
|
---|
427 | void QDial::mousePressEvent( QMouseEvent * e )
|
---|
428 | {
|
---|
429 | d->mousePressed = TRUE;
|
---|
430 | setValue( valueFromPoint( e->pos() ) );
|
---|
431 | emit dialPressed();
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | /*!
|
---|
436 | \reimp
|
---|
437 | */
|
---|
438 |
|
---|
439 | void QDial::mouseReleaseEvent( QMouseEvent * e )
|
---|
440 | {
|
---|
441 | d->mousePressed = FALSE;
|
---|
442 | setValue( valueFromPoint( e->pos() ) );
|
---|
443 | emit dialReleased();
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | /*!
|
---|
448 | \reimp
|
---|
449 | */
|
---|
450 |
|
---|
451 | void QDial::mouseMoveEvent( QMouseEvent * e )
|
---|
452 | {
|
---|
453 | if ( !d->mousePressed )
|
---|
454 | return;
|
---|
455 | if ( !d->tracking || (e->state() & LeftButton) == 0 )
|
---|
456 | return;
|
---|
457 | d->doNotEmit = TRUE;
|
---|
458 | setValue( valueFromPoint( e->pos() ) );
|
---|
459 | emit dialMoved( value() );
|
---|
460 | d->doNotEmit = FALSE;
|
---|
461 | }
|
---|
462 |
|
---|
463 |
|
---|
464 | /*!
|
---|
465 | \reimp
|
---|
466 | */
|
---|
467 | #ifndef QT_NO_WHEELEVENT
|
---|
468 | void QDial::wheelEvent( QWheelEvent *e )
|
---|
469 | {
|
---|
470 | setValue( value() - e->delta() / 120 );
|
---|
471 | }
|
---|
472 | #endif
|
---|
473 |
|
---|
474 | /*!
|
---|
475 | \reimp
|
---|
476 | */
|
---|
477 |
|
---|
478 | void QDial::focusInEvent( QFocusEvent * )
|
---|
479 | {
|
---|
480 | d->onlyOutside = TRUE;
|
---|
481 | repaintScreen();
|
---|
482 | d->onlyOutside = FALSE;
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|
486 | /*!
|
---|
487 | \reimp
|
---|
488 | */
|
---|
489 |
|
---|
490 | void QDial::focusOutEvent( QFocusEvent * )
|
---|
491 | {
|
---|
492 | d->onlyOutside = TRUE;
|
---|
493 | repaintScreen();
|
---|
494 | d->onlyOutside = FALSE;
|
---|
495 | }
|
---|
496 |
|
---|
497 | /*!
|
---|
498 | Reimplemented to ensure the display is correct and to emit the
|
---|
499 | valueChanged(int) signal when appropriate.
|
---|
500 | */
|
---|
501 |
|
---|
502 | void QDial::valueChange()
|
---|
503 | {
|
---|
504 | d->lines.resize( 0 );
|
---|
505 | repaintScreen();
|
---|
506 | if ( d->tracking || !d->doNotEmit ) {
|
---|
507 | emit valueChanged( value() );
|
---|
508 | #if defined(QT_ACCESSIBILITY_SUPPORT)
|
---|
509 | QAccessible::updateAccessibility( this, 0, QAccessible::ValueChanged );
|
---|
510 | #endif
|
---|
511 | }
|
---|
512 | }
|
---|
513 |
|
---|
514 |
|
---|
515 | /*!
|
---|
516 | Reimplemented to ensure tick-marks are consistent with the new range.
|
---|
517 | */
|
---|
518 |
|
---|
519 | void QDial::rangeChange()
|
---|
520 | {
|
---|
521 | d->lines.resize( 0 );
|
---|
522 | repaintScreen();
|
---|
523 | }
|
---|
524 |
|
---|
525 |
|
---|
526 | /*!
|
---|
527 | \internal
|
---|
528 | */
|
---|
529 |
|
---|
530 | int QDial::valueFromPoint( const QPoint & p ) const
|
---|
531 | {
|
---|
532 | double a = atan2( (double)height()/2.0 - p.y(),
|
---|
533 | (double)p.x() - width()/2.0 );
|
---|
534 | if ( a < m_pi/-2 )
|
---|
535 | a = a + m_pi*2;
|
---|
536 |
|
---|
537 | int dist = 0;
|
---|
538 | int minv = minValue(), maxv = maxValue();
|
---|
539 |
|
---|
540 | if ( minValue() < 0 ) {
|
---|
541 | dist = -minValue();
|
---|
542 | minv = 0;
|
---|
543 | maxv = maxValue() + dist;
|
---|
544 | }
|
---|
545 |
|
---|
546 | int r = maxv - minv;
|
---|
547 | int v;
|
---|
548 | if ( d->wrapping )
|
---|
549 | v = (int)(0.5 + minv + r*(m_pi*3/2-a)/(2*m_pi));
|
---|
550 | else
|
---|
551 | v = (int)(0.5 + minv + r*(m_pi*4/3-a)/(m_pi*10/6));
|
---|
552 |
|
---|
553 | if ( dist > 0 )
|
---|
554 | v -= dist;
|
---|
555 |
|
---|
556 | return bound( v );
|
---|
557 | }
|
---|
558 |
|
---|
559 |
|
---|
560 | /*!
|
---|
561 | \internal
|
---|
562 | */
|
---|
563 |
|
---|
564 | double QDial::angle( const QPoint &p1, const QPoint &p2 ) const
|
---|
565 | {
|
---|
566 | double _angle = 0.0;
|
---|
567 |
|
---|
568 | if ( p1.x() == p2.x() ) {
|
---|
569 | if ( p1.y() < p2.y() )
|
---|
570 | _angle = 270.0;
|
---|
571 | else
|
---|
572 | _angle = 90.0;
|
---|
573 | } else {
|
---|
574 | double x1, x2, y1, y2;
|
---|
575 |
|
---|
576 | if ( p1.x() <= p2.x() ) {
|
---|
577 | x1 = p1.x(); y1 = p1.y();
|
---|
578 | x2 = p2.x(); y2 = p2.y();
|
---|
579 | } else {
|
---|
580 | x2 = p1.x(); y2 = p1.y();
|
---|
581 | x1 = p2.x(); y1 = p2.y();
|
---|
582 | }
|
---|
583 |
|
---|
584 | double m = -( y2 - y1 ) / ( x2 - x1 );
|
---|
585 | _angle = atan( m ) * rad_factor;
|
---|
586 |
|
---|
587 | if ( p1.x() < p2.x() )
|
---|
588 | _angle = 180.0 - _angle;
|
---|
589 | else
|
---|
590 | _angle = -_angle;
|
---|
591 | }
|
---|
592 |
|
---|
593 | return _angle;
|
---|
594 | }
|
---|
595 |
|
---|
596 | void QDial::setWrapping( bool enable )
|
---|
597 | {
|
---|
598 | if ( d->wrapping == enable )
|
---|
599 | return;
|
---|
600 | d->lines.resize( 0 );
|
---|
601 | d->wrapping = enable;
|
---|
602 | d->eraseAreaValid = FALSE;
|
---|
603 | repaintScreen();
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | /*!
|
---|
608 | \property QDial::wrapping
|
---|
609 | \brief whether wrapping is enabled
|
---|
610 |
|
---|
611 | If TRUE, wrapping is enabled. This means that the arrow can be
|
---|
612 | turned around 360°. Otherwise there is some space at the bottom of
|
---|
613 | the dial which is skipped by the arrow.
|
---|
614 |
|
---|
615 | This property's default is FALSE.
|
---|
616 | */
|
---|
617 |
|
---|
618 | bool QDial::wrapping() const
|
---|
619 | {
|
---|
620 | return d->wrapping;
|
---|
621 | }
|
---|
622 |
|
---|
623 |
|
---|
624 | /*!
|
---|
625 | \property QDial::notchSize
|
---|
626 | \brief the current notch size
|
---|
627 |
|
---|
628 | The notch size is in range control units, not pixels, and if
|
---|
629 | possible it is a multiple of lineStep() that results in an
|
---|
630 | on-screen notch size near notchTarget().
|
---|
631 |
|
---|
632 | \sa notchTarget() lineStep()
|
---|
633 | */
|
---|
634 |
|
---|
635 | int QDial::notchSize() const
|
---|
636 | {
|
---|
637 | // radius of the arc
|
---|
638 | int r = QMIN( width(), height() )/2;
|
---|
639 | // length of the whole arc
|
---|
640 | int l = (int)(r*(d->wrapping ? 6 : 5)*m_pi/6);
|
---|
641 | // length of the arc from minValue() to minValue()+pageStep()
|
---|
642 | if ( maxValue() > minValue()+pageStep() )
|
---|
643 | l = (int)(0.5 + l * pageStep() / (maxValue()-minValue()));
|
---|
644 | // length of a lineStep() arc
|
---|
645 | l = l * lineStep() / pageStep();
|
---|
646 | if ( l < 1 )
|
---|
647 | l = 1;
|
---|
648 | // how many times lineStep can be draw in d->target pixels
|
---|
649 | l = (int)(0.5 + d->target / l);
|
---|
650 | // we want notchSize() to be a non-zero multiple of lineStep()
|
---|
651 | if ( !l )
|
---|
652 | l = 1;
|
---|
653 | return lineStep() * l;
|
---|
654 | }
|
---|
655 |
|
---|
656 | void QDial::setNotchTarget( double target )
|
---|
657 | {
|
---|
658 | d->lines.resize( 0 );
|
---|
659 | d->target = target;
|
---|
660 | d->eraseAreaValid = FALSE;
|
---|
661 | d->onlyOutside = TRUE;
|
---|
662 | repaintScreen();
|
---|
663 | d->onlyOutside = FALSE;
|
---|
664 | }
|
---|
665 |
|
---|
666 |
|
---|
667 | /*!
|
---|
668 | \property QDial::notchTarget
|
---|
669 | \brief the target number of pixels between notches
|
---|
670 |
|
---|
671 | The notch target is the number of pixels QDial attempts to put
|
---|
672 | between each notch.
|
---|
673 |
|
---|
674 | The actual size may differ from the target size.
|
---|
675 | */
|
---|
676 |
|
---|
677 | double QDial::notchTarget() const
|
---|
678 | {
|
---|
679 | return d->target;
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | /*!
|
---|
684 | Increments the dial's value() by one pageStep() of steps.
|
---|
685 | */
|
---|
686 |
|
---|
687 | void QDial::addPage()
|
---|
688 | {
|
---|
689 | QRangeControl::addPage();
|
---|
690 | }
|
---|
691 |
|
---|
692 |
|
---|
693 | /*!
|
---|
694 | Decrements the dial's value() by one pageStep() of steps.
|
---|
695 | */
|
---|
696 |
|
---|
697 | void QDial::subtractPage()
|
---|
698 | {
|
---|
699 | QRangeControl::subtractPage();
|
---|
700 | }
|
---|
701 |
|
---|
702 |
|
---|
703 | /*!
|
---|
704 | \fn void QDial::valueChanged( int value )
|
---|
705 |
|
---|
706 | This signal is emitted whenever the dial's \a value changes. The
|
---|
707 | frequency of this signal is influenced by setTracking().
|
---|
708 | */
|
---|
709 |
|
---|
710 | /*!
|
---|
711 | \fn void QDial::dialPressed()
|
---|
712 |
|
---|
713 | This signal is emitted when the user begins mouse interaction with
|
---|
714 | the dial.
|
---|
715 |
|
---|
716 | \sa dialReleased()
|
---|
717 | */
|
---|
718 |
|
---|
719 | /*!
|
---|
720 | \fn void QDial::dialMoved( int value )
|
---|
721 |
|
---|
722 | This signal is emitted whenever the dial \a value changes. The
|
---|
723 | frequency of this signal is \e not influenced by setTracking().
|
---|
724 |
|
---|
725 | \sa valueChanged()
|
---|
726 | */
|
---|
727 |
|
---|
728 | /*!
|
---|
729 | \fn void QDial::dialReleased()
|
---|
730 |
|
---|
731 | This signal is emitted when the user ends mouse interaction with
|
---|
732 | the dial.
|
---|
733 |
|
---|
734 | \sa dialPressed()
|
---|
735 | */
|
---|
736 |
|
---|
737 | void QDial::setNotchesVisible( bool b )
|
---|
738 | {
|
---|
739 | d->showNotches = b;
|
---|
740 | d->eraseAreaValid = FALSE;
|
---|
741 | d->onlyOutside = TRUE;
|
---|
742 | repaintScreen();
|
---|
743 | d->onlyOutside = FALSE;
|
---|
744 | }
|
---|
745 |
|
---|
746 | /*!
|
---|
747 | \property QDial::notchesVisible
|
---|
748 | \brief whether the notches are shown
|
---|
749 |
|
---|
750 | If TRUE, the notches are shown. If FALSE (the default) notches are
|
---|
751 | not shown.
|
---|
752 | */
|
---|
753 | bool QDial::notchesVisible() const
|
---|
754 | {
|
---|
755 | return d->showNotches;
|
---|
756 | }
|
---|
757 |
|
---|
758 | /*!
|
---|
759 | \reimp
|
---|
760 | */
|
---|
761 |
|
---|
762 | QSize QDial::minimumSizeHint() const
|
---|
763 | {
|
---|
764 | return QSize( 50, 50 );
|
---|
765 | }
|
---|
766 |
|
---|
767 | /*!
|
---|
768 | \reimp
|
---|
769 | */
|
---|
770 |
|
---|
771 | QSize QDial::sizeHint() const
|
---|
772 | {
|
---|
773 | return QSize( 100, 100 ).expandedTo( QApplication::globalStrut() );
|
---|
774 | }
|
---|
775 |
|
---|
776 |
|
---|
777 |
|
---|
778 | /*!
|
---|
779 | \internal
|
---|
780 | */
|
---|
781 |
|
---|
782 | QPointArray QDial::calcArrow( double &a ) const
|
---|
783 | {
|
---|
784 | int r = QMIN( width(), height() ) / 2;
|
---|
785 | if ( maxValue() == minValue() )
|
---|
786 | a = m_pi / 2;
|
---|
787 | else if ( d->wrapping )
|
---|
788 | a = m_pi * 3 / 2 - ( value() - minValue() ) * 2 * m_pi / ( maxValue() - minValue() );
|
---|
789 | else
|
---|
790 | a = ( m_pi * 8 - ( value() - minValue() ) * 10 * m_pi / ( maxValue() - minValue() ) ) / 6;
|
---|
791 |
|
---|
792 | int xc = width() / 2;
|
---|
793 | int yc = height() / 2;
|
---|
794 |
|
---|
795 | int len = r - calcBigLineSize() - 5;
|
---|
796 | if ( len < 5 )
|
---|
797 | len = 5;
|
---|
798 | int back = len / 4;
|
---|
799 | if ( back < 1 )
|
---|
800 | back = 1;
|
---|
801 |
|
---|
802 | QPointArray arrow( 3 );
|
---|
803 | arrow[0] = QPoint( (int)( 0.5 + xc + len * cos(a) ),
|
---|
804 | (int)( 0.5 + yc -len * sin( a ) ) );
|
---|
805 | arrow[1] = QPoint( (int)( 0.5 + xc + back * cos( a + m_pi * 5 / 6 ) ),
|
---|
806 | (int)( 0.5 + yc - back * sin( a + m_pi * 5 / 6 ) ) );
|
---|
807 | arrow[2] = QPoint( (int)( 0.5 + xc + back * cos( a - m_pi * 5 / 6 ) ),
|
---|
808 | (int)( 0.5 + yc - back * sin( a - m_pi * 5 / 6 ) ) );
|
---|
809 | return arrow;
|
---|
810 | }
|
---|
811 |
|
---|
812 | /*!
|
---|
813 | \internal
|
---|
814 | */
|
---|
815 |
|
---|
816 | QRect QDial::calcDial() const
|
---|
817 | {
|
---|
818 | double r = QMIN( width(), height() ) / 2.0;
|
---|
819 | double d_ = r / 6.0;
|
---|
820 | double dx = d_ + ( width() - 2 * r ) / 2.0 + 1;
|
---|
821 | double dy = d_ + ( height() - 2 * r ) / 2.0 + 1;
|
---|
822 | return QRect( int(dx), int(dy),
|
---|
823 | int(r * 2 - 2 * d_ - 2), int(r * 2 - 2 * d_ - 2) );
|
---|
824 | }
|
---|
825 |
|
---|
826 | /*!
|
---|
827 | \internal
|
---|
828 | */
|
---|
829 |
|
---|
830 | int QDial::calcBigLineSize() const
|
---|
831 | {
|
---|
832 | int r = QMIN( width(), height() ) / 2;
|
---|
833 | int bigLineSize = r / 6;
|
---|
834 | if ( bigLineSize < 4 )
|
---|
835 | bigLineSize = 4;
|
---|
836 | if ( bigLineSize > r / 2 )
|
---|
837 | bigLineSize = r / 2;
|
---|
838 | return bigLineSize;
|
---|
839 | }
|
---|
840 |
|
---|
841 | /*!
|
---|
842 | \internal
|
---|
843 | */
|
---|
844 |
|
---|
845 | void QDial::calcLines()
|
---|
846 | {
|
---|
847 | if ( !d->lines.size() ) {
|
---|
848 | double r = QMIN( width(), height() ) / 2.0;
|
---|
849 | int bigLineSize = calcBigLineSize();
|
---|
850 | double xc = width() / 2.0;
|
---|
851 | double yc = height() / 2.0;
|
---|
852 | int ns = notchSize();
|
---|
853 | int notches = ( maxValue() + ns - 1 - minValue() ) / ns;
|
---|
854 | d->lines.resize( 2 + 2 * notches );
|
---|
855 | int smallLineSize = bigLineSize / 2;
|
---|
856 | int i;
|
---|
857 | for( i = 0; i <= notches; i++ ) {
|
---|
858 | double angle = d->wrapping
|
---|
859 | ? m_pi * 3 / 2 - i * 2 * m_pi / notches
|
---|
860 | : (m_pi * 8 - i * 10 * m_pi / notches) / 6;
|
---|
861 |
|
---|
862 | double s = sin( angle ); // sin/cos aren't defined as const...
|
---|
863 | double c = cos( angle );
|
---|
864 | if ( i == 0 || ( ((ns * i ) % pageStep() ) == 0 ) ) {
|
---|
865 | d->lines[2*i] = QPoint( (int)( xc + ( r - bigLineSize ) * c ),
|
---|
866 | (int)( yc - ( r - bigLineSize ) * s ) );
|
---|
867 | d->lines[2*i+1] = QPoint( (int)( xc + r * c ),
|
---|
868 | (int)( yc - r * s ) );
|
---|
869 | } else {
|
---|
870 | d->lines[2*i] = QPoint( (int)( xc + ( r - 1 - smallLineSize ) * c ),
|
---|
871 | (int)( yc - ( r - 1 - smallLineSize ) * s ) );
|
---|
872 | d->lines[2*i+1] = QPoint( (int)( xc + ( r - 1 ) * c ),
|
---|
873 | (int)( yc -( r - 1 ) * s ) );
|
---|
874 | }
|
---|
875 | }
|
---|
876 | }
|
---|
877 | }
|
---|
878 |
|
---|
879 | /*!
|
---|
880 | \property QDial::minValue
|
---|
881 | \brief the current minimum value
|
---|
882 |
|
---|
883 | When setting this property, the \l QDial::maxValue is adjusted if
|
---|
884 | necessary to ensure that the range remains valid.
|
---|
885 |
|
---|
886 | \sa setRange()
|
---|
887 | */
|
---|
888 | int QDial::minValue() const
|
---|
889 | {
|
---|
890 | return QRangeControl::minValue();
|
---|
891 | }
|
---|
892 |
|
---|
893 | /*!
|
---|
894 | \property QDial::maxValue
|
---|
895 | \brief the current maximum value
|
---|
896 |
|
---|
897 | When setting this property, the \l QDial::minValue is adjusted if
|
---|
898 | necessary to ensure that the range remains valid.
|
---|
899 |
|
---|
900 | \sa setRange()
|
---|
901 | */
|
---|
902 | int QDial::maxValue() const
|
---|
903 | {
|
---|
904 | return QRangeControl::maxValue();
|
---|
905 | }
|
---|
906 |
|
---|
907 | void QDial::setMinValue( int minVal )
|
---|
908 | {
|
---|
909 | QRangeControl::setMinValue( minVal );
|
---|
910 | }
|
---|
911 |
|
---|
912 | void QDial::setMaxValue( int maxVal )
|
---|
913 | {
|
---|
914 | QRangeControl::setMaxValue( maxVal );
|
---|
915 | }
|
---|
916 |
|
---|
917 | /*!
|
---|
918 | \property QDial::lineStep
|
---|
919 | \brief the current line step
|
---|
920 |
|
---|
921 | setLineStep() calls the virtual stepChange() function if the new
|
---|
922 | line step is different from the previous setting.
|
---|
923 |
|
---|
924 | \sa QRangeControl::setSteps() pageStep setRange()
|
---|
925 | */
|
---|
926 |
|
---|
927 | int QDial::lineStep() const
|
---|
928 | {
|
---|
929 | return QRangeControl::lineStep();
|
---|
930 | }
|
---|
931 |
|
---|
932 | /*!
|
---|
933 | \property QDial::pageStep
|
---|
934 | \brief the current page step
|
---|
935 |
|
---|
936 | setPageStep() calls the virtual stepChange() function if the new
|
---|
937 | page step is different from the previous setting.
|
---|
938 |
|
---|
939 | \sa stepChange()
|
---|
940 | */
|
---|
941 | int QDial::pageStep() const
|
---|
942 | {
|
---|
943 | return QRangeControl::pageStep();
|
---|
944 | }
|
---|
945 |
|
---|
946 | void QDial::setLineStep( int i )
|
---|
947 | {
|
---|
948 | setSteps( i, pageStep() );
|
---|
949 | }
|
---|
950 |
|
---|
951 | void QDial::setPageStep( int i )
|
---|
952 | {
|
---|
953 | setSteps( lineStep(), i );
|
---|
954 | }
|
---|
955 |
|
---|
956 | /*!
|
---|
957 | \property QDial::value
|
---|
958 | \brief the current dial value
|
---|
959 |
|
---|
960 | This is guaranteed to be within the range
|
---|
961 | \l{QDial::minValue}..\l{QDial::maxValue}.
|
---|
962 |
|
---|
963 | \sa minValue maxValue
|
---|
964 | */
|
---|
965 |
|
---|
966 | int QDial::value() const
|
---|
967 | {
|
---|
968 | return QRangeControl::value();
|
---|
969 | }
|
---|
970 |
|
---|
971 | #endif // QT_FEATURE_DIAL
|
---|