~showard314/ubuntu/natty/qtiplot/Python2.7_fix

« back to all changes in this revision

Viewing changes to 3rdparty/qwt/src/qwt_scale_widget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2008-04-04 15:11:55 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080404151155-rjp12ziov4tryj0o
Tags: 0.9.4-1
* New upstream release.
* Refresh patches.
* Remove 04_homepage_url patch. Merged upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 
2
 * Qwt Widget Library
 
3
 * Copyright (C) 1997   Josef Wilgen
 
4
 * Copyright (C) 2002   Uwe Rathmann
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the Qwt License, Version 1.0
 
8
 *****************************************************************************/
 
9
 
 
10
// vim: expandtab
 
11
 
 
12
#include <qpainter.h>
 
13
#include <qevent.h>
 
14
#include "qwt_painter.h"
 
15
#include "qwt_color_map.h"
 
16
#include "qwt_scale_widget.h"
 
17
#include "qwt_scale_map.h"
 
18
#include "qwt_math.h"
 
19
#include "qwt_paint_buffer.h"
 
20
#include "qwt_scale_div.h"
 
21
#include "qwt_text.h"
 
22
 
 
23
class QwtScaleWidget::PrivateData
 
24
{
 
25
public:
 
26
    PrivateData():
 
27
        scaleDraw(NULL)
 
28
    {
 
29
        colorBar.colorMap = NULL;
 
30
    }
 
31
 
 
32
    ~PrivateData()
 
33
    {
 
34
        delete scaleDraw;
 
35
        delete colorBar.colorMap;
 
36
    }
 
37
 
 
38
    QwtScaleDraw *scaleDraw;
 
39
 
 
40
    int borderDist[2];
 
41
    int minBorderDist[2];
 
42
    int scaleLength;
 
43
    int margin;
 
44
    int penWidth;
 
45
 
 
46
    int titleOffset;
 
47
    int spacing;
 
48
    QwtText title;
 
49
 
 
50
    struct t_colorBar
 
51
    {
 
52
        bool isEnabled;
 
53
        int width;
 
54
        QwtDoubleInterval interval;
 
55
        QwtColorMap *colorMap;
 
56
    } colorBar;
 
57
};
 
58
 
 
59
/*!
 
60
  \brief Create a scale with the position QwtScaleWidget::Left
 
61
  \param parent Parent widget
 
62
*/
 
63
QwtScaleWidget::QwtScaleWidget(QWidget *parent):
 
64
    QWidget(parent)
 
65
{
 
66
    initScale(QwtScaleDraw::LeftScale);
 
67
}
 
68
 
 
69
#if QT_VERSION < 0x040000
 
70
/*!
 
71
  \brief Create a scale with the position QwtScaleWidget::Left
 
72
  \param parent Parent widget
 
73
  \param name Object name
 
74
*/
 
75
QwtScaleWidget::QwtScaleWidget(QWidget *parent, const char *name):
 
76
    QWidget(parent, name)
 
77
{
 
78
    initScale(QwtScaleDraw::LeftScale);
 
79
}
 
80
#endif
 
81
 
 
82
/*!
 
83
  \brief Constructor
 
84
  \param align Alignment. 
 
85
  \param parent Parent widget
 
86
*/
 
87
QwtScaleWidget::QwtScaleWidget(
 
88
        QwtScaleDraw::Alignment align, QWidget *parent):
 
89
    QWidget(parent)
 
90
{
 
91
    initScale(align);
 
92
}
 
93
 
 
94
//! Destructor
 
95
QwtScaleWidget::~QwtScaleWidget()
 
96
{
 
97
    delete d_data;
 
98
}
 
99
 
 
100
//! Initialize the scale
 
101
void QwtScaleWidget::initScale(QwtScaleDraw::Alignment align)
 
