|
@@
-1,224
+1,223
|
|
1
|
/****************************************************************************
|
|
1
|
/****************************************************************************
|
|
2
|
**
|
|
2
|
**
|
|
3
|
** Copyright (C) 2012 Digia Plc
|
|
3
|
** Copyright (C) 2012 Digia Plc
|
|
4
|
** All rights reserved.
|
|
4
|
** All rights reserved.
|
|
5
|
** For any questions to Digia, please use contact form at http://qt.digia.com
|
|
5
|
** For any questions to Digia, please use contact form at http://qt.digia.com
|
|
6
|
**
|
|
6
|
**
|
|
7
|
** This file is part of the Qt Commercial Charts Add-on.
|
|
7
|
** This file is part of the Qt Commercial Charts Add-on.
|
|
8
|
**
|
|
8
|
**
|
|
9
|
** $QT_BEGIN_LICENSE$
|
|
9
|
** $QT_BEGIN_LICENSE$
|
|
10
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
10
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
11
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
11
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
12
|
** Software or, alternatively, in accordance with the terms contained in
|
|
12
|
** Software or, alternatively, in accordance with the terms contained in
|
|
13
|
** a written agreement between you and Digia.
|
|
13
|
** a written agreement between you and Digia.
|
|
14
|
**
|
|
14
|
**
|
|
15
|
** If you have questions regarding the use of this file, please use
|
|
15
|
** If you have questions regarding the use of this file, please use
|
|
16
|
** contact form at http://qt.digia.com
|
|
16
|
** contact form at http://qt.digia.com
|
|
17
|
** $QT_END_LICENSE$
|
|
17
|
** $QT_END_LICENSE$
|
|
18
|
**
|
|
18
|
**
|
|
19
|
****************************************************************************/
|
|
19
|
****************************************************************************/
|
|
20
|
|
|
20
|
|
|
21
|
#include "mainwidget.h"
|
|
21
|
#include "mainwidget.h"
|
|
22
|
#include <QChart>
|
|
22
|
#include <QChart>
|
|
23
|
#include <QChartView>
|
|
23
|
#include <QChartView>
|
|
24
|
#include <QPushButton>
|
|
24
|
#include <QPushButton>
|
|
25
|
#include <QLabel>
|
|
25
|
#include <QLabel>
|
|
26
|
#include <QDebug>
|
|
26
|
#include <QDebug>
|
|
27
|
#include <QBarSet>
|
|
27
|
#include <QBarSet>
|
|
28
|
#include <QBarSeries>
|
|
28
|
#include <QBarSeries>
|
|
29
|
#include <QLegend>
|
|
29
|
#include <QLegend>
|
|
30
|
#include <QFormLayout>
|
|
30
|
#include <QFormLayout>
|
|
31
|
|
|
31
|
|
|
32
|
QTCOMMERCIALCHART_USE_NAMESPACE
|
|
32
|
QTCOMMERCIALCHART_USE_NAMESPACE
|
|
33
|
|
|
33
|
|
|
34
|
MainWidget::MainWidget(QWidget *parent) :
|
|
34
|
MainWidget::MainWidget(QWidget *parent) :
|
|
35
|
QWidget(parent)
|
|
35
|
QWidget(parent)
|
|
36
|
{
|
|
36
|
{
|
|
37
|
// Create buttons for ui
|
|
37
|
// Create buttons for ui
|
|
38
|
m_buttonLayout = new QGridLayout();
|
|
38
|
m_buttonLayout = new QGridLayout();
|
|
39
|
QPushButton *detachLegendButton = new QPushButton("detach legend");
|
|
39
|
QPushButton *detachLegendButton = new QPushButton("detach legend");
|
|
40
|
connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
|
|
40
|
connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
|
|
41
|
m_buttonLayout->addWidget(detachLegendButton, 0, 0);
|
|
41
|
m_buttonLayout->addWidget(detachLegendButton, 0, 0);
|
|
42
|
QPushButton *attachLegendButton = new QPushButton("attach legend");
|
|
42
|
QPushButton *attachLegendButton = new QPushButton("attach legend");
|
|
43
|
connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
|
|
43
|
connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
|
|
44
|
m_buttonLayout->addWidget(attachLegendButton, 1, 0);
|
|
44
|
m_buttonLayout->addWidget(attachLegendButton, 1, 0);
|
|
45
|
|
|
45
|
|
|
46
|
QPushButton *addSetButton = new QPushButton("add barset");
|
|
46
|
QPushButton *addSetButton = new QPushButton("add barset");
|
|
47
|
connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
|
|
47
|
connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
|
|
48
|
m_buttonLayout->addWidget(addSetButton, 2, 0);
|
|
48
|
m_buttonLayout->addWidget(addSetButton, 2, 0);
|
|
49
|
QPushButton *removeBarsetButton = new QPushButton("remove barset");
|
|
49
|
QPushButton *removeBarsetButton = new QPushButton("remove barset");
|
|
50
|
connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
|
|
50
|
connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
|
|
51
|
m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
|
|
51
|
m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
|
|
52
|
|
|
52
|
|
|
53
|
QPushButton *leftButton = new QPushButton("Align legend left");
|
|
53
|
QPushButton *leftButton = new QPushButton("Align legend left");
|
|
54
|
connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
|
|
54
|
connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
|
|
55
|
m_buttonLayout->addWidget(leftButton, 4, 0);
|
|
55
|
m_buttonLayout->addWidget(leftButton, 4, 0);
|
|
56
|
|
|
56
|
|
|
57
|
QPushButton *rightButton = new QPushButton("Align legend right");
|
|
57
|
QPushButton *rightButton = new QPushButton("Align legend right");
|
|
58
|
connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
|
|
58
|
connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
|
|
59
|
m_buttonLayout->addWidget(rightButton, 5, 0);
|
|
59
|
m_buttonLayout->addWidget(rightButton, 5, 0);
|
|
60
|
|
|
60
|
|
|
61
|
QPushButton *topButton = new QPushButton("Align legend top");
|
|
61
|
QPushButton *topButton = new QPushButton("Align legend top");
|
|
62
|
connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
|
|
62
|
connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
|
|
63
|
m_buttonLayout->addWidget(topButton, 6, 0);
|
|
63
|
m_buttonLayout->addWidget(topButton, 6, 0);
|
|
64
|
|
|
64
|
|
|
65
|
QPushButton *bottomButton = new QPushButton("Align legend bottom");
|
|
65
|
QPushButton *bottomButton = new QPushButton("Align legend bottom");
|
|
66
|
connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
|
|
66
|
connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
|
|
67
|
m_buttonLayout->addWidget(bottomButton, 7, 0);
|
|
67
|
m_buttonLayout->addWidget(bottomButton, 7, 0);
|
|
68
|
|
|
68
|
|
|
69
|
m_legendPosX = new QDoubleSpinBox();
|
|
69
|
m_legendPosX = new QDoubleSpinBox();
|
|
70
|
m_legendPosY = new QDoubleSpinBox();
|
|
70
|
m_legendPosY = new QDoubleSpinBox();
|
|
71
|
m_legendWidth = new QDoubleSpinBox();
|
|
71
|
m_legendWidth = new QDoubleSpinBox();
|
|
72
|
m_legendHeight = new QDoubleSpinBox();
|
|
72
|
m_legendHeight = new QDoubleSpinBox();
|
|
73
|
|
|
73
|
|
|
74
|
connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
|
|
74
|
connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
|
|
75
|
connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
|
|
75
|
connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
|
|
76
|
connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
|
|
76
|
connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
|
|
77
|
connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
|
|
77
|
connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
|
|
78
|
|
|
78
|
|
|
79
|
QFormLayout* legendLayout = new QFormLayout();
|
|
79
|
QFormLayout* legendLayout = new QFormLayout();
|
|
80
|
legendLayout->addRow("Horizontal position", m_legendPosX);
|
|
80
|
legendLayout->addRow("Horizontal position", m_legendPosX);
|
|
81
|
legendLayout->addRow("Vertical position", m_legendPosY);
|
|
81
|
legendLayout->addRow("Vertical position", m_legendPosY);
|
|
82
|
legendLayout->addRow("Width", m_legendWidth);
|
|
82
|
legendLayout->addRow("Width", m_legendWidth);
|
|
83
|
legendLayout->addRow("Height", m_legendHeight);
|
|
83
|
legendLayout->addRow("Height", m_legendHeight);
|
|
84
|
m_legendSettings = new QGroupBox("Detached legend");
|
|
84
|
m_legendSettings = new QGroupBox("Detached legend");
|
|
85
|
m_legendSettings->setLayout(legendLayout);
|
|
85
|
m_legendSettings->setLayout(legendLayout);
|
|
86
|
m_buttonLayout->addWidget(m_legendSettings);
|
|
86
|
m_buttonLayout->addWidget(m_legendSettings);
|
|
87
|
m_legendSettings->setVisible(false);
|
|
87
|
m_legendSettings->setVisible(false);
|
|
88
|
|
|
88
|
|
|
89
|
// Create chart view with the chart
|
|
89
|
// Create chart view with the chart
|
|
90
|
//![1]
|
|
|
|
|
91
|
m_chart = new QChart();
|
|
90
|
m_chart = new QChart();
|
|
92
|
m_chartView = new QChartView(m_chart, this);
|
|
91
|
m_chartView = new QChartView(m_chart, this);
|
|
93
|
//![1]
|
|
|
|
|
94
|
|
|
92
|
|
|
95
|
// Create layout for grid and detached legend
|
|
93
|
// Create layout for grid and detached legend
|
|
96
|
m_mainLayout = new QGridLayout();
|
|
94
|
m_mainLayout = new QGridLayout();
|
|
97
|
m_mainLayout->addLayout(m_buttonLayout, 0, 0);
|
|
95
|
m_mainLayout->addLayout(m_buttonLayout, 0, 0);
|
|
98
|
m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
|
|
96
|
m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
|
|
99
|
setLayout(m_mainLayout);
|
|
97
|
setLayout(m_mainLayout);
|
|
100
|
|
|
98
|
|
|
101
|
createSeries();
|
|
99
|
createSeries();
|
|
102
|
}
|
|
100
|
}
|
|
103
|
|
|
101
|
|
|
104
|
void MainWidget::createSeries()
|
|
102
|
void MainWidget::createSeries()
|
|
105
|
{
|
|
103
|
{
|
|
106
|
//![3]
|
|
|
|
|
107
|
m_series = new QBarSeries();
|
|
104
|
m_series = new QBarSeries();
|
|
108
|
addBarset();
|
|
105
|
addBarset();
|
|
109
|
addBarset();
|
|
106
|
addBarset();
|
|
110
|
addBarset();
|
|
107
|
addBarset();
|
|
111
|
addBarset();
|
|
108
|
addBarset();
|
|
112
|
|
|
109
|
|
|
113
|
m_chart->addSeries(m_series);
|
|
110
|
m_chart->addSeries(m_series);
|
|
114
|
m_chart->setTitle("Legend detach example");
|
|
111
|
m_chart->setTitle("Legend detach example");
|
|
115
|
|
|
112
|
//![1]
|
|
116
|
m_chart->legend()->setVisible(true);
|
|
113
|
m_chart->legend()->setVisible(true);
|
|
117
|
m_chart->legend()->setAlignment(Qt::AlignBottom);
|
|
114
|
m_chart->legend()->setAlignment(Qt::AlignBottom);
|
|
118
|
m_chart->axisY()->setNiceNumbersEnabled(true);
|
|
115
|
//![1]
|
|
119
|
|
|
116
|
|
|
|
|
|
117
|
m_chart->axisY()->setNiceNumbersEnabled(true);
|
|
120
|
m_chartView->setRenderHint(QPainter::Antialiasing);
|
|
118
|
m_chartView->setRenderHint(QPainter::Antialiasing);
|
|
121
|
//![3]
|
|
|
|
|
122
|
}
|
|
119
|
}
|
|
123
|
|
|
120
|
|
|
124
|
void MainWidget::showLegendSpinbox()
|
|
121
|
void MainWidget::showLegendSpinbox()
|
|
125
|
{
|
|
122
|
{
|
|
126
|
m_legendSettings->setVisible(true);
|
|
123
|
m_legendSettings->setVisible(true);
|
|
127
|
QRectF chartViewRect = m_chartView->rect();
|
|
124
|
QRectF chartViewRect = m_chartView->rect();
|
|
128
|
QRectF legendRect = m_chart->legend()->boundingRect();
|
|
125
|
QRectF legendRect = m_chart->legend()->boundingRect();
|
|
129
|
|
|
126
|
|
|
130
|
m_legendPosX->setMinimum(0);
|
|
127
|
m_legendPosX->setMinimum(0);
|
|
131
|
m_legendPosX->setMaximum(chartViewRect.width());
|
|
128
|
m_legendPosX->setMaximum(chartViewRect.width());
|
|
132
|
m_legendPosX->setValue(150);
|
|
129
|
m_legendPosX->setValue(150);
|
|
133
|
|
|
130
|
|
|
134
|
m_legendPosY->setMinimum(0);
|
|
131
|
m_legendPosY->setMinimum(0);
|
|
135
|
m_legendPosY->setMaximum(chartViewRect.height());
|
|
132
|
m_legendPosY->setMaximum(chartViewRect.height());
|
|
136
|
m_legendPosY->setValue(150);
|
|
133
|
m_legendPosY->setValue(150);
|
|
137
|
|
|
134
|
|
|
138
|
m_legendWidth->setMinimum(0);
|
|
135
|
m_legendWidth->setMinimum(0);
|
|
139
|
m_legendWidth->setMaximum(chartViewRect.width());
|
|
136
|
m_legendWidth->setMaximum(chartViewRect.width());
|
|
140
|
m_legendWidth->setValue(150);
|
|
137
|
m_legendWidth->setValue(150);
|
|
141
|
|
|
138
|
|
|
142
|
m_legendHeight->setMinimum(0);
|
|
139
|
m_legendHeight->setMinimum(0);
|
|
143
|
m_legendHeight->setMaximum(chartViewRect.height());
|
|
140
|
m_legendHeight->setMaximum(chartViewRect.height());
|
|
144
|
m_legendHeight->setValue(75);
|
|
141
|
m_legendHeight->setValue(75);
|
|
145
|
}
|
|
142
|
}
|
|
146
|
|
|
143
|
|
|
147
|
void MainWidget::hideLegendSpinbox()
|
|
144
|
void MainWidget::hideLegendSpinbox()
|
|
148
|
{
|
|
145
|
{
|
|
149
|
m_legendSettings->setVisible(false);
|
|
146
|
m_legendSettings->setVisible(false);
|
|
150
|
}
|
|
147
|
}
|
|
151
|
|
|
148
|
|
|
152
|
|
|
149
|
|
|
153
|
void MainWidget::detachLegend()
|
|
150
|
void MainWidget::detachLegend()
|
|
154
|
{
|
|
151
|
{
|
|
155
|
//![4]
|
|
152
|
//![2]
|
|
156
|
QLegend *legend = m_chart->legend();
|
|
153
|
QLegend *legend = m_chart->legend();
|
|
157
|
legend->detachFromChart();
|
|
154
|
legend->detachFromChart();
|
|
158
|
|
|
155
|
|
|
159
|
m_chart->legend()->setBackgroundVisible(true);
|
|
156
|
m_chart->legend()->setBackgroundVisible(true);
|
|
160
|
m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
|
|
157
|
m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
|
|
161
|
//![4]
|
|
158
|
//![2]
|
|
162
|
|
|
159
|
|
|
163
|
showLegendSpinbox();
|
|
160
|
showLegendSpinbox();
|
|
164
|
updateLegendLayout();
|
|
161
|
updateLegendLayout();
|
|
165
|
update();
|
|
162
|
update();
|
|
166
|
}
|
|
163
|
}
|
|
167
|
|
|
164
|
|
|
168
|
|
|
165
|
|
|
169
|
void MainWidget::attachLegend()
|
|
166
|
void MainWidget::attachLegend()
|
|
170
|
{
|
|
167
|
{
|
|
171
|
//![5]
|
|
168
|
//![3]
|
|
172
|
QLegend *legend = m_chart->legend();
|
|
169
|
QLegend *legend = m_chart->legend();
|
|
173
|
legend->attachToChart();
|
|
170
|
legend->attachToChart();
|
|
174
|
m_chart->legend()->setBackgroundVisible(false);
|
|
171
|
m_chart->legend()->setBackgroundVisible(false);
|
|
175
|
//![5]
|
|
172
|
//![3]
|
|
176
|
|
|
173
|
|
|
177
|
hideLegendSpinbox();
|
|
174
|
hideLegendSpinbox();
|
|
178
|
update();
|
|
175
|
update();
|
|
179
|
}
|
|
176
|
}
|
|
180
|
|
|
177
|
|
|
181
|
void MainWidget::addBarset()
|
|
178
|
void MainWidget::addBarset()
|
|
182
|
{
|
|
179
|
{
|
|
183
|
QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount()));
|
|
180
|
QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount()));
|
|
184
|
qreal delta = m_series->barsetCount() * 0.1;
|
|
181
|
qreal delta = m_series->barsetCount() * 0.1;
|
|
185
|
*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);
|
|
182
|
*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);
|
|
186
|
m_series->append(barSet);
|
|
183
|
m_series->append(barSet);
|
|
187
|
}
|
|
184
|
}
|
|
188
|
|
|
185
|
|
|
189
|
void MainWidget::removeBarset()
|
|
186
|
void MainWidget::removeBarset()
|
|
190
|
{
|
|
187
|
{
|
|
191
|
QList<QBarSet*> sets = m_series->barSets();
|
|
188
|
QList<QBarSet*> sets = m_series->barSets();
|
|
192
|
if (sets.count() > 0) {
|
|
189
|
if (sets.count() > 0) {
|
|
193
|
m_series->remove(sets.at(sets.count()-1));
|
|
190
|
m_series->remove(sets.at(sets.count()-1));
|
|
194
|
}
|
|
191
|
}
|
|
195
|
}
|
|
192
|
}
|
|
196
|
|
|
193
|
|
|
197
|
void MainWidget::setLegendLeft()
|
|
194
|
void MainWidget::setLegendLeft()
|
|
198
|
{
|
|
195
|
{
|
|
199
|
m_chart->legend()->setAlignment(Qt::AlignLeft);
|
|
196
|
m_chart->legend()->setAlignment(Qt::AlignLeft);
|
|
200
|
}
|
|
197
|
}
|
|
201
|
|
|
198
|
|
|
202
|
void MainWidget::setLegendRight()
|
|
199
|
void MainWidget::setLegendRight()
|
|
203
|
{
|
|
200
|
{
|
|
204
|
m_chart->legend()->setAlignment(Qt::AlignRight);
|
|
201
|
m_chart->legend()->setAlignment(Qt::AlignRight);
|
|
205
|
}
|
|
202
|
}
|
|
206
|
|
|
203
|
|
|
207
|
void MainWidget::setLegendTop()
|
|
204
|
void MainWidget::setLegendTop()
|
|
208
|
{
|
|
205
|
{
|
|
209
|
m_chart->legend()->setAlignment(Qt::AlignTop);
|
|
206
|
m_chart->legend()->setAlignment(Qt::AlignTop);
|
|
210
|
}
|
|
207
|
}
|
|
211
|
|
|
208
|
|
|
212
|
void MainWidget::setLegendBottom()
|
|
209
|
void MainWidget::setLegendBottom()
|
|
213
|
{
|
|
210
|
{
|
|
214
|
m_chart->legend()->setAlignment(Qt::AlignBottom);
|
|
211
|
m_chart->legend()->setAlignment(Qt::AlignBottom);
|
|
215
|
}
|
|
212
|
}
|
|
216
|
|
|
213
|
|
|
217
|
void MainWidget::updateLegendLayout()
|
|
214
|
void MainWidget::updateLegendLayout()
|
|
218
|
{
|
|
215
|
{
|
|
|
|
|
216
|
//![4]
|
|
219
|
m_chart->legend()->setGeometry(m_legendPosX->value()
|
|
217
|
m_chart->legend()->setGeometry(m_legendPosX->value()
|
|
220
|
,m_legendPosY->value()
|
|
218
|
,m_legendPosY->value()
|
|
221
|
,m_legendWidth->value()
|
|
219
|
,m_legendWidth->value()
|
|
222
|
,m_legendHeight->value());
|
|
220
|
,m_legendHeight->value());
|
|
223
|
m_chart->legend()->update();
|
|
221
|
m_chart->legend()->update();
|
|
|
|
|
222
|
//![4]
|
|
224
|
}
|
|
223
|
}
|