~ubuntu-branches/ubuntu/karmic/muse/karmic-proposed

« back to all changes in this revision

Viewing changes to widgets/slider.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2002-04-23 17:28:23 UTC
  • Revision ID: james.westby@ubuntu.com-20020423172823-w8yplzr81a759xa3
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <math.h>
 
2
#include "mmath.h"
 
3
#include <qdrawutl.h>
 
4
#include <qpainter.h>
 
5
#include "slider.h"
 
6
 
 
7
//-------------------------------------------------------------
 
8
//.H Slider | 3 | 02/04/98 | Qwt Widget Library | Qwt Programmer's Manual
 
9
//.I sliders Sliders
 
10
//.U  NAME
 
11
//      Slider - The Slider Widget
 
12
//
 
13
//.U  SYNOPSIS
 
14
//      #include <qwt_slider.h>
 
15
//
 
16
//.U DESCRIPTION
 
17
//      Slider is a slider widget which operates on an interval
 
18
//      of type double. Slider supports different layouts as
 
19
//      well as a scale.
 
20
//
 
21
//
 
22
//.U INHERITED CLASSES
 
23
//      @^SliderBase@, @^QwtScaleIf@
 
24
//
 
25
//.U PUBLIC MEMBERS
 
26
//.R
 
27
//      Slider::Slider -- Constructor
 
28
//      Slider::~Slider -- Destructor
 
29
//      Slider::setThumbLength -- Set the length of the slider's thumb
 
30
//      Slider::setThumbWidth -- Set the length of the slider's thumb
 
31
//      Slider::setBorderWidth -- Set the border width
 
32
//      Slider::setMargins -- Set the internal border distance
 
33
//
 
34
//.U SEE ALSO
 
35
//      @^SliderBase@ and @^QwtScaleIf@ for the descriptions
 
36
//      of the inherited members.
 
37
//------------------------------------------------------------
 
38
 
 
39
//------------------------------------------------------------
 
40
//.C MEMBER FUNCTION DESCRIPTION
 
41
//------------------------------------------------------------
 
42
 
 
43
 
 
44
//------------------------------------------------------------
 
45
//
 
46
//.F    Slider::Slider
 
47
//
 
48
//              Constructor
 
49
//
 
50
//.u    Syntax:
 
51
//.f    Slider::Slider(QWidget *parent, const char *name, OrientationX orient = Horizontal, ScalePos scalePos = None, int bgStyle = BgTrough)
 
52
//
 
53
//.u    Parameters
 
54
//.p
 
55
//      QWidget *parent --      parent widget
 
56
//      const char *name --     The Widget's name. Default = 0.
 
57
//      Orientation Orient --   Orientation of the slider. Can be Slider::Horizontal
 
58
//                              or Slider::Vertical.
 
59
//                              Defaults to Horizontal.
 
60
//      ScalePos scalePos --    Position of the scale.  Can be Slider::None,
 
61
//                              Slider::Left, Slider::Right, Slider::Top,
 
62
//                              or Slider::Bottom. Defaults to Slider::None.
 
63
//      int bgStyle --  Background style. Slider::BgTrough draws the
 
64
//                              slider button in a trough, Slider::BgSlot draws
 
65
//                              a slot underneath the button. An or-combination of both
 
66
//                              may also be used. The default is Slider::BgTrough.
 
67
//
 
68
//
 
69
//------------------------------------------------------------
 
70
Slider::Slider(QWidget *parent, const char *name,
 
71
   OrientationX orient, ScalePos scalePos, int bgStyle)
 
72
      : SliderBase(parent,name)
 
73
      {
 
74
      ScaleDraw::OrientationX so;
 
75
 
 
76
 
 
77
      if (bgStyle == BgSlot) {
 
78
            d_thumbLength = 16;
 
79
            d_thumbHalf = 8;
 
80
            d_thumbWidth = 30;
 
81
            }
 
82
      else {
 
83
            d_thumbLength = 30;
 
84
            d_thumbHalf = 15;
 
85
            d_thumbWidth = 16;
 
86
            }
 
87
 
 
88
      d_borderWidth = 2;
 
89
      d_scaleDist = 4;
 
90
      d_orient = orient;
 
91
      d_scaleStep = 0.0;
 
92
      d_scalePos = scalePos;
 
93
      d_xMargin = 0;
 
94
      d_yMargin = 0;
 
95
      d_bgStyle = bgStyle;
 
96
 
 
97
      if (bgStyle & BgTrough)
 
98
            d_bwTrough = d_borderWidth;
 
99
      else
 
100
            d_bwTrough = 0;
 
101
 
 
102
    d_sliderRect.setRect(0,0,8,8);
 
103
 
 
104
    switch(d_orient)
 
105
    {
 
106
    case Vertical:
 
107
        if (d_scalePos == Right)
 
108
           so = ScaleDraw::Right;
 
109
        else
 
110
           so = ScaleDraw::Left;
 
111
        break;
 
112
    default:
 
113
        if (d_scalePos == Bottom)
 
114
           so = ScaleDraw::Bottom;
 
115
        else
 
116
           so = ScaleDraw::Top;
 
117
        break;
 
118
    }
 
119
 
 
120
      d_scale.setGeometry(0, 0, 40, so);
 
121
        
 
122
      if (d_orient == Vertical)
 
123
            setMinimumSize(10,20);
 
124
      else
 
125
            setMinimumSize(20,10);
 
126
      }
 
