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