102
{
 
103
    d_data = new PrivateData;
 
104
 
 
105
#if QT_VERSION < 0x040000
 
106
    setWFlags(Qt::WNoAutoErase);
 
107
#endif 
 
108
 
 
109
    d_data->borderDist[0] = 0;
 
110
    d_data->borderDist[1] = 0;
 
111
    d_data->minBorderDist[0] = 0;
 
112
    d_data->minBorderDist[1] = 0;
 
113
    d_data->margin = 4;
 
114
    d_data->penWidth = 0;
 
115
    d_data->titleOffset = 0;
 
116
    d_data->spacing = 2;
 
117
 
 
118
    d_data->scaleDraw = new QwtScaleDraw;
 
119
    d_data->scaleDraw->setAlignment(align);
 
120
    d_data->scaleDraw->setLength(10);
 
121
 
 
122
    d_data->colorBar.colorMap = new QwtLinearColorMap();
 
123
    d_data->colorBar.isEnabled = false;
 
124
    d_data->colorBar.width = 10;
 
125
    
 
126
    const int flags = Qt::AlignHCenter
 
127
#if QT_VERSION < 0x040000
 
128
        | Qt::WordBreak | Qt::ExpandTabs;
 
129
#else
 
130
        | Qt::TextExpandTabs | Qt::TextWordWrap;
 
131
#endif
 
132
    d_data->title.setRenderFlags(flags); 
 
133
    d_data->title.setFont(font()); 
 
134
 
 
135
    QSizePolicy policy(QSizePolicy::MinimumExpanding,
 
136
        QSizePolicy::Fixed);
 
137
    if ( d_data->scaleDraw->orientation() == Qt::Vertical )
 
138
        policy.transpose();
 
139
 
 
140
    setSizePolicy(policy);
 
141
    
 
142
#if QT_VERSION >= 0x040000
 
143
    setAttribute(Qt::WA_WState_OwnSizePolicy, false);
 
144
#else
 
145
    clearWState( WState_OwnSizePolicy );
 
146
#endif
 
147
 
 
148
}
 
149
 
 
150
void QwtScaleWidget::setTitle(const QString &title)
 
151
{
 
152
    if ( d_data->title.text() != title )
 
153
    {
 
154
        d_data->title.setText(title);
 
155
        layoutScale();
 
156
    }
 
157
}
 
158
 
 
159
/*!
 
160
  \brief Give title new text contents
 
161
  \param title New title
 
162
  \sa QwtScaleWidget::title
 
163
  \warning The title flags are interpreted in
 
164
               direction of the label, AlignTop, AlignBottom can't be set
 
165
               as the title will always be aligned to the scale.
 
166
*/
 
167
void QwtScaleWidget::setTitle(const QwtText &title)
 
168
{
 
169
    QwtText t = title;
 
170
    const int flags = title.renderFlags() & ~(Qt::AlignTop | Qt::AlignBottom);
 
171
    t.setRenderFlags(flags);
 
172
 
 
173
    if (t != d_data->title)
 
174
    {
 
175
        d_data->title = t;
 
176
        layoutScale();
 
177
    }
 
178
}
 
179
 
 
180
/*!
 
181
  Change the alignment
 
182
 
 
183
  \param alignment New alignment
 
184
  \sa QwtScaleWidget::alignment
 
185
*/
 
186
void QwtScaleWidget::setAlignment(QwtScaleDraw::Alignment alignment)
 
187
{
 
188
#if QT_VERSION >= 0x040000
 
189
    if ( !testAttribute(Qt::WA_WState_OwnSizePolicy) )
 
190
#else
 
191
    if ( !testWState( WState_OwnSizePolicy ) )
 
192
#endif
 
193
    {
 
194
        QSizePolicy policy(QSizePolicy::MinimumExpanding,
 
195
            QSizePolicy::Fixed);
 
196
        if ( d_data->scaleDraw->orientation() == Qt::Vertical )
 
197
            policy.transpose();
 
198
        setSizePolicy(policy);
 
199
 
 
200
#if QT_VERSION >= 0x040000
 
201
        setAttribute(Qt::WA_WState_OwnSizePolicy, false);
 
202
#else
 
203
        clearWState( WState_OwnSizePolicy );
 
204
#endif
 
205
    }
 
206
 
 
207
    if (d_data->scaleDraw)
 
208
        d_data->scaleDraw->setAlignment(alignment);
 
209
    layoutScale();
 
210
}
 
211
 
 
212
        
 
213
/*! 
 
214
    \return position 
 
215
    \sa QwtScaleWidget::setPosition
 
216
*/
 
217
QwtScaleDraw::Alignment QwtScaleWidget::alignment() const 
 
218
{
 
219
    if (!scaleDraw())
 
220
        return QwtScaleDraw::LeftScale;
 
221
 
 
222
    return scaleDraw()->alignment();
 
223
}
 