127
 
 
128
 
 
129
//------------------------------------------------------------
 
130
//.F    Slider::~Slider
 
131
//              Destructor
 
132
//.u    Syntax
 
133
//.f    Slider::~Slider()
 
134
//------------------------------------------------------------
 
135
 
 
136
Slider::~Slider()
 
137
      {
 
138
      }
 
139
 
 
140
//------------------------------------------------------------
 
141
//
 
142
//.F    Slider::setBorderWidth
 
143
//      Change the slider's border width
 
144
//
 
145
//.u    Syntax
 
146
//.f    void Slider::setBorderWidth(int bd)
 
147
//
 
148
//.u    Parameters
 
149
//.p    int bd -- border width
 
150
//
 
151
//------------------------------------------------------------
 
152
void Slider::setBorderWidth(int bd)
 
153
{
 
154
    d_borderWidth = qwtMin(qwtMax(bd,0),10);
 
155
    if (d_bgStyle & BgTrough)
 
156
       d_bwTrough = d_borderWidth;
 
157
    else
 
158
       d_bwTrough = 0;
 
159
}
 
160
 
 
161
//----------------------------------------------------
 
162
//
 
163
//.F    Slider::setThumbLength
 
164
//
 
165
//      Set the slider's thumb length
 
166
//
 
167
//.u    Syntax
 
168
//      void Slider::setThumbLength(int l)
 
169
//
 
170
//.u    Parameters
 
171
//.p    int l   --              new length
 
172
//
 
173
//-----------------------------------------------------
 
174
void Slider::setThumbLength(int l)
 
175
{
 
176
    d_thumbLength = qwtMax(l,8);
 
177
    d_thumbHalf = d_thumbLength / 2;
 
178
    resize(size());
 
179
}
 
180
 
 
181
//------------------------------------------------------------
 
182
//
 
183
//.F    Slider::setThumbWidth
 
184
//      Change the width of the thumb
 
185
//
 
186
//.u    Syntax
 
187
//.p    void Slider::setThumbWidth(int w)
 
188
//
 
189
//.u    Parameters
 
190
//.p    int w -- new width
 
191
//
 
192
//------------------------------------------------------------
 
193
void Slider::setThumbWidth(int w)
 
194
{
 
195
    d_thumbWidth = qwtMax(w,4);
 
196
    resize(size());
 
197
}
 
198
 
 
199
 
 
200
//------------------------------------------------------------
 
201
//.-    
 
202
//.F    Slider::scaleChange
 
203
//      Notify changed scale
 
204
//
 
205
//.u    Syntax
 
206
//.f    void Slider::scaleChange()
 
207
//
 
208
//.u    Description
 
209
//      Called by QwtScaledWidget
 
210
//
 
211
//------------------------------------------------------------
 
212
void Slider::scaleChange()
 
213
{
 
214
    if (!hasUserScale())
 
215
       d_scale.setScale(minValue(), maxValue(), d_maxMajor, d_maxMinor);
 
216
    update();
 
217
}
 
218
 
 
219
 
 
220
//------------------------------------------------------------
 
221
//.-
 
222
//.F    Slider::fontChange
 
223
//      Notify change in font
 
224
//      
 
225
//.u    Syntax
 
226
//.f     Slider::fontChange(const QFont &oldFont)
 
227
//
 
228
//------------------------------------------------------------
 
229
void Slider::fontChange(const QFont & /*oldFont*/)
 
230
{
 
231
    repaint(FALSE);
 
232
}
 
233
 
 
234
//------------------------------------------------------------
 
235
//.-
 
236
//.F    Slider::drawSlider
 
237
//              Draw the slider into the specified rectangle.   
 
238
//
 
239
//.u    Syntax:
 
240
//.f    void Slider::drawSlider(QPaiter *p, const QRect &r)
 
