~l3on/ubuntu/oneiric/qwt/fix-921430

« back to all changes in this revision

Viewing changes to qwt-5.0.1/src/qwt_legend_item.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2007-10-05 15:20:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071005152041-qmybqh4fj9jejyo2
Tags: 5.0.2-2
* Handle nostrip build option. (Closes: #437877)
* Build libqwt5-doc package in binary-indep target. (Closes: #443110)

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 <qdrawutil.h>
14
 
#include <qstyle.h>
15
 
#include <qpen.h>
16
 
#if QT_VERSION >= 0x040000
17
 
#include <qevent.h>
18
 
#include <qstyleoption.h>
19
 
#endif
20
 
#include "qwt_math.h"
21
 
#include "qwt_painter.h"
22
 
#include "qwt_symbol.h"
23
 
#include "qwt_legend_item.h"
24
 
 
25
 
static const int ButtonFrame = 2;
26
 
static const int Margin = 2;
27
 
 
28
 
static QSize buttonShift(const QwtLegendItem *w)
29
 
{
30
 
#if QT_VERSION < 0x040000
31
 
    const int ph = w->style().pixelMetric(
32
 
        QStyle::PM_ButtonShiftHorizontal, w);
33
 
    const int pv = w->style().pixelMetric(
34
 
        QStyle::PM_ButtonShiftVertical, w);
35
 
#else
36
 
    QStyleOption option;
37
 
    option.init(w);
38
 
 
39
 
    const int ph = w->style()->pixelMetric(
40
 
        QStyle::PM_ButtonShiftHorizontal, &option, w);
41
 
    const int pv = w->style()->pixelMetric(
42
 
        QStyle::PM_ButtonShiftVertical, &option, w);
43
 
#endif
44
 
    return QSize(ph, pv);
45
 
}
46
 
 
47
 
class QwtLegendItem::PrivateData
48
 
{
49
 
public:
50
 
    PrivateData():
51
 
        itemMode(QwtLegend::ReadOnlyItem),
52
 
        isDown(false),
53
 
        identifierWidth(8),
54
 
        identifierMode(QwtLegendItem::ShowLine | QwtLegendItem::ShowText),
55
 
        curvePen(Qt::NoPen),
56
 
        spacing(Margin)
57
 
    {
58
 
    }
59
 
 
60
 
    QwtLegend::LegendItemMode itemMode;
61
 
    bool isDown;
62
 
 
63
 
    int identifierWidth;
64
 
    int identifierMode;
65
 
    QwtSymbol symbol;
66
 
    QPen curvePen;
67
 
 
68
 
    int spacing;
69
 
};
70
 
 
71
 
/*!
72
 
  \param parent Parent widget
73
 
*/
74
 
QwtLegendItem::QwtLegendItem(QWidget *parent):
75
 
    QwtTextLabel(parent)
76
 
{
77
 
    d_data = new PrivateData;
78
 
    init(QwtText());
79
 
}
80
 
 
81
 
/*!
82
 
  \param symbol Curve symbol
83
 
  \param curvePen Curve pen
84
 
  \param text Label text
85
 
  \param parent Parent widget
86
 
*/
87
 
QwtLegendItem::QwtLegendItem(const QwtSymbol &symbol, 
88
 
        const QPen &curvePen, const QwtText &text, 
89
 
        QWidget *parent):
90
 
    QwtTextLabel(parent)
91
 
{
92
 
    d_data = new PrivateData;
93
 
 
94
 
    d_data->symbol = symbol;
95
 
    d_data->curvePen = curvePen;
96
 
 
97
 
    init(text);
98
 
}
99
 
 
100
 
void QwtLegendItem::init(const QwtText &text)
101
 
{
102
 
    setMargin(Margin);
103
 
    setIndent(margin() + d_data->identifierWidth + 2 * d_data->spacing);
104
 
    setText(text);
105
 
}
106
 
 
107
 
//! Destructor
108
 
QwtLegendItem::~QwtLegendItem()
109
 
{
110
 
    delete d_data;
111
 
    d_data = NULL;
112
 
}
113
 
 
114
 
/*!
115
 
   Set the text to the legend item
116
 
 
117
 
   \param text Text label
118
 
    \sa QwtTextLabel::text()
119
 
*/
120
 
void QwtLegendItem::setText(const QwtText &text)
121
 
{
122
 
    const int flags = Qt::AlignLeft | Qt::AlignVCenter
123
 
#if QT_VERSION < 0x040000
124
 
        | Qt::WordBreak | Qt::ExpandTabs;
125
 
#else
126
 
        | Qt::TextExpandTabs | Qt::TextWordWrap;
127
 
#endif
128
 
 
129
 
    QwtText txt = text;
130
 
    txt.setRenderFlags(flags);
131
 
 
132
 
    QwtTextLabel::setText(txt);
133
 
}
134
 
 
135
 
/*!
136
 
   Set the item mode
137
 
   The default is QwtLegend::ReadOnlyItem
138
 
 
139
 
   \param mode Item mode
140
 
   \sa itemMode()
141
 
*/
142
 
void QwtLegendItem::setItemMode(QwtLegend::LegendItemMode mode) 
143
 
144
 
    d_data->itemMode = mode; 
145
 
    d_data->isDown = false; 
146
 
 
147
 
#if QT_VERSION >= 0x040000
148
 
    using namespace Qt;
149
 
#endif
150
 
    setFocusPolicy(mode != QwtLegend::ReadOnlyItem ? TabFocus : NoFocus);
151
 
    setMargin(ButtonFrame + Margin);
152
 
 
153
 
    updateGeometry();
154
 
}
155
 
 
156
 
/*! 
157
 
   Return the item mode
158
 
 
159
 
   \sa setItemMode()
160
 
*/
161
 
QwtLegend::LegendItemMode QwtLegendItem::itemMode() const 
162
 
163
 
    return d_data->itemMode; 
164
 
}
165
 
 
166
 
