@@ -1,237 +1,236 | |||
|
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 <QDebug> |
|
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(2), |
|
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 | m_textItem->setPen(pen); | |
|
58 | 57 | } |
|
59 | 58 | |
|
60 | 59 | QPen LegendMarker::pen() const |
|
61 | 60 | { |
|
62 | 61 | return m_rectItem->pen(); |
|
63 | 62 | } |
|
64 | 63 | |
|
65 | 64 | void LegendMarker::setBrush(const QBrush &brush) |
|
66 | 65 | { |
|
67 | 66 | m_rectItem->setBrush(brush); |
|
68 | 67 | } |
|
69 | 68 | |
|
70 | 69 | QBrush LegendMarker::brush() const |
|
71 | 70 | { |
|
72 | 71 | return m_rectItem->brush(); |
|
73 | 72 | } |
|
74 | 73 | |
|
75 | 74 | void LegendMarker::setFont(const QFont &font) |
|
76 | 75 | { |
|
77 | 76 | m_textItem->setFont(font); |
|
78 | 77 | QFontMetrics fn(font); |
|
79 | 78 | m_markerRect = QRectF(0,0,fn.height()/2,fn.height()/2); |
|
80 | 79 | updateGeometry(); |
|
81 | 80 | } |
|
82 | 81 | |
|
83 | 82 | QFont LegendMarker::font() const |
|
84 | 83 | { |
|
85 | 84 | return m_textItem->font(); |
|
86 | 85 | } |
|
87 | 86 | |
|
88 | 87 | void LegendMarker::setLabel(const QString label) |
|
89 | 88 | { |
|
90 | 89 | m_textItem->setText(label); |
|
91 | 90 | } |
|
92 | 91 | |
|
93 | 92 | QString LegendMarker::label() const |
|
94 | 93 | { |
|
95 | 94 | return m_textItem->text(); |
|
96 | 95 | } |
|
97 | 96 | |
|
98 | 97 | QRectF LegendMarker::boundingRect() const |
|
99 | 98 | { |
|
100 | 99 | return m_boundingRect; |
|
101 | 100 | } |
|
102 | 101 | |
|
103 | 102 | void LegendMarker::setLabelBrush(const QBrush &brush) |
|
104 | 103 | { |
|
105 | 104 | m_textItem->setBrush(brush); |
|
106 | 105 | } |
|
107 | 106 | |
|
108 | 107 | QBrush LegendMarker::labelBrush() const |
|
109 | 108 | { |
|
110 | 109 | return m_textItem->brush(); |
|
111 | 110 | } |
|
112 | 111 | |
|
113 | 112 | |
|
114 | 113 | void LegendMarker::setGeometry(const QRectF& rect) |
|
115 | 114 | { |
|
116 | 115 | const QRectF& textRect = m_textItem->boundingRect(); |
|
117 | 116 | |
|
118 | 117 | m_textItem->setPos(m_markerRect.width() + m_space + m_margin,rect.height()/2 - textRect.height()/2); |
|
119 | 118 | m_rectItem->setRect(m_markerRect); |
|
120 | 119 | m_rectItem->setPos(m_margin,rect.height()/2 - m_markerRect.height()/2); |
|
121 | 120 | |
|
122 | 121 | prepareGeometryChange(); |
|
123 | 122 | m_boundingRect = rect; |
|
124 | 123 | } |
|
125 | 124 | |
|
126 | 125 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
127 | 126 | { |
|
128 | 127 | Q_UNUSED(option) |
|
129 | 128 | Q_UNUSED(widget) |
|
130 | 129 | Q_UNUSED(painter) |
|
131 | 130 | } |
|
132 | 131 | |
|
133 | 132 | |
|
134 | 133 | QSizeF LegendMarker::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const |
|
135 | 134 | { |
|
136 | 135 | Q_UNUSED(constraint) |
|
137 | 136 | |
|
138 | 137 | QFontMetrics fn(m_textItem->font()); |
|
139 | 138 | QSizeF sh; |
|
140 | 139 | |
|
141 | 140 | switch (which) { |
|
142 | 141 | case Qt::MinimumSize: |
|
143 | 142 | sh = QSizeF(fn.boundingRect("...").width(),fn.height()); |
|
144 | 143 | break; |
|
145 | 144 | case Qt::PreferredSize: |
|
146 | 145 | sh = QSizeF(fn.boundingRect(m_textItem->text()).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin)); |
|
147 | 146 | break; |
|
148 | 147 | default: |
|
149 | 148 | break; |
|
150 | 149 | } |
|
151 | 150 | |
|
152 | 151 | return sh; |
|
153 | 152 | } |
|
154 | 153 | |
|
155 | 154 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
156 | 155 | { |
|
157 | 156 | QGraphicsObject::mousePressEvent(event); |
|
158 | 157 | qDebug()<<"Not implemented"; //TODO: selected signal removed for now |
|
159 | 158 | } |
|
160 | 159 | |
|
161 | 160 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
162 | 161 | |
|
163 | 162 | AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend), |
|
164 | 163 | m_series(series) |
|
165 | 164 | { |
|
166 | 165 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); |
|
167 | 166 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); |
|
168 | 167 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); |
|
169 | 168 | updated(); |
|
170 | 169 | } |
|
171 | 170 | |
|
172 | 171 | void AreaLegendMarker::updated() |
|
173 | 172 | { |
|
174 | 173 | setBrush(m_series->brush()); |
|
175 | 174 | setLabel(m_series->name()); |
|
176 | 175 | } |
|
177 | 176 | |
|
178 | 177 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
179 | 178 | |
|
180 | 179 | BarLegendMarker::BarLegendMarker(QAbstractBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend), |
|
181 | 180 | m_barset(barset) |
|
182 | 181 | { |
|
183 | 182 | //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected())); |
|
184 | 183 | QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated())); |
|
185 | 184 | updated(); |
|
186 | 185 | } |
|
187 | 186 | |
|
188 | 187 | void BarLegendMarker::updated() |
|
189 | 188 | { |
|
190 | 189 | setBrush(m_barset->brush()); |
|
191 | 190 | setLabel(m_barset->label()); |
|
192 | 191 | } |
|
193 | 192 | |
|
194 | 193 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
195 | 194 | |
|
196 | 195 | PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend), |
|
197 | 196 | m_pieslice(pieslice) |
|
198 | 197 | { |
|
199 | 198 | QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated())); |
|
200 | 199 | QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated())); |
|
201 | 200 | updated(); |
|
202 | 201 | } |
|
203 | 202 | |
|
204 | 203 | void PieLegendMarker::updated() |
|
205 | 204 | { |
|
206 | 205 | setBrush(m_pieslice->brush()); |
|
207 | 206 | setLabel(m_pieslice->label()); |
|
208 | 207 | } |
|
209 | 208 | |
|
210 | 209 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
211 | 210 | |
|
212 | 211 | XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend), |
|
213 | 212 | m_series(series) |
|
214 | 213 | { |
|
215 | 214 | //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected())); |
|
216 | 215 | QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated())); |
|
217 | 216 | QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated())); |
|
218 | 217 | updated(); |
|
219 | 218 | } |
|
220 | 219 | |
|
221 | 220 | void XYLegendMarker::updated() |
|
222 | 221 | { |
|
223 | 222 | setLabel(m_series->name()); |
|
224 | 223 | |
|
225 | 224 | if(m_series->type()== QAbstractSeries::SeriesTypeScatter) |
|
226 | 225 | { |
|
227 | 226 | setBrush(m_series->brush()); |
|
228 | 227 | |
|
229 | 228 | } |
|
230 | 229 | else { |
|
231 | 230 | setBrush(QBrush(m_series->pen().color())); |
|
232 | 231 | } |
|
233 | 232 | } |
|
234 | 233 | |
|
235 | 234 | #include "moc_legendmarker_p.cpp" |
|
236 | 235 | |
|
237 | 236 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,534 +1,537 | |||
|
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 | #include "legendlayout_p.h" |
|
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 "qabstractbarseries.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 | #include <QGraphicsLayout> |
|
45 | 45 | #include <QGraphicsSceneEvent> |
|
46 | 46 | |
|
47 | 47 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
48 | 48 | |
|
49 | 49 | /*! |
|
50 | 50 | \class QLegend |
|
51 | 51 | \brief Legend object |
|
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 | \qmlclass Legend QLegend |
|
65 | 65 | \brief Legend is part of QtCommercial Chart QML API. |
|
66 | 66 | |
|
67 | 67 | Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when |
|
68 | 68 | series have been changed. Legend is used via ChartView class. For example: |
|
69 | 69 | \code |
|
70 | 70 | ChartView { |
|
71 | 71 | legend.visible: true |
|
72 | 72 | legend.alignment: Qt.AlignBottom |
|
73 | 73 | // Add a few series... |
|
74 | 74 | } |
|
75 | 75 | \endcode |
|
76 | 76 | |
|
77 | 77 | \image examples_percentbarchart_legend.png |
|
78 | 78 | */ |
|
79 | 79 | |
|
80 | 80 | /*! |
|
81 | 81 | \property QLegend::alignment |
|
82 | 82 | \brief The alignment of the legend. |
|
83 | 83 | |
|
84 | 84 | Legend paints on the defined position in the chart. The following alignments are supported: |
|
85 | 85 | Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined. |
|
86 | 86 | */ |
|
87 | 87 | /*! |
|
88 | 88 | \qmlproperty Qt.Alignment Legend::alignment |
|
89 | 89 | \brief The alignment of the legend. |
|
90 | 90 | |
|
91 | 91 | Legend paints on the defined position in the chart. The following alignments are supported: |
|
92 | 92 | Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined. |
|
93 | 93 | */ |
|
94 | 94 | |
|
95 | 95 | /*! |
|
96 | 96 | \property QLegend::backgroundVisible |
|
97 | 97 | Whether the legend background is visible or not. |
|
98 | 98 | */ |
|
99 | 99 | /*! |
|
100 | 100 | \qmlproperty bool Legend::backgroundVisible |
|
101 | 101 | Whether the legend background is visible or not. |
|
102 | 102 | */ |
|
103 | 103 | |
|
104 | 104 | /*! |
|
105 | 105 | \property QLegend::color |
|
106 | 106 | The color of the legend, i.e. the background (brush) color. Note that if you change the color |
|
107 | 107 | of the legend, the style of the legend brush is set to Qt::SolidPattern. |
|
108 | 108 | */ |
|
109 | 109 | /*! |
|
110 | 110 | \qmlproperty color Legend::color |
|
111 | 111 | The color of the legend, i.e. the background (brush) color. |
|
112 | 112 | */ |
|
113 | 113 | |
|
114 | 114 | /*! |
|
115 | 115 | \property QLegend::borderColor |
|
116 | 116 | The border color of the legend, i.e. the line color. |
|
117 | 117 | */ |
|
118 | 118 | /*! |
|
119 | 119 | \qmlproperty color Legend::borderColor |
|
120 | 120 | The border color of the legend, i.e. the line color. |
|
121 | 121 | */ |
|
122 | 122 | |
|
123 | 123 | /*! |
|
124 | 124 | \property QLegend::font |
|
125 | 125 | The font of markers used by legend |
|
126 | 126 | */ |
|
127 | 127 | /*! |
|
128 | 128 | \qmlproperty color Legend::font |
|
129 | 129 | The font of markers used by legend |
|
130 | 130 | */ |
|
131 | 131 | |
|
132 | 132 | /*! |
|
133 | 133 | \property QLegend::labelColor |
|
134 | 134 | The color of brush used to draw labels. |
|
135 | 135 | */ |
|
136 | 136 | /*! |
|
137 | 137 | \qmlproperty color QLegend::labelColor |
|
138 | 138 | The color of brush used to draw labels. |
|
139 | 139 | */ |
|
140 | 140 | |
|
141 | 141 | /*! |
|
142 | 142 | \fn void QLegend::backgroundVisibleChanged(bool) |
|
143 | 143 | The visibility of the legend background changed to \a visible. |
|
144 | 144 | */ |
|
145 | 145 | |
|
146 | 146 | /*! |
|
147 | 147 | \fn void QLegend::colorChanged(QColor) |
|
148 | 148 | The color of the legend background changed to \a color. |
|
149 | 149 | */ |
|
150 | 150 | |
|
151 | 151 | /*! |
|
152 | 152 | \fn void QLegend::borderColorChanged(QColor) |
|
153 | 153 | The border color of the legend background changed to \a color. |
|
154 | 154 | */ |
|
155 | 155 | |
|
156 | 156 | /*! |
|
157 | 157 | \fn void QLegend::fontChanged(QFont) |
|
158 | 158 | The font of markers of the legend changed to \a font. |
|
159 | 159 | */ |
|
160 | 160 | |
|
161 | 161 | /*! |
|
162 | 162 | \fn void QLegend::labelColorChanged(QColor color) |
|
163 | 163 | This signal is emitted when the color of brush used to draw labels has changed to \a color. |
|
164 | 164 | */ |
|
165 | 165 | |
|
166 | 166 | /*! |
|
167 | 167 | Constructs the legend object and sets the parent to \a parent |
|
168 | 168 | */ |
|
169 | 169 | |
|
170 | 170 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), |
|
171 | 171 | d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this)) |
|
172 | 172 | { |
|
173 | 173 | setZValue(ChartPresenter::LegendZValue); |
|
174 | 174 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
175 | 175 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*))); |
|
176 | 176 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*))); |
|
177 | 177 | QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*))); |
|
178 | 178 | setLayout(d_ptr->m_layout); |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | /*! |
|
182 | 182 | Destroys the legend object. Legend is always owned by a QChart, so an application should never call this. |
|
183 | 183 | */ |
|
184 | 184 | QLegend::~QLegend() |
|
185 | 185 | { |
|
186 | 186 | } |
|
187 | 187 | |
|
188 | 188 | /*! |
|
189 | 189 | \internal |
|
190 | 190 | */ |
|
191 | 191 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
192 | 192 | { |
|
193 | 193 | Q_UNUSED(option) |
|
194 | 194 | Q_UNUSED(widget) |
|
195 | 195 | if(!d_ptr->m_backgroundVisible) return; |
|
196 | 196 | |
|
197 | 197 | painter->setOpacity(opacity()); |
|
198 | 198 | painter->setPen(d_ptr->m_pen); |
|
199 | 199 | painter->setBrush(d_ptr->m_brush); |
|
200 | 200 | painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height())); |
|
201 | 201 | |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | |
|
205 | 205 | /*! |
|
206 | 206 | Sets the \a brush of legend. Brush affects the background of legend. |
|
207 | 207 | */ |
|
208 | 208 | void QLegend::setBrush(const QBrush &brush) |
|
209 | 209 | { |
|
210 | 210 | if (d_ptr->m_brush != brush) { |
|
211 | 211 | d_ptr->m_brush = brush; |
|
212 | 212 | update(); |
|
213 | 213 | emit colorChanged(brush.color()); |
|
214 | 214 | } |
|
215 | 215 | } |
|
216 | 216 | |
|
217 | 217 | /*! |
|
218 | 218 | Returns the brush used by legend. |
|
219 | 219 | */ |
|
220 | 220 | QBrush QLegend::brush() const |
|
221 | 221 | { |
|
222 | 222 | return d_ptr->m_brush; |
|
223 | 223 | } |
|
224 | 224 | |
|
225 | 225 | void QLegend::setColor(QColor color) |
|
226 | 226 | { |
|
227 | 227 | QBrush b = d_ptr->m_brush; |
|
228 | 228 | if (b.style() != Qt::SolidPattern || b.color() != color) { |
|
229 | 229 | b.setStyle(Qt::SolidPattern); |
|
230 | 230 | b.setColor(color); |
|
231 | 231 | setBrush(b); |
|
232 | 232 | } |
|
233 | 233 | } |
|
234 | 234 | |
|
235 | 235 | QColor QLegend::color() |
|
236 | 236 | { |
|
237 | 237 | return d_ptr->m_brush.color(); |
|
238 | 238 | } |
|
239 | 239 | |
|
240 | 240 | /*! |
|
241 | 241 | Sets the \a pen of legend. Pen affects the legend borders. |
|
242 | 242 | */ |
|
243 | 243 | void QLegend::setPen(const QPen &pen) |
|
244 | 244 | { |
|
245 | 245 | if (d_ptr->m_pen != pen) { |
|
246 | 246 | d_ptr->m_pen = pen; |
|
247 | 247 | update(); |
|
248 | 248 | emit borderColorChanged(pen.color()); |
|
249 | 249 | } |
|
250 | 250 | } |
|
251 | 251 | |
|
252 | 252 | /*! |
|
253 | 253 | Returns the pen used by legend |
|
254 | 254 | */ |
|
255 | 255 | |
|
256 | 256 | QPen QLegend::pen() const |
|
257 | 257 | { |
|
258 | 258 | return d_ptr->m_pen; |
|
259 | 259 | } |
|
260 | 260 | |
|
261 | 261 | void QLegend::setFont(const QFont &font) |
|
262 | 262 | { |
|
263 | 263 | if (d_ptr->m_font != font) { |
|
264 | 264 | d_ptr->m_font = font; |
|
265 | 265 | |
|
266 | 266 | foreach (LegendMarker *marker, d_ptr->markers()) { |
|
267 | 267 | marker->setFont(d_ptr->m_font); |
|
268 | 268 | } |
|
269 | 269 | layout()->invalidate(); |
|
270 | 270 | emit fontChanged(font); |
|
271 | 271 | } |
|
272 | 272 | } |
|
273 | 273 | |
|
274 | 274 | QFont QLegend::font() const |
|
275 | 275 | { |
|
276 | 276 | return d_ptr->m_font; |
|
277 | 277 | } |
|
278 | 278 | |
|
279 | 279 | void QLegend::setBorderColor(QColor color) |
|
280 | 280 | { |
|
281 | 281 | QPen p = d_ptr->m_pen; |
|
282 | 282 | if (p.color() != color) { |
|
283 | 283 | p.setColor(color); |
|
284 | 284 | setPen(p); |
|
285 | 285 | } |
|
286 | 286 | } |
|
287 | 287 | |
|
288 | 288 | QColor QLegend::borderColor() |
|
289 | 289 | { |
|
290 | 290 | return d_ptr->m_pen.color(); |
|
291 | 291 | } |
|
292 | 292 | |
|
293 | 293 | /*! |
|
294 | 294 | Set brush used to draw labels to \a brush. |
|
295 | 295 | */ |
|
296 | 296 | void QLegend::setLabelBrush(const QBrush &brush) |
|
297 | 297 | { |
|
298 | 298 | if (d_ptr->m_labelBrush != brush) { |
|
299 | 299 | d_ptr->m_labelBrush = brush; |
|
300 | 300 | foreach (LegendMarker *marker, d_ptr->markers()) { |
|
301 | 301 | marker->setLabelBrush(d_ptr->m_labelBrush); |
|
302 | // Note: The pen of the marker rectangle could be exposed in the public QLegend API | |
|
303 | // instead of mapping it from label brush color | |
|
304 | marker->setPen(brush.color()); | |
|
302 | 305 | } |
|
303 | 306 | emit labelColorChanged(brush.color()); |
|
304 | 307 | } |
|
305 | 308 | } |
|
306 | 309 | |
|
307 | 310 | /*! |
|
308 | 311 | Brush used to draw labels. |
|
309 | 312 | */ |
|
310 | 313 | QBrush QLegend::labelBrush() const |
|
311 | 314 | { |
|
312 | 315 | return d_ptr->m_labelBrush; |
|
313 | 316 | } |
|
314 | 317 | |
|
315 | 318 | void QLegend::setLabelColor(QColor color) |
|
316 | 319 | { |
|
317 | 320 | QBrush b = d_ptr->m_labelBrush; |
|
318 | 321 | if (b.style() != Qt::SolidPattern || b.color() != color) { |
|
319 | 322 | b.setStyle(Qt::SolidPattern); |
|
320 | 323 | b.setColor(color); |
|
321 | 324 | setLabelBrush(b); |
|
322 | 325 | } |
|
323 | 326 | } |
|
324 | 327 | |
|
325 | 328 | QColor QLegend::labelColor() const |
|
326 | 329 | { |
|
327 | 330 | return d_ptr->m_labelBrush.color(); |
|
328 | 331 | } |
|
329 | 332 | |
|
330 | 333 | |
|
331 | 334 | void QLegend::setAlignment(Qt::Alignment alignment) |
|
332 | 335 | { |
|
333 | 336 | if(d_ptr->m_alignment!=alignment) { |
|
334 | 337 | d_ptr->m_alignment = alignment; |
|
335 | 338 | updateGeometry(); |
|
336 | 339 | if(isAttachedToChart()){ |
|
337 | 340 | d_ptr->m_presenter->layout()->invalidate(); |
|
338 | 341 | }else{ |
|
339 | 342 | layout()->invalidate(); |
|
340 | 343 | } |
|
341 | 344 | } |
|
342 | 345 | } |
|
343 | 346 | |
|
344 | 347 | Qt::Alignment QLegend::alignment() const |
|
345 | 348 | { |
|
346 | 349 | return d_ptr->m_alignment; |
|
347 | 350 | } |
|
348 | 351 | |
|
349 | 352 | /*! |
|
350 | 353 | Detaches the legend from chart. Chart won't change layout of the legend. |
|
351 | 354 | */ |
|
352 | 355 | void QLegend::detachFromChart() |
|
353 | 356 | { |
|
354 | 357 | d_ptr->m_attachedToChart = false; |
|
355 | 358 | d_ptr->m_layout->invalidate(); |
|
356 | 359 | setParent(0); |
|
357 | 360 | |
|
358 | 361 | } |
|
359 | 362 | |
|
360 | 363 | /*! |
|
361 | 364 | Attaches the legend to chart. Chart may change layout of the legend. |
|
362 | 365 | */ |
|
363 | 366 | void QLegend::attachToChart() |
|
364 | 367 | { |
|
365 | 368 | d_ptr->m_attachedToChart = true; |
|
366 | 369 | d_ptr->m_layout->invalidate(); |
|
367 | 370 | setParent(d_ptr->m_chart); |
|
368 | 371 | } |
|
369 | 372 | |
|
370 | 373 | /*! |
|
371 | 374 | Returns true, if legend is attached to chart. |
|
372 | 375 | */ |
|
373 | 376 | bool QLegend::isAttachedToChart() |
|
374 | 377 | { |
|
375 | 378 | return d_ptr->m_attachedToChart; |
|
376 | 379 | } |
|
377 | 380 | |
|
378 | 381 | /*! |
|
379 | 382 | Sets the visibility of legend background to \a visible |
|
380 | 383 | */ |
|
381 | 384 | void QLegend::setBackgroundVisible(bool visible) |
|
382 | 385 | { |
|
383 | 386 | if(d_ptr->m_backgroundVisible != visible) { |
|
384 | 387 | d_ptr->m_backgroundVisible = visible; |
|
385 | 388 | update(); |
|
386 | 389 | emit backgroundVisibleChanged(visible); |
|
387 | 390 | } |
|
388 | 391 | } |
|
389 | 392 | |
|
390 | 393 | /*! |
|
391 | 394 | Returns the visibility of legend background |
|
392 | 395 | */ |
|
393 | 396 | bool QLegend::isBackgroundVisible() const |
|
394 | 397 | { |
|
395 | 398 | return d_ptr->m_backgroundVisible; |
|
396 | 399 | } |
|
397 | 400 | |
|
398 | 401 | /*! |
|
399 | 402 | \internal \a event see QGraphicsWidget for details |
|
400 | 403 | */ |
|
401 | 404 | void QLegend::hideEvent(QHideEvent *event) |
|
402 | 405 | { |
|
403 | 406 | QGraphicsWidget::hideEvent(event); |
|
404 | 407 | d_ptr->m_presenter->layout()->invalidate(); |
|
405 | 408 | } |
|
406 | 409 | |
|
407 | 410 | /*! |
|
408 | 411 | \internal \a event see QGraphicsWidget for details |
|
409 | 412 | */ |
|
410 | 413 | void QLegend::showEvent(QShowEvent *event) |
|
411 | 414 | { |
|
412 | 415 | QGraphicsWidget::showEvent(event); |
|
413 | 416 | d_ptr->m_presenter->layout()->invalidate(); |
|
414 | 417 | } |
|
415 | 418 | |
|
416 | 419 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
417 | 420 | |
|
418 | 421 | QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q): |
|
419 | 422 | q_ptr(q), |
|
420 | 423 | m_presenter(presenter), |
|
421 | 424 | m_layout(new LegendLayout(q)), |
|
422 | 425 | m_chart(chart), |
|
423 | 426 | m_items(new QGraphicsItemGroup(q)), |
|
424 | 427 | m_alignment(Qt::AlignTop), |
|
425 | 428 | m_brush(QBrush()), |
|
426 | 429 | m_pen(QPen()), |
|
427 | 430 | m_labelBrush(QBrush()), |
|
428 | 431 | m_diameter(5), |
|
429 | 432 | m_attachedToChart(true), |
|
430 | 433 | m_backgroundVisible(false) |
|
431 | 434 | { |
|
432 | 435 | |
|
433 | 436 | } |
|
434 | 437 | |
|
435 | 438 | QLegendPrivate::~QLegendPrivate() |
|
436 | 439 | { |
|
437 | 440 | |
|
438 | 441 | } |
|
439 | 442 | |
|
440 | 443 | void QLegendPrivate::setOffset(qreal x, qreal y) |
|
441 | 444 | { |
|
442 | 445 | m_layout->setOffset(x,y); |
|
443 | 446 | } |
|
444 | 447 | |
|
445 | 448 | QPointF QLegendPrivate::offset() const |
|
446 | 449 | { |
|
447 | 450 | return m_layout->offset(); |
|
448 | 451 | } |
|
449 | 452 | |
|
450 | 453 | int QLegendPrivate::roundness(qreal size) |
|
451 | 454 | { |
|
452 | 455 | return 100*m_diameter/int(size); |
|
453 | 456 | } |
|
454 | 457 | |
|
455 | 458 | void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain) |
|
456 | 459 | { |
|
457 | 460 | Q_UNUSED(domain) |
|
458 | 461 | |
|
459 | 462 | QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr); |
|
460 | 463 | |
|
461 | 464 | foreach(LegendMarker* marker, markers) { |
|
462 | 465 | marker->setFont(m_font); |
|
463 | 466 | marker->setLabelBrush(m_labelBrush); |
|
464 | 467 | marker->setVisible(series->isVisible()); |
|
465 | 468 | m_items->addToGroup(marker); |
|
466 | 469 | m_markers<<marker; |
|
467 | 470 | } |
|
468 | 471 | |
|
469 | 472 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged())); |
|
470 | 473 | |
|
471 | 474 | if(series->type() == QAbstractSeries::SeriesTypePie) { |
|
472 | 475 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
473 | 476 | QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
474 | 477 | QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
475 | 478 | } |
|
476 | 479 | |
|
477 | 480 | q_ptr->layout()->invalidate(); |
|
478 | 481 | q_ptr->layout()->activate(); |
|
479 | 482 | } |
|
480 | 483 | |
|
481 | 484 | void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series) |
|
482 | 485 | { |
|
483 | 486 | foreach (LegendMarker *marker, m_markers) { |
|
484 | 487 | if (marker->series() == series) { |
|
485 | 488 | delete marker; |
|
486 | 489 | m_markers.removeAll(marker); |
|
487 | 490 | } |
|
488 | 491 | } |
|
489 | 492 | |
|
490 | 493 | if(series->type() == QAbstractSeries::SeriesTypePie) |
|
491 | 494 | { |
|
492 | 495 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
493 | 496 | QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
494 | 497 | QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries())); |
|
495 | 498 | } |
|
496 | 499 | |
|
497 | 500 | q_ptr->layout()->invalidate(); |
|
498 | 501 | } |
|
499 | 502 | |
|
500 | 503 | void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series) |
|
501 | 504 | { |
|
502 | 505 | // TODO: find out which markers are are added or removed. Update them |
|
503 | 506 | // TODO: better implementation |
|
504 | 507 | handleSeriesRemoved(series); |
|
505 | 508 | Domain domain; |
|
506 | 509 | handleSeriesAdded(series, &domain); |
|
507 | 510 | } |
|
508 | 511 | |
|
509 | 512 | void QLegendPrivate::handleUpdatePieSeries() |
|
510 | 513 | { |
|
511 | 514 | //TODO: reimplement to be optimal |
|
512 | 515 | QPieSeries* series = qobject_cast<QPieSeries *> (sender()); |
|
513 | 516 | Q_ASSERT(series); |
|
514 | 517 | handleSeriesRemoved(series); |
|
515 | 518 | handleSeriesAdded(series, 0); |
|
516 | 519 | } |
|
517 | 520 | |
|
518 | 521 | void QLegendPrivate::handleSeriesVisibleChanged() |
|
519 | 522 | { |
|
520 | 523 | QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender()); |
|
521 | 524 | |
|
522 | 525 | foreach (LegendMarker* marker, m_markers) { |
|
523 | 526 | if (marker->series() == series) { |
|
524 | 527 | marker->setVisible(series->isVisible()); |
|
525 | 528 | } |
|
526 | 529 | } |
|
527 | 530 | |
|
528 | 531 | q_ptr->layout()->invalidate(); |
|
529 | 532 | } |
|
530 | 533 | |
|
531 | 534 | #include "moc_qlegend.cpp" |
|
532 | 535 | #include "moc_qlegend_p.cpp" |
|
533 | 536 | |
|
534 | 537 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now