1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
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
*****************************************************************************/
13
#include <qdrawutil.h>
16
#if QT_VERSION >= 0x040000
18
#include <qstyleoption.h>
21
#include "qwt_painter.h"
22
#include "qwt_symbol.h"
23
#include "qwt_legend_item.h"
25
static const int ButtonFrame = 2;
26
static const int Margin = 2;
28
static QSize buttonShift(const QwtLegendItem *w)
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);
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);
47
class QwtLegendItem::PrivateData
51
itemMode(QwtLegend::ReadOnlyItem),
54
identifierMode(QwtLegendItem::ShowLine | QwtLegendItem::ShowText),
60
QwtLegend::LegendItemMode itemMode;
72
\param parent Parent widget
74
QwtLegendItem::QwtLegendItem(QWidget *parent):
77
d_data = new PrivateData;
82
\param symbol Curve symbol
83
\param curvePen Curve pen
84
\param text Label text
85
\param parent Parent widget
87
QwtLegendItem::QwtLegendItem(const QwtSymbol &symbol,
88
const QPen &curvePen, const QwtText &text,
92
d_data = new PrivateData;
94
d_data->symbol = symbol;
95
d_data->curvePen = curvePen;
100
void QwtLegendItem::init(const QwtText &text)
103
setIndent(margin() + d_data->identifierWidth + 2 * d_data->spacing);
108
QwtLegendItem::~QwtLegendItem()
115
Set the text to the legend item
117
\param text Text label
118
\sa QwtTextLabel::text()
120
void QwtLegendItem::setText(const QwtText &text)
122
const int flags = Qt::AlignLeft | Qt::AlignVCenter
123
#if QT_VERSION < 0x040000
124
| Qt::WordBreak | Qt::ExpandTabs;
126
| Qt::TextExpandTabs | Qt::TextWordWrap;
130
txt.setRenderFlags(flags);
132
QwtTextLabel::setText(txt);
137
The default is QwtLegend::ReadOnlyItem
139
\param mode Item mode
142
void QwtLegendItem::setItemMode(QwtLegend::LegendItemMode mode)
144
d_data->itemMode = mode;
145
d_data->isDown = false;
147
#if QT_VERSION >= 0x040000
150
setFocusPolicy(mode != QwtLegend::ReadOnlyItem ? TabFocus : NoFocus);
151
setMargin(ButtonFrame + Margin);
161
QwtLegend::LegendItemMode QwtLegendItem::itemMode() const
163
return d_data->itemMode;
168
Default is ShowLine | ShowText.
169
\param mode Or'd values of IdentifierMode
173
void QwtLegendItem::setIdentifierMode(int mode)
175
if ( mode != d_data->identifierMode )
177
d_data->identifierMode = mode;
183
Or'd values of IdentifierMode.
184
\sa setIdentifierMode(), IdentifierMode
186
int QwtLegendItem::identifierMode() const
188
return d_data->identifierMode;
192
Set the width for the identifier
195
\param width New width
197
\sa identifierMode(), identifierWidth
199
void QwtLegendItem::setIdentfierWidth(int width)
201
width = qwtMax(width, 0);
202
if ( width != d_data->identifierWidth )
204
d_data->identifierWidth = width;
205
setIndent(margin() + d_data->identifierWidth
206
+ 2 * d_data->spacing);
210
Return the width of the identifier
212
\sa setIdentfierWidth
214
int QwtLegendItem::identifierWidth() const
216
return d_data->identifierWidth;
221
\param spacing Spacing
222
\sa spacing(), identifierWidth(), QwtTextLabel::margin()
224
void QwtLegendItem::setSpacing(int spacing)
226
spacing = qwtMax(spacing, 0);
227
if ( spacing != d_data->spacing )
229
d_data->spacing = spacing;
230
setIndent(margin() + d_data->identifierWidth
231
+ 2 * d_data->spacing);
237
\sa setSpacing(), identifierWidth(), QwtTextLabel::margin()
239
int QwtLegendItem::spacing() const
241
return d_data->spacing;
250
void QwtLegendItem::setSymbol(const QwtSymbol &symbol)
252
if ( symbol != d_data->symbol )
254
d_data->symbol = symbol;
260
\return The curve symbol.
263
const QwtSymbol& QwtLegendItem::symbol() const
265
return d_data->symbol;
275
void QwtLegendItem::setCurvePen(const QPen &pen)
277
if ( pen != d_data->curvePen )
279
d_data->curvePen = pen;
285
\return The curve pen.
288
const QPen& QwtLegendItem::curvePen() const
290
return d_data->curvePen;
294
Paint the identifier to a given rect.
295
\param painter Painter
296
\param rect Rect where to paint
298
void QwtLegendItem::drawIdentifier(
299
QPainter *painter, const QRect &rect) const
301
if ( rect.isEmpty() )
304
if ( (d_data->identifierMode & ShowLine ) && (d_data->curvePen.style() != Qt::NoPen) )
307
painter->setPen(d_data->curvePen);
308
QwtPainter::drawLine(painter, rect.left(), rect.center().y(),
309
rect.right(), rect.center().y());
313
if ( (d_data->identifierMode & ShowSymbol)
314
&& (d_data->symbol.style() != QwtSymbol::NoSymbol) )
317
QwtPainter::metricsMap().screenToLayout(d_data->symbol.size());
319
// scale the symbol size down if it doesn't fit into rect.
321
if ( rect.width() < symbolSize.width() )
324
double(symbolSize.width()) / double(rect.width());
325
symbolSize.setWidth(rect.width());
326
symbolSize.setHeight(qRound(symbolSize.height() / ratio));
328
if ( rect.height() < symbolSize.height() )
331
double(symbolSize.width()) / double(rect.width());
332
symbolSize.setHeight(rect.height());
333
symbolSize.setWidth(qRound(symbolSize.width() / ratio));
337
symbolRect.setSize(symbolSize);
338
symbolRect.moveCenter(rect.center());
341
painter->setBrush(d_data->symbol.brush());
342
painter->setPen(d_data->symbol.pen());
343
d_data->symbol.draw(painter, symbolRect);
349
Draw the legend item to a given rect.
350
\param painter Painter
351
\param rect Rect where to paint the button
354
void QwtLegendItem::drawItem(QPainter *painter, const QRect &rect) const
358
const QwtMetricsMap &map = QwtPainter::metricsMap();
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);
364
const QRect identifierRect(rect.x() + m, rect.y(),
365
identifierWidth, rect.height());
366
drawIdentifier(painter, identifierRect);
370
QRect titleRect = rect;
371
titleRect.setX(identifierRect.right() + 2 * spacing);
373
text().draw(painter, titleRect);
378
void QwtLegendItem::paintEvent(QPaintEvent *e)
380
const QRect cr = contentsRect();
382
QPainter painter(this);
383
painter.setClipRegion(e->region());
385
if ( d_data->isDown )
387
qDrawWinButton(&painter, 0, 0, width(), height(),
388
#if QT_VERSION < 0x040000
398
if ( d_data->isDown )
400
const QSize shiftSize = buttonShift(this);
401
painter.translate(shiftSize.width(), shiftSize.height());
404
painter.setClipRect(cr);
406
drawContents(&painter);
409
rect.setX(rect.x() + margin());
410
if ( d_data->itemMode != QwtLegend::ReadOnlyItem )
411
rect.setX(rect.x() + ButtonFrame);
413
rect.setWidth(d_data->identifierWidth);
415
drawIdentifier(&painter, rect);
420
void QwtLegendItem::mousePressEvent(QMouseEvent *e)
422
if ( e->button() != Qt::LeftButton )
425
switch(d_data->itemMode)
427
case QwtLegend::ClickableItem:
432
case QwtLegend::CheckableItem:
441
void QwtLegendItem::mouseReleaseEvent(QMouseEvent *e)
443
if ( !e->button() == Qt::LeftButton )
446
if ( d_data->itemMode == QwtLegend::ClickableItem )
450
void QwtLegendItem::keyPressEvent(QKeyEvent *e)
452
if ( e->key() != Qt::Key_Space || e->isAutoRepeat() )
455
switch(d_data->itemMode)
457
case QwtLegend::ClickableItem:
462
case QwtLegend::CheckableItem:
471
void QwtLegendItem::keyReleaseEvent(QKeyEvent *e)
473
if ( e->key() != Qt::Key_Space || e->isAutoRepeat() )
476
if ( d_data->itemMode == QwtLegend::ClickableItem )
480
void QwtLegendItem::setChecked(bool on)
482
if ( d_data->itemMode == QwtLegend::CheckableItem )
484
const bool isBlocked = signalsBlocked();
489
blockSignals(isBlocked);
493
bool QwtLegendItem::isChecked() const
495
return d_data->itemMode == QwtLegend::CheckableItem && isDown();
498
void QwtLegendItem::setDown(bool down)
500
if ( down == d_data->isDown )
503
d_data->isDown = down;
506
if ( d_data->itemMode == QwtLegend::ClickableItem )
508
if ( d_data->isDown )
517
if ( d_data->itemMode == QwtLegend::CheckableItem )
518
emit checked(d_data->isDown);
521
bool QwtLegendItem::isDown() const
523
return d_data->isDown;
526
QSize QwtLegendItem::sizeHint() const
528
QSize sz = QwtTextLabel::sizeHint();
529
if ( d_data->itemMode != QwtLegend::ReadOnlyItem )
530
sz += buttonShift(this);
535
void QwtLegendItem::drawText(QPainter *painter, const QRect &rect)
537
QwtTextLabel::drawText(painter, rect);