##// END OF EJS Templates
Added remove single box...
Mika Salmela -
r2482:63ca3532f831
parent child
Show More
@@ -1,198 +1,215
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 "boxplotchartitem_p.h"
22 22 #include "qboxplotseries_p.h"
23 23 #include "bar_p.h"
24 24 #include "qbarset_p.h"
25 25 #include "qabstractbarseries_p.h"
26 26 #include "qbarset.h"
27 27 #include "boxwhiskers_p.h"
28 28 #include <QPainter>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 BoxPlotChartItem::BoxPlotChartItem(QBoxPlotSeries *series, QGraphicsItem* item) :
33 33 ChartItem(series->d_func(), item),
34 34 m_series(series),
35 35 m_animation(0),
36 36 m_animate(0)
37 37 {
38 connect(series, SIGNAL(barsetsRemoved(QList<QBarSet*>)), this, SLOT(handleBarsetRemove(QList<QBarSet*>)));
38 39 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
39 40 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
40 41 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
41 42 connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdatedBars()));
42 43 // QBoxPlotSeriesPrivate calls handleDataStructureChanged(), don't do it here
43 44 setZValue(ChartPresenter::BoxPlotSeriesZValue);
44 45
45 46 m_barSets = m_series->barSets();
46 47 }
47 48
48 49 BoxPlotChartItem::~BoxPlotChartItem()
49 50 {
50 51 qDebug() << "BoxPlotChartItem::~BoxPlotChartItem() " << m_seriesIndex;
51 52 }
52 53
53 54 void BoxPlotChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
54 55 {
55 56 Q_UNUSED(painter);
56 57 Q_UNUSED(option);
57 58 Q_UNUSED(widget);
58 59
59 60 //painter->setClipRect(QRectF(QPointF(0,0),domain()->size()));
60 61
61 62 //qDebug() << "ALERT EMPTY: BoxPlotChartItem::paint";
62 63 }
63 64
64 65 void BoxPlotChartItem::setAnimation(BoxPlotAnimation *animation)
65 66 {
66 67 //qDebug() << "BoxPlotChartItem::setAnimation :" << animation;
67 68
68 69 m_animation = animation;
69 70 if (m_animation) {
70 71 foreach (BoxWhiskers *item, m_boxTable.values()) {
71 72 m_animation->addBox(item);
72 73 }
73 74 handleDomainUpdated();
74 75 }
75 76 }
76 77
77 78 void BoxPlotChartItem::handleDataStructureChanged()
78 79 {
79 //qDebug() << "BoxPlotChartItem::handleDataStructureChanged()";
80 qDebug() << "BoxPlotChartItem::handleDataStructureChanged()";
81
80 82 int setCount = m_series->count();
81 83
82 84 for (int s = 0; s < setCount; s++) {
83 85 QBarSet *set = m_series->d_func()->barsetAt(s);
84 86
85 87 BoxWhiskers *boxWhiskersItem = m_boxTable.value(set);
86 88 if (boxWhiskersItem == 0) {
87 89 // Item is not yet created, make a box and add it to hash table
88 90 boxWhiskersItem = new BoxWhiskers(domain(), this);
89 91 m_boxTable.insert(set, boxWhiskersItem);
90 92
91 93 boxWhiskersItem->setBrush(m_series->brush());
92 94 boxWhiskersItem->setPen(m_series->pen());
93 95 }
94 96 updateBoxGeometry(boxWhiskersItem, s);
95 97
96 98 boxWhiskersItem->updateGeometry();
97 99
98 100 if (m_animation)
99 101 m_animation->addBox(boxWhiskersItem);
100 102 }
101 103
102 104 //
103 105 handleDomainUpdated();
104 106 }
105 107
106 108 void BoxPlotChartItem::handleUpdatedBars()
107 109 {
108 //qDebug() << "BoxPlotChartItem::handleUpdatedBars()";
110 qDebug() << "BoxPlotChartItem::handleUpdatedBars()";
109 111
110 112 foreach (BoxWhiskers *item, m_boxTable.values()) {
111 113 item->setBrush(m_series->brush());
112 114 item->setPen(m_series->pen());
113 115 }
114 116 }
115 117
118 void BoxPlotChartItem::handleBarsetRemove(QList<QBarSet*> barSets)
119 {
120 //qDebug() << "BoxPlotChartItem::handleBarsetRemove";
121
122 foreach (QBarSet *set, barSets) {
123 BoxWhiskers *boxItem = m_boxTable.value(set);
124 m_boxTable.remove(set);
125 delete boxItem;
126 }
127
128 // We trust that series emits the restructuredBars, which handles restructuring
129 }
130
116 131 void BoxPlotChartItem::handleDomainUpdated()
117 132 {
118 133 //qDebug() << "BoxPlotChartItem::handleDomainUpdated() domain()->size() = " << domain()->size();
119 134
120 135 if ((domain()->size().width() <= 0) || (domain()->size().height() <= 0))
121 136 return;
122 137
123 138 // Set my bounding rect to same as domain size
124 139 m_boundingRect.setRect(0.0, 0.0, domain()->size().width(), domain()->size().height());
125 140
126 141 foreach (BoxWhiskers *item, m_boxTable.values()) {
127 142 // Update the domain size for each BoxWhisker item
128 143 item->setDomainSize(domain()->size());
129 144
130 145 // If the animation is set, start the animation for each BoxWhisker item
131 146 if (m_animation) {
132 147 presenter()->startAnimation(m_animation->boxAnimation(item));
133 148 }
134 149 }
135 150 }
136 151
137 152 void BoxPlotChartItem::handleLayoutChanged()
138 153 {
154 qDebug() << "BoxPlotChartItem::handleLayoutChanged";
155
139 156 foreach (BoxWhiskers *item, m_boxTable.values()) {
140 157 if (m_animation)
141 158 m_animation->setAnimationStart(item);
142 159
143 160 bool dirty = updateBoxGeometry(item, item->m_data.m_index);
144 161 if (dirty && m_animation)
145 162 presenter()->startAnimation(m_animation->boxChangeAnimation(item));
146 163 else
147 164 item->updateGeometry();
148 165 }
149 166 }
150 167
151 168 QRectF BoxPlotChartItem::boundingRect() const
152 169 {
153 170 return m_boundingRect;
154 171 }
155 172
156 173 void BoxPlotChartItem::initializeLayout()
157 174 {
158 175 qDebug() << "ALERT EMPTY: BoxPlotChartItem::initializeLayout";
159 176 }
160 177
161 178 QVector<QRectF> BoxPlotChartItem::calculateLayout()
162 179 {
163 180 return QVector<QRectF>();
164 181 }
165 182
166 183 bool BoxPlotChartItem::updateBoxGeometry(BoxWhiskers *box, int index)
167 184 {
168 185 bool changed = false;
169 186
170 187 QBarSet *set = m_series->d_func()->barsetAt(index);
171 188 BoxWhiskersData &data = box->m_data;
172 189
173 190 if ((data.m_lowerExtreme != set->at(0)) || (data.m_lowerQuartile != set->at(1)) ||
174 191 (data.m_median != set->at(2)) || (data.m_upperQuartile != set->at(3)) || (data.m_upperExtreme != set->at(4)))
175 192 changed = true;
176 193
177 194 data.m_lowerExtreme = set->at(0);
178 195 data.m_lowerQuartile = set->at(1);
179 196 data.m_median = set->at(2);
180 197 data.m_upperQuartile = set->at(3);
181 198 data.m_upperExtreme = set->at(4);
182 199 data.m_index = index;
183 200 data.m_boxItems = m_series->count();
184 201
185 202 data.m_maxX = domain()->maxX();
186 203 data.m_minX = domain()->minX();
187 204 data.m_maxY = domain()->maxY();
188 205 data.m_minY = domain()->minY();
189 206
190 207 data.m_seriesIndex = m_seriesIndex;
191 208 data.m_seriesCount = m_seriesCount;
192 209
193 210 return changed;
194 211 }
195 212
196 213 #include "moc_boxplotchartitem_p.cpp"
197 214
198 215 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,86 +1,88
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
31 31 #ifndef BOXPLOTCHARTITEM_H
32 32 #define BOXPLOTCHARTITEM_H
33 33
34 34 #include "boxwhiskers_p.h"
35 35 #include "qboxplotseries.h"
36 36 #include "chartitem_p.h"
37 37 #include "boxplotanimation_p.h"
38 #include "qbarset.h"
38 39 #include <QGraphicsItem>
39 40
40 41 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41 42
42 43 class BoxPlotSeriesPrivate;
43 44
44 45 class BoxPlotChartItem : public ChartItem
45 46 {
46 47 Q_OBJECT
47 48 public:
48 49 BoxPlotChartItem(QBoxPlotSeries *series, QGraphicsItem* item =0);
49 50 ~BoxPlotChartItem();
50 51
51 52 void setAnimation(BoxPlotAnimation *animation);
52 53
53 54 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
54 55 QRectF boundingRect() const;
55 56
56 57 public Q_SLOTS:
57 58 void handleDataStructureChanged();
58 59 void handleDomainUpdated();
59 60 void handleLayoutChanged();
60 61 void handleUpdatedBars();
62 void handleBarsetRemove(QList<QBarSet*> barSets);
61 63
62 64 private:
63 65 virtual QVector<QRectF> calculateLayout();
64 66 void initializeLayout();
65 67 bool updateBoxGeometry(BoxWhiskers *box, int index);
66 68
67 69 protected:
68 70 friend class QBoxPlotSeriesPrivate;
69 71 QBoxPlotSeries *m_series; // Not owned.
70 72 QList<BoxWhiskers *> m_boxes;
71 73 QHash<QBarSet *, BoxWhiskers *> m_boxTable;
72 74 QList<QBarSet *> m_barSets;
73 75 int m_seriesIndex;
74 76 int m_seriesCount;
75 77
76 78 BoxPlotAnimation *m_animation;
77 79 bool m_animate;
78 80
79 81 QRectF m_boundingRect;
80 82
81 83 //QList<BoxPlotAnimation *> m_animations;
82 84 };
83 85
84 86 QTCOMMERCIALCHART_END_NAMESPACE
85 87
86 88 #endif // BOXPLOTCHARTITEM_H
@@ -1,189 +1,190
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 "boxwhiskers_p.h"
22 22 #include <QPainter>
23 23 #include <QDebug>
24 24 #include <QWidget>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 BoxWhiskers::BoxWhiskers(AbstractDomain *domain, QGraphicsObject *parent) :
29 29 QGraphicsObject(parent),
30 30 m_domain(domain)
31 31 {
32 //qDebug() << "BoxWhiskers::BoxWhiskers()";
32 33 }
33 34
34 35 BoxWhiskers::~BoxWhiskers()
35 36 {
36 37 //qDebug() << "BoxWhiskers::~BoxWhiskers()";
37 38 }
38 39
39 40 void BoxWhiskers::mousePressEvent(QGraphicsSceneMouseEvent *event)
40 41 {
41 42 Q_UNUSED(event)
42 43
43 44 qDebug() << "BoxWhiskers::mousePressEvent";
44 45 }
45 46
46 47 void BoxWhiskers::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
47 48 {
48 49 Q_UNUSED(event)
49 50
50 51 qDebug() << "BoxWhiskers::hoverEnterEvent";
51 52 }
52 53
53 54 void BoxWhiskers::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
54 55 {
55 56 Q_UNUSED(event)
56 57
57 58 qDebug() << "BoxWhiskers::hoverLeaveEvent";
58 59 }
59 60
60 61 void BoxWhiskers::setBrush(const QBrush &brush)
61 62 {
62 63 m_brush = brush;
63 64 }
64 65
65 66 void BoxWhiskers::setPen(const QPen &pen)
66 67 {
67 68 m_pen = pen;
68 69 }
69 70
70 71 void BoxWhiskers::setLayout(const BoxWhiskersData &data)
71 72 {
72 73 m_data = data;
73 74 // if (m_data.m_index == 1)
74 75 // qDebug() << "BoxWhiskers::setLayout";
75 76 updateGeometry();
76 77 update();
77 78 }
78 79
79 80
80 81 QSizeF BoxWhiskers::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
81 82 {
82 83 //Q_UNUSED(which)
83 84 Q_UNUSED(constraint)
84 85
85 86 qDebug() << "BoxWhiskers::sizeHint, which = " << which;
86 87
87 88 return QSizeF();
88 89 }
89 90
90 91 void BoxWhiskers::setGeometry(const QRectF &rect) // TODO: Unused?
91 92 {
92 93 Q_UNUSED(rect)
93 94
94 95 qDebug() << "BoxWhiskers::setGeometry";
95 96 }
96 97
97 98 void BoxWhiskers::setDomainSize(const QSizeF &size)
98 99 {
99 100 m_domainSize = size;
100 101
101 102 updateBoundingRect();
102 103 }
103 104
104 105 QRectF BoxWhiskers::boundingRect() const
105 106 {
106 107 return m_boundingRect;
107 108 }
108 109
109 110 void BoxWhiskers::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
110 111 {
111 112 Q_UNUSED(option)
112 113 Q_UNUSED(widget)
113 114 //Q_UNUSED(painter)
114 115
115 116 //painter->save();
116 117 //painter->setClipRect(parentItem()->boundingRect());
117 118 painter->setPen(m_pen);
118 119 painter->setBrush(m_brush);
119 120 qreal spanY = m_data.m_maxY - m_data.m_minY;
120 121 //painter->setClipRect(parentItem()->boundingRect());
121 122 painter->scale(m_domainSize.width() / m_data.m_boxItems, m_domainSize.height() / spanY);
122 123 painter->drawPath(m_boxPath);
123 124 //painter->restore();
124 125 }
125 126
126 127 void BoxWhiskers::updateGeometry()
127 128 {
128 129 prepareGeometryChange();
129 130
130 131 QPainterPath path;
131 132
132 133 qreal columnWidth = 1.0 / m_data.m_seriesCount;
133 134 qreal left = 0.25 * columnWidth + columnWidth * m_data.m_seriesIndex;
134 135 qreal right = 0.75 * columnWidth + columnWidth * m_data.m_seriesIndex;
135 136 qreal middle = 0.5 * columnWidth + columnWidth * m_data.m_seriesIndex;
136 137
137 138 //whisker = 0.35 0.75
138 139
139 140 // Upper whisker
140 141 path.moveTo(left + m_data.m_index, m_data.m_maxY - m_data.m_upperExtreme);
141 142 path.lineTo(right + m_data.m_index, m_data.m_maxY - m_data.m_upperExtreme);
142 143 path.moveTo(middle + m_data.m_index, m_data.m_maxY - m_data.m_upperExtreme);
143 144 path.lineTo(middle + m_data.m_index, m_data.m_maxY - m_data.m_upperQuartile);
144 145 path.closeSubpath();
145 146
146 147 // Middle Box
147 148 path.addRect(left + m_data.m_index, m_data.m_maxY - m_data.m_upperQuartile,
148 149 0.5 * columnWidth, m_data.m_upperQuartile - m_data.m_lowerQuartile);
149 150
150 151 // Median/mean line
151 152 path.moveTo(left + m_data.m_index, m_data.m_maxY - m_data.m_median);
152 153 path.lineTo(right + m_data.m_index, m_data.m_maxY - m_data.m_median);
153 154
154 155 // Lower whisker
155 156 path.moveTo(left + m_data.m_index, m_data.m_maxY - m_data.m_lowerExtreme);
156 157 path.lineTo(right + m_data.m_index, m_data.m_maxY - m_data.m_lowerExtreme);
157 158 path.moveTo(middle + m_data.m_index, m_data.m_maxY - m_data.m_lowerExtreme);
158 159 path.lineTo(middle + m_data.m_index, m_data.m_maxY - m_data.m_lowerQuartile);
159 160 path.closeSubpath();
160 161
161 162 m_boxPath = path;
162 163
163 164 updateBoundingRect();
164 165
165 166 // qreal scaleY = m_domainSize.height() / (m_data.m_maxY - m_data.m_minY);
166 167 // qreal scaleX = m_domainSize.width() / m_data.m_boxItems;
167 168 // QRectF br = path.boundingRect();
168 169 // m_boundingRect = QRectF( br.x() * scaleX, br.y() * scaleY, br.width() * scaleX, br.height() * scaleY);
169 170
170 171 if (m_data.m_index == 5) {
171 172 //qDebug() << "myValue = " << myValue;
172 173 //qDebug() << "m_data.m_upperExtreme" << m_data.m_upperExtreme;
173 174 //qDebug() << "m_boundingRect = " << m_boundingRect;
174 175 // qDebug() << "x = " << m_boundingRect.x() << ", y = " << m_boundingRect.y() << ", width = "
175 176 // << m_boundingRect.width() << ", height = " << m_boundingRect.height();
176 177 }
177 178 }
178 179
179 180 void BoxWhiskers::updateBoundingRect()
180 181 {
181 182 qreal scaleY = m_domainSize.height() / (m_data.m_maxY - m_data.m_minY);
182 183 qreal scaleX = m_domainSize.width() / m_data.m_boxItems;
183 184 QRectF br = m_boxPath.boundingRect();
184 185 m_boundingRect = QRectF( br.x() * scaleX, br.y() * scaleY, br.width() * scaleX, br.height() * scaleY);
185 186 }
186 187
187 188 #include "moc_boxwhiskers_p.cpp"
188 189
189 190 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,283 +1,303
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 "customtablemodel.h"
23 23 #include <QVBarModelMapper>
24 24 #include <QTableView>
25 25 #include <QHeaderView>
26 26 #include <QChartView>
27 27 #include <QBoxPlotSeries>
28 28 #include <QBarSet>
29 29 #include <QLegend>
30 30 #include <QBarCategoryAxis>
31 31 #include <QBrush>
32 32 #include <QColor>
33 33 #include <QPushButton>
34 34 #include <QComboBox>
35 35 #include <QSpinBox>
36 36 #include <QCheckBox>
37 37 #include <QGridLayout>
38 38 #include <QHBoxLayout>
39 39 #include <QLabel>
40 40 #include <QSpacerItem>
41 41 #include <QMessageBox>
42 42 #include <cmath>
43 43 #include <QDebug>
44 44 #include <QStandardItemModel>
45 45 #include <QBarCategoryAxis>
46 46
47 47
48 48 QTCOMMERCIALCHART_USE_NAMESPACE
49 49
50 50 QString addCategories[] = {"Jul", "Aug", "Sep", "Nov", "Dec"};
51 51
52 52 MainWidget::MainWidget(QWidget *parent) :
53 53 QWidget(parent),
54 54 m_chart(0),
55 55 rowPos(0),
56 56 nSeries(0),
57 57 nNewBoxes(0)
58 58 {
59 59 m_chart = new QChart();
60 60
61 61 // Grid layout for the controls for configuring the chart widget
62 62 QGridLayout *grid = new QGridLayout();
63 63
64 64 // Create add a series button
65 65 QPushButton *addSeriesButton = new QPushButton("Add a series");
66 66 connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries()));
67 67 grid->addWidget(addSeriesButton, rowPos++, 1);
68 68
69 69 // Create remove a series button
70 70 QPushButton *removeSeriesButton = new QPushButton("Remove a series");
71 71 connect(removeSeriesButton, SIGNAL(clicked()), this, SLOT(removeSeries()));
72 72 grid->addWidget(removeSeriesButton, rowPos++, 1);
73 73
74
75 74 // Create add a single box button
76 75 QPushButton *addBoxButton = new QPushButton("Add a box");
77 76 connect(addBoxButton, SIGNAL(clicked()), this, SLOT(addBox()));
78 77 grid->addWidget(addBoxButton, rowPos++, 1);
79 78
79 // Create add a single box button
80 QPushButton *removeBoxButton = new QPushButton("Remove a box");
81 connect(removeBoxButton, SIGNAL(clicked()), this, SLOT(removeBox()));
82 grid->addWidget(removeBoxButton, rowPos++, 1);
83
80 84 initThemeCombo(grid);
81 85 initCheckboxes(grid);
82 86
83 87 m_model = new CustomTableModel;
84 88 QTableView *tableView = new QTableView;
85 89 tableView->setModel(m_model);
86 90 tableView->setMaximumWidth(200);
87 91 grid->addWidget(tableView, rowPos++, 0, 3, 2, Qt::AlignLeft);
88 92 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
89 93 tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
90 94 tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
91 95 #else
92 96 tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
93 97 tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
94 98 #endif
95 99
96 100 // add row with empty label to make all the other rows static
97 101 grid->addWidget(new QLabel(""), grid->rowCount(), 0);
98 102 grid->setRowStretch(grid->rowCount() - 1, 1);
99 103
100 104 // Create chart view with the chart
101 105 m_chartView = new QChartView(m_chart, this);
102 106 //m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
103 107
104 108 // Another grid layout as a main layout
105 109 QGridLayout *mainLayout = new QGridLayout();
106 110 mainLayout->addLayout(grid, 0, 0);
107 111 mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
108 112 setLayout(mainLayout);
109 113
110 114 legendToggled(false);
111 115 animationToggled(false);
112 116 }
113 117
114 118 // Combo box for selecting theme
115 119 void MainWidget::initThemeCombo(QGridLayout *grid)
116 120 {
117 121 QComboBox *chartTheme = new QComboBox();
118 122 chartTheme->addItem("Default");
119 123 chartTheme->addItem("Light");
120 124 chartTheme->addItem("Blue Cerulean");
121 125 chartTheme->addItem("Dark");
122 126 chartTheme->addItem("Brown Sand");
123 127 chartTheme->addItem("Blue NCS");
124 128 chartTheme->addItem("High Contrast");
125 129 chartTheme->addItem("Blue Icy");
126 130 connect(chartTheme, SIGNAL(currentIndexChanged(int)),
127 131 this, SLOT(changeChartTheme(int)));
128 132 grid->addWidget(new QLabel("Chart theme:"), rowPos, 0);
129 133 grid->addWidget(chartTheme, rowPos++, 1);
130 134 }
131 135
132 136 // Different check boxes for customizing chart
133 137 void MainWidget::initCheckboxes(QGridLayout *grid)
134 138 {
135 139 QCheckBox *animationCheckBox = new QCheckBox("Animation");
136 140 connect(animationCheckBox, SIGNAL(toggled(bool)), this, SLOT(animationToggled(bool)));
137 141 animationCheckBox->setChecked(false);
138 142 grid->addWidget(animationCheckBox, rowPos++, 0);
139 143
140 144 QCheckBox *legendCheckBox = new QCheckBox("Legend");
141 145 connect(legendCheckBox, SIGNAL(toggled(bool)), this, SLOT(legendToggled(bool)));
142 146 legendCheckBox->setChecked(false);
143 147 grid->addWidget(legendCheckBox, rowPos++, 0);
144 148
145 149 QCheckBox *titleCheckBox = new QCheckBox("Title");
146 150 connect(titleCheckBox, SIGNAL(toggled(bool)), this, SLOT(titleToggled(bool)));
147 151 titleCheckBox->setChecked(false);
148 152 grid->addWidget(titleCheckBox, rowPos++, 0);
149 153
150 154 QCheckBox *modelMapperCheckBox = new QCheckBox("Use model mapper");
151 155 connect(modelMapperCheckBox, SIGNAL(toggled(bool)), this, SLOT(modelMapperToggled(bool)));
152 156 modelMapperCheckBox->setChecked(false);
153 157 grid->addWidget(modelMapperCheckBox, rowPos++, 0);
154 158
155 159 }
156 160
157 161 void MainWidget::addSeries()
158 162 {
159 163 qDebug() << "BoxPlotTester::MainWidget::addSeries()";
160 164
161 165 if (nSeries > 9)
162 166 return;
163 167
164 168 // Initial data
165 169 //![1]
166 170 QBarSet *set0 = new QBarSet("Jan");
167 171 QBarSet *set1 = new QBarSet("Feb");
168 172 QBarSet *set2 = new QBarSet("Mar");
169 173 QBarSet *set3 = new QBarSet("Apr");
170 174 QBarSet *set4 = new QBarSet("May");
171 175 QBarSet *set5 = new QBarSet("Jun");
172 176
173 177 // low bot med top upp
174 178 *set0 << 3 << 4 << 4.4 << 6 << 7;
175 179 *set1 << 5 << 6 << 7.5 << 8 << 12;
176 180 *set2 << 3 << 5 << 5.7 << 8 << 9;
177 181 *set3 << 5 << 6 << 6.8 << 7 << 8;
178 182 *set4 << 4 << 5 << 5.2 << 6 << 7;
179 183 *set5 << 4 << 7 << 8.2 << 9 << 10;
180 184
181 185 m_series[nSeries] = new QBoxPlotSeries();
182 186 m_series[nSeries]->append(set0);
183 187 m_series[nSeries]->append(set1);
184 188 m_series[nSeries]->append(set2);
185 189 m_series[nSeries]->append(set3);
186 190 m_series[nSeries]->append(set4);
187 191 m_series[nSeries]->append(set5);
188 192 m_series[nSeries]->type();
189 193 m_series[nSeries]->setName("Box & Whiskers");
190 194
191 195 m_chart->addSeries(m_series[nSeries]);
192 196
193 197 if (nSeries == 0) {
194 198 QStringList categories;
195 199 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
196 200 m_axis = new QBarCategoryAxis();
197 201 m_axis->append(categories);
198 202 m_chart->createDefaultAxes();
199 203 m_chart->setAxisX(m_axis, m_series[nSeries]);
200 204 }
201 205
202 206 nSeries++;
203 207 }
204 208
205 209 void MainWidget::removeSeries()
206 210 {
207 211 if (nSeries > 0) {
208 212 nSeries--;
209 213 m_chart->removeSeries(m_series[nSeries]);
210 214 delete m_series[nSeries];
215 } else {
216 qDebug() << "Create a series first";
211 217 }
212 218 }
213 219
214 220 void MainWidget::addBox()
215 221 {
216 222 qDebug() << "BoxPlotTester::MainWidget::addBox()";
217 223
218 QBarSet *newSet = new QBarSet("New");
219 *newSet << 5 << 6 << 6.8 << 7 << 8;
224 if (nSeries > 0) {
225 QBarSet *newSet = new QBarSet("New");
226 *newSet << 5 << 6 << 6.8 << 7 << 8;
220 227
221 m_series[0]->append(newSet);
228 m_series[0]->append(newSet);
222 229
223 m_axis->append(addCategories[nNewBoxes]);
230 m_axis->append(addCategories[nNewBoxes]);
224 231
225 nNewBoxes++;
232 nNewBoxes++;
233 }
234 }
235
236 void MainWidget::removeBox()
237 {
238 qDebug() << "MainWidget::removeBox";
239
240 if (nSeries > 0) {
241 QList<QBarSet *> sets = m_series[0]->barSets();
242 m_series[0]->remove(sets.at(m_series[0]->count() - 3));
243 } else {
244 qDebug() << "Create a series first";
245 }
226 246 }
227 247
228 248 void MainWidget::animationToggled(bool enabled)
229 249 {
230 250 qDebug() << "BoxPlotTester::Animation toggled to " << enabled;
231 251 if (enabled)
232 252 m_chart->setAnimationOptions(QChart::SeriesAnimations);
233 253 else
234 254 m_chart->setAnimationOptions(QChart::NoAnimation);
235 255 }
236 256
237 257 void MainWidget::legendToggled(bool enabled)
238 258 {
239 259 qDebug() << "BoxPlotTester::Legend toggled to " << enabled;
240 260 m_chart->legend()->setVisible(enabled);
241 261 if (enabled)
242 262 m_chart->legend()->setAlignment(Qt::AlignBottom);
243 263 }
244 264
245 265 void MainWidget::titleToggled(bool enabled)
246 266 {
247 267 qDebug() << "BoxPlotTester::Title toggled to " << enabled;
248 268 if (enabled)
249 269 m_chart->setTitle("Simple boxplotchart example");
250 270 else
251 271 m_chart->setTitle("");
252 272 }
253 273
254 274 void MainWidget::modelMapperToggled(bool enabled)
255 275 {
256 276 if (enabled) {
257 277 m_series[nSeries] = new QBoxPlotSeries();
258 278
259 279 int first = 0;
260 280 int count = 5;
261 281 QVBarModelMapper *mapper = new QVBarModelMapper(this);
262 282 mapper->setFirstBarSetColumn(0);
263 283 mapper->setLastBarSetColumn(5);
264 284 mapper->setFirstRow(first);
265 285 mapper->setRowCount(count);
266 286 mapper->setSeries(m_series[nSeries]);
267 287 mapper->setModel(m_model);
268 288 m_chart->addSeries(m_series[nSeries]);
269 289
270 290 nSeries++;
271 291 } else {
272 292 removeSeries();
273 293 }
274 294 }
275 295
276 296 void MainWidget::changeChartTheme(int themeIndex)
277 297 {
278 298 qDebug() << "BoxPlotTester::changeChartTheme: " << themeIndex;
279 299 if (themeIndex == 0)
280 300 m_chart->setTheme(QChart::ChartThemeLight);
281 301 else
282 302 m_chart->setTheme((QChart::ChartTheme) (themeIndex - 1));
283 303 }
@@ -1,70 +1,71
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 "customtablemodel.h"
28 28 #include <QWidget>
29 29 #include <QBoxPlotSeries>
30 30 #include <QBarCategoryAxis>
31 31
32 32 class QGridLayout;
33 33
34 34 QTCOMMERCIALCHART_USE_NAMESPACE
35 35
36 36 class MainWidget : public QWidget
37 37 {
38 38 Q_OBJECT
39 39 public:
40 40 explicit MainWidget(QWidget *parent = 0);
41 41
42 42 signals:
43 43
44 44 private:
45 45 void initThemeCombo(QGridLayout *grid);
46 46 void initCheckboxes(QGridLayout *grid);
47 47
48 48 private slots:
49 49 void addSeries();
50 50 void removeSeries();
51 51 void addBox();
52 void removeBox();
52 53 void animationToggled(bool enabled);
53 54 void legendToggled(bool enabled);
54 55 void titleToggled(bool enabled);
55 56 void modelMapperToggled(bool enabled);
56 57 void changeChartTheme(int themeIndex);
57 58
58 59 private:
59 60 QChart *m_chart;
60 61 QChartView *m_chartView;
61 62 QGridLayout *m_scatterLayout;
62 63 QBarCategoryAxis *m_axis;
63 64 CustomTableModel *m_model;
64 65 int rowPos;
65 66 int nSeries;
66 67 int nNewBoxes;
67 68 QBoxPlotSeries *m_series[10];
68 69 };
69 70
70 71 #endif // MAINWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now