##// END OF EJS Templates
Added size factor property to pie
Tero Ahola -
r60:0be2c26f8dc1
parent child
Show More
1 NO CONTENT: modified file
@@ -9,7 +9,7 integrated_build:{
9 9 #this is ugly hack to work around missing rpath, it simply copies lib
10 10 win32:{
11 11 copylib.target = $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
12 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\QtCommercialChartd.dll $$CHART_BUILD_BIN_DIR
12 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\\QtCommercialChartd.dll $$CHART_BUILD_BIN_DIR
13 13 copylib.depends = $$CHART_BUILD_LIB_DIR/QtCommercialChartd.dll
14 14 PRE_TARGETDEPS += $$CHART_BUILD_BIN_DIR/QtCommercialChartd.dll
15 15 QMAKE_EXTRA_TARGETS +=copylib
@@ -21,7 +21,7 integrated_build:{
21 21 #this is ugly hack to work around missing rpath, it simply copies lib
22 22 win32:{
23 23 copylib.target = $$CHART_BUILD_BIN_DIR/QtCommercialChart
24 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\QtCommercialChart.dll $$CHART_BUILD_BIN_DIR
24 copylib.commands = $$QMAKE_COPY $$CHART_BUILD_LIB_DIR\\QtCommercialChart.dll $$CHART_BUILD_BIN_DIR
25 25 copylib.depends = $$CHART_BUILD_LIB_DIR/QtCommercialChart.dll
26 26 PRE_TARGETDEPS += $$CHART_BUILD_BIN_DIR/QtCommercialChart
27 27 QMAKE_EXTRA_TARGETS +=copylib
@@ -4,10 +4,11
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 PieSlice::PieSlice(const QColor& color, qreal startAngle, qreal span)
7 PieSlice::PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect)
8 8 : m_color(color),
9 9 m_startAngle(startAngle),
10 m_span(span)
10 m_span(span),
11 m_rect(rect)
11 12 {
12 13 setAcceptHoverEvents(true);
13 14 }
@@ -18,7 +19,7 PieSlice::~PieSlice()
18 19
19 20 QRectF PieSlice::boundingRect() const
20 21 {
21 return parentItem()->boundingRect();
22 return m_rect;
22 23 }
23 24
24 25 QPainterPath PieSlice::shape() const
@@ -11,7 +11,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11 class PieSlice : public QGraphicsItem
12 12 {
13 13 public:
14 PieSlice(const QColor& color, qreal startAngle, qreal span);
14 PieSlice(const QColor& color, qreal startAngle, qreal span, QRectF rect);
15 15 ~PieSlice();
16 16
17 17 public: // from QGraphicsItem
@@ -20,10 +20,11 public: // from QGraphicsItem
20 20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
21 21 void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
22 22
23 private:
23 public:
24 24 QColor m_color;
25 25 qreal m_startAngle;
26 26 qreal m_span;
27 QRectF m_rect;
27 28 };
28 29
29 30 QTCOMMERCIALCHART_END_NAMESPACE
@@ -7,7 +7,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
9 9 QChartSeries(parent),
10 m_x(x)
10 m_x(x),
11 m_sizeFactor(1.0)
11 12 {
12 13 // Create slices
13 14 qreal fullPie = 360;
@@ -17,23 +18,62 QPieSeries::QPieSeries(QList<qreal> x, QGraphicsObject *parent) :
17 18
18 19 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent);
19 20 Q_ASSERT(parentItem);
21 m_chartSize = parentItem->boundingRect();
20 22 qreal angle = 0;
21 23 // TODO: no need to create new slices in case size changed; we should re-use the existing ones
22 24 foreach (qreal value, m_x) {
23 25 qreal span = value / total * fullPie;
24 PieSlice *slice = new PieSlice(randomColor(), angle, span);
26 PieSlice *slice = new PieSlice(randomColor(), angle, span, parentItem->boundingRect());
25 27 slice->setParentItem(parentItem);
26 28 m_slices.append(slice);
27 29 angle += span;
28 30 }
31
32 resizeSlices(m_chartSize);
29 33 }
30 34
31 35 QPieSeries::~QPieSeries()
32 36 {
37 while (m_slices.count())
38 delete m_slices.takeLast();
39 }
40
41 void QPieSeries::chartSizeChanged(QRectF chartRect)
42 {
43 // TODO: allow user setting the size?
44 // TODO: allow user defining the margins?
45 m_chartSize = chartRect;
46 resizeSlices(m_chartSize);
33 47 }
34 48
35 void QPieSeries::chartSizeChanged(QRectF /*rect*/)
49 void QPieSeries::resizeSlices(QRectF rect)
36 50 {
51 QRectF tempRect = rect;
52 if (tempRect.width() < tempRect.height()) {
53 tempRect.setWidth(tempRect.width() * m_sizeFactor);
54 tempRect.setHeight(tempRect.width());
55 tempRect.moveCenter(rect.center());
56 } else {
57 tempRect.setHeight(tempRect.height() * m_sizeFactor);
58 tempRect.setWidth(tempRect.height());
59 tempRect.moveCenter(rect.center());
60 }
61
62 foreach (PieSlice *slice, m_slices)
63 slice->m_rect = tempRect;
64 }
65
66 void QPieSeries::setSizeFactor(qreal factor)
67 {
68 if (factor > 0.0)
69 m_sizeFactor = factor;
70 resizeSlices(m_chartSize);
71
72 // Initiate update via the parent graphics item
73 // TODO: potential issue: what if this function is called from the parent context?
74 QGraphicsItem *parentItem = qobject_cast<QGraphicsItem *>(parent());
75 Q_ASSERT(parentItem);
76 parentItem->update();
37 77 }
38 78
39 79 QColor QPieSeries::randomColor()
@@ -45,9 +85,6 QColor QPieSeries::randomColor()
45 85 return c;
46 86 }
47 87
48 void QPieSeries::setData(QList<int> data)
49 {
50 }
51 88
52 89 #include "moc_qpieseries.cpp"
53 90
@@ -13,12 +13,14 class PieSlice;
13 13 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries
14 14 {
15 15 Q_OBJECT
16
16 17 public:
17 18 // TODO: use a generic data class instead of x and y
18 19 QPieSeries(QList<qreal> x, QGraphicsObject *parent = 0);
19 20 ~QPieSeries();
20 21 QColor randomColor();
21 void setData(QList<int> data);
22 void setSizeFactor(qreal sizeFactor);
23 qreal sizeFactor() { return m_sizeFactor; }
22 24
23 25 public: // from QChartSeries
24 26 QChartSeriesType type() const { return QChartSeries::SeriesTypePie; }
@@ -27,12 +29,14 public Q_SLOTS:
27 29 void chartSizeChanged(QRectF rect);
28 30
29 31 private:
32 void resizeSlices(QRectF rect);
30 33 //Q_DECLARE_PRIVATE(QPieSeries)
31 34 Q_DISABLE_COPY(QPieSeries)
32 35 // TODO: move the followin to internal impl
33 36 QList<qreal> m_x;
34 37 QList<PieSlice*> m_slices;
35 QSizeF m_size;
38 QRectF m_chartSize;
39 qreal m_sizeFactor;
36 40 };
37 41
38 42 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,5 +1,9
1 include( ../../common.pri )
2 include( ../../integrated.pri )
1 !include( ../../common.pri ) {
2 error( "Couldn't find the common.pri file!" )
3 }
4 !include( ../../integrated.pri ) {
5 error( "Couldn't find the integrated.pri file !")
6 }
3 7
4 8 TARGET = chartwidgettest
5 9 TEMPLATE = app
@@ -9,19 +13,13 contains(QT_MAJOR_VERSION, 5) {
9 13 QT += widgets
10 14 }
11 15
12
13 16 OBJECTS_DIR = tmp
14 17 MOC_DIR = tmp
15 18
16 19 SOURCES += main.cpp \
17 20 mainwidget.cpp \
18 # qscatterseries.cpp \
19 # qseriespointgraphicsitem.cpp \
20 21 dataseriedialog.cpp
21 22
22 23 HEADERS += \
23 24 mainwidget.h \
24 # qscatterseries.h \
25 # qseriespointgraphicsitem.h \
26 25 dataseriedialog.h
27
@@ -1,5 +1,7
1 1 #include "mainwidget.h"
2 2 #include "dataseriedialog.h"
3 #include "qchartseries.h"
4 #include "qpieseries.h"
3 5 #include <qxychartseries.h>
4 6 #include <QPushButton>
5 7 #include <QComboBox>
@@ -56,7 +58,7 MainWidget::MainWidget(QWidget *parent) :
56 58 connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int)));
57 59
58 60 QGridLayout *grid = new QGridLayout();
59 QHBoxLayout *hbox = new QHBoxLayout();
61 QGridLayout *mainLayout = new QGridLayout();
60 62 //grid->addWidget(new QLabel("Add series:"), 0, 0);
61 63 grid->addWidget(addSeriesButton, 0, 1);
62 64 grid->addWidget(new QLabel("Background:"), 2, 0);
@@ -74,23 +76,42 MainWidget::MainWidget(QWidget *parent) :
74 76 grid->addWidget(new QLabel(""), 8, 0);
75 77 grid->setRowStretch(8, 1);
76 78
77 hbox->addLayout(grid);
79 mainLayout->addLayout(grid, 0, 0);
80
81 // Scatter specific settings
82 m_scatterLayout = new QGridLayout();
83 m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0);
84 m_scatterLayout->setEnabled(false);
85
86 // Pie specific settings
87 m_pieLayout = new QGridLayout();
88 m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0);
89 QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox();
90 pieSizeSpin->setMinimum(LONG_MIN);
91 pieSizeSpin->setMaximum(LONG_MAX);
92 pieSizeSpin->setValue(1.0);
93 pieSizeSpin->setSingleStep(0.1);
94 connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double)));
95 m_pieLayout->setEnabled(false);
96 m_pieLayout->addWidget(pieSizeSpin, 0, 1);
97
98 mainLayout->addLayout(m_scatterLayout, 1, 0);
99 mainLayout->addLayout(m_pieLayout, 2, 0);
78 100
79 101 m_chartWidget = new QChartWidget(this);
80 102 //m_chartWidget->setColor(Qt::red);
81 hbox->addWidget(m_chartWidget);
103 mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1);
82 104 // hbox->setStretch(1, 1);
83 105
84 setLayout(hbox);
106 setLayout(mainLayout);
85 107
86 108 m_autoScaleCheck->setChecked(true);
87 chartTypeChanged(4);
88 109 testDataChanged(0);
89 110 }
90 111
91 112 void MainWidget::addSeries()
92 113 {
93 DataSerieDialog dialog(m_defaultSeries, this);
114 DataSerieDialog dialog(m_defaultSeriesName, this);
94 115 connect(&dialog, SIGNAL(accepted(QString, QString)), this, SLOT(addSeries(QString, QString)));
95 116 dialog.exec();
96 117 }
@@ -98,9 +119,8 void MainWidget::addSeries()
98 119 void MainWidget::addSeries(QString series, QString data)
99 120 {
100 121 qDebug() << "addSeries: " << series << " data: " << data;
101 m_defaultSeries = series;
102
103 QXYChartSeries* series0 = QXYChartSeries::create();
122 m_defaultSeriesName = series;
123 QChartSeries *newSeries = QXYChartSeries::create();
104 124
105 125 // TODO: a dedicated data class for storing x and y values
106 126 QList<qreal> x;
@@ -112,56 +132,59 void MainWidget::addSeries(QString series, QString data)
112 132 y.append(i);
113 133 }
114 134 for (int i = 0; i < 20; i++)
115 series0->add(i, i);
135 ((QXYChartSeries *)newSeries)->add(i, i);
116 136 } else if (data == "linear, 1M") {
117 137 for (int i = 0; i < 10000; i++) {
118 138 x.append(i);
119 139 y.append(20);
120 140 }
121 141 for (int i = 0; i < 1000000; i++)
122 series0->add(i, 10);
142 ((QXYChartSeries *)newSeries)->add(i, 10);
123 143 } else if (data == "SIN") {
124 144 for (int i = 0; i < 100; i++) {
125 145 x.append(i);
126 146 y.append(abs(sin(3.14159265358979 / 50 * i) * 100));
127 147 }
128 148 for (int i = 0; i < 100; i++)
129 series0->add(i, abs(sin(3.14159265358979 / 50 * i) * 100));
149 ((QXYChartSeries *)newSeries)->add(i, abs(sin(3.14159265358979 / 50 * i) * 100));
130 150 } else if (data == "SIN + random") {
131 151 for (qreal i = 0; i < 100; i += 0.1) {
132 152 x.append(i + (rand() % 5));
133 153 y.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
134 154 }
135 155 for (qreal i = 0; i < 100; i += 0.1)
136 series0->add(i + (rand() % 5), abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
156 ((QXYChartSeries *)newSeries)->add(i + (rand() % 5), abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5));
137 157 } else {
138 158 // TODO: check if data has a valid file name
139 159 }
140 160
141 161 // TODO: color of the series
142 162 if (series == "Scatter") {
143 /*QChartSeries* scatterSeries = */
144 m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter);
163 newSeries = m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypeScatter);
145 164 } else if (series == "Pie") {
146 m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypePie);
165 newSeries = m_chartWidget->createSeries(x, y, QChartSeries::SeriesTypePie);
147 166 } else if (series == "Line") {
148 m_chartWidget->addSeries(series0);
167 m_chartWidget->addSeries(newSeries);
149 168 } else {
150 169 // TODO
151 170 }
171
172 setCurrentSeries(newSeries);
152 173 }
153 174
154 void MainWidget::chartTypeChanged(int itemIndex)
175 void MainWidget::setCurrentSeries(QChartSeries *series)
155 176 {
156 // TODO: change chart type
157 switch (itemIndex) {
158 case 4:
159 //m_chartWidget->setType(4);
177 m_currentSeries = series;
178 switch (m_currentSeries->type()) {
179 case QChartSeries::SeriesTypeLine:
160 180 break;
161 default: {
162 //m_chartWidget->setType(0);
181 case QChartSeries::SeriesTypeScatter:
182 break;
183 case QChartSeries::SeriesTypePie:
184 break;
185 default:
186 Q_ASSERT(false);
163 187 break;
164 }
165 188 }
166 189 }
167 190
@@ -239,3 +262,10 void MainWidget::yMaxChanged(int value)
239 262 {
240 263 qDebug() << "yMaxChanged: " << value;
241 264 }
265
266 void MainWidget::setPieSizeFactor(double size)
267 {
268 QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries);
269 Q_ASSERT(pie);
270 pie->setSizeFactor(qreal(size));
271 }
@@ -7,6 +7,7
7 7
8 8 class QSpinBox;
9 9 class QCheckBox;
10 class QGridLayout;
10 11
11 12 QTCOMMERCIALCHART_USE_NAMESPACE
12 13
@@ -19,7 +20,6 public:
19 20 signals:
20 21
21 22 private slots:
22 void chartTypeChanged(int itemIndex);
23 23 void addSeries();
24 24 void addSeries(QString series, QString data);
25 25 void testDataChanged(int itemIndex);
@@ -29,6 +29,8 private slots:
29 29 void xMaxChanged(int value);
30 30 void yMinChanged(int value);
31 31 void yMaxChanged(int value);
32 void setCurrentSeries(QChartSeries *series);
33 void setPieSizeFactor(double margin);
32 34
33 35 private:
34 36 QChartWidget *m_chartWidget;
@@ -37,7 +39,10 private:
37 39 QSpinBox *m_xMaxSpin;
38 40 QSpinBox *m_yMinSpin;
39 41 QSpinBox *m_yMaxSpin;
40 QString m_defaultSeries;
42 QString m_defaultSeriesName;
43 QChartSeries *m_currentSeries;
44 QGridLayout *m_scatterLayout;
45 QGridLayout *m_pieLayout;
41 46 };
42 47
43 48 #endif // MAINWIDGET_H
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now