/*!
167
 
  Set identifier mode.
168
 
  Default is ShowLine | ShowText.
169
 
  \param mode Or'd values of IdentifierMode
170
 
 
171
 
  \sa identifierMode()
172
 
*/
173
 
void QwtLegendItem::setIdentifierMode(int mode)
174
 
{
175
 
    if ( mode != d_data->identifierMode )
176
 
    {
177
 
        d_data->identifierMode = mode;
178
 
        update();
179
 
    }
180
 
}
181
 
 
182
 
/*!
183
 
  Or'd values of IdentifierMode.
184
 
  \sa setIdentifierMode(), IdentifierMode
185
 
*/
186
 
int QwtLegendItem::identifierMode() const 
187
 
188
 
    return d_data->identifierMode; 
189
 
}
190
 
 
191
 
/*!
192
 
  Set the width for the identifier
193
 
  Default is 8 pixels
194
 
 
195
 
  \param width New width
196
 
 
197
 
  \sa identifierMode(), identifierWidth
198
 
*/
199
 
void QwtLegendItem::setIdentfierWidth(int width)
200
 
{
201
 
    width = qwtMax(width, 0);
202
 
    if ( width != d_data->identifierWidth )
203
 
    {
204
 
        d_data->identifierWidth = width;
205
 
        setIndent(margin() + d_data->identifierWidth 
206
 
            + 2 * d_data->spacing);
207
 
    }
208
 
}
209
 
/*!
210
 
   Return the width of the identifier
211
 
 
212
 
   \sa setIdentfierWidth
213
 
*/
214
 
int QwtLegendItem::identifierWidth() const
215
 
{
216
 
    return d_data->identifierWidth;
217
 
}
218
 
 
219
 
/*!
220
 
   Change the spacing
221
 
   \param spacing Spacing
222
 
   \sa spacing(), identifierWidth(), QwtTextLabel::margin()
223
 
*/
224
 
void QwtLegendItem::setSpacing(int spacing)
225
 
{
226
 
    spacing = qwtMax(spacing, 0);
227
 
    if ( spacing != d_data->spacing )
228
 
    {
229
 
        d_data->spacing = spacing;
230
 
        setIndent(margin() + d_data->identifierWidth 
231
 
            + 2 * d_data->spacing);
232
 
    }
233
 
}
234
 
 
235
 
/*!
236
 
   Return the spacing
237
 
   \sa setSpacing(), identifierWidth(), QwtTextLabel::margin()
238
 
*/
239
 
int QwtLegendItem::spacing() const
240
 
{
241
 
    return d_data->spacing;
242
 
}
243
 
 
244
 
/*! 
245
 
  Set curve symbol.
246
 
  \param symbol Symbol
247
 
 
248
 
  \sa symbol()
249
 
*/
250
 
void QwtLegendItem::setSymbol(const QwtSymbol &symbol) 
251
 
{
252
 
    if ( symbol != d_data->symbol )
253
 
    {
254
 
        d_data->symbol = symbol;
255
 
        update();
256
 
    }
257
 
}
258
 
    
259
 
/*!
260
 
  \return The curve symbol.
261
 
  \sa setSymbol()
262
 
*/
263
 
const QwtSymbol& QwtLegendItem::symbol() const 
264
 
265
 
    return d_data->symbol; 
266
 
}
267
 
    
268
 
 
269
 
/*! 
270
 
  Set curve pen.
271
 
  \param pen Curve pen
272
 
 
273
 
  \sa curvePen()
274
 
*/
275
 
