##// END OF EJS Templates
legend font fix
sauimone -
r1522:7ac28ceb0acb
parent child
Show More
@@ -1,224 +1,262
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 "mainwidget.h"
22 22 #include <QChart>
23 23 #include <QChartView>
24 24 #include <QPushButton>
25 25 #include <QLabel>
26 26 #include <QDebug>
27 27 #include <QBarSet>
28 28 #include <QBarSeries>
29 29 #include <QLegend>
30 30 #include <QFormLayout>
31 31
32 32 QTCOMMERCIALCHART_USE_NAMESPACE
33 33
34 34 MainWidget::MainWidget(QWidget *parent) :
35 35 QWidget(parent)
36 36 {
37 37 // Create buttons for ui
38 38 m_buttonLayout = new QGridLayout();
39 39 QPushButton *detachLegendButton = new QPushButton("detach legend");
40 40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
41 41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
42 42 QPushButton *attachLegendButton = new QPushButton("attach legend");
43 43 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
44 44 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
45 45
46 46 QPushButton *addSetButton = new QPushButton("add barset");
47 47 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
48 48 m_buttonLayout->addWidget(addSetButton, 2, 0);
49 49 QPushButton *removeBarsetButton = new QPushButton("remove barset");
50 50 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
51 51 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
52 52
53 53 QPushButton *leftButton = new QPushButton("Align legend left");
54 54 connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
55 55 m_buttonLayout->addWidget(leftButton, 4, 0);
56 56
57 57 QPushButton *rightButton = new QPushButton("Align legend right");
58 58 connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
59 59 m_buttonLayout->addWidget(rightButton, 5, 0);
60 60
61 61 QPushButton *topButton = new QPushButton("Align legend top");
62 62 connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
63 63 m_buttonLayout->addWidget(topButton, 6, 0);
64 64
65 65 QPushButton *bottomButton = new QPushButton("Align legend bottom");
66 66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
67 67 m_buttonLayout->addWidget(bottomButton, 7, 0);
68 68
69 QPushButton *boldButton = new QPushButton("Toggle bold");
70 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
71 m_buttonLayout->addWidget(boldButton, 8, 0);
72
73 QPushButton *italicButton = new QPushButton("Toggle italic");
74 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
75 m_buttonLayout->addWidget(italicButton, 9, 0);
76
69 77 m_legendPosX = new QDoubleSpinBox();
70 78 m_legendPosY = new QDoubleSpinBox();
71 79 m_legendWidth = new QDoubleSpinBox();
72 80 m_legendHeight = new QDoubleSpinBox();
73 81
74 82 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
75 83 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
76 84 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
77 85 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
78 86
79 87 QFormLayout* legendLayout = new QFormLayout();
80 88 legendLayout->addRow("Horizontal position", m_legendPosX);
81 89 legendLayout->addRow("Vertical position", m_legendPosY);
82 90 legendLayout->addRow("Width", m_legendWidth);
83 91 legendLayout->addRow("Height", m_legendHeight);
84 92 m_legendSettings = new QGroupBox("Detached legend");
85 93 m_legendSettings->setLayout(legendLayout);
86 94 m_buttonLayout->addWidget(m_legendSettings);
87 95 m_legendSettings->setVisible(false);
88 96
89 97 // Create chart view with the chart
90 98 m_chart = new QChart();
91 99 m_chartView = new QChartView(m_chart, this);
92 100
101 // Create spinbox to modify font size
102 m_fontSize = new QDoubleSpinBox();
103 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
104 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
105
106 QFormLayout* fontLayout = new QFormLayout();
107 fontLayout->addRow("Legend font size", m_fontSize);
108
93 109 // Create layout for grid and detached legend
94 110 m_mainLayout = new QGridLayout();
95 111 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
112 m_mainLayout->addLayout(fontLayout,1,0);
96 113 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
97 114 setLayout(m_mainLayout);
98 115
99 116 createSeries();
100 117 }
101 118
102 119 void MainWidget::createSeries()
103 120 {
104 121 m_series = new QBarSeries();
105 122 addBarset();
106 123 addBarset();
107 124 addBarset();
108 125 addBarset();
109 126
110 127 m_chart->addSeries(m_series);
111 128 m_chart->setTitle("Legend detach example");
112 129 //![1]
113 130 m_chart->legend()->setVisible(true);
114 131 m_chart->legend()->setAlignment(Qt::AlignBottom);
115 132 //![1]
116 133
117 134 m_chart->axisY()->setNiceNumbersEnabled(true);
118 135 m_chartView->setRenderHint(QPainter::Antialiasing);
119 136 }
120 137
121 138 void MainWidget::showLegendSpinbox()
122 139 {
123 140 m_legendSettings->setVisible(true);
124 141 QRectF chartViewRect = m_chartView->rect();
125 142 QRectF legendRect = m_chart->legend()->boundingRect();
126 143
127 144 m_legendPosX->setMinimum(0);
128 145 m_legendPosX->setMaximum(chartViewRect.width());
129 146 m_legendPosX->setValue(150);
130 147
131 148 m_legendPosY->setMinimum(0);
132 149 m_legendPosY->setMaximum(chartViewRect.height());
133 150 m_legendPosY->setValue(150);
134 151
135 152 m_legendWidth->setMinimum(0);
136 153 m_legendWidth->setMaximum(chartViewRect.width());
137 154 m_legendWidth->setValue(150);
138 155
139 156 m_legendHeight->setMinimum(0);
140 157 m_legendHeight->setMaximum(chartViewRect.height());
141 158 m_legendHeight->setValue(75);
142 159 }
143 160
144 161 void MainWidget::hideLegendSpinbox()
145 162 {
146 163 m_legendSettings->setVisible(false);
147 164 }
148 165
149 166
150 167 void MainWidget::detachLegend()
151 168 {
152 169 //![2]
153 170 QLegend *legend = m_chart->legend();
154 171 legend->detachFromChart();
155 172
156 173 m_chart->legend()->setBackgroundVisible(true);
157 174 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
158 175 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
159 176 //![2]
160 177
161 178 showLegendSpinbox();
162 179 updateLegendLayout();
163 180 update();
164 181 }
165 182
166 183
167 184 void MainWidget::attachLegend()
168 185 {
169 186 //![3]
170 187 QLegend *legend = m_chart->legend();
171 188 legend->attachToChart();
172 189 m_chart->legend()->setBackgroundVisible(false);
173 190 //![3]
174 191
175 192 hideLegendSpinbox();
176 193 update();
177 194 }
178 195
179 196 void MainWidget::addBarset()
180 197 {
181 198 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count()));
182 199 qreal delta = m_series->count() * 0.1;
183 200 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
184 201 m_series->append(barSet);
185 202 }
186 203
187 204 void MainWidget::removeBarset()
188 205 {
189 206 QList<QBarSet*> sets = m_series->barSets();
190 207 if (sets.count() > 0) {
191 208 m_series->remove(sets.at(sets.count()-1));
192 209 }
193 210 }
194 211
195 212 void MainWidget::setLegendLeft()
196 213 {
197 214 m_chart->legend()->setAlignment(Qt::AlignLeft);
198 215 }
199 216
200 217 void MainWidget::setLegendRight()
201 218 {
202 219 m_chart->legend()->setAlignment(Qt::AlignRight);
203 220 }
204 221
205 222 void MainWidget::setLegendTop()
206 223 {
207 224 m_chart->legend()->setAlignment(Qt::AlignTop);
208 225 }
209 226
210 227 void MainWidget::setLegendBottom()
211 228 {
212 229 m_chart->legend()->setAlignment(Qt::AlignBottom);
213 230 }
214 231
232 void MainWidget::toggleBold()
233 {
234 QFont font = m_chart->legend()->font();
235 font.setBold(!font.bold());
236 m_chart->legend()->setFont(font);
237 }
238
239 void MainWidget::toggleItalic()
240 {
241 QFont font = m_chart->legend()->font();
242 font.setItalic(!font.italic());
243 m_chart->legend()->setFont(font);
244 }
245
246 void MainWidget::fontSizeChanged()
247 {
248 QFont font = m_chart->legend()->font();
249 font.setPointSizeF(m_fontSize->value());
250 m_chart->legend()->setFont(font);
251 }
252
215 253 void MainWidget::updateLegendLayout()
216 254 {
217 255 //![4]
218 256 m_chart->legend()->setGeometry(m_legendPosX->value()
219 257 ,m_legendPosY->value()
220 258 ,m_legendWidth->value()
221 259 ,m_legendHeight->value());
222 260 m_chart->legend()->update();
223 261 //![4]
224 262 }
@@ -1,78 +1,86
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 MAINWIDGET_H
22 22 #define MAINWIDGET_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "qchart.h"
26 26 #include "qchartview.h"
27 27 #include <QWidget>
28 28 #include <QGraphicsWidget>
29 29 #include <QGridLayout>
30 30 #include <QGraphicsGridLayout>
31 31 #include <QDoubleSpinBox>
32 32 #include <QGroupBox>
33 33 #include <QBarSeries>
34 34
35 35 QTCOMMERCIALCHART_USE_NAMESPACE
36 36
37 37 class MainWidget : public QWidget
38 38 {
39 39 Q_OBJECT
40 40 public:
41 41 explicit MainWidget(QWidget *parent = 0);
42 42 void createSeries();
43 43 void showLegendSpinbox();
44 44 void hideLegendSpinbox();
45 45
46 46 signals:
47 47
48 48 public slots:
49 49 void detachLegend();
50 50 void attachLegend();
51 51 void addBarset();
52 52 void removeBarset();
53 53
54 54 void setLegendLeft();
55 55 void setLegendRight();
56 56 void setLegendTop();
57 57 void setLegendBottom();
58 58
59 void toggleBold();
60 void toggleItalic();
61 void fontSizeChanged();
62
59 63 void updateLegendLayout();
60 64
65
61 66 private:
62 67
63 68 QChart *m_chart;
64 69 QBarSeries *m_series;
65 70
66 71 QChartView *m_chartView;
67 72 QGridLayout *m_mainLayout;
68 73 QGridLayout *m_buttonLayout;
74 QGridLayout *m_fontLayout;
75
76 QDoubleSpinBox *m_fontSize;
69 77
70 78 // For detached layout
71 79 QGroupBox* m_legendSettings;
72 80 QDoubleSpinBox *m_legendPosX;
73 81 QDoubleSpinBox *m_legendPosY;
74 82 QDoubleSpinBox *m_legendWidth;
75 83 QDoubleSpinBox *m_legendHeight;
76 84 };
77 85
78 86 #endif // MAINWIDGET_H
@@ -1,198 +1,209
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 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 {
48 48 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
49 49 m_rectItem->setRect(m_markerRect);
50 50 updateLayout();
51 51 }
52 52
53 53 void LegendMarker::setPen(const QPen &pen)
54 54 {
55 55 m_textItem->setPen(pen);
56 56 updateLayout();
57 57 }
58 58
59 59 QPen LegendMarker::pen() const
60 60 {
61 61 return m_textItem->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 void LegendMarker::setFont(const QFont &font)
75 {
76 m_textItem->setFont(font);
77 updateLayout();
78 }
79
80 QFont LegendMarker::font() const
81 {
82 return m_textItem->font();
83 }
84
74 85 void LegendMarker::setLabel(const QString label)
75 86 {
76 87 m_textItem->setText(label);
77 88 updateLayout();
78 89 }
79 90
80 91 void LegendMarker::setSize(const QSize& size)
81 92 {
82 93 m_markerRect = QRectF(0,0,size.width(),size.height());
83 94 }
84 95
85 96 QString LegendMarker::label() const
86 97 {
87 98 return m_textItem->text();
88 99 }
89 100
90 101 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
91 102 {
92 103 Q_UNUSED(option)
93 104 Q_UNUSED(widget)
94 105 Q_UNUSED(painter)
95 106 }
96 107
97 108 QRectF LegendMarker::boundingRect() const
98 109 {
99 110 return m_boundingRect;
100 111 }
101 112
102 113 void LegendMarker::updateLayout()
103 114 {
104 115
105 116 static const qreal margin = 2;
106 117 static const qreal space = 4;
107 118
108 119 const QRectF& textRect = m_textItem->boundingRect();
109 120 prepareGeometryChange();
110 121 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
111 122 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
112 123 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
113 124
114 125 }
115 126
116 127 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
117 128 {
118 129 QGraphicsObject::mousePressEvent(event);
119 130 qDebug()<<"Not implemented"; //TODO: selected signal removed for now
120 131 }
121 132
122 133 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
123 134
124 135 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
125 136 m_series(series)
126 137 {
127 138 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
128 139 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
129 140 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
130 141 updated();
131 142 }
132 143
133 144 void AreaLegendMarker::updated()
134 145 {
135 146 setBrush(m_series->brush());
136 147 setLabel(m_series->name());
137 148 }
138 149
139 150 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
140 151
141 152 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
142 153 m_barset(barset)
143 154 {
144 155 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
145 156 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
146 157 updated();
147 158 }
148 159
149 160 void BarLegendMarker::updated()
150 161 {
151 162 setBrush(m_barset->brush());
152 163 setLabel(m_barset->label());
153 164 }
154 165
155 166 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156 167
157 168 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
158 169 m_pieslice(pieslice)
159 170 {
160 171 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
161 172 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
162 173 updated();
163 174 }
164 175
165 176 void PieLegendMarker::updated()
166 177 {
167 178 setBrush(m_pieslice->brush());
168 179 setLabel(m_pieslice->label());
169 180 }
170 181
171 182 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
172 183
173 184 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
174 185 m_series(series)
175 186 {
176 187 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
177 188 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
178 189 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
179 190 updated();
180 191 }
181 192
182 193 void XYLegendMarker::updated()
183 194 {
184 195 setLabel(m_series->name());
185 196
186 197 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
187 198 {
188 199 setBrush(m_series->brush());
189 200
190 201 }
191 202 else {
192 203 setBrush(QBrush(m_series->pen().color()));
193 204 }
194 205 }
195 206
196 207 #include "moc_legendmarker_p.cpp"
197 208
198 209 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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef LEGENDMARKER_P_H
31 31 #define LEGENDMARKER_P_H
32 32
33 33 #include "qchartglobal.h"
34 34 #include <QGraphicsObject>
35 35 #include <QBrush>
36 36 #include <QPen>
37 37 #include <QGraphicsSimpleTextItem>
38 38
39 39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 40
41 41 class QAbstractSeries;
42 42 class QAreaSeries;
43 43 class QXYSeries;
44 44 class QBarSet;
45 45 class QBarSeries;
46 46 class QPieSlice;
47 47 class QLegend;
48 48 class QPieSeries;
49 49
50 50 class LegendMarker : public QGraphicsObject
51 51 {
52 52 Q_OBJECT
53 53
54 54 public:
55 55 explicit LegendMarker(QAbstractSeries *m_series, QLegend *parent);
56 56
57 57 void setPen(const QPen &pen);
58 58 QPen pen() const;
59 59 void setBrush(const QBrush &brush);
60 60 QBrush brush() const;
61 61
62 void setFont(const QFont &font);
63 QFont font() const;
64
62 65 void setSize(const QSize& size);
63 66
64 67 void setLabel(const QString label);
65 68 QString label() const;
66 69
67 70 QAbstractSeries *series() const { return m_series;}
68 71
69 72 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
70 73
71 74 QRectF boundingRect() const;
72 75
73 76 void updateLayout();
74 77
75 78 protected:
76 79 // From QGraphicsObject
77 80 void mousePressEvent(QGraphicsSceneMouseEvent *event);
78 81
79 82 public Q_SLOTS:
80 83 virtual void updated() = 0;
81 84
82 85 protected:
83 86 QAbstractSeries *m_series;
84 87 QRectF m_markerRect;
85 88 QRectF m_boundingRect;
86 89 QLegend* m_legend;
87 90 QGraphicsSimpleTextItem *m_textItem;
88 91 QGraphicsRectItem *m_rectItem;
89 92
90 93 };
91 94
92 95 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93 96 class XYLegendMarker : public LegendMarker
94 97 {
95 98 public:
96 99 XYLegendMarker(QXYSeries *series, QLegend *legend);
97 100 protected:
98 101 void updated();
99 102 private:
100 103 QXYSeries *m_series;
101 104 };
102 105 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103 106 class AreaLegendMarker : public LegendMarker
104 107 {
105 108 public:
106 109 AreaLegendMarker(QAreaSeries *series, QLegend *legend);
107 110 protected:
108 111 void updated();
109 112 private:
110 113 QAreaSeries *m_series;
111 114 };
112 115 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
113 116 class BarLegendMarker : public LegendMarker
114 117 {
115 118 public:
116 119 BarLegendMarker(QBarSeries *barseries, QBarSet *barset,QLegend *legend);
117 120 protected:
118 121 void updated();
119 122 private:
120 123 QBarSet *m_barset;
121 124 };
122 125 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
123 126 class PieLegendMarker : public LegendMarker
124 127 {
125 128 public:
126 129 PieLegendMarker(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend);
127 130 protected:
128 131 void updated();
129 132 private:
130 133 QPieSlice *m_pieslice;
131 134 };
132 135
133 136 QTCOMMERCIALCHART_END_NAMESPACE
134 137 #endif // LEGENDMARKER_P_H
@@ -1,760 +1,801
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 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 \property QLegend::font
125 The font of markers used by legend
126 */
127 /*!
128 \qmlproperty color Legend::font
129 The font of markers used by legend
130 */
131
132 /*!
124 133 \fn void QLegend::backgroundVisibleChanged(bool)
125 134 The visibility of the legend background changed to \a visible.
126 135 */
127 136
128 137 /*!
129 138 \fn void QLegend::colorChanged(QColor)
130 139 The color of the legend background changed to \a color.
131 140 */
132 141
133 142 /*!
134 143 \fn void QLegend::borderColorChanged(QColor)
135 144 The border color of the legend background changed to \a color.
136 145 */
137 146
138 147 /*!
148 \fn void QLegend::fontChanged(QFont)
149 The font of markers of the legend changed to \a font.
150 */
151
152 /*!
139 153 \fn qreal QLegend::minWidth() const
140 154 Returns minimum width of the legend
141 155 */
142 156
143 157 /*!
144 158 \fn qreal QLegend::minHeight() const
145 159 Returns minimum height of the legend
146 160 */
147 161
148 162 /*!
149 163 Constructs the legend object and sets the parent to \a parent
150 164 */
151 165
152 166 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
153 167 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
154 168 {
155 169 setZValue(ChartPresenter::LegendZValue);
156 170 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
157 171 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
158 172 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
159 173 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
160 174 }
161 175
162 176 /*!
163 177 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
164 178 */
165 179 QLegend::~QLegend()
166 180 {
167 181 }
168 182
169 183 /*!
170 184 \internal
171 185 */
172 186 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
173 187 {
174 188 Q_UNUSED(option)
175 189 Q_UNUSED(widget)
176 190 if(!d_ptr->m_backgroundVisible) return;
177 191
178 192 painter->setOpacity(opacity());
179 193 painter->setPen(d_ptr->m_pen);
180 194 painter->setBrush(d_ptr->m_brush);
181 195 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
182 196 }
183 197
184 198 /*!
185 199 \internal
186 200 */
187 201 QRectF QLegend::boundingRect() const
188 202 {
189 203 return d_ptr->m_rect;
190 204 }
191 205
192 206 /*!
193 207 Sets the \a brush of legend. Brush affects the background of legend.
194 208 */
195 209 void QLegend::setBrush(const QBrush &brush)
196 210 {
197 211 if (d_ptr->m_brush != brush) {
198 212 d_ptr->m_brush = brush;
199 213 update();
200 214 }
201 215 }
202 216
203 217 /*!
204 218 Returns the brush used by legend.
205 219 */
206 220 QBrush QLegend::brush() const
207 221 {
208 222 return d_ptr->m_brush;
209 223 }
210 224
211 225 void QLegend::setColor(QColor color)
212 226 {
213 227 QBrush b = d_ptr->m_brush;
214 228 if (b.style() != Qt::SolidPattern || b.color() != color) {
215 229 b.setStyle(Qt::SolidPattern);
216 230 b.setColor(color);
217 231 setBrush(b);
218 232 emit colorChanged(color);
219 233 }
220 234 }
221 235
222 236 QColor QLegend::color()
223 237 {
224 238 return d_ptr->m_brush.color();
225 239 }
226 240
227 241 /*!
228 242 Sets the \a pen of legend. Pen affects the legend borders.
229 243 */
230 244 void QLegend::setPen(const QPen &pen)
231 245 {
232 246 if (d_ptr->m_pen != pen) {
233 247 d_ptr->m_pen = pen;
234 248 update();
235 249 }
236 250 }
237 251
238 252 /*!
239 253 Returns the pen used by legend
240 254 */
241 255
242 256 QPen QLegend::pen() const
243 257 {
244 258 return d_ptr->m_pen;
245 259 }
246 260
247 261 void QLegend::setBorderColor(QColor color)
248 262 {
249 263 QPen p = d_ptr->m_pen;
250 264 if (p.color() != color) {
251 265 p.setColor(color);
252 266 setPen(p);
253 267 emit borderColorChanged(color);
254 268 }
255 269 }
256 270
257 271 QColor QLegend::borderColor()
258 272 {
259 273 return d_ptr->m_pen.color();
260 274 }
261 275
276 void QLegend::setFont(const QFont &font)
277 {
278 if (d_ptr->m_font != font) {
279 d_ptr->setFont(font);
280 emit fontChanged(font);
281 }
282 }
283
284 QFont QLegend::font() const
285 {
286 return d_ptr->m_font;
287 }
288
262 289 void QLegend::setAlignment(Qt::Alignment alignment)
263 290 {
264 291 if(d_ptr->m_alignment!=alignment) {
265 292 d_ptr->m_alignment = alignment;
266 293 d_ptr->updateLayout();
267 294 }
268 295 }
269 296
270 297 Qt::Alignment QLegend::alignment() const
271 298 {
272 299 return d_ptr->m_alignment;
273 300 }
274 301
275 302 /*!
276 303 Detaches the legend from chart. Chart won't change layout of the legend.
277 304 */
278 305 void QLegend::detachFromChart()
279 306 {
280 307 d_ptr->m_attachedToChart = false;
281 308 }
282 309
283 310 /*!
284 311 Attaches the legend to chart. Chart may change layout of the legend.
285 312 */
286 313 void QLegend::attachToChart()
287 314 {
288 315 d_ptr->attachToChart();
289 316 }
290 317
291 318 /*!
292 319 Returns true, if legend is attached to chart.
293 320 */
294 321 bool QLegend::isAttachedToChart()
295 322 {
296 323 return d_ptr->m_attachedToChart;
297 324 }
298 325
299 326 /*!
300 327 Sets the visibility of legend background to \a visible
301 328 */
302 329 void QLegend::setBackgroundVisible(bool visible)
303 330 {
304 331 if(d_ptr->m_backgroundVisible != visible) {
305 332 d_ptr->m_backgroundVisible = visible;
306 333 update();
307 334 emit backgroundVisibleChanged(visible);
308 335 }
309 336 }
310 337
311 338 /*!
312 339 Returns the visibility of legend background
313 340 */
314 341 bool QLegend::isBackgroundVisible() const
315 342 {
316 343 return d_ptr->m_backgroundVisible;
317 344 }
318 345
319 346 /*!
320 347 \internal \a event see QGraphicsWidget for details
321 348 */
322 349 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
323 350 {
324 351 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
325 352 QGraphicsWidget::resizeEvent(event);
326 353 if(d_ptr->m_rect != rect) {
327 354 d_ptr->m_rect = rect;
328 355 d_ptr->updateLayout();
329 356 }
330 357 }
331 358
332 359 /*!
333 360 \internal \a event see QGraphicsWidget for details
334 361 */
335 362 void QLegend::hideEvent(QHideEvent *event)
336 363 {
337 364 QGraphicsWidget::hideEvent(event);
338 365 setEnabled(false);
339 366 d_ptr->updateLayout();
340 367 }
341 368
342 369 /*!
343 370 \internal \a event see QGraphicsWidget for details
344 371 */
345 372 void QLegend::showEvent(QShowEvent *event)
346 373 {
347 374 QGraphicsWidget::showEvent(event);
348 375 setEnabled(true);
349 376 d_ptr->updateLayout();
350 377 }
351 378
352 379 qreal QLegend::minWidth() const
353 380 {
354 381 return d_ptr->m_minWidth;
355 382 }
356 383
357 384 qreal QLegend::minHeight() const
358 385 {
359 386 return d_ptr->m_minHeight;
360 387 }
361 388
362 389 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
363 390
364 391 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
365 392 q_ptr(q),
366 393 m_presenter(presenter),
367 394 m_chart(chart),
368 395 m_markers(new QGraphicsItemGroup(q)),
369 396 m_alignment(Qt::AlignTop),
370 397 m_brush(QBrush()),
371 398 m_pen(QPen()),
372 399 m_offsetX(0),
373 400 m_offsetY(0),
374 401 m_minWidth(0),
375 402 m_minHeight(0),
376 403 m_width(0),
377 404 m_height(0),
378 405 m_diameter(5),
379 406 m_attachedToChart(true),
380 407 m_backgroundVisible(false)
381 408 {
382 409
383 410 }
384 411
385 412 QLegendPrivate::~QLegendPrivate()
386 413 {
387 414
388 415 }
389 416
390 417 void QLegendPrivate::setOffset(qreal x, qreal y)
391 418 {
392 419 bool scrollHorizontal = true;
393 420 switch(m_alignment) {
394 421 case Qt::AlignTop:
395 422 case Qt::AlignBottom: {
396 423 scrollHorizontal = true;
397 424 break;
398 425 }
399 426 case Qt::AlignLeft:
400 427 case Qt::AlignRight: {
401 428 scrollHorizontal = false;
402 429 break;
403 430 }
404 431 }
405 432
406 433 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
407 434 if (!m_attachedToChart) {
408 435 scrollHorizontal = !scrollHorizontal;
409 436 }
410 437
411 438 // Limit offset between m_minOffset and m_maxOffset
412 439 if (scrollHorizontal) {
413 440 if(m_width<=m_rect.width()) return;
414 441
415 442 if (x != m_offsetX) {
416 443 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
417 444 m_markers->setPos(-m_offsetX,m_rect.top());
418 445 }
419 446 } else {
420 447 if(m_height<=m_rect.height()) return;
421 448
422 449 if (y != m_offsetY) {
423 450 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
424 451 m_markers->setPos(m_rect.left(),-m_offsetY);
425 452 }
426 453 }
427 454 }
428 455
429 456 QPointF QLegendPrivate::offset() const
430 457 {
431 458 return QPointF(m_offsetX,m_offsetY);
432 459 }
433 460
434 461 void QLegendPrivate::updateLayout()
435 462 {
436 463 if (!m_attachedToChart) {
437 464 updateDetachedLayout();
438 465 return;
439 466 }
440 467
441 468 m_offsetX=0;
442 469 QList<QGraphicsItem *> items = m_markers->childItems();
443 470
444 471 if(items.isEmpty()) return;
445 472
446 473 m_minWidth=0;
447 474 m_minHeight=0;
448 475
449 476 switch(m_alignment) {
450 477
451 478 case Qt::AlignTop:
452 479 case Qt::AlignBottom: {
453 480 QPointF point = m_rect.topLeft();
454 481 m_width = 0;
455 482 foreach (QGraphicsItem *item, items) {
456 483 if (item->isVisible()) {
457 484 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
458 485 const QRectF& rect = item->boundingRect();
459 486 qreal w = rect.width();
460 487 m_minWidth=qMax(m_minWidth,w);
461 488 m_minHeight=qMax(m_minHeight,rect.height());
462 489 m_width+=w;
463 490 point.setX(point.x() + w);
464 491 }
465 492 }
466 493 if(m_width<m_rect.width()) {
467 494 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
468 495 }
469 496 else {
470 497 m_markers->setPos(m_rect.topLeft());
471 498 }
472 499 m_height=m_minHeight;
473 500 }
474 501 break;
475 502 case Qt::AlignLeft:
476 503 case Qt::AlignRight: {
477 504 QPointF point = m_rect.topLeft();
478 505 m_height = 0;
479 506 foreach (QGraphicsItem *item, items) {
480 507 if (item->isVisible()) {
481 508 item->setPos(point);
482 509 const QRectF& rect = item->boundingRect();
483 510 qreal h = rect.height();
484 511 m_minWidth=qMax(m_minWidth,rect.width());
485 512 m_minHeight=qMax(m_minHeight,h);
486 513 m_height+=h;
487 514 point.setY(point.y() + h);
488 515 }
489 516 }
490 517 if(m_height<m_rect.height()) {
491 518 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
492 519 }
493 520 else {
494 521 m_markers->setPos(m_rect.topLeft());
495 522 }
496 523 m_width=m_minWidth;
497 524 }
498 525 break;
499 526 }
500 527
501 528 m_minOffsetX = 0;
502 529 m_minOffsetY = 0;
503 530 m_maxOffsetX = m_width - m_rect.width();
504 531 m_maxOffsetY = m_height - m_rect.height();
505 532
506 533 m_presenter->updateLayout();
507 534 }
508 535
509 536 void QLegendPrivate::updateDetachedLayout()
510 537 {
511 538 // Detached layout is different.
512 539 // In detached mode legend may have multiple rows and columns, so layout calculations
513 540 // differ a log from attached mode.
514 541 // Also the scrolling logic is bit different.
515 542 m_offsetX=0;
516 543 m_offsetY=0;
517 544 QList<QGraphicsItem *> items = m_markers->childItems();
518 545
519 546 if(items.isEmpty()) return;
520 547
521 548 m_minWidth = 0;
522 549 m_minHeight = 0;
523 550
524 551 switch (m_alignment) {
525 552 case Qt::AlignTop: {
526 553 QPointF point = m_rect.topLeft();
527 554 m_width = 0;
528 555 m_height = 0;
529 556 for (int i=0; i<items.count(); i++) {
530 557 QGraphicsItem *item = items.at(i);
531 558 if (item->isVisible()) {
532 559 const QRectF& rect = item->boundingRect();
533 560 qreal w = rect.width();
534 561 qreal h = rect.height();
535 562 m_minWidth = qMax(m_minWidth,w);
536 563 m_minHeight = qMax(m_minHeight,rect.height());
537 564 m_height = qMax(m_height,h);
538 565 item->setPos(point.x(),point.y());
539 566 point.setX(point.x() + w);
540 567 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
541 568 // Next item would go off rect.
542 569 point.setX(m_rect.topLeft().x());
543 570 point.setY(point.y() + h);
544 571 if (i+1 < items.count()) {
545 572 m_height += h;
546 573 }
547 574 }
548 575 }
549 576 }
550 577 m_markers->setPos(m_rect.topLeft());
551 578 m_width = m_minWidth;
552 579
553 580 m_minOffsetX = 0;
554 581 m_minOffsetY = 0;
555 582 m_maxOffsetX = m_width - m_rect.width();
556 583 m_maxOffsetY = m_height - m_rect.height();
557 584 }
558 585 break;
559 586 case Qt::AlignBottom: {
560 587 QPointF point = m_rect.bottomLeft();
561 588 m_width = 0;
562 589 m_height = 0;
563 590 for (int i=0; i<items.count(); i++) {
564 591 QGraphicsItem *item = items.at(i);
565 592 if (item->isVisible()) {
566 593 const QRectF& rect = item->boundingRect();
567 594 qreal w = rect.width();
568 595 qreal h = rect.height();
569 596 m_minWidth = qMax(m_minWidth,w);
570 597 m_minHeight = qMax(m_minHeight,rect.height());
571 598 m_height = qMax(m_height,h);
572 599 item->setPos(point.x(),point.y() - h);
573 600 point.setX(point.x() + w);
574 601 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
575 602 // Next item would go off rect.
576 603 point.setX(m_rect.bottomLeft().x());
577 604 point.setY(point.y() - h);
578 605 if (i+1 < items.count()) {
579 606 m_height += h;
580 607 }
581 608 }
582 609 }
583 610 }
584 611 m_markers->setPos(m_rect.topLeft());
585 612 m_width = m_minWidth;
586 613
587 614 m_minOffsetX = 0;
588 615 m_minOffsetY = qMin(m_rect.topLeft().y(), m_rect.topLeft().y() - m_height + m_rect.height());
589 616 m_maxOffsetX = m_width - m_rect.width();
590 617 m_maxOffsetY = 0;
591 618 }
592 619 break;
593 620 case Qt::AlignLeft: {
594 621 QPointF point = m_rect.topLeft();
595 622 m_width = 0;
596 623 m_height = 0;
597 624 qreal maxWidth = 0;
598 625 for (int i=0; i<items.count(); i++) {
599 626 QGraphicsItem *item = items.at(i);
600 627 if (item->isVisible()) {
601 628 const QRectF& rect = item->boundingRect();
602 629 qreal w = rect.width();
603 630 qreal h = rect.height();
604 631 m_minWidth = qMax(m_minWidth,rect.width());
605 632 m_minHeight = qMax(m_minHeight,h);
606 633 maxWidth = qMax(maxWidth,w);
607 634 item->setPos(point.x(),point.y());
608 635 point.setY(point.y() + h);
609 636 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
610 637 // Next item would go off rect.
611 638 point.setX(point.x() + maxWidth);
612 639 point.setY(m_rect.topLeft().y());
613 640 if (i+1 < items.count()) {
614 641 m_width += maxWidth;
615 642 maxWidth = 0;
616 643 }
617 644 }
618 645 }
619 646 }
620 647 m_width += maxWidth;
621 648 m_markers->setPos(m_rect.topLeft());
622 649 m_height = m_minHeight;
623 650
624 651 m_minOffsetX = 0;
625 652 m_minOffsetY = 0;
626 653 m_maxOffsetX = m_width - m_rect.width();
627 654 m_maxOffsetY = m_height - m_rect.height();
628 655 }
629 656 break;
630 657 case Qt::AlignRight: {
631 658 QPointF point = m_rect.topRight();
632 659 m_width = 0;
633 660 m_height = 0;
634 661 qreal maxWidth = 0;
635 662 for (int i=0; i<items.count(); i++) {
636 663 QGraphicsItem *item = items.at(i);
637 664 if (item->isVisible()) {
638 665 const QRectF& rect = item->boundingRect();
639 666 qreal w = rect.width();
640 667 qreal h = rect.height();
641 668 m_minWidth = qMax(m_minWidth,rect.width());
642 669 m_minHeight = qMax(m_minHeight,h);
643 670 maxWidth = qMax(maxWidth,w);
644 671 item->setPos(point.x() - w,point.y());
645 672 point.setY(point.y() + h);
646 673 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
647 674 // Next item would go off rect.
648 675 point.setX(point.x() - maxWidth);
649 676 point.setY(m_rect.topLeft().y());
650 677 if (i+1 < items.count()) {
651 678 m_width += maxWidth;
652 679 maxWidth = 0;
653 680 }
654 681 }
655 682 }
656 683 }
657 684 m_width += maxWidth;
658 685 m_markers->setPos(m_rect.topLeft());
659 686 m_height = m_minHeight;
660 687
661 688 m_minOffsetX = qMin(m_rect.topLeft().x(), m_rect.topLeft().x() - m_width + m_rect.width());
662 689 m_minOffsetY = 0;
663 690 m_maxOffsetX = 0;
664 691 m_maxOffsetY = m_height - m_rect.height();
665 692 }
666 693 break;
667 694 default:
668 695 break;
669 696 }
670 697 }
671 698
672 699 void QLegendPrivate::attachToChart()
673 700 {
674 701 m_attachedToChart = true;
675 702 q_ptr->setParent(m_chart);
676 703 }
677 704
678 705 int QLegendPrivate::roundness(qreal size)
679 706 {
680 707 return 100*m_diameter/int(size);
681 708 }
682 709
710 void QLegendPrivate::setFont(const QFont &font)
711 {
712 m_font = font;
713 QList<QGraphicsItem *> items = m_markers->childItems();
714
715 foreach (QGraphicsItem *markers, items) {
716 LegendMarker *marker = static_cast<LegendMarker*>(markers);
717 marker->setFont(m_font);
718 }
719 updateLayout();
720 }
721
683 722 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
684 723 {
685 724 Q_UNUSED(domain)
686 725
687 726 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
688 foreach(LegendMarker* marker, markers)
727 foreach(LegendMarker* marker, markers) {
728 marker->setFont(m_font);
689 729 m_markers->addToGroup(marker);
730 }
690 731
691 732 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
692 733
693 734 if(series->type() == QAbstractSeries::SeriesTypePie) {
694 735 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
695 736 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
696 737 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
697 738 }
698 739
699 740 updateLayout();
700 741 }
701 742
702 743 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
703 744 {
704 745 QList<QGraphicsItem *> items = m_markers->childItems();
705 746
706 747 foreach (QGraphicsItem *markers, items) {
707 748 LegendMarker *marker = static_cast<LegendMarker*>(markers);
708 749 if (marker->series() == series) {
709 750 delete marker;
710 751 }
711 752 }
712 753
713 754 if(series->type() == QAbstractSeries::SeriesTypePie)
714 755 {
715 756 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
716 757 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
717 758 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
718 759 }
719 760
720 761 updateLayout();
721 762 }
722 763
723 764 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
724 765 {
725 766 // TODO: find out which markers are are added or removed. Update them
726 767 // TODO: better implementation
727 768 handleSeriesRemoved(series);
728 769 Domain domain;
729 770 handleSeriesAdded(series, &domain);
730 771 }
731 772
732 773 void QLegendPrivate::handleUpdatePieSeries()
733 774 {
734 775 //TODO: reimplement to be optimal
735 776 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
736 777 Q_ASSERT(series);
737 778 handleSeriesRemoved(series);
738 779 handleSeriesAdded(series, 0);
739 780 }
740 781
741 782 void QLegendPrivate::handleSeriesVisibleChanged()
742 783 {
743 784 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
744 785 QList<QGraphicsItem *> items = m_markers->childItems();
745 786
746 787 foreach (QGraphicsItem *markers, items) {
747 788 LegendMarker *marker = static_cast<LegendMarker*>(markers);
748 789 if (marker->series() == series) {
749 790 marker->setVisible(!marker->isVisible());
750 791 }
751 792 }
752 793
753 794 updateLayout();
754 795 }
755 796
756 797
757 798 #include "moc_qlegend.cpp"
758 799 #include "moc_qlegend_p.cpp"
759 800
760 801 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,101 +1,106
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 QLEGEND_H
22 22 #define QLEGEND_H
23 23
24 24 #include <QChartGlobal>
25 25 #include <QGraphicsWidget>
26 26 #include <QPen>
27 27 #include <QBrush>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 class Domain;
32 32 class LegendMarker;
33 33 class QPieSlice;
34 34 class QXYSeries;
35 35 class QBarSet;
36 36 class QBarSeries;
37 37 class QPieSeries;
38 38 class QAreaSeries;
39 39 class LegendScrollButton;
40 40 class QChart;
41 41 class QLegendPrivate;
42 42
43 43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
44 44 {
45 45 Q_OBJECT
46 46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
47 47 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
48 48 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
49 49 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
50 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
50 51
51 52 private:
52 53 explicit QLegend(QChart *chart);
53 54
54 55 public:
55 56 ~QLegend();
56 57
57 58 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
58 59 QRectF boundingRect() const;
59 60
60 61 void setBrush(const QBrush &brush);
61 62 QBrush brush() const;
62 63 void setColor(QColor color);
63 64 QColor color();
64 65
65 66 void setPen(const QPen &pen);
66 67 QPen pen() const;
67 68 void setBorderColor(QColor color);
68 69 QColor borderColor();
69 70
71 void setFont(const QFont &font);
72 QFont font() const;
73
70 74 void setAlignment(Qt::Alignment alignment);
71 75 Qt::Alignment alignment() const;
72 76
73 77 void detachFromChart();
74 78 void attachToChart();
75 79 bool isAttachedToChart();
76 80
77 81 qreal minWidth() const;
78 82 qreal minHeight() const;
79 83
80 84 void setBackgroundVisible(bool visible = true);
81 85 bool isBackgroundVisible() const;
82 86
83 87 protected:
84 88 void resizeEvent(QGraphicsSceneResizeEvent *event);
85 89 void hideEvent(QHideEvent *event);
86 90 void showEvent(QShowEvent *event);
87 91
88 92 Q_SIGNALS:
89 93 void backgroundVisibleChanged(bool visible);
90 94 void colorChanged(QColor color);
91 95 void borderColorChanged(QColor color);
96 void fontChanged(QFont font);
92 97
93 98 private:
94 99 QScopedPointer<QLegendPrivate> d_ptr;
95 100 Q_DISABLE_COPY(QLegend)
96 101 friend class LegendScroller;
97 102 };
98 103
99 104 QTCOMMERCIALCHART_END_NAMESPACE
100 105
101 106 #endif // QLEGEND_H
@@ -1,91 +1,93
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 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QLEGEND_P_H
31 31 #define QLEGEND_P_H
32 32
33 33 #include "qlegend.h"
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 class QChart;
38 38 class ChartPresenter;
39 39 class QAbstractSeries;
40 40
41 41 class QLegendPrivate : public QObject
42 42 {
43 43 Q_OBJECT
44 44 public:
45 45 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
46 46 ~QLegendPrivate();
47 47
48 48 void setOffset(qreal x, qreal y);
49 49 QPointF offset() const;
50 50 void updateLayout();
51 51 void updateDetachedLayout();
52 52 void attachToChart();
53 53 int roundness(qreal size);
54 void setFont(const QFont &font);
54 55
55 56 public Q_SLOTS:
56 57 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
57 58 void handleSeriesRemoved(QAbstractSeries *series);
58 59 void handleSeriesUpdated(QAbstractSeries *series);
59 60 void handleUpdatePieSeries(); //TODO remove this function
60 61 void handleSeriesVisibleChanged();
61 62
62 63 private:
63 64 QLegend *q_ptr;
64 65 ChartPresenter *m_presenter;
65 66 QChart* m_chart;
66 67 QGraphicsItemGroup* m_markers;
67 68 Qt::Alignment m_alignment;
68 69 QBrush m_brush;
69 70 QPen m_pen;
71 QFont m_font;
70 72 QRectF m_rect;
71 73 qreal m_offsetX;
72 74 qreal m_offsetY;
73 75 qreal m_minOffsetX;
74 76 qreal m_minOffsetY;
75 77 qreal m_maxOffsetX;
76 78 qreal m_maxOffsetY;
77 79 qreal m_minWidth;
78 80 qreal m_minHeight;
79 81 qreal m_width;
80 82 qreal m_height;
81 83 qreal m_diameter;
82 84 bool m_attachedToChart;
83 85 bool m_backgroundVisible;
84 86
85 87 friend class QLegend;
86 88
87 89 };
88 90
89 91 QTCOMMERCIALCHART_END_NAMESPACE
90 92
91 93 #endif
General Comments 0
You need to be logged in to leave comments. Login now