224
 
 
225
/*!
 
226
  Specify distances of the scale's endpoints from the
 
227
  widget's borders. The actual borders will never be less
 
228
  than minimum border distance.
 
229
  \param dist1 Left or top Distance
 
230
  \param dist2 Right or bottom distance
 
231
  \sa QwtScaleWidget::borderDist
 
232
*/
 
233
void QwtScaleWidget::setBorderDist(int dist1, int dist2)
 
234
{
 
235
    if ( dist1 != d_data->borderDist[0] || dist2 != d_data->borderDist[1] )
 
236
    {
 
237
        d_data->borderDist[0] = dist1;
 
238
        d_data->borderDist[1] = dist2;
 
239
        layoutScale();
 
240
    }
 
241
}
 
242
 
 
243
/*!
 
244
  \brief Specify the margin to the colorBar/base line.
 
245
  \param margin Margin
 
246
  \sa QwtScaleWidget::margin
 
247
*/
 
248
void QwtScaleWidget::setMargin(int margin)
 
249
{
 
250
    margin = qwtMax( 0, margin );
 
251
    if ( margin != d_data->margin )
 
252
    {
 
253
        d_data->margin = margin;
 
254
        layoutScale();
 
255
    }
 
256
}
 
257
 
 
258
/*!
 
259
  \brief Specify the distance between color bar, scale and title
 
260
  \param spacing Spacing
 
261
  \sa QwtScaleWidget::spacing
 
262
*/
 
263
void QwtScaleWidget::setSpacing(int spacing)
 
264
{
 
265
    spacing = qwtMax( 0, spacing );
 
266
    if ( spacing != d_data->spacing )
 
267
    {
 
268
        d_data->spacing = spacing;
 
269
        layoutScale();
 
270
    }
 
271
}
 
272
 
 
273
/*!
 
274
  \brief Specify the width of the scale pen
 
275
  \param width Pen width
 
276
  \sa QwtScaleWidget::penWidth
 
277
*/
 
278
void QwtScaleWidget::setPenWidth(int width)
 
279
{
 
280
    if ( width < 0 )
 
281
        width = 0;
 
282
 
 
283
    if ( width != d_data->penWidth )
 
284
    {
 
285
        d_data->penWidth = width;
 
286
        layoutScale();
 
287
    }
 
288
}
 
289
 
 
290
/*!
 
291
  \brief Change the alignment for the labels.
 
292
 
 
293
  \sa QwtScaleDraw::setLabelAlignment(), QwtScaleWidget::setLabelRotation()
 
294
*/
 
295
#if QT_VERSION < 0x040000
 
296
void QwtScaleWidget::setLabelAlignment(int alignment)
 
297
#else
 
298
void QwtScaleWidget::setLabelAlignment(Qt::Alignment alignment)
 
299
#endif
 
300
{
 
301
    d_data->scaleDraw->setLabelAlignment(alignment);
 
302
    layoutScale();
 
303
}
 
304
 
 
305
/*!
 
306
  \brief Change the rotation for the labels.
 
307
  See QwtScaleDraw::setLabelRotation().
 
308
  \sa QwtScaleDraw::setLabelRotation(), QwtScaleWidget::setLabelFlags()
 
309
*/
 
310
void QwtScaleWidget::setLabelRotation(double rotation)
 
311
{
 
312
    d_data->scaleDraw->setLabelRotation(rotation);
 
313
    layoutScale();
 
314
}
 
315
 
 
316
/*!
 
317
  \brief Set a scale draw
 
318
  sd has to be created with new and will be deleted in
 
319
  QwtScaleWidget::~QwtScale or the next call of QwtScaleWidget::setScaleDraw.
 
320
*/
 
321
void QwtScaleWidget::setScaleDraw(QwtScaleDraw *sd)
 
322
{
 
323
    if ( sd == NULL || sd == d_data->scaleDraw )
 
324
        return;
 
325
 
 
326
    if ( d_data->scaleDraw )
 
327
        sd->setAlignment(d_data->scaleDraw->alignment());
 
328
 
 
329
    delete d_data->scaleDraw;
 
330
    d_data->scaleDraw = sd;
 
331
 
 
332
    layoutScale();
 
333
}
 
334
 
 
335
/*! 
 
336
    scaleDraw of this scale
 
337
    \sa QwtScaleDraw::setScaleDraw
 
338
*/
 