241
//
 
242
//.u    Parameters:
 
243
//.p    QPainter *p, const QRect &r
 
244
//
 
245
//------------------------------------------------------------
 
246
void Slider::drawSlider (QPainter *p, const QRect &r)
 
247
{
 
248
 
 
249
    QColorGroup g = colorGroup();
 
250
    QBrush brBack(g.background());
 
251
    QBrush brMid;
 
252
    QBrush brDark(g.dark());
 
253
 
 
254
    QRect cr;
 
255
 
 
256
    int ipos,dist1;
 
257
    double rpos;
 
258
    int lineDist;
 
259
 
 
260
    if (d_bwTrough > 0)
 
261
    {
 
262
        qDrawShadePanel(p, r.x(), r.y(),
 
263
                        r.width(), r.height(),
 
264
                        g, TRUE, d_bwTrough,0);
 
265
        cr.setRect(r.x() + d_bwTrough,
 
266
                   r.y() + d_bwTrough,
 
267
                   r.width() - 2*d_bwTrough,
 
268
                   r.height() - 2*d_bwTrough);
 
269
        brMid = QBrush(g.mid());
 
270
 
 
271
    }
 
272
    else
 
273
    {
 
274
        cr = r;
 
275
        brMid = brBack;
 
276
    }
 
277
 
 
278
    rpos = (value()  - minValue()) / (maxValue() - minValue());
 
279
 
 
280
    lineDist = d_borderWidth - 1;
 
281
    if (lineDist < 1) lineDist = 1;
 
282
 
 
283
    if (d_orient == Horizontal)
 
284
    {
 
285
        
 
286
        dist1 = int(double(cr.width() - d_thumbLength) * rpos);
 
287
        ipos =  cr.x() + dist1;
 
288
        markerPos = ipos + d_thumbHalf;
 
289
 
 
290
        //
 
291
        // draw background
 
292
        //
 
293
        if (d_bgStyle & BgSlot)
 
294
        {
 
295
            drawHsBgSlot(p, cr, QRect(ipos, cr.y(), d_thumbLength, cr.height()), brMid);
 
296
        }
 
297
        else
 
298
        {
 
299
            p->fillRect(cr.x(),cr.y(),dist1,cr.height(),brMid);
 
300
            p->fillRect(ipos + d_thumbLength, cr.y(),
 
301
                 cr.width() - d_thumbLength - dist1, cr.height(),brMid);
 
302
        }
 
303
        
 
304
        //
 
305
        //      Draw thumb
 
306
        //
 
307
        qDrawShadePanel(p,ipos, cr.y(), d_thumbLength, cr.height(),
 
308
                        colorGroup(), FALSE, d_borderWidth, &brBack);
 
309
        
 
310
        if (lineDist > 1)
 
311
           qDrawShadeLine(p,markerPos, cr.y() + lineDist , markerPos,
 
312
                          cr.y() + cr.height() - lineDist,
 
313
                          colorGroup(), TRUE, 1);
 
314
        else
 
315
        {
 
316
            p->setPen(g.dark());
 
317
            p->drawLine(markerPos -1 , cr.y() + lineDist, markerPos -1,
 
318
                        cr.y() + cr.height() - lineDist - 1);
 
319
            p->setPen(g.light());
 
320
            p->drawLine(markerPos, cr.y() + lineDist, markerPos,
 
321
                        cr.y() + cr.height() - lineDist - 1);
 
322
        }
 
323
        
 
324
                        
 
325
    }
 
326
    else
 
327
    {
 
328
        dist1 = int(double(cr.height() - d_thumbLength) * (1.0 - rpos));
 
329
        ipos = cr.y() + dist1;
 
330
        markerPos = ipos + d_thumbHalf;
 
331
 
 
332
        if ( d_bgStyle & BgSlot)
 
333
        {
 
334
            drawVsBgSlot(p, cr, QRect(cr.left(), ipos, cr.width(),
 
335
                                      d_thumbLength), brMid);
 
336
        }
 
337
        else
 
338
        {
 
339
            p->fillRect(cr.x(),cr.y(),cr.width(),ipos,brMid);
 
340
            p->fillRect(cr.x(), ipos + d_thumbLength, cr.width(),
 
341
                        cr.height() - d_thumbLength - dist1, brMid);
 
342
        }
 
343
        
 
344
        qDrawShadePanel(p,cr.x(),ipos , cr.width(), d_thumbLength,
 
345
                        colorGroup(),FALSE,d_borderWidth, &brBack);
 
346
        if (lineDist > 1)
 
347
           qDrawShadeLine(p, cr.x() + lineDist , markerPos,
 
348
                          cr.x() + cr.width() - lineDist, markerPos,
 
349
                          colorGroup(), TRUE, 1);
 
350
        else {
 
351
                
 
352
                p->setPen(g.dark());
 
353
                p->drawLine(cr.x() + lineDist, markerPos - 1 ,
 
354
                            cr.x() + cr.width() -  lineDist - 1, markerPos - 1);
 
355
                p->setPen(g.light());
 
356
                p->drawLine(cr.x() + lineDist, markerPos,
 
357
                            cr.x() + cr.width() -  lineDist - 1 , markerPos);
 
358
            }
 
359
    }
 
360
 
 
361
}
 