void QwtLegendItem::setCurvePen(const QPen &pen) 
276
 
{
277
 
    if ( pen != d_data->curvePen )
278
 
    {
279
 
        d_data->curvePen = pen;
280
 
        update();
281
 
    }
282
 
}
283
 
 
284
 
/*!
285
 
  \return The curve pen.
286
 
  \sa setCurvePen()
287
 
*/
288
 
const QPen& QwtLegendItem::curvePen() const 
289
 
290
 
    return d_data->curvePen; 
291
 
}
292
 
 
293
 
/*! 
294
 
  Paint the identifier to a given rect.
295
 
  \param painter Painter
296
 
  \param rect Rect where to paint
297
 
*/
298
 
void QwtLegendItem::drawIdentifier(
299
 
    QPainter *painter, const QRect &rect) const
300
 
{
301
 
    if ( rect.isEmpty() )
302
 
        return;
303
 
 
304
 
    if ( (d_data->identifierMode & ShowLine ) && (d_data->curvePen.style() != Qt::NoPen) )
305
 
    {
306
 
        painter->save();
307
 
        painter->setPen(d_data->curvePen);
308
 
        QwtPainter::drawLine(painter, rect.left(), rect.center().y(), 
309
 
            rect.right(), rect.center().y());
310
 
        painter->restore();
311
 
    }
312
 
 
313
 
    if ( (d_data->identifierMode & ShowSymbol) 
314
 
        && (d_data->symbol.style() != QwtSymbol::NoSymbol) )
315
 
    {
316
 
        QSize symbolSize = 
317
 
            QwtPainter::metricsMap().screenToLayout(d_data->symbol.size());
318
 
 
319
 
        // scale the symbol size down if it doesn't fit into rect.
320
 
 
321
 
        if ( rect.width() < symbolSize.width() )
322
 
        {
323
 
            const double ratio = 
324
 
                double(symbolSize.width()) / double(rect.width());
325
 
            symbolSize.setWidth(rect.width());
326
 
            symbolSize.setHeight(qRound(symbolSize.height() / ratio));
327
 
        }
328
 
        if ( rect.height() < symbolSize.height() )
329
 
        {
330
 
            const double ratio = 
331
 
                double(symbolSize.width()) / double(rect.width());
332
 
            symbolSize.setHeight(rect.height());
333
 
            symbolSize.setWidth(qRound(symbolSize.width() / ratio));
334
 
        }
335
 
 
336
 
        QRect symbolRect;
337
 
        symbolRect.setSize(symbolSize);
338
 
        symbolRect.moveCenter(rect.center());
339
 
 
340
 
        painter->save();
341
 
        painter->setBrush(d_data->symbol.brush());
342
 
        painter->setPen(d_data->symbol.pen());
343
 
        d_data->symbol.draw(painter, symbolRect);
344
 
        painter->restore();
345
 
    }
346
 
}
347
 
 
348
 
/*!
349
 
  Draw the legend item to a given rect.
350
 
  \param painter Painter
351
 
  \param rect Rect where to paint the button
352
 
*/
353
 
 
354
 
void QwtLegendItem::drawItem(QPainter *painter, const QRect &rect) const
355
 
{
356
 
    painter->save();
357
 
 
358
 
    const QwtMetricsMap &map = QwtPainter::metricsMap();
359
 
 
360
 
    const int m = map.screenToLayoutX(margin());
361
 
    const int spacing = map.screenToLayoutX(d_data->spacing);
362
 
    const int identifierWidth = map.screenToLayoutX(d_data->identifierWidth);
363
 
 
364
 
    const QRect identifierRect(rect.x() + m, rect.y(), 
365
 
        identifierWidth, rect.height());
366
 
    drawIdentifier(painter, identifierRect);
367
 
 
368
 
    // Label
369
 
 
370
 
    QRect titleRect = rect;
371
 
    titleRect.setX(identifierRect.right() + 2 * spacing);
372
 
     
373
 
    text().draw(painter, titleRect);
374
 
 
375
 
    painter->restore();
376
 
}
377
 
 
378
 
void QwtLegendItem::paintEvent(QPaintEvent *e)
379
 