339
const QwtScaleDraw *QwtScaleWidget::scaleDraw() const 
 
340
 
341
    return d_data->scaleDraw; 
 
342
}
 
343
 
 
344
/*! 
 
345
    scaleDraw of this scale
 
346
    \sa QwtScaleDraw::setScaleDraw
 
347
*/
 
348
QwtScaleDraw *QwtScaleWidget::scaleDraw() 
 
349
 
350
    return d_data->scaleDraw; 
 
351
}
 
352
 
 
353
/*! 
 
354
    \return title 
 
355
    \sa QwtScaleWidget::setTitle
 
356
*/
 
357
QwtText QwtScaleWidget::title() const 
 
358
{
 
359
    return d_data->title;
 
360
}
 
361
 
 
362
/*! 
 
363
    \return start border distance 
 
364
    \sa QwtScaleWidget::setBorderDist
 
365
*/
 
366
int QwtScaleWidget::startBorderDist() const 
 
367
 
368
    return d_data->borderDist[0]; 
 
369
}  
 
370
 
 
371
/*! 
 
372
    \return end border distance 
 
373
    \sa QwtScaleWidget::setBorderDist
 
374
*/
 
375
int QwtScaleWidget::endBorderDist() const 
 
376
 
377
    return d_data->borderDist[1]; 
 
378
}
 
379
 
 
380
/*! 
 
381
    \return margin
 
382
    \sa QwtScaleWidget::setMargin
 
383
*/
 
384
int QwtScaleWidget::margin() const 
 
385
 
386
    return d_data->margin; 
 
387
}
 
388
 
 
389
/*! 
 
390
    \return distance between scale and title
 
391
    \sa QwtScaleWidget::setMargin
 
392
*/
 
393
int QwtScaleWidget::spacing() const 
 
394
 
395
    return d_data->spacing; 
 
396
}
 
397
 
 
398
/*! 
 
399
    \return Scale pen width
 
400
    \sa QwtScaleWidget::setPenWidth
 
401
*/
 
402
int QwtScaleWidget::penWidth() const
 