362
 
 
363
//------------------------------------------------------------
 
364
//.-
 
365
//.F    Slider::drawSlotBg
 
366
//
 
367
//
 
368
//.u    Syntax
 
369
//.f    void Slider::drawSlotBg(QPainter *p, const QRect &rBound, const QRect &rThumb, const QRect &rSlot, const QBrush &brBack)
 
370
//
 
371
//.u    Parameters
 
372
//.p    QPainter *p, const QRect &rBound, const QRect &rThumb, const QRect &rSlot, const QBrush &brBack
 
373
//
 
374
//------------------------------------------------------------
 
375
void Slider::drawHsBgSlot(QPainter *p, const QRect &rBound, const QRect &rThumb, const QBrush &brBack)
 
376
{
 
377
    int ws, ds, dLeft;
 
378
    int lPos, rPos;
 
379
    QRect rSlot;
 
380
    const QColorGroup &g = colorGroup();
 
381
 
 
382
    ws = rBound.height();
 
383
    if ((ws / 2) * 2 != ws)
 
384
       ws = 5;
 
385
    else
 
386
       ws = 4;
 
387
 
 
388
    ds = qwtMax(1, d_thumbLength/2 - 4);
 
389
    dLeft = rThumb.left() - rBound.left();
 
390
 
 
391
    rSlot = QRect(rBound.x() + ds, rBound.y() + (rBound.height() - ws) / 2,
 
392
                  rBound.width() - 2 * ds, ws);
 
393
 
 
394
    rPos = qwtMin(rSlot.x(), rThumb.left());
 
395
 
 
396
    if (rThumb.left() > rBound.x())
 
397
    {
 
398
        p->fillRect(rBound.x(),rBound.y(),dLeft, rSlot.top() - rBound.top(), brBack);
 
399
        p->fillRect(rBound.x(),rSlot.bottom() + 1,dLeft,
 
400
                    rBound.bottom() - rSlot.bottom(),brBack);
 
401
        if (rPos > rBound.left())
 
402
           p->fillRect(rBound.x(),rSlot.y(),
 
403
                       rPos - rBound.left(),ws,brBack);
 
404
 
 
405
        p->setPen(g.dark());
 
406
        if (rSlot.x() < rThumb.left())
 
407
           p->drawLine(rSlot.x(), rSlot.bottom(), rSlot.x(), rSlot.top());
 
408
        if (rSlot.x() < rThumb.left() - 1)
 
409
        {
 
410
            p->drawLine(rSlot.x(), rSlot.top(), rThumb.left() - 1, rSlot.top());
 
411
            p->setPen(g.light());
 
412
            p->drawLine(rSlot.x() + 1, rSlot.bottom(),
 
413
                        rThumb.left() - 1, rSlot.bottom());
 
414
        
 
415
            p->fillRect(rSlot.x() + 1, rSlot.y() + 1, dLeft - ds -1,
 
416
                        rSlot.height() -2, QBrush(black));
 
417
        
 
418
        }
 
419
    }
 
420
 
 
421
    lPos = qwtMax(rSlot.right(), rThumb.right()) + 1;
 
422
    if (rThumb.right() < rBound.right())
 
423
    {
 
424
        p->fillRect(rThumb.right() + 1,rBound.y(),rBound.right() - rThumb.right(),
 
425
                    rSlot.top() - rBound.top(), brBack);
 
426
        p->fillRect(rThumb.right() + 1,rSlot.bottom() + 1,
 
427
                    rBound.right() - rThumb.right(),
 
428
                    rBound.bottom() - rSlot.bottom(),brBack);
 
429
        if (lPos <= rBound.right())
 
430
           p->fillRect(lPos, rSlot.y() , rBound.right() - lPos + 1, ws ,brBack);
 
431
 
 
432
        p->setPen(g.dark());
 
433
        if (rSlot.right() > rThumb.right())
 
434
        {
 
435
            p->drawLine(rThumb.right() + 1, rSlot.top(), rSlot.right(), rSlot.top());
 
436
            p->setPen(g.light());
 
437
            p->drawLine(rSlot.right(), rSlot.bottom(), rSlot.right(), rSlot.top() + 1);
 
438
        }
 
439
 
 
440
        if (rSlot.right() > rThumb.right() + 1)
 
441
        {
 
442
            p->setPen(g.light());
 
443
            p->drawLine(rThumb.right() + 1, rSlot.bottom(),
 
444
                        rSlot.right() -1, rSlot.bottom());
 
445
            p->fillRect(rThumb.right() + 1, rSlot.y() + 1,
 
446
                        rSlot.right() - rThumb.right() - 1,
 
447
                        rSlot.height() -2, QBrush(black));
 
448
        }
 
449
    }
 
450
 
 
451
}
 