{
380
 
    const QRect cr = contentsRect();
381
 
 
382
 
    QPainter painter(this);
383
 
    painter.setClipRegion(e->region());
384
 
 
385
 
    if ( d_data->isDown )
386
 
    {
387
 
        qDrawWinButton(&painter, 0, 0, width(), height(), 
388
 
#if QT_VERSION < 0x040000
389
 
            colorGroup(), 
390
 
#else
391
 
            palette(),
392
 
#endif
393
 
            true);
394
 
    }
395
 
 
396
 
    painter.save();
397
 
 
398
 
    if ( d_data->isDown )
399
 
    {
400
 
        const QSize shiftSize = buttonShift(this);
401
 
        painter.translate(shiftSize.width(), shiftSize.height());
402
 
    }
403
 
 
404
 
    painter.setClipRect(cr);
405
 
 
406
 
    drawContents(&painter);
407
 
 
408
 
    QRect rect = cr;
409
 
    rect.setX(rect.x() + margin());
410
 
    if ( d_data->itemMode != QwtLegend::ReadOnlyItem )
411
 
        rect.setX(rect.x() + ButtonFrame);
412
 
 
413
 
    rect.setWidth(d_data->identifierWidth);
414
 
 
415
 
    drawIdentifier(&painter, rect);
416
 
 
417
 
    painter.restore();
418
 
}
419
 
 
420
 
void QwtLegendItem::mousePressEvent(QMouseEvent *e)
421
 
{
422
 
    if ( e->button() != Qt::LeftButton )
423
 
        return;
424
 
 
425
 
    switch(d_data->itemMode)
426
 
    {
427
 
        case QwtLegend::ClickableItem:
428
 
        {
429
 
            setDown(true);
430
 
            break;
431
 
        }
432
 
        case QwtLegend::CheckableItem:
433
 
        {
434
 
            setDown(!isDown());
435
 
            break;
436
 
        }
437
 
        default:;
438
 
    }
439
 
}
440
 
 
441
 
void QwtLegendItem::mouseReleaseEvent(QMouseEvent *e)
442
 
{
443
 
    if ( !e->button() == Qt::LeftButton )
444
 
        return;
445
 
 
446
 
    if ( d_data->itemMode == QwtLegend::ClickableItem )
447
 
        setDown(false);
448
 
}
449
 
 
450
 
void QwtLegendItem::keyPressEvent(QKeyEvent *e)
451
 
{
452
 
    if ( e->key() != Qt::Key_Space || e->isAutoRepeat() )
453
 
        return;
454
 
 
455
 
    switch(d_data->itemMode)
456
 
    {
457
 
        case QwtLegend::ClickableItem:
458
 
        {
459
 
            setDown(true);
460
 
            break;
461
 
        }
462
 
        case QwtLegend::CheckableItem:
463
 
        {
464
 
            setDown(!isDown());
465
 
            break;
466
 
        }
467
 
        default:;
468
 
    }
469
 
}
470
 
 
471
 
void QwtLegendItem::keyReleaseEvent(QKeyEvent *e)
472
 
{
473
 
    if ( e->key() != Qt::Key_Space || e->isAutoRepeat() )
474
 
        return;
475
 
 
476
 
    if ( d_data->itemMode == QwtLegend::ClickableItem )
477
 
        setDown(false);
478
 
}
479
 
 
480
 
void QwtLegendItem::setChecked(bool on)
481
 
{
482
 
    if ( d_data->itemMode == QwtLegend::CheckableItem )
483
 
    {
484
 
        const bool isBlocked = signalsBlocked();
485
 
        blockSignals(true);
486
 
 
487
 
        setDown(on);
488
 
 
489
 
        blockSignals(isBlocked);
490
 
    }
491
 
}
492
 
 
493
 
bool QwtLegendItem::isChecked() const
494
 
{
495
 
    return d_data->itemMode == QwtLegend::CheckableItem && isDown();
496
 
}
497
 
 
498
 
void QwtLegendItem::setDown(bool down)
499
 
{
500
 
    if ( down == d_data->isDown )
501
 
        return;
502
 
 
503
 
    d_data->isDown = down;
504
 
    update();
505
 
 
506
 
    if ( d_data->itemMode == QwtLegend::ClickableItem )
507
 
    {
508
 
        if ( d_data->isDown )
509
 
            emit pressed();
510
 
        else
511
 
        {
512
 
            emit released();
513
 
            emit clicked();
514
 
        }
515
 
    }
516
 
 
517
 
    if ( d_data->itemMode == QwtLegend::CheckableItem )
518
 
        emit checked(d_data->isDown);
519
 
}
520
 
 
521
 
bool QwtLegendItem::isDown() const
522
 
{
523
 
    return d_data->isDown;
524
 
}
525
 
 
526
 
QSize QwtLegendItem::sizeHint() const
527
 
{
528
 
    QSize sz = QwtTextLabel::sizeHint();
529
 
    if ( d_data->itemMode != QwtLegend::ReadOnlyItem )
530
 
        sz += buttonShift(this);
531
 
 
532
 
    return sz;
533
 
}
534
 
 
535
 
void QwtLegendItem::drawText(QPainter *painter, const QRect &rect)
536
 
{
537
 
    QwtTextLabel::drawText(painter, rect);
538
 
}