@@ -1,254 +1,255 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 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 "legendmarker_p.h" |
|
21 | #include "legendmarker_p.h" | |
22 | #include "qxyseries.h" |
|
22 | #include "qxyseries.h" | |
23 | #include "qxyseries_p.h" |
|
23 | #include "qxyseries_p.h" | |
24 | #include "qlegend.h" |
|
24 | #include "qlegend.h" | |
25 | #include "qabstractbarseries.h" |
|
25 | #include "qabstractbarseries.h" | |
26 | #include "qpieseries.h" |
|
26 | #include "qpieseries.h" | |
27 | #include "qpieslice.h" |
|
27 | #include "qpieslice.h" | |
28 | #include "qbarset.h" |
|
28 | #include "qbarset.h" | |
29 | #include "qbarset_p.h" |
|
29 | #include "qbarset_p.h" | |
30 | #include "qareaseries.h" |
|
30 | #include "qareaseries.h" | |
31 | #include "qareaseries_p.h" |
|
31 | #include "qareaseries_p.h" | |
32 | #include <QPainter> |
|
32 | #include <QPainter> | |
33 | #include <QGraphicsSceneEvent> |
|
33 | #include <QGraphicsSceneEvent> | |
34 | #include <QGraphicsSimpleTextItem> |
|
34 | #include <QGraphicsSimpleTextItem> | |
35 | #include <QGraphicsLayout> |
|
35 | #include <QGraphicsLayout> | |
36 |
|
36 | |||
37 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
37 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
38 |
|
38 | |||
39 | LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : |
|
39 | LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : | |
40 | QGraphicsObject(legend), |
|
40 | QGraphicsObject(legend), | |
41 | m_series(series), |
|
41 | m_series(series), | |
42 | m_markerRect(0,0,10.0,10.0), |
|
42 | m_markerRect(0,0,10.0,10.0), | |
43 | m_boundingRect(0,0,0,0), |
|
43 | m_boundingRect(0,0,0,0), | |
44 | m_legend(legend), |
|
44 | m_legend(legend), | |
45 | m_textItem(new QGraphicsSimpleTextItem(this)), |
|
45 | m_textItem(new QGraphicsSimpleTextItem(this)), | |
46 | m_rectItem(new QGraphicsRectItem(this)), |
|
46 | m_rectItem(new QGraphicsRectItem(this)), | |
47 | m_margin(4), |
|
47 | m_margin(4), | |
48 | m_space(4) |
|
48 | m_space(4) | |
49 | { |
|
49 | { | |
50 | //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
50 | //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); | |
51 | m_rectItem->setRect(m_markerRect); |
|
51 | m_rectItem->setRect(m_markerRect); | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | void LegendMarker::setPen(const QPen &pen) |
|
54 | void LegendMarker::setPen(const QPen &pen) | |
55 | { |
|
55 | { | |
56 | m_rectItem->setPen(pen); |
|
56 | m_rectItem->setPen(pen); | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | QPen LegendMarker::pen() const |
|
59 | QPen LegendMarker::pen() const | |
60 | { |
|
60 | { | |
61 | return m_rectItem->pen(); |
|
61 | return m_rectItem->pen(); | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | void LegendMarker::setBrush(const QBrush &brush) |
|
64 | void LegendMarker::setBrush(const QBrush &brush) | |
65 | { |
|
65 | { | |
66 | m_rectItem->setBrush(brush); |
|
66 | m_rectItem->setBrush(brush); | |
67 | } |
|
67 | } | |
68 |
|
68 | |||
69 | QBrush LegendMarker::brush() const |
|
69 | QBrush LegendMarker::brush() const | |
70 | { |
|
70 | { | |
71 | return m_rectItem->brush(); |
|
71 | return m_rectItem->brush(); | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | void LegendMarker::setFont(const QFont &font) |
|
74 | void LegendMarker::setFont(const QFont &font) | |
75 | { |
|
75 | { | |
76 | m_textItem->setFont(font); |
|
76 | m_textItem->setFont(font); | |
77 | QFontMetrics fn(font); |
|
77 | QFontMetrics fn(font); | |
78 | m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2); |
|
78 | m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2); | |
79 | updateGeometry(); |
|
79 | updateGeometry(); | |
80 | m_legend->layout()->invalidate(); |
|
80 | m_legend->layout()->invalidate(); | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | QFont LegendMarker::font() const |
|
83 | QFont LegendMarker::font() const | |
84 | { |
|
84 | { | |
85 | return m_textItem->font(); |
|
85 | return m_textItem->font(); | |
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 | void LegendMarker::setLabel(const QString label) |
|
88 | void LegendMarker::setLabel(const QString label) | |
89 | { |
|
89 | { | |
90 | m_text = label; |
|
90 | m_text = label; | |
91 | updateGeometry(); |
|
91 | updateGeometry(); | |
92 | m_legend->layout()->invalidate(); |
|
92 | m_legend->layout()->invalidate(); | |
93 | } |
|
93 | } | |
94 |
|
94 | |||
95 | QString LegendMarker::label() const |
|
95 | QString LegendMarker::label() const | |
96 | { |
|
96 | { | |
97 | return m_text; |
|
97 | return m_text; | |
98 | } |
|
98 | } | |
99 |
|
99 | |||
100 | QRectF LegendMarker::boundingRect() const |
|
100 | QRectF LegendMarker::boundingRect() const | |
101 | { |
|
101 | { | |
102 | return m_boundingRect; |
|
102 | return m_boundingRect; | |
103 | } |
|
103 | } | |
104 |
|
104 | |||
105 | void LegendMarker::setLabelBrush(const QBrush &brush) |
|
105 | void LegendMarker::setLabelBrush(const QBrush &brush) | |
106 | { |
|
106 | { | |
107 | m_textItem->setBrush(brush); |
|
107 | m_textItem->setBrush(brush); | |
108 | } |
|
108 | } | |
109 |
|
109 | |||
110 | QBrush LegendMarker::labelBrush() const |
|
110 | QBrush LegendMarker::labelBrush() const | |
111 | { |
|
111 | { | |
112 | return m_textItem->brush(); |
|
112 | return m_textItem->brush(); | |
113 | } |
|
113 | } | |
114 |
|
114 | |||
115 |
|
115 | |||
116 | void LegendMarker::setGeometry(const QRectF& rect) |
|
116 | void LegendMarker::setGeometry(const QRectF& rect) | |
117 | { |
|
117 | { | |
118 | QFontMetrics fn (font()); |
|
118 | QFontMetrics fn (font()); | |
119 |
|
119 | |||
120 | int width = rect.width(); |
|
120 | int width = rect.width(); | |
121 | qreal x = m_margin + m_markerRect.width() + m_space + m_margin; |
|
121 | qreal x = m_margin + m_markerRect.width() + m_space + m_margin; | |
122 | qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin); |
|
122 | qreal y = qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin); | |
123 |
|
123 | |||
124 | if (fn.boundingRect(m_text).width() + x > width) |
|
124 | if (fn.boundingRect(m_text).width() + x > width) | |
125 | { |
|
125 | { | |
126 | QString string = m_text + "..."; |
|
126 | QString string = m_text + "..."; | |
127 | while(fn.boundingRect(string).width() + x > width && string.length() > 3) |
|
127 | while(fn.boundingRect(string).width() + x > width && string.length() > 3) | |
128 | string.remove(string.length() - 4, 1); |
|
128 | string.remove(string.length() - 4, 1); | |
129 | m_textItem->setText(string); |
|
129 | m_textItem->setText(string); | |
130 | } |
|
130 | } | |
131 | else |
|
131 | else | |
132 | m_textItem->setText(m_text); |
|
132 | m_textItem->setText(m_text); | |
133 |
|
133 | |||
134 | const QRectF& textRect = m_textItem->boundingRect(); |
|
134 | const QRectF& textRect = m_textItem->boundingRect(); | |
135 |
|
135 | |||
136 |
|
136 | |||
137 | m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2); |
|
137 | m_textItem->setPos(x-m_margin,y/2 - textRect.height()/2); | |
138 | m_rectItem->setRect(m_markerRect); |
|
138 | m_rectItem->setRect(m_markerRect); | |
139 | m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2); |
|
139 | m_rectItem->setPos(m_margin,y/2 - m_markerRect.height()/2); | |
140 |
|
140 | |||
141 | prepareGeometryChange(); |
|
141 | prepareGeometryChange(); | |
142 | m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y); |
|
142 | m_boundingRect = QRectF(0,0,x+textRect.width()+m_margin,y); | |
143 | } |
|
143 | } | |
144 |
|
144 | |||
145 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
145 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
146 | { |
|
146 | { | |
147 | Q_UNUSED(option) |
|
147 | Q_UNUSED(option) | |
148 | Q_UNUSED(widget) |
|
148 | Q_UNUSED(widget) | |
149 | Q_UNUSED(painter) |
|
149 | Q_UNUSED(painter) | |
150 | } |
|
150 | } | |
151 |
|
151 | |||
152 | QSizeF LegendMarker::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const |
|
152 | QSizeF LegendMarker::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const | |
153 | { |
|
153 | { | |
154 | Q_UNUSED(constraint) |
|
154 | Q_UNUSED(constraint) | |
155 |
|
155 | |||
156 | QFontMetrics fn(m_textItem->font()); |
|
156 | QFontMetrics fn(m_textItem->font()); | |
157 | QSizeF sh; |
|
157 | QSizeF sh; | |
158 |
|
158 | |||
159 | switch (which) { |
|
159 | switch (which) { | |
160 | case Qt::MinimumSize: |
|
160 | case Qt::MinimumSize: | |
161 | 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)); |
|
161 | 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)); | |
162 | break; |
|
162 | break; | |
163 | case Qt::PreferredSize: |
|
163 | case Qt::PreferredSize: | |
164 | sh = QSizeF(fn.boundingRect(m_text).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); |
|
164 | sh = QSizeF(fn.boundingRect(m_text).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); | |
165 | break; |
|
165 | break; | |
166 | default: |
|
166 | default: | |
167 | break; |
|
167 | break; | |
168 | } |
|
168 | } | |
169 | return sh; |
|
169 | return sh; | |
170 | } |
|
170 | } | |
171 |
|
171 | |||
172 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
172 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) | |
173 | { |
|
173 | { | |
174 | QGraphicsObject::mousePressEvent(event); |
|
174 | QGraphicsObject::mousePressEvent(event); | |
175 | //TODO: selected signal removed for now |
|
175 | //TODO: selected signal removed for now | |
176 | } |
|
176 | } | |
177 |
|
177 | |||
178 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
178 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
179 |
|
179 | |||
180 | AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), |
|
180 | AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), | |
181 | m_series(series) |
|
181 | m_series(series) | |
182 | { |
|
182 | { | |
183 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); |
|
183 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | |
184 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); |
|
184 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); | |
185 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); |
|
185 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | |
186 | updated(); |
|
186 | updated(); | |
187 | } |
|
187 | } | |
188 |
|
188 | |||
189 | void AreaLegendMarker::updated() |
|
189 | void AreaLegendMarker::updated() | |
190 | { |
|
190 | { | |
191 | setBrush(m_series->brush()); |
|
191 | setBrush(m_series->brush()); | |
192 | setLabel(m_series->name()); |
|
192 | setLabel(m_series->name()); | |
193 | } |
|
193 | } | |
194 |
|
194 | |||
195 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
195 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
196 |
|
196 | |||
197 | BarLegendMarker::BarLegendMarker(QAbstractBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend), |
|
197 | BarLegendMarker::BarLegendMarker(QAbstractBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend), | |
198 | m_barset(barset) |
|
198 | m_barset(barset) | |
199 | { |
|
199 | { | |
200 | //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); |
|
200 | //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); | |
201 | QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); |
|
201 | QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); | |
|
202 | QObject::connect(barset, SIGNAL(labelChanged()), this, SLOT(updated())); | |||
202 | updated(); |
|
203 | updated(); | |
203 | } |
|
204 | } | |
204 |
|
205 | |||
205 | void BarLegendMarker::updated() |
|
206 | void BarLegendMarker::updated() | |
206 | { |
|
207 | { | |
207 | setBrush(m_barset->brush()); |
|
208 | setBrush(m_barset->brush()); | |
208 | setLabel(m_barset->label()); |
|
209 | setLabel(m_barset->label()); | |
209 | } |
|
210 | } | |
210 |
|
211 | |||
211 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
212 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
212 |
|
213 | |||
213 | PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), |
|
214 | PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), | |
214 | m_pieslice(pieslice) |
|
215 | m_pieslice(pieslice) | |
215 | { |
|
216 | { | |
216 | QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); |
|
217 | QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); | |
217 | QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); |
|
218 | QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); | |
218 | updated(); |
|
219 | updated(); | |
219 | } |
|
220 | } | |
220 |
|
221 | |||
221 | void PieLegendMarker::updated() |
|
222 | void PieLegendMarker::updated() | |
222 | { |
|
223 | { | |
223 | setBrush(m_pieslice->brush()); |
|
224 | setBrush(m_pieslice->brush()); | |
224 | setLabel(m_pieslice->label()); |
|
225 | setLabel(m_pieslice->label()); | |
225 | } |
|
226 | } | |
226 |
|
227 | |||
227 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
228 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
228 |
|
229 | |||
229 | XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend), |
|
230 | XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend), | |
230 | m_series(series) |
|
231 | m_series(series) | |
231 | { |
|
232 | { | |
232 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); |
|
233 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); | |
233 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); |
|
234 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); | |
234 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); |
|
235 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | |
235 | updated(); |
|
236 | updated(); | |
236 | } |
|
237 | } | |
237 |
|
238 | |||
238 | void XYLegendMarker::updated() |
|
239 | void XYLegendMarker::updated() | |
239 | { |
|
240 | { | |
240 | setLabel(m_series->name()); |
|
241 | setLabel(m_series->name()); | |
241 |
|
242 | |||
242 | if(m_series->type()== QAbstractSeries::SeriesTypeScatter) |
|
243 | if(m_series->type()== QAbstractSeries::SeriesTypeScatter) | |
243 | { |
|
244 | { | |
244 | setBrush(m_series->brush()); |
|
245 | setBrush(m_series->brush()); | |
245 |
|
246 | |||
246 | } |
|
247 | } | |
247 | else { |
|
248 | else { | |
248 | setBrush(QBrush(m_series->pen().color())); |
|
249 | setBrush(QBrush(m_series->pen().color())); | |
249 | } |
|
250 | } | |
250 | } |
|
251 | } | |
251 |
|
252 | |||
252 | #include "moc_legendmarker_p.cpp" |
|
253 | #include "moc_legendmarker_p.cpp" | |
253 |
|
254 | |||
254 | QTCOMMERCIALCHART_END_NAMESPACE |
|
255 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now