452
 
 
453
//------------------------------------------------------------
 
454
//.-
 
455
//.F    Slider::drawVsBgSlot
 
456
//
 
457
//
 
458
//.u    Syntax
 
459
//.f    void Slider::drawVsBgSlot(QPainter *p, const QRect &rBound, const QRect &rThumb, const QBrush &brBack)
 
460
//
 
461
//.u    Parameters
 
462
//.p    QPainter *p, const QRect &rBound, const QRect &rThumb, const QBrush &brBack
 
463
//
 
464
//.u    Return Value
 
465
//
 
466
//.u    Description
 
467
//
 
468
//------------------------------------------------------------
 
469
void Slider::drawVsBgSlot(QPainter *p, const QRect &rBound, const QRect &rThumb, const QBrush &brBack)
 
470
{
 
471
 
 
472
    int ws, ds, dTop;
 
473
    int lPos, hPos;
 
474
    QRect rSlot;
 
475
    const QColorGroup &g = colorGroup();
 
476
 
 
477
    ws = rBound.width();
 
478
    if ((ws / 2) * 2 != ws)
 
479
       ws = 5;
 
480
    else
 
481
       ws = 4;
 
482
 
 
483
    ds = qwtMax(1, d_thumbLength/2 - 4);
 
484
    dTop = rThumb.top() - rBound.top();
 
485
 
 
486
    rSlot = QRect(rBound.x() + (rBound.width() - ws) / 2, rBound.y() + ds,
 
487
                  ws, rBound.height() - 2 * ds);
 
488
 
 
489
    hPos = qwtMin(rSlot.y(), rThumb.top());
 
490
 
 
491
    if (rThumb.top() > rBound.top())
 
492
    {
 
493
        p->fillRect(rBound.x(),rBound.y(), rSlot.left() - rBound.left(),dTop, brBack);
 
494
        p->fillRect(rSlot.right() + 1, rBound.y(),
 
495
                    rBound.right() - rSlot.right(), dTop,brBack);
 
496
        if (hPos > rBound.top())
 
497
           p->fillRect(rSlot.x(),rBound.y(), ws,
 
498
                       hPos - rBound.top(),brBack);
 
499
 
 
500
        p->setPen(g.dark());
 
501
        if (rSlot.top() < rThumb.top())
 
502
           p->drawLine(rSlot.left(), rSlot.top(), rSlot.right(), rSlot.top());
 
503
 
 
504
        
 
505
        if (rSlot.top() < rThumb.top() - 1)
 
506
        {
 
507
            p->drawLine(rSlot.left(), rThumb.top() - 1, rSlot.left(), rSlot.top());
 
508
            p->setPen(g.light());
 
509
            p->drawLine(rSlot.right(), rSlot.top() + 1, rSlot.right(),
 
510
                        rThumb.top() - 1);
 
511
        
 
512
            p->fillRect(rSlot.x() + 1, rSlot.y() + 1, rSlot.width() - 2,
 
513
                        dTop - ds -1, QBrush(black));
 
514
        
 
515
        }
 
516
    }
 
517
 
 
518
    lPos = qwtMax(rSlot.bottom(), rThumb.bottom()) + 1;
 
519
    if (rThumb.bottom() < rBound.bottom())
 
520
    {
 
521
        p->fillRect(rBound.left(), rThumb.bottom() + 1,
 
522
                    rSlot.left() - rBound.left(),
 
523
                    rBound.bottom() - rThumb.bottom(), brBack);
 
524
        p->fillRect(rSlot.right() + 1, rThumb.bottom() + 1,
 
525
                    rBound.right() - rSlot.right(),
 
526
                    rBound.bottom() - rThumb.bottom(), brBack);
 
527
        if (lPos <= rBound.bottom())
 
528
           p->fillRect(rSlot.left(), lPos, ws, rBound.bottom() - lPos + 1, brBack);
 
529
 
 
530
        p->setPen(g.dark());
 
531
        if (rSlot.bottom() > rThumb.bottom())
 
532
        {
 
533
            p->drawLine(rSlot.left(), rThumb.bottom() + 1, rSlot.left(), rSlot.bottom());
 
534
            p->setPen(g.light());
 
535
            p->drawLine(rSlot.left() * 1, rSlot.bottom(), rSlot.right(), rSlot.bottom());
 
536
        }
 
537
 
 
538
        if (rSlot.bottom() > rThumb.bottom() + 1)
 
539
        {
 
540
            p->setPen(g.light());
 
541
            p->drawLine(rSlot.right(), rThumb.bottom() + 1, rSlot.right(),
 
542
                        rSlot.bottom());
 
543
            p->fillRect(rSlot.left() + 1, rThumb.bottom() + 1,
 
544
                        rSlot.width() - 2, rSlot.bottom() - rThumb.bottom() - 1,
 
545
                        QBrush(black));
 
546
        }
 
547
    }
 
548
 
 
549
}
 