403
{
 
404
    return d_data->penWidth;
 
405
 
406
/*!
 
407
  \brief paintEvent
 
408
*/
 
409
void QwtScaleWidget::paintEvent(QPaintEvent *e)
 
410
{
 
411
    const QRect &ur = e->rect();
 
412
    if ( ur.isValid() )
 
413
    {
 
414
#if QT_VERSION < 0x040000
 
415
        QwtPaintBuffer paintBuffer(this, ur);
 
416
        draw(paintBuffer.painter());
 
417
#else
 
418
        QPainter painter(this);
 
419
        draw(&painter);
 
420
#endif
 
421
    }
 
422
}
 
423
 
 
424
/*!
 
425
  \brief draw the scale
 
426
*/
 
427
void QwtScaleWidget::draw(QPainter *painter) const
 
428
{
 
429
    painter->save();
 
430
 
 
431
    QPen scalePen = painter->pen();
 
432
    scalePen.setWidth(d_data->penWidth);
 
433
    painter->setPen(scalePen);
 
434
    
 
435
#if QT_VERSION < 0x040000
 
436
    d_data->scaleDraw->draw(painter, colorGroup());
 
437
#else
 
438
    d_data->scaleDraw->draw(painter, palette());
 
439
#endif
 
440
    painter->restore();
 
441
 
 
442
    if ( d_data->colorBar.isEnabled && d_data->colorBar.width > 0 &&
 
443
        d_data->colorBar.interval.isValid() )
 
444
    {
 
445
        drawColorBar(painter, colorBarRect(rect()));
 
446
    }
 
447
 
 
448
    QRect r = rect();
 
449
    if ( d_data->scaleDraw->orientation() == Qt::Horizontal )
 
450
    {
 
451
        r.setLeft(r.left() + d_data->borderDist[0]);
 
452
        r.setWidth(r.width() - d_data->borderDist[1]);
 
453
    }
 
454
    else
 
455
    {
 
456
        r.setTop(r.top() + d_data->borderDist[0]);
 
457
        r.setHeight(r.height() - d_data->borderDist[1]);
 
458
    }
 
459
 
 
460
    if ( !d_data->title.isEmpty() )
 
461
    {
 
462
        QRect tr = r;
 
463
        switch(d_data->scaleDraw->alignment())
 
464
        {
 
465
            case QwtScaleDraw::LeftScale:
 
466
                tr.setRight( r.right() - d_data->titleOffset );
 
467
                break;
 
468
 
 
469
            case QwtScaleDraw::RightScale:
 
470
                tr.setLeft( r.left() + d_data->titleOffset );
 
471
                break;
 
472
 
 
473
            case QwtScaleDraw::BottomScale:
 
474
                tr.setTop( r.top() + d_data->titleOffset );
 
475
                break;
 
476
 
 
477
            case QwtScaleDraw::TopScale:
 
478
            default:
 
479
                tr.setBottom( r.bottom() - d_data->titleOffset );
 
480
                break;
 
481
        }
 
482
 
 
483
        drawTitle(painter, d_data->scaleDraw->alignment(), tr);
 
484
    }
 
485
}
 
486
 
 
487
QRect QwtScaleWidget::colorBarRect(const QRect& rect) const
 
488
{
 
489
    QRect cr = rect;
 
490
 
 
491
    if ( d_data->scaleDraw->orientation() == Qt::Horizontal )
 
492
    {
 
493
        cr.setLeft(cr.left() + d_data->borderDist[0]);
 
494
        cr.setWidth(cr.width() - d_data->borderDist[1] + 1);
 
495
    }
 
496
    else
 
497
    {
 
498
        cr.setTop(cr.top() + d_data->borderDist[0]);
 
499
        cr.setHeight(cr.height() - d_data->borderDist[1] + 1);
 
500
    }
 
501
 
 
502
    switch(d_data->scaleDraw->alignment())
 
503
    {
 
504
        case QwtScaleDraw::LeftScale:
 
505
        {
 
506
            cr.setLeft( cr.right() - d_data->spacing 
 
507
                - d_data->colorBar.width + 1 );
 
508
            cr.setWidth(d_data->colorBar.width);
 
509
            break;
 
510
        }
 
511
 
 
512
        case QwtScaleDraw::RightScale:
 
513
        {
 
514
            cr.setLeft( cr.left() + d_data->spacing );
 
515
            cr.setWidth(d_data->colorBar.width);
 
516
            break;
 
517
        }
 
518
 
 
519
        case QwtScaleDraw::BottomScale:
 
520
        {
 
521
            cr.setTop( cr.top() + d_data->spacing );
 
522
            cr.setHeight(d_data->colorBar.width);
 
523
            break;
 
524
        }
 
525
 
 
526
        case QwtScaleDraw::TopScale:
 
527
        {
 
528
            cr.setTop( cr.bottom() - d_data->spacing
 
529
                - d_data->colorBar.width + 1 );
 
530
            cr.setHeight(d_data->colorBar.width);
 
531
            break;
 
532
        }
 
533
    }
 
534
 
 
535
    return cr;
 
536
}
 
537
 
 
538
/*!
 
539
  \brief resizeEvent
 
540
*/
 
541
void QwtScaleWidget::resizeEvent(QResizeEvent *)
 
542
{
 
543
    layoutScale(false);
 
544
}
 
545
 
 
546
//! Recalculate the scale's geometry and layout based on
 
547
//  the current rect and fonts.
 
548
//  \param update_geometry   notify the layout system and call update
 
549
//         to redraw the scale
 
550
 
 
551
void QwtScaleWidget::layoutScale( bool update_geometry )
 
552
{
 
553
    int bd0, bd1;
 
554
    getBorderDistHint(bd0, bd1);
 
555
    if ( d_data->borderDist[0] > bd0 )
 
556
        bd0 = d_data->borderDist[0];
 
557
    if ( d_data->borderDist[1] > bd1 )
 
558
        bd1 = d_data->borderDist[1];
 
559
 
 
560
    int colorBarWidth = 0;
 
561
    if ( d_data->colorBar.isEnabled && d_data->colorBar.interval.isValid() )
 
562
        colorBarWidth = d_data->colorBar.width + d_data->spacing;
 
563
 
 
564
    const QRect r = rect();
 
565
    int x, y, length;
 
566
 
 
567
    if ( d_data->scaleDraw->orientation() == Qt::Vertical )
 
568
    {
 
569
        y = r.top() + bd0;
 
570
        length = r.height() - (bd0 + bd1);
 
571
 
 
572
        if ( d_data->scaleDraw->alignment() == QwtScaleDraw::LeftScale )
 
573
            x = r.right() - d_data->margin - colorBarWidth;
 
574
        else
 
575
            x = r.left() + d_data->margin + colorBarWidth;
 
576
    }
 
577
    else
 
578
    {
 
579
        x = r.left() + bd0; 
 
580
        length = r.width() - (bd0 + bd1);
 
581
 
 
582
        if ( d_data->scaleDraw->alignment() == QwtScaleDraw::BottomScale )
 
583
            y = r.top() + d_data->margin + colorBarWidth;
 
584
        else
 
585
            y = r.bottom() - d_data->margin - colorBarWidth;
 
586
    }
 
587
 
 
588
    d_data->scaleDraw->move(x, y);
 
589
    d_data->scaleDraw->setLength(length);
 
590
 
 
591
    d_data->titleOffset = d_data->margin + d_data->spacing +
 
592
        colorBarWidth +
 
593
        d_data->scaleDraw->extent(QPen(Qt::black, d_data->penWidth), font());
 
594
 
 
595
    if ( update_geometry )
 
596
    {
 
597
      updateGeometry();
 
598
      update();
 
599
    }
 
600
}
 
601
 
 
602
void QwtScaleWidget::drawColorBar(QPainter *painter, const QRect& rect) const
 
603
{
 
604
    if ( !d_data->colorBar.interval.isValid() )
 
605
        return;
 
606
 
 
607
    const QwtScaleDraw* sd = d_data->scaleDraw;
 
608
 
 
609
    QwtPainter::drawColorBar(painter, *d_data->colorBar.colorMap, 
 
610
        d_data->colorBar.interval.normalized(), sd->map(), 
 
611
        sd->orientation(), rect);
 
612
}
 
613
 
 
614
/*!
 
615
  Rotate and paint a title according to its position into a given rectangle.
 
616
  \param painter Painter
 
617
  \param align Alignment
 
618
  \param rect Bounding rectangle
 
619
*/
 
620
 
 
621
void QwtScaleWidget::drawTitle(QPainter *painter,
 
622
    QwtScaleDraw::Alignment align, const QRect &rect) const
 
623
{
 
624
    QRect r;
 
625
    double angle;
 
626
    int flags = d_data->title.renderFlags() & 
 
627
        ~(Qt::AlignTop | Qt::AlignBottom | Qt::AlignVCenter);
 
628
 
 
629
    switch(align)
 
630
    {
 
631
        case QwtScaleDraw::LeftScale:
 
632
            flags |= Qt::AlignTop;
 
633
            angle = -90.0;
 
634
            r.setRect(rect.left(), rect.bottom(), rect.height(), rect.width());
 
635
            break;
 
636
        case QwtScaleDraw::RightScale:
 
637
            flags |= Qt::AlignTop;
 
638
            angle = 90.0;
 
639
            r.setRect(rect.right(), rect.top(), rect.height(), rect.width());
 
640
            break;
 
641
        case QwtScaleDraw::TopScale:
 
642
            flags |= Qt::AlignTop;
 
643
            angle = 0.0;
 
644
            r = rect;
 
645
            break;
 
646
        case QwtScaleDraw::BottomScale:
 
647
        default:
 
648
            flags |= Qt::AlignBottom;
 
649
            angle = 0.0;
 
650
            r = rect;
 
651
            break;
 
652
    }
 
653
 
 
654
    painter->save();
 
655
    painter->setFont(font());
 
656
#if QT_VERSION < 0x040000
 
657
    painter->setPen(colorGroup().color(QColorGroup::Text));
 
658
#else
 
659
    painter->setPen(palette().color(QPalette::Text));
 
660
#endif
 
661
 
 
662
    painter->translate(r.x(), r.y());
 
663
    if (angle != 0.0)
 
664
        painter->rotate(angle);
 
665
 
 
666
    QwtText title = d_data->title;
 
667
    title.setRenderFlags(flags);
 
668
    title.draw(painter, QRect(0, 0, r.width(), r.height()));
 
669
 
 
670
    painter->restore();
 
671
}
 
672
 
 
673
/*!
 
674
  \brief Notify a change of the scale
 
675
 
 
676
  This virtual function can be overloaded by derived
 
677
  classes. The default implementation updates the geometry
 
678
  and repaints the widget.
 
679
*/
 
680
 
 
681
void QwtScaleWidget::scaleChange()
 
682
{
 
683
    layoutScale();
 
684
}
 
685
 
 
686
/*!
 
687
  \return a size hint
 
688
*/
 
689
QSize QwtScaleWidget::sizeHint() const
 
690
{
 
691
    return minimumSizeHint();
 
692
}
 
693
 
 
694
/*!
 
695
  \return a minimum size hint
 
696
*/
 
697
QSize QwtScaleWidget::minimumSizeHint() const
 
698
{
 
699
    const Qt::Orientation o = d_data->scaleDraw->orientation();
 
700
 
 
701
    // Border Distance cannot be less than the scale borderDistHint
 
702
    // Note, the borderDistHint is already included in minHeight/minWidth
 
703
    int length = 0;
 
704
    int mbd1, mbd2;
 
705
    getBorderDistHint(mbd1, mbd2);
 
706
    length += qwtMax( 0, d_data->borderDist[0] - mbd1 );
 
707
    length += qwtMax( 0, d_data->borderDist[1] - mbd2 );
 
708
    length += d_data->scaleDraw->minLength(
 
709
        QPen(Qt::black, d_data->penWidth), font());
 
710
 
 
711
    int dim = dimForLength(length, font());
 
712
    if ( length < dim )
 
713
    {
 
714
        // compensate for long titles
 
715
        length = dim;
 
716
        dim = dimForLength(length, font());
 
717
    }
 
718
 
 
719
    QSize size(length + 2, dim);
 
720
    if ( o == Qt::Vertical )
 
721
        size.transpose();
 
722
 
 
723
    return size;
 
724
}
 
725
 
 
726
/*!
 
727
  \brief Find the height of the title for a given width.
 
728
  \param width Width
 
729
  \return height Height
 
730
 */
 
731
 
 
732
int QwtScaleWidget::titleHeightForWidth(int width) const
 
733
{
 
734
    return d_data->title.heightForWidth(width, font());
 
735
}
 
736
 
 
737
/*!
 
738
  \brief Find the minimum dimension for a given length.
 
739
         dim is the height, length the width seen in
 
740
         direction of the title.
 
741
  \param length width for horizontal, height for vertical scales
 
742
  \param scaleFont Font of the scale
 
743
  \return height for horizontal, width for vertical scales
 
744
*/
 
745
 
 
746
int QwtScaleWidget::dimForLength(int length, const QFont &scaleFont) const
 
747
{
 
748
    int dim = d_data->margin;
 
749
    dim += d_data->scaleDraw->extent(
 
750
        QPen(Qt::black, d_data->penWidth), scaleFont);
 
751
 
 
752
    if ( !d_data->title.isEmpty() )
 
753
        dim += titleHeightForWidth(length) + d_data->spacing;
 
754
 
 
755
    if ( d_data->colorBar.isEnabled && d_data->colorBar.interval.isValid() )
 
756
        dim += d_data->colorBar.width + d_data->spacing;
 
757
 
 
758
    return dim;
 
759
}
 
760
 
 
761
/*!
 
762
  \brief Calculate a hint for the border distances.
 
763
 
 
764
  This member function calculates the distance
 
765
  of the scale's endpoints from the widget borders which
 
766
  is required for the mark labels to fit into the widget.
 
767
  The maximum of this distance an the minimum border distance
 
768
  is returned.
 
769
 
 
770
  \warning
 
771
  <ul> <li>The minimum border distance depends on the font.</ul>
 
772
  \sa setMinBorderDist(), getMinBorderDist(), setBorderDist()
 
773
*/
 
774
void QwtScaleWidget::getBorderDistHint(int &start, int &end) const
 
775
{
 
776
    d_data->scaleDraw->getBorderDistHint(font(), start, end);
 
777
 
 
778
    if ( start < d_data->minBorderDist[0] )
 
779
        start = d_data->minBorderDist[0];
 
780
 
 
781
    if ( end < d_data->minBorderDist[1] )
 
782
        end = d_data->minBorderDist[1];
 
783
}
 
784
 
 
785
/*!
 
786
  Set a minimum value for the distances of the scale's endpoints from 
 
787
  the widget borders. This is useful to avoid that the scales
 
788
  are "jumping", when the tick labels or their positions change 
 
789
  often.
 
790
 
 
791
  \sa getMinBorderDist(), getBorderDistHint()
 
792
*/
 
793
void QwtScaleWidget::setMinBorderDist(int start, int end)
 
794
{
 
795
    d_data->minBorderDist[0] = start;
 
796
    d_data->minBorderDist[1] = end;
 
797
}
 
798
 
 
799
/*!
 
800
  Get the minimum value for the distances of the scale's endpoints from 
 
801
  the widget borders.
 
802
 
 
803
  \sa setMinBorderDist(), getBorderDistHint()
 
804
*/
 
805
void QwtScaleWidget::getMinBorderDist(int &start, int &end) const
 
806
{
 
807
    start = d_data->minBorderDist[0];
 
808
    end = d_data->minBorderDist[1];
 
809
}
 
810
 
 
811
#if QT_VERSION < 0x040000
 
812
 
 
813
/*!
 
814
  \brief Notify a change of the font
 
815
 
 
816
  This virtual function may be overloaded by derived widgets.
 
817
  The default implementation resizes the scale and repaints
 
818
  the widget.
 
819
  \param oldFont Previous font
 
820
*/
 
821
void QwtScaleWidget::fontChange(const QFont &oldFont)
 
822
{
 
823
    QWidget::fontChange( oldFont );
 
824
    layoutScale();
 
825
}
 
826
 
 
827
#endif
 
828
 
 
829
/*!
 
830
  \brief Assign a scale division
 
831
 
 
832
  The scale division determines where to set the tick marks.
 
833
 
 
834
  \param transformation Transformation, needed to translate between
 
835
                        scale and pixal values
 
836
  \param scaleDiv Scale Division
 
837
  \sa For more information about scale divisions, see QwtScaleDiv.
 
838
*/
 
839
void QwtScaleWidget::setScaleDiv(
 
840
    QwtScaleTransformation *transformation,
 
841
    const QwtScaleDiv &scaleDiv)
 
842
{
 
843
    QwtScaleDraw *sd = d_data->scaleDraw;
 
844
    if (sd->scaleDiv() != scaleDiv ||
 
845
        sd->map().transformation()->type() != transformation->type() )
 
846
    {
 
847
        sd->setTransformation(transformation);
 
848
        sd->setScaleDiv(scaleDiv);
 
849
        layoutScale();
 
850
 
 
851
        emit scaleDivChanged();
 
852
    }
 
853
    else
 
854
        delete transformation;
 
855
}
 
856
 
 
857
void QwtScaleWidget::setColorBarEnabled(bool on)
 
858
{
 
859
    if ( on != d_data->colorBar.isEnabled )
 
860
    {
 
861
        d_data->colorBar.isEnabled = on;
 
862
        layoutScale();
 
863
    }
 
864
}
 
865
 
 
866
bool QwtScaleWidget::isColorBarEnabled() const
 
867
{
 
868
    return d_data->colorBar.isEnabled;
 
869
}
 
870
 
 
871
 
 
872
void QwtScaleWidget::setColorBarWidth(int width)
 
873
{
 
874
    if ( width != d_data->colorBar.width )
 
875
    {
 
876
        d_data->colorBar.width = width;
 
877
        if ( isColorBarEnabled() )
 
878
            layoutScale();
 
879
    }
 
880
}
 
881
 
 
882
int QwtScaleWidget::colorBarWidth() const
 
883
{
 
884
    return d_data->colorBar.width;
 
885
}
 
886
 
 
887
QwtDoubleInterval QwtScaleWidget::colorBarInterval() const
 
888
{
 
889
    return d_data->colorBar.interval;
 
890
}
 
891
 
 
892
void QwtScaleWidget::setColorMap(const QwtDoubleInterval &interval,
 
893
    const QwtColorMap &colorMap)
 
894
{
 
895
    d_data->colorBar.interval = interval;
 
896
 
 
897
    delete d_data->colorBar.colorMap;
 
898
    d_data->colorBar.colorMap = colorMap.copy();
 
899
 
 
900
    if ( isColorBarEnabled() )
 
901
        layoutScale();
 
902
}
 
903
 
 
904
const QwtColorMap &QwtScaleWidget::colorMap() const
 
905
{
 
906
    return *d_data->colorBar.colorMap;
 
907
}