##// END OF EJS Templates
Fix legend markers truncation...
Miikka Heikkinen -
r2544:9f0ff996f528
parent child
Show More
@@ -1,181 +1,186
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QPainter>
21 #include <QPainter>
22 #include <QGraphicsSceneEvent>
22 #include <QGraphicsSceneEvent>
23 #include <QGraphicsTextItem>
23 #include <QGraphicsTextItem>
24
24
25 #include "qlegend.h"
25 #include "qlegend.h"
26 #include "qlegend_p.h"
26 #include "qlegend_p.h"
27 #include "qlegendmarker.h"
27 #include "qlegendmarker.h"
28 #include "qlegendmarker_p.h"
28 #include "qlegendmarker_p.h"
29 #include "legendmarkeritem_p.h"
29 #include "legendmarkeritem_p.h"
30 #include "chartpresenter_p.h"
30 #include "chartpresenter_p.h"
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
34 LegendMarkerItem::LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent) :
35 QGraphicsObject(parent),
35 QGraphicsObject(parent),
36 m_marker(marker),
36 m_marker(marker),
37 m_markerRect(0,0,10.0,10.0),
37 m_markerRect(0,0,10.0,10.0),
38 m_boundingRect(0,0,0,0),
38 m_boundingRect(0,0,0,0),
39 m_textItem(new QGraphicsTextItem(this)),
39 m_textItem(new QGraphicsTextItem(this)),
40 m_rectItem(new QGraphicsRectItem(this)),
40 m_rectItem(new QGraphicsRectItem(this)),
41 m_margin(4),
41 m_margin(4),
42 m_space(4),
42 m_space(4),
43 m_hovering(false),
43 m_hovering(false),
44 m_pressPos(0, 0)
44 m_pressPos(0, 0)
45 {
45 {
46 m_rectItem->setRect(m_markerRect);
46 m_rectItem->setRect(m_markerRect);
47 setAcceptHoverEvents(true);
47 setAcceptHoverEvents(true);
48 }
48 }
49
49
50 LegendMarkerItem::~LegendMarkerItem()
50 LegendMarkerItem::~LegendMarkerItem()
51 {
51 {
52 if (m_hovering) {
52 if (m_hovering) {
53 emit m_marker->q_ptr->hovered(false);
53 emit m_marker->q_ptr->hovered(false);
54 }
54 }
55 }
55 }
56
56
57 void LegendMarkerItem::setPen(const QPen &pen)
57 void LegendMarkerItem::setPen(const QPen &pen)
58 {
58 {
59 m_rectItem->setPen(pen);
59 m_rectItem->setPen(pen);
60 }
60 }
61
61
62 QPen LegendMarkerItem::pen() const
62 QPen LegendMarkerItem::pen() const
63 {
63 {
64 return m_rectItem->pen();
64 return m_rectItem->pen();
65 }
65 }
66
66
67 void LegendMarkerItem::setBrush(const QBrush &brush)
67 void LegendMarkerItem::setBrush(const QBrush &brush)
68 {
68 {
69 m_rectItem->setBrush(brush);
69 m_rectItem->setBrush(brush);
70 }
70 }
71
71
72 QBrush LegendMarkerItem::brush() const
72 QBrush LegendMarkerItem::brush() const
73 {
73 {
74 return m_rectItem->brush();
74 return m_rectItem->brush();
75 }
75 }
76
76
77 void LegendMarkerItem::setFont(const QFont &font)
77 void LegendMarkerItem::setFont(const QFont &font)
78 {
78 {
79 m_textItem->setFont(font);
79 m_textItem->setFont(font);
80 QFontMetrics fn(font);
80 QFontMetrics fn(font);
81 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
81 m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2);
82 updateGeometry();
82 updateGeometry();
83 }
83 }
84
84
85 QFont LegendMarkerItem::font() const
85 QFont LegendMarkerItem::font() const
86 {
86 {
87 return m_textItem->font();
87 return m_textItem->font();
88 }
88 }
89
89
90 void LegendMarkerItem::setLabel(const QString label)
90 void LegendMarkerItem::setLabel(const QString label)
91 {
91 {
92 m_label = label;
92 m_label = label;
93 updateGeometry();
93 updateGeometry();
94 }
94 }
95
95
96 QString LegendMarkerItem::label() const
96 QString LegendMarkerItem::label() const
97 {
97 {
98 return m_label;
98 return m_label;
99 }
99 }
100
100
101 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
101 void LegendMarkerItem::setLabelBrush(const QBrush &brush)
102 {
102 {
103 m_textItem->setDefaultTextColor(brush.color());
103 m_textItem->setDefaultTextColor(brush.color());
104 }
104 }
105
105
106 QBrush LegendMarkerItem::labelBrush() const
106 QBrush LegendMarkerItem::labelBrush() const
107 {
107 {
108 return QBrush(m_textItem->defaultTextColor());
108 return QBrush(m_textItem->defaultTextColor());
109 }
109 }
110
110
111 void LegendMarkerItem::setGeometry(const QRectF &rect)
111 void LegendMarkerItem::setGeometry(const QRectF &rect)
112 {
112 {
113 int width = rect.width();
113 qreal width = rect.width();
114 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
114 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
115 QRectF truncatedRect;
115 QRectF truncatedRect;
116
116
117 m_textItem->setHtml(ChartPresenter::truncatedText(m_font, m_label, qreal(0.0), width - x, Qt::Horizontal, truncatedRect));
117 m_textItem->setHtml(ChartPresenter::truncatedText(m_textItem->font(), m_label, qreal(0.0), width - x, Qt::Horizontal, truncatedRect));
118
118
119 qreal y = qMax(m_markerRect.height() + 2 * m_margin, truncatedRect.height() + 2 * m_margin);
119 qreal y = qMax(m_markerRect.height() + 2 * m_margin, truncatedRect.height() + 2 * m_margin);
120
120
121 const QRectF &textRect = m_textItem->boundingRect();
121 const QRectF &textRect = m_textItem->boundingRect();
122
122
123 m_textItem->setPos(x - m_margin, y / 2 - textRect.height() / 2);
123 m_textItem->setPos(x - m_margin, y / 2 - textRect.height() / 2);
124 m_rectItem->setRect(m_markerRect);
124 m_rectItem->setRect(m_markerRect);
125 m_rectItem->setPos(m_margin, y / 2 - m_markerRect.height() / 2);
125 m_rectItem->setPos(m_margin, y / 2 - m_markerRect.height() / 2);
126
126
127 prepareGeometryChange();
127 prepareGeometryChange();
128 m_boundingRect = QRectF(0, 0, x + textRect.width() + m_margin, y);
128 m_boundingRect = QRectF(0, 0, x + textRect.width() + m_margin, y);
129 }
129 }
130
130
131 QRectF LegendMarkerItem::boundingRect() const
131 QRectF LegendMarkerItem::boundingRect() const
132 {
132 {
133 return m_boundingRect;
133 return m_boundingRect;
134 }
134 }
135
135
136 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
136 void LegendMarkerItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
137 {
137 {
138 Q_UNUSED(option)
138 Q_UNUSED(option)
139 Q_UNUSED(widget)
139 Q_UNUSED(widget)
140 Q_UNUSED(painter)
140 Q_UNUSED(painter)
141 }
141 }
142
142
143 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
143 QSizeF LegendMarkerItem::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
144 {
144 {
145 Q_UNUSED(constraint)
145 Q_UNUSED(constraint)
146
146
147 QFontMetrics fn(m_textItem->font());
148 QSizeF sh;
147 QSizeF sh;
149
148
150 switch (which) {
149 switch (which) {
151 case Qt::MinimumSize:
150 case Qt::MinimumSize: {
152 sh = QSizeF(fn.boundingRect("...").width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
151 QRectF labelRect = ChartPresenter::textBoundingRect(m_textItem->font(), "...");
153 break;
152 sh = QSizeF(labelRect.width() + (2.0 * m_margin) + m_space + m_markerRect.width(),
154 case Qt::PreferredSize:
153 qMax(m_markerRect.height(), labelRect.height()) + (2.0 * m_margin));
155 sh = QSizeF(fn.boundingRect(m_label).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
154 break;
156 break;
155 }
157 default:
156 case Qt::PreferredSize: {
158 break;
157 QRectF labelRect = ChartPresenter::textBoundingRect(m_textItem->font(), m_label);
159 }
158 sh = QSizeF(labelRect.width() + (2.0 * m_margin) + m_space + m_markerRect.width(),
160
159 qMax(m_markerRect.height(), labelRect.height()) + (2.0 * m_margin));
161 return sh;
160 break;
161 }
162 default:
163 break;
164 }
165
166 return sh;
162 }
167 }
163
168
164 void LegendMarkerItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
169 void LegendMarkerItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
165 {
170 {
166 Q_UNUSED(event)
171 Q_UNUSED(event)
167 m_hovering = true;
172 m_hovering = true;
168 emit m_marker->q_ptr->hovered(true);
173 emit m_marker->q_ptr->hovered(true);
169 }
174 }
170
175
171 void LegendMarkerItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
176 void LegendMarkerItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
172 {
177 {
173 Q_UNUSED(event)
178 Q_UNUSED(event)
174 m_hovering = false;
179 m_hovering = false;
175 emit m_marker->q_ptr->hovered(false);
180 emit m_marker->q_ptr->hovered(false);
176 }
181 }
177
182
178
183
179 #include "moc_legendmarkeritem_p.cpp"
184 #include "moc_legendmarkeritem_p.cpp"
180
185
181 QTCOMMERCIALCHART_END_NAMESPACE
186 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,102 +1,101
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef LEGENDMARKERITEM_P_H
30 #ifndef LEGENDMARKERITEM_P_H
31 #define LEGENDMARKERITEM_P_H
31 #define LEGENDMARKERITEM_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QGraphicsObject>
34 #include <QGraphicsObject>
35 #include <QFont>
35 #include <QFont>
36 #include <QBrush>
36 #include <QBrush>
37 #include <QPen>
37 #include <QPen>
38 #include <QGraphicsTextItem>
38 #include <QGraphicsTextItem>
39 #include <QGraphicsLayoutItem>
39 #include <QGraphicsLayoutItem>
40
40
41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
42
42
43 class QLegendMarkerPrivate;
43 class QLegendMarkerPrivate;
44
44
45 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem
45 class LegendMarkerItem : public QGraphicsObject, public QGraphicsLayoutItem
46 {
46 {
47 Q_OBJECT
47 Q_OBJECT
48 Q_INTERFACES(QGraphicsLayoutItem)
48 Q_INTERFACES(QGraphicsLayoutItem)
49 public:
49 public:
50 explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0);
50 explicit LegendMarkerItem(QLegendMarkerPrivate *marker, QGraphicsObject *parent = 0);
51 ~LegendMarkerItem();
51 ~LegendMarkerItem();
52
52
53 void setPen(const QPen &pen);
53 void setPen(const QPen &pen);
54 QPen pen() const;
54 QPen pen() const;
55
55
56 void setBrush(const QBrush &brush);
56 void setBrush(const QBrush &brush);
57 QBrush brush() const;
57 QBrush brush() const;
58
58
59 void setFont(const QFont &font);
59 void setFont(const QFont &font);
60 QFont font() const;
60 QFont font() const;
61
61
62 void setLabel(const QString label);
62 void setLabel(const QString label);
63 QString label() const;
63 QString label() const;
64
64
65 void setLabelBrush(const QBrush &brush);
65 void setLabelBrush(const QBrush &brush);
66 QBrush labelBrush() const;
66 QBrush labelBrush() const;
67
67
68 void setGeometry(const QRectF &rect);
68 void setGeometry(const QRectF &rect);
69 QRectF boundingRect() const;
69 QRectF boundingRect() const;
70
70
71 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
71 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
72 QSizeF sizeHint (Qt::SizeHint which, const QSizeF &constraint) const;
72 QSizeF sizeHint (Qt::SizeHint which, const QSizeF &constraint) const;
73
73
74 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
74 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
75 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
75 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
76
76
77 protected:
77 protected:
78 QLegendMarkerPrivate *m_marker; // Knows
78 QLegendMarkerPrivate *m_marker; // Knows
79 QRectF m_markerRect;
79 QRectF m_markerRect;
80 QRectF m_boundingRect;
80 QRectF m_boundingRect;
81 QGraphicsTextItem *m_textItem;
81 QGraphicsTextItem *m_textItem;
82 QGraphicsRectItem *m_rectItem;
82 QGraphicsRectItem *m_rectItem;
83 qreal m_margin;
83 qreal m_margin;
84 qreal m_space;
84 qreal m_space;
85 QString m_label;
85 QString m_label;
86
86
87 QBrush m_labelBrush;
87 QBrush m_labelBrush;
88 QFont m_font;
89 QPen m_pen;
88 QPen m_pen;
90 QBrush m_brush;
89 QBrush m_brush;
91 bool m_hovering;
90 bool m_hovering;
92
91
93 QPointF m_pressPos;
92 QPointF m_pressPos;
94
93
95 friend class QLegendMarker;
94 friend class QLegendMarker;
96 friend class QLegendMarkerPrivate;
95 friend class QLegendMarkerPrivate;
97 friend class LegendLayout;
96 friend class LegendLayout;
98 };
97 };
99
98
100 QTCOMMERCIALCHART_END_NAMESPACE
99 QTCOMMERCIALCHART_END_NAMESPACE
101
100
102 #endif // LEGENDMARKERITEM_P_H
101 #endif // LEGENDMARKERITEM_P_H
General Comments 0
You need to be logged in to leave comments. Login now