550
 
 
551
//------------------------------------------------------------
 
552
//.-
 
553
//.F    Slider::getValue
 
554
//      Determine the value corresponding to a specified
 
555
//      mouse location.
 
556
//
 
557
//.u    Syntax
 
558
//.f     double Slider::getValue(const QPoint &p)
 
559
//
 
560
//.u    Parameters
 
561
//.p    const QPoint &p --
 
562
//
 
563
//.u    Description
 
564
//      Called by SliderBase
 
565
//------------------------------------------------------------
 
566
double Slider::getValue( const QPoint &p)
 
567
{
 
568
    double rv;
 
569
    int pos;
 
570
    QRect r = d_sliderRect;
 
571
 
 
572
    r.setLeft(r.left() + d_bwTrough);
 
573
    r.setRight(r.right() - d_bwTrough);
 
574
    r.setTop(r.top() - d_bwTrough);
 
575
    r.setBottom(r.bottom() - d_bwTrough);
 
576
 
 
577
    if (d_orient == Horizontal)
 
578
    {
 
579
        
 
580
        if (r.width() <= d_thumbLength)
 
581
        {
 
582
            rv = 0.5 * (minValue() + maxValue());
 
583
        }
 
584
        else
 
585
        {
 
586
            pos = p.x() - r.x() - d_thumbHalf;
 
587
            rv  =  minValue() +
 
588
               rint( (maxValue() - minValue()) * double(pos)
 
589
                    / double(r.width() - d_thumbLength)
 
590
                    / step() ) * step();
 
591
        }
 
592
        
 
593
    }
 
594
    else
 
595
    {
 
596
        if (r.height() <= d_thumbLength)
 
597
        {
 
598
            rv = 0.5 * (minValue() + maxValue());
 
599
        }
 
600
        else
 
601
        {
 
602
            pos = p.y() - r.y() - d_thumbHalf;
 
603
            rv =  minValue() +
 
604
               rint( (maxValue() - minValue()) *
 
605
                    (1.0 - double(pos)
 
606
                     / double(r.height() - d_thumbLength))
 
607
                    / step() ) * step();
 
608
        }
 
609
        
 
610
    }
 
611
 
 
612
    return(rv);
 
613
}
 
614
 
 
615
 
 
616
//------------------------------------------------------------
 
617
//.-
 
618
//.F    Slider::getScrollMode
 
619
//      Determine scrolling mode and direction
 
620
//
 
621
//.u    Syntax
 
622
//.f     void Slider::getScrollMode( const QPoint &p, int &scrollMode, int &direction )
 
623
//
 
624
//.u    Parameters
 
625
//.p    const QPoint &p -- point
 
626
//
 
627
//.u    Description
 
628
//      Called by SliderBase
 
629
//
 
630
//------------------------------------------------------------
 
631
void Slider::getScrollMode( const QPoint &p, int &scrollMode, int &direction )
 
632
{
 
633
    int currentPos;
 
634
 
 
635
    if (d_orient == Horizontal)
 
636
       currentPos = p.x();
 
637
    else
 
638
       currentPos = p.y();
 
639
 
 
640
    if (d_sliderRect.contains(p))
 
641
    {
 
642
        if ((currentPos > markerPos - d_thumbHalf)      
 
643
            && (currentPos < markerPos + d_thumbHalf))
 
644
        {
 
645
            scrollMode = ScrMouse;
 
646
            direction = 0;
 
647
        }
 
648
        else
 
649
        {
 
650
            scrollMode = ScrPage;
 
651
            if (((currentPos > markerPos) && (d_orient == Horizontal))
 
652
                || ((currentPos <= markerPos) && (d_orient != Horizontal)))
 
653
               direction = 1;
 
654
            else
 
655
               direction = -1;
 
656
        }
 
657
    }
 
658
    else
 
659
    {
 
660
        scrollMode = ScrNone;
 
661
        direction = 0;
 
662
    }
 
663
 
 
664
}
 
