@@ -1,195 +1,198 | |||
|
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 "qbarseries.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 <QDebug> |
|
36 | 36 | |
|
37 | 37 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
38 | 38 | |
|
39 |
LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : |
|
|
40 | m_series(series), | |
|
41 | m_markerRect(0,0,10.0,10.0), | |
|
42 | m_boundingRect(0,0,0,0), | |
|
43 | m_legend(legend), | |
|
44 | m_textItem(new QGraphicsSimpleTextItem(this)), | |
|
45 |
|
|
|
39 | LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : | |
|
40 | QGraphicsObject(legend), | |
|
41 | m_series(series), | |
|
42 | m_markerRect(0,0,10.0,10.0), | |
|
43 | m_boundingRect(0,0,0,0), | |
|
44 | m_legend(legend), | |
|
45 | m_textItem(new QGraphicsSimpleTextItem(this)), | |
|
46 | m_rectItem(new QGraphicsRectItem(this)) | |
|
46 | 47 | { |
|
47 | 48 | //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
48 | 49 | m_rectItem->setRect(m_markerRect); |
|
49 | 50 | updateLayout(); |
|
50 | 51 | } |
|
51 | 52 | |
|
52 | 53 | void LegendMarker::setPen(const QPen &pen) |
|
53 | 54 | { |
|
54 | 55 | m_textItem->setPen(pen); |
|
55 | 56 | updateLayout(); |
|
56 | 57 | } |
|
57 | 58 | |
|
58 | 59 | QPen LegendMarker::pen() const |
|
59 | 60 | { |
|
60 | 61 | return m_textItem->pen(); |
|
61 | 62 | } |
|
62 | 63 | |
|
63 | 64 | void LegendMarker::setBrush(const QBrush &brush) |
|
64 | 65 | { |
|
65 | 66 | m_rectItem->setBrush(brush); |
|
66 | 67 | } |
|
67 | 68 | |
|
68 | 69 | QBrush LegendMarker::brush() const |
|
69 | 70 | { |
|
70 | 71 | return m_rectItem->brush(); |
|
71 | 72 | } |
|
72 | 73 | |
|
73 | 74 | void LegendMarker::setLabel(const QString name) |
|
74 | 75 | { |
|
75 | 76 | m_textItem->setText(name); |
|
76 | 77 | updateLayout(); |
|
77 | 78 | } |
|
78 | 79 | |
|
79 | 80 | void LegendMarker::setSize(const QSize& size) |
|
80 | 81 | { |
|
81 | 82 | m_markerRect = QRectF(0,0,size.width(),size.height()); |
|
82 | 83 | } |
|
83 | 84 | |
|
84 | 85 | QString LegendMarker::label() const |
|
85 | 86 | { |
|
86 | 87 | return m_textItem->text(); |
|
87 | 88 | } |
|
88 | 89 | |
|
89 | 90 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
90 | 91 | { |
|
91 | 92 | Q_UNUSED(option) |
|
92 | 93 | Q_UNUSED(widget) |
|
93 | 94 | Q_UNUSED(painter) |
|
94 | 95 | } |
|
95 | 96 | |
|
96 | 97 | QRectF LegendMarker::boundingRect() const |
|
97 | 98 | { |
|
98 | 99 | return m_boundingRect; |
|
99 | 100 | } |
|
100 | 101 | |
|
101 | 102 | void LegendMarker::updateLayout() |
|
102 | 103 | { |
|
103 | 104 | |
|
104 | 105 | static const qreal margin = 2; |
|
105 | 106 | static const qreal space = 4; |
|
106 | 107 | |
|
107 | 108 | const QRectF& textRect = m_textItem->boundingRect(); |
|
108 | 109 | prepareGeometryChange(); |
|
109 | 110 | m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin)); |
|
110 | 111 | m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2); |
|
111 | 112 | m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2); |
|
112 | 113 | |
|
113 | 114 | } |
|
114 | 115 | |
|
115 | 116 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
116 | 117 | { |
|
117 | 118 | QGraphicsObject::mousePressEvent(event); |
|
118 | 119 | qDebug()<<"Not implemented"; //TODO: selected signal removed for now |
|
119 | 120 | } |
|
120 | 121 | |
|
121 | 122 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
122 | 123 | |
|
123 | 124 | AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), |
|
124 | 125 | m_series(series) |
|
125 | 126 | { |
|
126 | 127 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); |
|
127 | 128 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); |
|
129 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | |
|
128 | 130 | updated(); |
|
129 | 131 | } |
|
130 | 132 | |
|
131 | 133 | void AreaLegendMarker::updated() |
|
132 | 134 | { |
|
133 | 135 | setBrush(m_series->brush()); |
|
134 | 136 | setLabel(m_series->name()); |
|
135 | 137 | } |
|
136 | 138 | |
|
137 | 139 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
138 | 140 | |
|
139 | 141 | BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend), |
|
140 | 142 | m_barset(barset) |
|
141 | 143 | { |
|
142 | 144 | //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); |
|
143 | 145 | QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); |
|
144 | 146 | updated(); |
|
145 | 147 | } |
|
146 | 148 | |
|
147 | 149 | void BarLegendMarker::updated() |
|
148 | 150 | { |
|
149 | 151 | setBrush(m_barset->brush()); |
|
150 | 152 | setLabel(m_barset->name()); |
|
151 | 153 | } |
|
152 | 154 | |
|
153 | 155 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
154 | 156 | |
|
155 | 157 | PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), |
|
156 | 158 | m_pieslice(pieslice) |
|
157 | 159 | { |
|
158 | 160 | QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); |
|
159 | 161 | QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); |
|
160 | 162 | updated(); |
|
161 | 163 | } |
|
162 | 164 | |
|
163 | 165 | void PieLegendMarker::updated() |
|
164 | 166 | { |
|
165 | 167 | setBrush(m_pieslice->brush()); |
|
166 | 168 | setLabel(m_pieslice->label()); |
|
167 | 169 | } |
|
168 | 170 | |
|
169 | 171 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
170 | 172 | |
|
171 | 173 | XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend), |
|
172 | 174 | m_series(series) |
|
173 | 175 | { |
|
174 | 176 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); |
|
175 | 177 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); |
|
178 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); | |
|
176 | 179 | updated(); |
|
177 | 180 | } |
|
178 | 181 | |
|
179 | 182 | void XYLegendMarker::updated() |
|
180 | 183 | { |
|
181 | 184 | setLabel(m_series->name()); |
|
182 | 185 | |
|
183 | 186 | if(m_series->type()== QAbstractSeries::SeriesTypeScatter) |
|
184 | 187 | { |
|
185 | 188 | setBrush(m_series->brush()); |
|
186 | 189 | |
|
187 | 190 | } |
|
188 | 191 | else { |
|
189 | 192 | setBrush(QBrush(m_series->pen().color())); |
|
190 | 193 | } |
|
191 | 194 | } |
|
192 | 195 | |
|
193 | 196 | #include "moc_legendmarker_p.cpp" |
|
194 | 197 | |
|
195 | 198 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,478 +1,476 | |||
|
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 "qlegend.h" |
|
22 | 22 | #include "qlegend_p.h" |
|
23 | 23 | #include "qabstractseries.h" |
|
24 | 24 | #include "qabstractseries_p.h" |
|
25 | 25 | #include "qchart_p.h" |
|
26 | 26 | |
|
27 | 27 | #include "legendmarker_p.h" |
|
28 | 28 | #include "qxyseries.h" |
|
29 | 29 | #include "qlineseries.h" |
|
30 | 30 | #include "qareaseries.h" |
|
31 | 31 | #include "qscatterseries.h" |
|
32 | 32 | #include "qsplineseries.h" |
|
33 | 33 | #include "qbarseries.h" |
|
34 | 34 | #include "qstackedbarseries.h" |
|
35 | 35 | #include "qpercentbarseries.h" |
|
36 | 36 | #include "qbarset.h" |
|
37 | 37 | #include "qpieseries.h" |
|
38 | 38 | #include "qpieseries_p.h" |
|
39 | 39 | #include "qpieslice.h" |
|
40 | 40 | #include "chartpresenter_p.h" |
|
41 | 41 | #include <QPainter> |
|
42 | 42 | #include <QPen> |
|
43 | 43 | #include <QTimer> |
|
44 | 44 | |
|
45 | 45 | #include <QGraphicsSceneEvent> |
|
46 | 46 | |
|
47 | 47 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
48 | 48 | |
|
49 | 49 | /*! |
|
50 | 50 | \class QLegend |
|
51 | 51 | \brief part of QtCommercial chart API. |
|
52 | 52 | \mainclass |
|
53 | 53 | |
|
54 | 54 | QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when |
|
55 | 55 | series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and |
|
56 | 56 | handle the drawing manually. |
|
57 | 57 | User isn't supposed to create or delete legend objects, but can reference it via QChart class. |
|
58 | 58 | |
|
59 | 59 | \image examples_percentbarchart_legend.png |
|
60 | 60 | |
|
61 | 61 | \sa QChart |
|
62 | 62 | */ |
|
63 | 63 | |
|
64 | 64 | /*! |
|
65 | 65 | \enum QLegend::Alignment |
|
66 | 66 | |
|
67 | 67 | This enum describes the possible position for legend inside chart. |
|
68 | 68 | |
|
69 | 69 | \value AlignmentTop |
|
70 | 70 | \value AlignmentBottom |
|
71 | 71 | \value AlignmentLeft |
|
72 | 72 | \value AlignmentRight |
|
73 | 73 | */ |
|
74 | 74 | |
|
75 | 75 | /*! |
|
76 | 76 | \fn qreal QLegend::minWidth() const |
|
77 | 77 | Returns minimum width of the legend |
|
78 | 78 | */ |
|
79 | 79 | |
|
80 | 80 | /*! |
|
81 | 81 | \fn qreal QLegend::minHeight() const |
|
82 | 82 | Returns minimum height of the legend |
|
83 | 83 | */ |
|
84 | 84 | |
|
85 | 85 | /*! |
|
86 | 86 | Constructs the legend object and sets the parent to \a parent |
|
87 | 87 | */ |
|
88 | 88 | |
|
89 | 89 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), |
|
90 | 90 | d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this)) |
|
91 | 91 | { |
|
92 | 92 | setZValue(ChartPresenter::LegendZValue); |
|
93 | 93 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
94 | 94 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); |
|
95 | 95 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*))); |
|
96 | 96 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | 99 | /*! |
|
100 | 100 | Destroys the legend object. Legend is always owned by a QChart, so an application should never call this. |
|
101 | 101 | */ |
|
102 | 102 | QLegend::~QLegend() |
|
103 | 103 | { |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | /*! |
|
107 | 107 | Paints the legend to given \a painter. Paremeters \a option and \a widget arent used. |
|
108 | 108 | */ |
|
109 | 109 | |
|
110 | 110 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
111 | 111 | { |
|
112 | 112 | Q_UNUSED(option) |
|
113 | 113 | Q_UNUSED(widget) |
|
114 | 114 | if(!d_ptr->m_backgroundVisible) return; |
|
115 | 115 | |
|
116 | 116 | painter->setOpacity(opacity()); |
|
117 | 117 | painter->setPen(d_ptr->m_pen); |
|
118 | 118 | painter->setBrush(d_ptr->m_brush); |
|
119 | 119 | painter->drawRect(boundingRect()); |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | /*! |
|
123 | 123 | Bounding rect of legend. |
|
124 | 124 | */ |
|
125 | 125 | |
|
126 | 126 | QRectF QLegend::boundingRect() const |
|
127 | 127 | { |
|
128 | 128 | return d_ptr->m_rect; |
|
129 | 129 | } |
|
130 | 130 | |
|
131 | 131 | /*! |
|
132 | 132 | Sets the \a brush of legend. Brush affects the background of legend. |
|
133 | 133 | */ |
|
134 | 134 | void QLegend::setBrush(const QBrush &brush) |
|
135 | 135 | { |
|
136 | 136 | if (d_ptr->m_brush != brush) { |
|
137 | 137 | d_ptr->m_brush = brush; |
|
138 | 138 | update(); |
|
139 | 139 | } |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | /*! |
|
143 | 143 | Returns the brush used by legend. |
|
144 | 144 | */ |
|
145 | 145 | QBrush QLegend::brush() const |
|
146 | 146 | { |
|
147 | 147 | return d_ptr->m_brush; |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | /*! |
|
151 | 151 | Sets the \a pen of legend. Pen affects the legend borders. |
|
152 | 152 | */ |
|
153 | 153 | void QLegend::setPen(const QPen &pen) |
|
154 | 154 | { |
|
155 | 155 | if (d_ptr->m_pen != pen) { |
|
156 | 156 | d_ptr->m_pen = pen; |
|
157 | 157 | update(); |
|
158 | 158 | } |
|
159 | 159 | } |
|
160 | 160 | |
|
161 | 161 | /*! |
|
162 | 162 | Returns the pen used by legend |
|
163 | 163 | */ |
|
164 | 164 | |
|
165 | 165 | QPen QLegend::pen() const |
|
166 | 166 | { |
|
167 | 167 | return d_ptr->m_pen; |
|
168 | 168 | } |
|
169 | 169 | |
|
170 | 170 | /*! |
|
171 | 171 | Sets the \a alignment for legend. Legend tries to paint itself on the defined position in chart. |
|
172 | 172 | \sa QLegend::Alignment |
|
173 | 173 | */ |
|
174 | 174 | void QLegend::setAlignment(QLegend::Alignments alignment) |
|
175 | 175 | { |
|
176 | 176 | if(d_ptr->m_alignment!=alignment) { |
|
177 | 177 | d_ptr->m_alignment = alignment; |
|
178 | 178 | d_ptr->updateLayout(); |
|
179 | 179 | } |
|
180 | 180 | } |
|
181 | 181 | |
|
182 | 182 | /*! |
|
183 | 183 | Returns the preferred layout for legend |
|
184 | 184 | */ |
|
185 | 185 | QLegend::Alignments QLegend::alignment() const |
|
186 | 186 | { |
|
187 | 187 | return d_ptr->m_alignment; |
|
188 | 188 | } |
|
189 | 189 | |
|
190 | 190 | /*! |
|
191 | 191 | Detaches the legend from chart. Chart won't change layout of the legend. |
|
192 | 192 | */ |
|
193 | 193 | void QLegend::detachFromChart() |
|
194 | 194 | { |
|
195 | 195 | d_ptr->m_attachedToChart = false; |
|
196 | 196 | } |
|
197 | 197 | |
|
198 | 198 | /*! |
|
199 | 199 | Attaches the legend to chart. Chart may change layout of the legend. |
|
200 | 200 | */ |
|
201 | 201 | void QLegend::attachToChart() |
|
202 | 202 | { |
|
203 | 203 | d_ptr->attachToChart(); |
|
204 | 204 | } |
|
205 | 205 | |
|
206 | 206 | /*! |
|
207 | 207 | Returns true, if legend is attached to chart. |
|
208 | 208 | */ |
|
209 | 209 | bool QLegend::isAttachedToChart() |
|
210 | 210 | { |
|
211 | 211 | return d_ptr->m_attachedToChart; |
|
212 | 212 | } |
|
213 | 213 | |
|
214 | 214 | /*! |
|
215 | 215 | Sets the legend's scrolling offset to value defined by \a point. |
|
216 | 216 | */ |
|
217 | 217 | void QLegend::setOffset(const QPointF& point) |
|
218 | 218 | { |
|
219 | 219 | d_ptr->setOffset(point.x(),point.y()); |
|
220 | 220 | } |
|
221 | 221 | |
|
222 | 222 | /*! |
|
223 | 223 | Returns the legend's scrolling offset. |
|
224 | 224 | */ |
|
225 | 225 | QPointF QLegend::offset() const |
|
226 | 226 | { |
|
227 | 227 | return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY); |
|
228 | 228 | } |
|
229 | 229 | |
|
230 | 230 | /*! |
|
231 | 231 | Sets the visibility of legend background to \a visible |
|
232 | 232 | */ |
|
233 | 233 | void QLegend::setBackgroundVisible(bool visible) |
|
234 | 234 | { |
|
235 | 235 | if(d_ptr->m_backgroundVisible!=visible) |
|
236 | 236 | { |
|
237 | 237 | d_ptr->m_backgroundVisible=visible; |
|
238 | 238 | update(); |
|
239 | 239 | } |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | /*! |
|
243 | 243 | Returns the visibility of legend background |
|
244 | 244 | */ |
|
245 | 245 | bool QLegend::isBackgroundVisible() const |
|
246 | 246 | { |
|
247 | 247 | return d_ptr->m_backgroundVisible; |
|
248 | 248 | } |
|
249 | 249 | |
|
250 | 250 | /*! |
|
251 | 251 | \internal \a event see QGraphicsWidget for details |
|
252 | 252 | */ |
|
253 | 253 | void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
254 | 254 | { |
|
255 | 255 | const QRectF& rect = QRectF(QPoint(0,0),event->newSize()); |
|
256 | 256 | QGraphicsWidget::resizeEvent(event); |
|
257 | 257 | if(d_ptr->m_rect != rect) { |
|
258 | 258 | d_ptr->m_rect = rect; |
|
259 | 259 | d_ptr->updateLayout(); |
|
260 | 260 | } |
|
261 | 261 | } |
|
262 | 262 | |
|
263 | 263 | /*! |
|
264 | 264 | \internal \a event see QGraphicsWidget for details |
|
265 | 265 | */ |
|
266 | 266 | void QLegend::hideEvent(QHideEvent *event) |
|
267 | 267 | { |
|
268 | 268 | QGraphicsWidget::hideEvent(event); |
|
269 | 269 | setEnabled(false); |
|
270 | 270 | d_ptr->updateLayout(); |
|
271 | 271 | } |
|
272 | 272 | |
|
273 | 273 | /*! |
|
274 | 274 | \internal \a event see QGraphicsWidget for details |
|
275 | 275 | */ |
|
276 | 276 | void QLegend::showEvent(QShowEvent *event) |
|
277 | 277 | { |
|
278 | 278 | QGraphicsWidget::showEvent(event); |
|
279 | 279 | setEnabled(true); |
|
280 | 280 | d_ptr->updateLayout(); |
|
281 | 281 | } |
|
282 | 282 | |
|
283 | 283 | qreal QLegend::minWidth() const |
|
284 | 284 | { |
|
285 | 285 | return d_ptr->m_minWidth; |
|
286 | 286 | } |
|
287 | 287 | |
|
288 | 288 | qreal QLegend::minHeight() const |
|
289 | 289 | { |
|
290 | 290 | return d_ptr->m_minHeight; |
|
291 | 291 | } |
|
292 | 292 | |
|
293 | 293 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
294 | 294 | |
|
295 | 295 | QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q): |
|
296 | 296 | q_ptr(q), |
|
297 | 297 | m_presenter(presenter), |
|
298 | 298 | m_chart(chart), |
|
299 | 299 | m_markers(new QGraphicsItemGroup(q)), |
|
300 | 300 | m_alignment(QLegend::AlignmentTop), |
|
301 | 301 | m_offsetX(0), |
|
302 | 302 | m_offsetY(0), |
|
303 | 303 | m_minWidth(0), |
|
304 | 304 | m_minHeight(0), |
|
305 | 305 | m_width(0), |
|
306 | 306 | m_height(0), |
|
307 | 307 | m_attachedToChart(true), |
|
308 | 308 | m_backgroundVisible(false) |
|
309 | 309 | { |
|
310 | 310 | |
|
311 | 311 | } |
|
312 | 312 | |
|
313 | 313 | QLegendPrivate::~QLegendPrivate() |
|
314 | 314 | { |
|
315 | 315 | |
|
316 | 316 | } |
|
317 | 317 | |
|
318 | 318 | void QLegendPrivate::setOffset(qreal x, qreal y) |
|
319 | 319 | { |
|
320 | 320 | |
|
321 | 321 | switch(m_alignment) { |
|
322 | 322 | |
|
323 | 323 | case QLegend::AlignmentTop: |
|
324 | 324 | case QLegend::AlignmentBottom: { |
|
325 | 325 | if(m_width<=m_rect.width()) return; |
|
326 | 326 | |
|
327 | 327 | if (x != m_offsetX) { |
|
328 | 328 | m_offsetX = qBound(qreal(0), x, m_width - m_rect.width()); |
|
329 | 329 | m_markers->setPos(-m_offsetX,m_rect.top()); |
|
330 | 330 | } |
|
331 | 331 | break; |
|
332 | 332 | } |
|
333 | 333 | case QLegend::AlignmentLeft: |
|
334 | 334 | case QLegend::AlignmentRight: { |
|
335 | 335 | |
|
336 | 336 | if(m_height<=m_rect.height()) return; |
|
337 | 337 | |
|
338 | 338 | if (y != m_offsetY) { |
|
339 | 339 | m_offsetY = qBound(qreal(0), y, m_height - m_rect.height()); |
|
340 | 340 | m_markers->setPos(m_rect.left(),-m_offsetY); |
|
341 | 341 | } |
|
342 | 342 | break; |
|
343 | 343 | } |
|
344 | 344 | } |
|
345 | 345 | } |
|
346 | 346 | |
|
347 | 347 | |
|
348 | 348 | void QLegendPrivate::updateLayout() |
|
349 | 349 | { |
|
350 | 350 | m_offsetX=0; |
|
351 | 351 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
352 | 352 | |
|
353 | 353 | if(items.isEmpty()) return; |
|
354 | 354 | |
|
355 | 355 | m_minWidth=0; |
|
356 | 356 | m_minHeight=0; |
|
357 | 357 | |
|
358 | 358 | switch(m_alignment) { |
|
359 | 359 | |
|
360 | 360 | case QLegend::AlignmentTop: |
|
361 | 361 | case QLegend::AlignmentBottom: { |
|
362 | 362 | QPointF point = m_rect.topLeft(); |
|
363 | 363 | m_width = 0; |
|
364 | 364 | foreach (QGraphicsItem *item, items) { |
|
365 | 365 | item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2); |
|
366 | 366 | const QRectF& rect = item->boundingRect(); |
|
367 | 367 | qreal w = rect.width(); |
|
368 | 368 | m_minWidth=qMax(m_minWidth,w); |
|
369 | 369 | m_minHeight=qMax(m_minHeight,rect.height()); |
|
370 | 370 | m_width+=w; |
|
371 | 371 | point.setX(point.x() + w); |
|
372 | 372 | } |
|
373 | 373 | if(m_width<m_rect.width()) { |
|
374 | 374 | m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top()); |
|
375 | 375 | } |
|
376 | 376 | else { |
|
377 | 377 | m_markers->setPos(m_rect.topLeft()); |
|
378 | 378 | } |
|
379 | 379 | m_height=m_minHeight; |
|
380 | 380 | } |
|
381 | 381 | break; |
|
382 | 382 | case QLegend::AlignmentLeft: |
|
383 | 383 | case QLegend::AlignmentRight: { |
|
384 | 384 | QPointF point = m_rect.topLeft(); |
|
385 | 385 | m_height = 0; |
|
386 | 386 | foreach (QGraphicsItem *item, items) { |
|
387 | 387 | item->setPos(point); |
|
388 | 388 | const QRectF& rect = item->boundingRect(); |
|
389 | 389 | qreal h = rect.height(); |
|
390 | 390 | m_minWidth=qMax(m_minWidth,rect.width()); |
|
391 | 391 | m_minHeight=qMax(m_minHeight,h); |
|
392 | 392 | m_height+=h; |
|
393 | 393 | point.setY(point.y() + h); |
|
394 | 394 | } |
|
395 | 395 | if(m_height<m_rect.height()) { |
|
396 | 396 | m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2); |
|
397 | 397 | } |
|
398 | 398 | else { |
|
399 | 399 | m_markers->setPos(m_rect.topLeft()); |
|
400 | 400 | } |
|
401 | 401 | m_width=m_minWidth; |
|
402 | 402 | } |
|
403 | 403 | break; |
|
404 | 404 | } |
|
405 | 405 | |
|
406 | 406 | if (m_attachedToChart) { |
|
407 | 407 | m_presenter->updateLayout(); |
|
408 | 408 | } |
|
409 | 409 | } |
|
410 | 410 | |
|
411 | 411 | void QLegendPrivate::attachToChart() |
|
412 | 412 | { |
|
413 | 413 | m_attachedToChart = true; |
|
414 | 414 | q_ptr->setParent(m_chart); |
|
415 | 415 | } |
|
416 | 416 | |
|
417 | 417 | void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) |
|
418 | 418 | { |
|
419 | 419 | Q_UNUSED(domain) |
|
420 | 420 | |
|
421 | 421 | QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr); |
|
422 |
foreach(LegendMarker* marker |
|
|
422 | foreach(LegendMarker* marker, markers) | |
|
423 | 423 | m_markers->addToGroup(marker); |
|
424 | 424 | |
|
425 | if(series->type() == QAbstractSeries::SeriesTypePie) | |
|
426 | { | |
|
425 | if(series->type() == QAbstractSeries::SeriesTypePie) { | |
|
427 | 426 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
428 | 427 | QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
429 | 428 | QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
430 | 429 | } |
|
431 | 430 | |
|
432 | 431 | updateLayout(); |
|
433 | 432 | } |
|
434 | 433 | |
|
435 | 434 | void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series) |
|
436 | 435 | { |
|
437 | ||
|
438 | 436 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
439 | 437 | |
|
440 | 438 | foreach (QGraphicsItem *markers, items) { |
|
441 | 439 | LegendMarker *marker = static_cast<LegendMarker*>(markers); |
|
442 | 440 | if (marker->series() == series) { |
|
443 | 441 | delete marker; |
|
444 | 442 | } |
|
445 | 443 | } |
|
446 | 444 | |
|
447 | 445 | if(series->type() == QAbstractSeries::SeriesTypePie) |
|
448 | 446 | { |
|
449 | 447 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
450 | 448 | QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
451 | 449 | QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
452 | 450 | } |
|
453 | 451 | |
|
454 | 452 | updateLayout(); |
|
455 | 453 | } |
|
456 | 454 | |
|
457 | 455 | void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series) |
|
458 | 456 | { |
|
459 | 457 | // TODO: find out which markers are are added or removed. Update them |
|
460 | 458 | // TODO: better implementation |
|
461 | 459 | handleSeriesRemoved(series); |
|
462 | 460 | Domain domain; |
|
463 | 461 | handleSeriesAdded(series, &domain); |
|
464 | 462 | } |
|
465 | 463 | |
|
466 | 464 | void QLegendPrivate::handleUpdatePieSeries() |
|
467 | 465 | { |
|
468 | 466 | //TODO: reimplement to be optimal |
|
469 | 467 | QPieSeries* series = qobject_cast<QPieSeries *> (sender()); |
|
470 | 468 | Q_ASSERT(series); |
|
471 | 469 | handleSeriesRemoved(series); |
|
472 | 470 | handleSeriesAdded(series, 0); |
|
473 | 471 | } |
|
474 | 472 | |
|
475 | 473 | #include "moc_qlegend.cpp" |
|
476 | 474 | #include "moc_qlegend_p.cpp" |
|
477 | 475 | |
|
478 | 476 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,134 +1,137 | |||
|
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 "qabstractseries.h" |
|
22 | 22 | #include "qabstractseries_p.h" |
|
23 | 23 | #include "chartdataset_p.h" |
|
24 | 24 | |
|
25 | 25 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
26 | 26 | |
|
27 | 27 | /*! |
|
28 | 28 | \class QAbstractSeries |
|
29 | 29 | \brief Base class for all QtCommercial Chart series. |
|
30 | 30 | \mainclass |
|
31 | 31 | |
|
32 | 32 | Usually you use the series type specific inherited classes instead of the base class. |
|
33 | 33 | \sa QXYSeries, QLineSeries, QSplineSeries, QScatterSeries, QAreaSeries, QBarSeries, QStackedBarSeries, |
|
34 | 34 | QPercentBarSeries, QPieSeries |
|
35 | 35 | */ |
|
36 | 36 | |
|
37 | 37 | /*! |
|
38 | 38 | \enum QAbstractSeries::SeriesType |
|
39 | 39 | |
|
40 | 40 | The type of the series object. |
|
41 | 41 | |
|
42 | 42 | \value SeriesTypeLine |
|
43 | 43 | \value SeriesTypeArea |
|
44 | 44 | \value SeriesTypeBar |
|
45 | 45 | \value SeriesTypeStackedBar |
|
46 | 46 | \value SeriesTypePercentBar |
|
47 | 47 | \value SeriesTypeGroupedBar |
|
48 | 48 | \value SeriesTypePie |
|
49 | 49 | \value SeriesTypeScatter |
|
50 | 50 | \value SeriesTypeSpline |
|
51 | 51 | */ |
|
52 | 52 | |
|
53 | 53 | /*! |
|
54 | 54 | \fn QSeriesType QAbstractSeries::type() const |
|
55 | 55 | \brief The type of the series. |
|
56 | 56 | */ |
|
57 | 57 | |
|
58 | 58 | /*! |
|
59 | 59 | \property QAbstractSeries::name |
|
60 | 60 | \brief name of the series property |
|
61 | 61 | */ |
|
62 | 62 | |
|
63 | 63 | /*! |
|
64 | 64 | \fn void QAbstractSeries::setName(const QString& name) |
|
65 | 65 | \brief Sets a \a name for the series. |
|
66 | 66 | |
|
67 | 67 | The name of a series is shown in the legend for QXYSeries. |
|
68 | 68 | \sa QChart::setTitle() |
|
69 | 69 | \sa QPieSlice::setLabel() |
|
70 | 70 | \sa QBarSet::setName() |
|
71 | 71 | */ |
|
72 | 72 | |
|
73 | 73 | /*! |
|
74 | 74 | \internal |
|
75 | 75 | \brief Constructs ChartSeries object with \a parent. |
|
76 | 76 | */ |
|
77 | 77 | QAbstractSeries::QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent) : |
|
78 | 78 | QObject(parent), |
|
79 | 79 | d_ptr(&d) |
|
80 | 80 | { |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | /*! |
|
84 | 84 | \brief Virtual destructor for the chart series. |
|
85 | 85 | */ |
|
86 | 86 | QAbstractSeries::~QAbstractSeries() |
|
87 | 87 | { |
|
88 | 88 | if(d_ptr->m_dataset) qFatal("Still binded series detected !"); |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | void QAbstractSeries::setName(const QString& name) |
|
92 | 92 | { |
|
93 |
d_ptr->m_name |
|
|
93 | if (name != d_ptr->m_name) { | |
|
94 | d_ptr->m_name = name; | |
|
95 | nameChanged(); | |
|
96 | } | |
|
94 | 97 | } |
|
95 | 98 | |
|
96 | 99 | /*! |
|
97 | 100 | \brief Returns the name of the series. |
|
98 | 101 | \sa setName() |
|
99 | 102 | */ |
|
100 | 103 | QString QAbstractSeries::name() const |
|
101 | 104 | { |
|
102 | 105 | return d_ptr->m_name; |
|
103 | 106 | } |
|
104 | 107 | |
|
105 | 108 | /*! |
|
106 | 109 | \brief Returns the chart where series belongs to. |
|
107 | 110 | |
|
108 | 111 | Set automatically when the series is added to the chart |
|
109 | 112 | and unset when the series is removed from the chart. |
|
110 | 113 | */ |
|
111 | 114 | QChart* QAbstractSeries::chart() const |
|
112 | 115 | { |
|
113 | 116 | return d_ptr->m_chart; |
|
114 | 117 | } |
|
115 | 118 | |
|
116 | 119 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
|
117 | 120 | |
|
118 | 121 | QAbstractSeriesPrivate::QAbstractSeriesPrivate(QAbstractSeries* q): |
|
119 | 122 | q_ptr(q), |
|
120 | 123 | m_chart(0), |
|
121 | 124 | m_dataset(0) |
|
122 | 125 | { |
|
123 | 126 | } |
|
124 | 127 | |
|
125 | 128 | QAbstractSeriesPrivate::~QAbstractSeriesPrivate() |
|
126 | 129 | { |
|
127 | 130 | } |
|
128 | 131 | |
|
129 | 132 | #include "moc_qabstractseries.cpp" |
|
130 | 133 | #include "moc_qabstractseries_p.cpp" |
|
131 | 134 | |
|
132 | 135 | QTCOMMERCIALCHART_END_NAMESPACE |
|
133 | 136 | |
|
134 | 137 |
@@ -1,71 +1,74 | |||
|
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 | #ifndef QABSTRACTSERIES_H |
|
22 | 22 | #define QABSTRACTSERIES_H |
|
23 | 23 | |
|
24 | 24 | #include <qchartglobal.h> |
|
25 | 25 | #include <QObject> |
|
26 | 26 | #include <QPen> |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | class QAbstractSeriesPrivate; |
|
31 | 31 | class QChart; |
|
32 | 32 | |
|
33 | 33 | class QTCOMMERCIALCHART_EXPORT QAbstractSeries : public QObject |
|
34 | 34 | { |
|
35 | 35 | Q_OBJECT |
|
36 | Q_PROPERTY(QString name READ name WRITE setName) | |
|
36 | Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) | |
|
37 | 37 | Q_ENUMS(SeriesType) |
|
38 | 38 | |
|
39 | 39 | public: |
|
40 | 40 | enum SeriesType { |
|
41 | 41 | SeriesTypeLine, |
|
42 | 42 | SeriesTypeArea, |
|
43 | 43 | SeriesTypeBar, |
|
44 | 44 | SeriesTypeStackedBar, |
|
45 | 45 | SeriesTypePercentBar, |
|
46 | 46 | SeriesTypeGroupedBar, |
|
47 | 47 | SeriesTypePie, |
|
48 | 48 | SeriesTypeScatter, |
|
49 | 49 | SeriesTypeSpline |
|
50 | 50 | }; |
|
51 | 51 | |
|
52 | 52 | protected: |
|
53 | 53 | QAbstractSeries(QAbstractSeriesPrivate &d, QObject *parent = 0); |
|
54 | 54 | |
|
55 | 55 | public: |
|
56 | 56 | ~QAbstractSeries(); |
|
57 | 57 | virtual SeriesType type() const = 0; |
|
58 | 58 | void setName(const QString& name); |
|
59 | 59 | QString name() const; |
|
60 | 60 | QChart* chart() const; |
|
61 | 61 | |
|
62 | Q_SIGNALS: | |
|
63 | void nameChanged(); | |
|
64 | ||
|
62 | 65 | protected: |
|
63 | 66 | QScopedPointer<QAbstractSeriesPrivate> d_ptr; |
|
64 | 67 | friend class ChartDataSet; |
|
65 | 68 | friend class ChartPresenter; |
|
66 | 69 | friend class QLegendPrivate; |
|
67 | 70 | }; |
|
68 | 71 | |
|
69 | 72 | QTCOMMERCIALCHART_END_NAMESPACE |
|
70 | 73 | |
|
71 | 74 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now