665
 
 
666
//------------------------------------------------------------
 
667
//.-
 
668
//.F    Slider::paintEvent
 
669
//      Qt paint event
 
670
//
 
671
//.u    Parameters
 
672
//.p    QPaintEvent *e
 
673
//
 
674
//.u    Syntax
 
675
//.f    void Slider::paintEvent(QPaintEvent *e)
 
676
//
 
677
//------------------------------------------------------------
 
678
void Slider::paintEvent(QPaintEvent * /*e*/)
 
679
{
 
680
    QPainter p;
 
681
 
 
682
    if(p.begin(this))
 
683
    {
 
684
        if (d_scalePos != None)
 
685
        {
 
686
            p.fillRect(this->rect(), this->backgroundColor());
 
687
            d_scale.draw(&p);
 
688
        }
 
689
        
 
690
        drawSlider(&p, d_sliderRect);
 
691
    }
 
692
    p.end();
 
693
 
 
694
}
 
695
 
 
696
//------------------------------------------------------------
 
697
//.-
 
698
//.F    Slider::resizeEvent
 
699
//      Qt resize event
 
700
//
 
701
//.u    Parameters
 
702
//.p    QResizeEvent *e
 
703
//
 
704
//.u    Syntax
 
705
//.f    void Slider::resizeEvent(QResizeEvent *e)
 
706
//
 
707
//------------------------------------------------------------
 
708
void Slider::resizeEvent(QResizeEvent *e)
 
709
{
 
710
 
 
711
    d_resized = TRUE;
 
712
    QSize s = e->size();
 
713
    int sliderWidth = d_thumbWidth + 2 * d_bwTrough;
 
714
 
 
715
    // reposition slider
 
716
    if(d_orient == Horizontal)
 
717
    {
 
718
        switch(d_scalePos)
 
719
        {
 
720
        case Top:
 
721
        
 
722
            d_sliderRect.setRect(this->rect().x() + d_xMargin,
 
723
                                 this->rect().y() + s.height() - 1
 
724
                                 - d_yMargin - sliderWidth,
 
725
                                 s.width() - 2 * d_xMargin,
 
726
                                 sliderWidth);
 
727
            d_scale.setGeometry(d_sliderRect.x() + d_bwTrough + d_thumbHalf,
 
728
                                d_sliderRect.y() - d_scaleDist,
 
729
                                d_sliderRect.width() - d_thumbLength - 2*d_bwTrough,
 
730
                                ScaleDraw::Top);
 
731
        
 
732
            break;
 
733
        
 
734
        case Bottom:
 
735
        
 
736
            d_sliderRect.setRect(this->rect().x() + d_xMargin,
 
737
                                 this->rect().y() + d_yMargin,
 
738
                                 s.width() - 2*d_xMargin,
 
739
                                 sliderWidth);
 
740
            d_scale.setGeometry(d_sliderRect.x() + d_bwTrough + d_thumbHalf,
 
741
                                d_sliderRect.y() + d_sliderRect.height() +  d_scaleDist,
 
742
                                d_sliderRect.width() - d_thumbLength - 2*d_bwTrough,
 
743
                                ScaleDraw::Bottom);
 
744
        
 
745
            break;
 
746
        
 
747
        default:
 
748
            d_sliderRect.setRect(this->rect().x(), this->rect().x(),
 
749
                                 s.width(), s.height());
 
750
            break;
 
751
        }
 
752
    }
 
753
    else
 
754
    {
 
755
        switch(d_scalePos)
 
756
        {
 
757
        case Left:
 
758
            d_sliderRect.setRect(this->rect().x() + s.width()
 
759
                                 - sliderWidth - 1 - d_xMargin,
 
760
                                 this->rect().y() + d_yMargin,
 
761
                                 sliderWidth,
 
762
                                 s.height() - 2 * d_yMargin);
 
763
            d_scale.setGeometry(d_sliderRect.x() - d_scaleDist,
 
764
                                d_sliderRect.y() + d_thumbHalf + d_bwTrough,
 
765
                                s.height() - d_thumbLength - 2*d_bwTrough,
 
766
                                ScaleDraw::Left);
 
767
        
 
768
            break;
 
769
        case Right:
 
770
            d_sliderRect.setRect(this->rect().x() + d_xMargin,
 
771
                                 this->rect().y() + d_yMargin,
 
772
                                 sliderWidth,
 
773
                                 s.height() - 2* d_yMargin);
 
774
            d_scale.setGeometry(this->rect().x() + d_sliderRect.width()
 
775
                                + d_scaleDist,
 
776
                                d_sliderRect.y() + d_thumbHalf + d_bwTrough,
 
777
                                s.height() - d_thumbLength - 2*d_bwTrough,
 
778
                                ScaleDraw::Right);
 
779
            break;
 
780
        default:
 
781
            d_sliderRect.setRect(this->rect().x(), this->rect().x(),
 
782
                                 s.width(), s.height());
 
783
            break;
 
784
        }
 
785
    }
 
786
 
 
787
}
 
788
 
 
789
//------------------------------------------------------------
 
790
//.-
 
791
//.F    Slider::valueChange
 
792
//      Notify change of value
 
793
//
 
794
//.u    Syntax
 
795
//.f    void Slider::valueChange()
 
796
//
 
797
//------------------------------------------------------------
 
798
void Slider::valueChange()
 
799
{
 
800
    QPainter p;
 
801
 
 
802
    p.begin(this);
 
803
    drawSlider(&p,d_sliderRect);
 
804
    p.end();
 
805
 
 
806
    SliderBase::valueChange();
 
807
 
 
808
}
 
809
 
 
810
 
 
811
//------------------------------------------------------------
 
812
//.-    
 
813
//.F    Slider::rangeChange
 
814
//      Notify change of range
 
815
//
 
816
//.u    Description
 
817
//
 
818
//.u    Syntax
 
819
//.f    void Slider::rangeChange()
 
820
//
 
821
//------------------------------------------------------------
 
822
void Slider::rangeChange()
 
823
{
 
824
    if (!hasUserScale())
 
825
       d_scale.setScale(minValue(), maxValue(), d_maxMajor, d_maxMinor);
 
826
    SliderBase::rangeChange();
 
827
    repaint(FALSE);
 
828
}
 
829
 
 
830
//------------------------------------------------------------
 
831
//
 
832
//.F    Slider::setMargins
 
833
//      Set distances between the widget's border and
 
834
//      internals.
 
835
//
 
836
//.u    Syntax
 
837
//.f    void Slider::setMargins(int hor, int vert)
 
838
//
 
839
//.u    Parameters
 
840
//.p    int hor, int vert -- Margins
 
841
//
 
842
//------------------------------------------------------------
 
843
void Slider::setMargins(int hor, int vert)
 
844
{
 
845
    d_xMargin = qwtMax(0, hor);
 
846
    d_yMargin = qwtMin(0, vert);
 
847
    resize(this->size());
 
848
}
 
849
 
 
850
//------------------------------------------------------------
 
851
//
 
852
//.F    Slider::sizeHint
 
853
//      Return a recommended size
 
854
//
 
855
//.u    Syntax
 
856
//.f    QSize Slider::sizeHint() const
 
857
//
 
858
//.u    Note
 
859
//      The return value of sizeHint() depends on the font and the
 
860
//      scale.
 
861
//------------------------------------------------------------
 
862
 
 
863
QSize Slider::sizeHint() const
 
864
      {
 
865
      QPainter p;
 
866
      int msWidth = 0, msHeight = 0;
 
867
 
 
868
      int w = 40;
 
869
      int h = 40;
 
870
      if (d_scalePos != None) {
 
871
            if (p.begin(this)) {
 
872
                  msWidth = d_scale.maxWidth(&p, FALSE);
 
873
                  msHeight = d_scale.maxHeight(&p);
 
874
                  }
 
875
            p.end();
 
876
 
 
877
            switch(d_orient) {
 
878
                  case Vertical:
 
879
                        w = 2*d_xMargin + d_thumbWidth + 2*d_bwTrough + msWidth + d_scaleDist + 2;
 
880
                        break;
 
881
                  default:
 
882
                        h = 2*d_yMargin + d_thumbWidth + 2*d_bwTrough + msHeight + d_scaleDist;
 
883
                        break;
 
884
                  }
 
885
            }
 
886
      else {      // no scale
 
887
            switch(d_orient) {
 
888
                  case Vertical:
 
889
                        w = 16 + 2 * d_bwTrough;
 
890
                        break;
 
891
                  default:
 
892
                        h = 16 + 2 * d_bwTrough;
 
893
                        break;
 
894
                  }
 
895
            }
 
896
      return QSize(w, h);
 
897
      }