@@ -1,12 +1,10 | |||
|
1 | 1 | !include( ../example.pri ) { |
|
2 | 2 | error( "Couldn't find the example.pri file!" ) |
|
3 | 3 | } |
|
4 | 4 | |
|
5 | 5 | TARGET = barchart |
|
6 | 6 | SOURCES += main.cpp \ |
|
7 |
chartwidget.cpp |
|
|
8 | custombarset.cpp | |
|
7 | chartwidget.cpp | |
|
9 | 8 | HEADERS += \ |
|
10 |
chartwidget.h |
|
|
11 | custombarset.h | |
|
9 | chartwidget.h | |
|
12 | 10 |
@@ -1,53 +1,54 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartview.h> |
|
4 | 4 | #include <qbarchartseries.h> |
|
5 | 5 | #include <qbarset.h> |
|
6 | 6 | #include <qbarcategory.h> |
|
7 | 7 | |
|
8 | #include "custombarset.h" | |
|
9 | ||
|
10 | 8 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | 9 | |
|
12 | 10 | int main(int argc, char *argv[]) |
|
13 | 11 | { |
|
14 | 12 | QApplication a(argc, argv); |
|
15 | 13 | QMainWindow window; |
|
16 | 14 | |
|
17 | 15 | QBarCategory *category = new QBarCategory; |
|
18 | 16 | *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
19 | 17 | |
|
20 | 18 | QBarChartSeries* series= new QBarChartSeries(category); |
|
21 | 19 | |
|
22 | 20 | // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals |
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
|
21 | QBarSet *set0 = new QBarSet("Bub"); | |
|
22 | QBarSet *set1 = new QBarSet("Bob"); | |
|
23 | QBarSet *set2 = new QBarSet("Guybrush"); | |
|
24 | QBarSet *set3 = new QBarSet("Larry"); | |
|
25 | QBarSet *set4 = new QBarSet("Zak"); | |
|
28 | 26 | |
|
29 | 27 | // Create some test data to chart |
|
30 | 28 | *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; |
|
31 | 29 | *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2; |
|
32 | 30 | *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5; |
|
33 | 31 | *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; |
|
34 | 32 | *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6; |
|
35 | 33 | |
|
36 | 34 | series->addBarSet(set0); |
|
37 | 35 | series->addBarSet(set1); |
|
38 | 36 | series->addBarSet(set2); |
|
39 | 37 | series->addBarSet(set3); |
|
40 | 38 | series->addBarSet(set4); |
|
41 | 39 | |
|
40 | series->enableToolTip(true); | |
|
41 | series->enableFloatingValues(true); | |
|
42 | ||
|
42 | 43 | QChartView* chartView = new QChartView(&window); |
|
43 | 44 | chartView->addSeries(series); |
|
44 | 45 | chartView->setChartTitle("simple stacked barchart"); |
|
45 | 46 | chartView->setChartTheme(QChart::ChartThemeIcy); |
|
46 | 47 | |
|
47 | 48 | window.setCentralWidget(chartView); |
|
48 | 49 | window.resize(400, 300); |
|
49 | 50 | window.show(); |
|
50 | 51 | |
|
51 | 52 | return a.exec(); |
|
52 | 53 | } |
|
53 | 54 |
@@ -1,53 +1,54 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <QStandardItemModel> |
|
4 | 4 | #include <qpercentbarchartseries.h> |
|
5 | 5 | #include <qbarcategory.h> |
|
6 | 6 | #include <qchartview.h> |
|
7 | ||
|
8 | #include "custombarset.h" | |
|
7 | #include <qbarset.h> | |
|
9 | 8 | |
|
10 | 9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | 10 | |
|
12 | 11 | int main(int argc, char *argv[]) |
|
13 | 12 | { |
|
14 | 13 | QApplication a(argc, argv); |
|
15 | 14 | QMainWindow window; |
|
16 | 15 | |
|
17 | 16 | QBarCategory *category = new QBarCategory; |
|
18 | 17 | *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
19 | 18 | |
|
20 | 19 | QPercentBarChartSeries* series = new QPercentBarChartSeries(category); |
|
21 | 20 | |
|
22 | // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals | |
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 | CustomBarSet *set4 = new CustomBarSet("Zak"); | |
|
21 | QBarSet *set0 = new QBarSet("Bub"); | |
|
22 | QBarSet *set1 = new QBarSet("Bob"); | |
|
23 | QBarSet *set2 = new QBarSet("Guybrush"); | |
|
24 | QBarSet *set3 = new QBarSet("Larry"); | |
|
25 | QBarSet *set4 = new QBarSet("Zak"); | |
|
28 | 26 | |
|
29 | 27 | // Create some test data to chart |
|
30 | 28 | *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; |
|
31 | 29 | *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2; |
|
32 | 30 | *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5; |
|
33 | 31 | *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; |
|
34 | 32 | *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6; |
|
35 | 33 | |
|
36 | 34 | series->addBarSet(set0); |
|
37 | 35 | series->addBarSet(set1); |
|
38 | 36 | series->addBarSet(set2); |
|
39 | 37 | series->addBarSet(set3); |
|
40 | 38 | series->addBarSet(set4); |
|
41 | 39 | |
|
40 | series->enableToolTip(true); | |
|
41 | series->enableFloatingValues(true); | |
|
42 | ||
|
42 | 43 | QChartView* chartView = new QChartView(&window); |
|
43 | 44 | chartView->addSeries(series); |
|
44 | 45 | chartView->setChartTitle("simple percent barchart"); |
|
45 | 46 | chartView->setChartTheme(QChart::ChartThemeIcy); |
|
46 | 47 | |
|
47 | 48 | window.setCentralWidget(chartView); |
|
48 | 49 | window.resize(400, 300); |
|
49 | 50 | window.show(); |
|
50 | 51 | |
|
51 | 52 | return a.exec(); |
|
52 | 53 | } |
|
53 | 54 |
@@ -1,12 +1,10 | |||
|
1 | 1 | !include( ../example.pri ) { |
|
2 | 2 | error( "Couldn't find the example.pri file!" ) |
|
3 | 3 | } |
|
4 | 4 | |
|
5 | 5 | TARGET = percentbarchart |
|
6 | 6 | SOURCES += main.cpp \ |
|
7 |
chartwidget.cpp |
|
|
8 | custombarset.cpp | |
|
7 | chartwidget.cpp | |
|
9 | 8 | HEADERS += \ |
|
10 |
chartwidget.h |
|
|
11 | custombarset.h | |
|
9 | chartwidget.h | |
|
12 | 10 |
@@ -1,53 +1,53 | |||
|
1 | 1 | #include <QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartview.h> |
|
4 | 4 | #include <qstackedbarchartseries.h> |
|
5 | 5 | #include <qbarset.h> |
|
6 | 6 | #include <qbarcategory.h> |
|
7 | 7 | |
|
8 | #include "custombarset.h" | |
|
9 | ||
|
10 | 8 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | 9 | |
|
12 | 10 | int main(int argc, char *argv[]) |
|
13 | 11 | { |
|
14 | 12 | QApplication a(argc, argv); |
|
15 | 13 | QMainWindow window; |
|
16 | 14 | |
|
17 | 15 | QBarCategory *category = new QBarCategory; |
|
18 | 16 | *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
19 | 17 | |
|
20 | 18 | QStackedBarChartSeries* series = new QStackedBarChartSeries(category); |
|
21 | 19 | |
|
22 | // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals | |
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 | CustomBarSet *set4 = new CustomBarSet("Zak"); | |
|
20 | QBarSet *set0 = new QBarSet("Bub"); | |
|
21 | QBarSet *set1 = new QBarSet("Bob"); | |
|
22 | QBarSet *set2 = new QBarSet("Guybrush"); | |
|
23 | QBarSet *set3 = new QBarSet("Larry"); | |
|
24 | QBarSet *set4 = new QBarSet("Zak"); | |
|
28 | 25 | |
|
29 | 26 | // Create some test data to chart |
|
30 | 27 | *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; |
|
31 | 28 | *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2; |
|
32 | 29 | *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5; |
|
33 | 30 | *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; |
|
34 | 31 | *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6; |
|
35 | 32 | |
|
36 | 33 | series->addBarSet(set0); |
|
37 | 34 | series->addBarSet(set1); |
|
38 | 35 | series->addBarSet(set2); |
|
39 | 36 | series->addBarSet(set3); |
|
40 | 37 | series->addBarSet(set4); |
|
41 | 38 | |
|
39 | series->enableToolTip(true); | |
|
40 | series->enableFloatingValues(true); | |
|
41 | ||
|
42 | 42 | QChartView* chartView = new QChartView(&window); |
|
43 | 43 | chartView->addSeries(series); |
|
44 | 44 | chartView->setChartTitle("simple stacked barchart"); |
|
45 | 45 | chartView->setChartTheme(QChart::ChartThemeIcy); |
|
46 | 46 | |
|
47 | 47 | window.setCentralWidget(chartView); |
|
48 | 48 | window.resize(400, 300); |
|
49 | 49 | window.show(); |
|
50 | 50 | |
|
51 | 51 | return a.exec(); |
|
52 | 52 | } |
|
53 | 53 |
@@ -1,11 +1,9 | |||
|
1 | 1 | !include( ../example.pri ) { |
|
2 | 2 | error( "Couldn't find the example.pri file!" ) |
|
3 | 3 | } |
|
4 | 4 | TARGET = stackedbarchart |
|
5 | 5 | SOURCES += main.cpp \ |
|
6 |
chartwidget.cpp |
|
|
7 | custombarset.cpp | |
|
6 | chartwidget.cpp | |
|
8 | 7 | HEADERS += \ |
|
9 |
chartwidget.h |
|
|
10 | custombarset.h | |
|
8 | chartwidget.h | |
|
11 | 9 |
@@ -1,171 +1,158 | |||
|
1 | 1 | #include "barpresenterbase.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barvalue_p.h" |
|
4 | 4 | #include "barlabel_p.h" |
|
5 | 5 | #include "separator_p.h" |
|
6 | 6 | #include "qbarset.h" |
|
7 | 7 | #include "qbarchartseries.h" |
|
8 | 8 | #include <QDebug> |
|
9 | 9 | #include <QToolTip> |
|
10 | 10 | |
|
11 | 11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | 12 | |
|
13 | 13 | BarPresenterBase::BarPresenterBase(QBarChartSeries *series, QGraphicsItem *parent) |
|
14 | 14 | : ChartItem(parent) |
|
15 | 15 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
16 | 16 | ,mLayoutSet(false) |
|
17 | 17 | ,mLayoutDirty(true) |
|
18 | ,mSeparatorsEnabled(false) | |
|
18 | 19 | ,mSeries(series) |
|
19 | 20 | { |
|
20 | connect(series,SIGNAL(floatingValuesEnabled(bool)),this,SLOT(enableFloatingValues(bool))); | |
|
21 | connect(series,SIGNAL(toolTipEnabled(bool)),this,SLOT(enableToolTip(bool))); | |
|
22 | connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool))); | |
|
23 | 21 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); |
|
22 | connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool))); | |
|
24 | 23 | dataChanged(); |
|
25 | 24 | } |
|
26 | 25 | |
|
27 | 26 | BarPresenterBase::~BarPresenterBase() |
|
28 | 27 | { |
|
29 | disconnect(this,SLOT(enableFloatingValues(bool))); | |
|
30 | disconnect(this,SLOT(enableToolTip(bool))); | |
|
31 | disconnect(this,SLOT(enableSeparators(bool))); | |
|
32 | 28 | disconnect(this,SLOT(showToolTip(QPoint,QString))); |
|
33 | 29 | delete mSeries; |
|
34 | 30 | } |
|
35 | 31 | |
|
36 | 32 | void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
37 | 33 | { |
|
38 | 34 | if (!mLayoutSet) { |
|
39 | 35 | qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; |
|
40 | 36 | return; |
|
41 | 37 | } |
|
42 | 38 | // if (mLayoutDirty) { |
|
43 | 39 | // Layout or data has changed. Need to redraw. |
|
44 | 40 | foreach(QGraphicsItem* i, childItems()) { |
|
45 | 41 | i->paint(painter,option,widget); |
|
46 | 42 | } |
|
47 | 43 | // } |
|
48 | 44 | } |
|
49 | 45 | |
|
50 | 46 | QRectF BarPresenterBase::boundingRect() const |
|
51 | 47 | { |
|
52 | 48 | return QRectF(0,0,mWidth,mHeight); |
|
53 | 49 | } |
|
54 | 50 | |
|
55 | 51 | void BarPresenterBase::setBarWidth( int w ) |
|
56 | 52 | { |
|
57 | 53 | mBarDefaultWidth = w; |
|
58 | 54 | } |
|
59 | 55 | |
|
60 | 56 | void BarPresenterBase::dataChanged() |
|
61 | 57 | { |
|
62 | 58 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
63 | 59 | qDebug() << "datachanged"; |
|
64 | 60 | // Delete old bars |
|
65 | 61 | foreach (QGraphicsItem* item, childItems()) { |
|
66 | 62 | delete item; |
|
67 | 63 | } |
|
68 | 64 | |
|
69 | 65 | mBars.clear(); |
|
70 | 66 | mLabels.clear(); |
|
71 | 67 | mSeparators.clear(); |
|
72 | 68 | mFloatingValues.clear(); |
|
73 | 69 | |
|
74 | 70 | // Create new graphic items for bars |
|
75 | 71 | for (int c=0; c<mSeries->countCategories(); c++) { |
|
76 | 72 | for (int s=0; s<mSeries->countSets(); s++) { |
|
77 | 73 | QBarSet *set = mSeries->setAt(s); |
|
78 | 74 | Bar *bar = new Bar(this); |
|
79 | 75 | childItems().append(bar); |
|
80 | 76 | mBars.append(bar); |
|
81 | 77 | connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); |
|
82 | // TODO: should the event be passed to set or not? | |
|
83 | 78 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint))); |
|
84 | 79 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved())); |
|
85 | 80 | } |
|
86 | 81 | } |
|
87 | 82 | |
|
88 | 83 | // Create labels |
|
89 | 84 | int count = mSeries->countCategories(); |
|
90 | 85 | for (int i=0; i<count; i++) { |
|
91 | 86 | BarLabel* label = new BarLabel(this); |
|
92 | 87 | label->set(mSeries->label(i)); |
|
93 | 88 | childItems().append(label); |
|
94 | 89 | mLabels.append(label); |
|
95 | 90 | } |
|
96 | 91 | |
|
97 | 92 | // Create separators |
|
98 | 93 | count = mSeries->countCategories() - 1; // There is one less separator than columns |
|
99 | 94 | for (int i=0; i<count; i++) { |
|
100 | 95 | Separator* sep = new Separator(this); |
|
101 | 96 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme |
|
97 | sep->setVisible(mSeparatorsEnabled); | |
|
102 | 98 | childItems().append(sep); |
|
103 | 99 | mSeparators.append(sep); |
|
104 | 100 | } |
|
105 | 101 | |
|
106 | 102 | // Create floating values |
|
107 | 103 | for (int category=0; category<mSeries->countCategories(); category++) { |
|
108 | 104 | for (int s=0; s<mSeries->countSets(); s++) { |
|
109 | 105 | QBarSet *set = mSeries->setAt(s); |
|
110 | 106 | BarValue *value = new BarValue(*set, this); |
|
111 | 107 | childItems().append(value); |
|
112 | 108 | mFloatingValues.append(value); |
|
113 | 109 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); |
|
114 | 110 | } |
|
115 | 111 | } |
|
116 | 112 | |
|
117 | 113 | // TODO: if (autolayout) { layoutChanged() } or something |
|
118 | 114 | mLayoutDirty = true; |
|
119 | 115 | } |
|
120 | 116 | |
|
121 | 117 | //handlers |
|
122 | 118 | |
|
123 | 119 | void BarPresenterBase::handleModelChanged(int index) |
|
124 | 120 | { |
|
125 | 121 | // qDebug() << "BarPresenterBase::handleModelChanged" << index; |
|
126 | 122 | dataChanged(); |
|
127 | 123 | } |
|
128 | 124 | |
|
129 | 125 | void BarPresenterBase::handleDomainChanged(const Domain& domain) |
|
130 | 126 | { |
|
131 | 127 | // qDebug() << "BarPresenterBase::handleDomainChanged"; |
|
132 | 128 | // TODO: Figure out the use case for this. |
|
133 | 129 | // Affects the size of visible item, so layout is changed. |
|
134 | 130 | // layoutChanged(); |
|
135 | 131 | } |
|
136 | 132 | |
|
137 | 133 | void BarPresenterBase::handleGeometryChanged(const QRectF& rect) |
|
138 | 134 | { |
|
139 | 135 | mWidth = rect.width(); |
|
140 | 136 | mHeight = rect.height(); |
|
141 | 137 | layoutChanged(); |
|
142 | 138 | mLayoutSet = true; |
|
143 | 139 | setPos(rect.topLeft()); |
|
144 | 140 | } |
|
145 | 141 | |
|
146 | void BarPresenterBase::enableFloatingValues(bool enabled) | |
|
147 | { | |
|
148 | mFloatingValuesEnabled = enabled; | |
|
149 | } | |
|
150 | ||
|
151 | void BarPresenterBase::enableToolTip(bool enabled) | |
|
142 | void BarPresenterBase::showToolTip(QPoint pos, QString tip) | |
|
152 | 143 | { |
|
153 | mToolTipEnabled = enabled; | |
|
144 | // TODO: cool tooltip instead of default | |
|
145 | QToolTip::showText(pos,tip); | |
|
154 | 146 | } |
|
155 | 147 | |
|
156 | 148 | void BarPresenterBase::enableSeparators(bool enabled) |
|
157 | 149 | { |
|
158 | mSeparatorsEnabled = enabled; | |
|
159 | } | |
|
160 | ||
|
161 | void BarPresenterBase::showToolTip(QPoint pos, QString tip) | |
|
162 | { | |
|
163 | if (mToolTipEnabled) { | |
|
164 | // TODO: cool tooltip instead of default | |
|
165 | QToolTip::showText(pos,tip); | |
|
150 | for (int i=0; i<mSeparators.count(); i++) { | |
|
151 | mSeparators.at(i)->setVisible(enabled); | |
|
166 | 152 | } |
|
153 | mSeparatorsEnabled = enabled; | |
|
167 | 154 | } |
|
168 | 155 | |
|
169 | 156 | #include "moc_barpresenterbase.cpp" |
|
170 | 157 | |
|
171 | 158 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,76 +1,72 | |||
|
1 | 1 | #ifndef BARPRESENTERBASE_H |
|
2 | 2 | #define BARPRESENTERBASE_H |
|
3 | 3 | |
|
4 | 4 | #include "chartitem_p.h" |
|
5 | 5 | #include "qbarchartseries.h" |
|
6 | 6 | #include <QPen> |
|
7 | 7 | #include <QBrush> |
|
8 | 8 | #include <QGraphicsItem> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | class Bar; |
|
13 | 13 | class BarLabel; |
|
14 | 14 | class Separator; |
|
15 | 15 | class BarValue; |
|
16 | 16 | |
|
17 | 17 | // Common implemantation of different presenters. Not to be instantiated. |
|
18 | 18 | // TODO: combine this with BarPresenter and derive other presenters from it? |
|
19 | 19 | class BarPresenterBase : public QObject, public ChartItem |
|
20 | 20 | { |
|
21 | 21 | Q_OBJECT |
|
22 | 22 | public: |
|
23 | 23 | BarPresenterBase(QBarChartSeries *series, QGraphicsItem *parent = 0); |
|
24 | 24 | ~BarPresenterBase(); |
|
25 | 25 | |
|
26 | 26 | public: |
|
27 | 27 | // From QGraphicsItem |
|
28 | 28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
29 | 29 | QRectF boundingRect() const; |
|
30 | 30 | |
|
31 | 31 | // TODO: these may change with layout awarness. |
|
32 | 32 | void setBarWidth( int w ); |
|
33 | 33 | |
|
34 | 34 | // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it |
|
35 | 35 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
36 | 36 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes |
|
37 | 37 | |
|
38 | 38 | protected slots: |
|
39 | 39 | void handleModelChanged(int index); |
|
40 | 40 | void handleDomainChanged(const Domain& domain); |
|
41 | 41 | void handleGeometryChanged(const QRectF& size); |
|
42 | 42 | |
|
43 | 43 | // Internal slots |
|
44 | void enableFloatingValues(bool enabled=true); // enables floating values on top of bars | |
|
45 | void enableToolTip(bool enabled=true); // enables tooltips | |
|
46 | void enableSeparators(bool enabled=true); // enables separators between categories | |
|
47 | 44 | void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled) |
|
45 | void enableSeparators(bool enabled); | |
|
48 | 46 | |
|
49 | 47 | protected: |
|
50 | 48 | |
|
51 | 49 | // TODO: consider these. |
|
52 | 50 | int mHeight; // Layout spesific |
|
53 | 51 | int mWidth; |
|
54 | 52 | int mBarDefaultWidth; |
|
55 | 53 | |
|
56 | 54 | bool mLayoutSet; // True, if component has been laid out. |
|
57 | 55 | bool mLayoutDirty; |
|
58 | 56 | |
|
59 | bool mFloatingValuesEnabled; | |
|
60 | bool mToolTipEnabled; | |
|
61 | 57 | bool mSeparatorsEnabled; |
|
62 | 58 | |
|
63 | 59 | // Owned |
|
64 | 60 | QBarChartSeries* mSeries; |
|
65 | 61 | |
|
66 | 62 | // Not owned. |
|
67 | 63 | QList<Bar*> mBars; |
|
68 | 64 | QList<BarLabel*> mLabels; |
|
69 | 65 | QList<Separator*> mSeparators; |
|
70 | 66 | QList<BarValue*> mFloatingValues; |
|
71 | 67 | |
|
72 | 68 | }; |
|
73 | 69 | |
|
74 | 70 | QTCOMMERCIALCHART_END_NAMESPACE |
|
75 | 71 | |
|
76 | 72 | #endif // BARPRESENTERBASE_H |
@@ -1,116 +1,128 | |||
|
1 | 1 | #include <QDebug> |
|
2 | 2 | #include "qbarchartseries.h" |
|
3 | 3 | #include "qbarcategory.h" |
|
4 | 4 | #include "qbarset.h" |
|
5 | 5 | #include "barchartmodel_p.h" |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | QBarChartSeries::QBarChartSeries(QBarCategory *category, QObject *parent) |
|
11 | 11 | : QChartSeries(parent) |
|
12 | 12 | ,mModel(new BarChartModel(category, this)) |
|
13 | 13 | { |
|
14 | 14 | } |
|
15 | 15 | |
|
16 | 16 | void QBarChartSeries::addBarSet(QBarSet *set) |
|
17 | 17 | { |
|
18 | connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(bool))); | |
|
19 | connect(this,SIGNAL(separatorsEnabled(bool)),set,SLOT(enableSeparators(bool))); | |
|
20 | connect(this,SIGNAL(toolTipEnabled(bool)),set,SLOT(enableToolTip(bool))); | |
|
21 | connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); | |
|
22 | 18 | mModel->addBarSet(set); |
|
23 | 19 | } |
|
24 | 20 | |
|
25 | 21 | void QBarChartSeries::removeBarSet(QBarSet *set) |
|
26 | 22 | { |
|
27 | disconnect(set,SLOT(enableFloatingValues(bool))); | |
|
28 | disconnect(set,SLOT(enableSeparators(bool))); | |
|
29 | disconnect(set,SLOT(enableToolTip(bool))); | |
|
30 | 23 | mModel->removeBarSet(set); |
|
31 | 24 | } |
|
32 | 25 | |
|
33 | 26 | int QBarChartSeries::countSets() |
|
34 | 27 | { |
|
35 | 28 | return mModel->countSets(); |
|
36 | 29 | } |
|
37 | 30 | |
|
38 | 31 | QBarSet* QBarChartSeries::nextSet(bool getFirst) |
|
39 | 32 | { |
|
40 | 33 | return mModel->nextSet(getFirst); |
|
41 | 34 | } |
|
42 | 35 | |
|
43 | 36 | QBarSet* QBarChartSeries::setAt(int index) |
|
44 | 37 | { |
|
45 | 38 | return mModel->setAt(index); |
|
46 | 39 | } |
|
47 | 40 | |
|
48 | 41 | QList<QString> QBarChartSeries::legend() |
|
49 | 42 | { |
|
50 | 43 | return mModel->legend(); |
|
51 | 44 | } |
|
52 | 45 | |
|
53 | 46 | QString QBarChartSeries::label(int category) |
|
54 | 47 | { |
|
55 | 48 | return mModel->label(category); |
|
56 | 49 | } |
|
57 | 50 | |
|
58 | 51 | void QBarChartSeries::enableFloatingValues(bool enabled) |
|
59 | 52 | { |
|
60 | emit floatingValuesEnabled(enabled); | |
|
53 | if (enabled) { | |
|
54 | for (int i=0; i<mModel->countSets(); i++) { | |
|
55 | QBarSet *set = mModel->setAt(i); | |
|
56 | connect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues())); | |
|
57 | } | |
|
58 | } else { | |
|
59 | for (int i=0; i<mModel->countSets(); i++) { | |
|
60 | QBarSet *set = mModel->setAt(i); | |
|
61 | disconnect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues())); | |
|
62 | } | |
|
63 | } | |
|
61 | 64 | } |
|
62 | 65 | |
|
63 | 66 | void QBarChartSeries::enableToolTip(bool enabled) |
|
64 | 67 | { |
|
65 | emit toolTipEnabled(enabled); | |
|
68 | if (enabled) { | |
|
69 | for (int i=0; i<mModel->countSets(); i++) { | |
|
70 | QBarSet *set = mModel->setAt(i); | |
|
71 | connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); | |
|
72 | } | |
|
73 | } else { | |
|
74 | for (int i=0; i<mModel->countSets(); i++) { | |
|
75 | QBarSet *set = mModel->setAt(i); | |
|
76 | disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); | |
|
77 | } | |
|
78 | } | |
|
66 | 79 | } |
|
67 | 80 | |
|
68 | 81 | void QBarChartSeries::enableSeparators(bool enabled) |
|
69 | 82 | { |
|
70 | 83 | emit separatorsEnabled(enabled); |
|
71 | 84 | } |
|
72 | 85 | |
|
73 | 86 | int QBarChartSeries::countCategories() |
|
74 | 87 | { |
|
75 | 88 | return mModel->countCategories(); |
|
76 | 89 | } |
|
77 | 90 | |
|
78 | 91 | qreal QBarChartSeries::min() |
|
79 | 92 | { |
|
80 | 93 | return mModel->min(); |
|
81 | 94 | } |
|
82 | 95 | |
|
83 | 96 | qreal QBarChartSeries::max() |
|
84 | 97 | { |
|
85 | 98 | return mModel->max(); |
|
86 | 99 | } |
|
87 | 100 | |
|
88 | 101 | qreal QBarChartSeries::valueAt(int set, int category) |
|
89 | 102 | { |
|
90 | 103 | return mModel->valueAt(set,category); |
|
91 | 104 | } |
|
92 | 105 | |
|
93 | 106 | qreal QBarChartSeries::percentageAt(int set, int category) |
|
94 | 107 | { |
|
95 | 108 | return mModel->percentageAt(set,category); |
|
96 | 109 | } |
|
97 | 110 | |
|
98 | 111 | qreal QBarChartSeries::categorySum(int category) |
|
99 | 112 | { |
|
100 | 113 | return mModel->categorySum(category); |
|
101 | 114 | } |
|
102 | 115 | |
|
103 | 116 | qreal QBarChartSeries::maxCategorySum() |
|
104 | 117 | { |
|
105 | 118 | return mModel->maxCategorySum(); |
|
106 | 119 | } |
|
107 | 120 | |
|
108 | 121 | BarChartModel& QBarChartSeries::model() |
|
109 | 122 | { |
|
110 | 123 | return *mModel; |
|
111 | 124 | } |
|
112 | 125 | |
|
113 | ||
|
114 | 126 | #include "moc_qbarchartseries.cpp" |
|
115 | 127 | |
|
116 | 128 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,66 +1,66 | |||
|
1 | 1 | #ifndef BARCHARTSERIES_H |
|
2 | 2 | #define BARCHARTSERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartseries.h" |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | class QBarCategory; |
|
9 | 9 | class QBarSet; |
|
10 | 10 | class BarChartModel; |
|
11 | 11 | |
|
12 | 12 | // Container for series |
|
13 | 13 | class QTCOMMERCIALCHART_EXPORT QBarChartSeries : public QChartSeries |
|
14 | 14 | { |
|
15 | 15 | Q_OBJECT |
|
16 | 16 | public: |
|
17 | 17 | QBarChartSeries(QBarCategory *category, QObject* parent=0); |
|
18 | 18 | |
|
19 | 19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } |
|
20 | 20 | |
|
21 | 21 | void addBarSet(QBarSet *set); // Takes ownership of set |
|
22 | 22 | void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set |
|
23 | 23 | int countSets(); |
|
24 | QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true | |
|
24 | QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true | |
|
25 | 25 | QBarSet *setAt(int index); |
|
26 | 26 | |
|
27 | 27 | QList<QString> legend(); // Returns legend of series (ie. names of all sets in series) |
|
28 | 28 | QString label(int category); |
|
29 | 29 | |
|
30 | public Q_SLOTS: | |
|
31 | // Disabled by default. Call these to change behavior. | |
|
32 | void enableFloatingValues(bool enabled=true); // enables floating values on top of bars | |
|
33 | void enableToolTip(bool enabled=true); // enables tooltips | |
|
34 | void enableSeparators(bool enabled=true); // enables separators between categories | |
|
35 | ||
|
36 | 30 | public: |
|
37 | 31 | // TODO: Functions below this are not part of api and will be moved |
|
38 |
// to private implementation, when we start using it |
|
|
32 | // to private implementation, when we start using it | |
|
33 | // TODO: TO PIMPL ---> | |
|
39 | 34 | int countCategories(); |
|
40 | 35 | qreal min(); |
|
41 | 36 | qreal max(); |
|
42 | 37 | qreal valueAt(int set, int category); |
|
43 | 38 | qreal percentageAt(int set, int category); |
|
44 | ||
|
45 | 39 | qreal categorySum(int category); |
|
46 | 40 | qreal maxCategorySum(); |
|
47 | ||
|
48 | 41 | BarChartModel& model(); |
|
42 | // <--- TO PIMPL | |
|
49 | 43 | |
|
50 | 44 | signals: |
|
51 | 45 | void changed(int index); |
|
52 | 46 | |
|
53 | 47 | // TODO: internal signals, these to private implementation. |
|
48 | // TODO: TO PIMPL ---> | |
|
54 | 49 | void floatingValuesEnabled(bool enabled); |
|
55 | 50 | void toolTipEnabled(bool enabled); |
|
56 | 51 | void separatorsEnabled(bool enabled); |
|
57 | 52 | void showToolTip(QPoint pos, QString tip); |
|
53 | // <--- TO PIMPL | |
|
54 | ||
|
55 | public Q_SLOTS: | |
|
56 | void enableFloatingValues(bool enabled=true); // enables floating values on top of bars | |
|
57 | void enableToolTip(bool enabled=true); // enables tooltips | |
|
58 | void enableSeparators(bool enabled=true); // enables separators between categories | |
|
58 | 59 | |
|
59 | 60 | protected: |
|
60 | 61 | BarChartModel* mModel; |
|
61 | ||
|
62 | 62 | }; |
|
63 | 63 | |
|
64 | 64 | QTCOMMERCIALCHART_END_NAMESPACE |
|
65 | 65 | |
|
66 | 66 | #endif // BARCHARTSERIES_H |
@@ -1,111 +1,88 | |||
|
1 | 1 | #include "qbarset.h" |
|
2 | 2 | #include <QDebug> |
|
3 | 3 | #include <QToolTip> |
|
4 | 4 | |
|
5 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 6 | |
|
7 | 7 | QBarSet::QBarSet(QString name, QObject *parent) |
|
8 | 8 | : QObject(parent) |
|
9 | 9 | ,mName(name) |
|
10 | ,mToolTipEnabled(true) // TODO: these 2 as false by default, when implementation is ready | |
|
11 | ,mFloatingValuesEnabled(true) | |
|
12 | 10 | { |
|
13 | 11 | } |
|
14 | 12 | |
|
15 | 13 | void QBarSet::setName(QString name) |
|
16 | 14 | { |
|
17 | 15 | mName = name; |
|
18 | 16 | } |
|
19 | 17 | QString QBarSet::name() |
|
20 | 18 | { |
|
21 | 19 | return mName; |
|
22 | 20 | } |
|
23 | 21 | |
|
24 | 22 | QBarSet& QBarSet::operator << (const qreal &value) |
|
25 | 23 | { |
|
26 | 24 | mValues.append(value); |
|
27 | 25 | return *this; |
|
28 | 26 | } |
|
29 | 27 | |
|
30 | 28 | int QBarSet::count() |
|
31 | 29 | { |
|
32 | 30 | return mValues.count(); |
|
33 | 31 | } |
|
34 | 32 | |
|
35 | 33 | qreal QBarSet::valueAt(int index) |
|
36 | 34 | { |
|
37 | 35 | return mValues.at(index); |
|
38 | 36 | } |
|
39 | 37 | |
|
40 | 38 | void QBarSet::setValue(int index, qreal value) |
|
41 | 39 | { |
|
42 | 40 | mValues.replace(index,value); |
|
43 | 41 | } |
|
44 | 42 | |
|
45 | 43 | void QBarSet::setPen(const QPen& pen) |
|
46 | 44 | { |
|
47 | 45 | mPen = pen; |
|
48 | 46 | } |
|
49 | 47 | |
|
50 | 48 | const QPen& QBarSet::pen() const |
|
51 | 49 | { |
|
52 | 50 | return mPen; |
|
53 | 51 | } |
|
54 | 52 | |
|
55 | 53 | void QBarSet::setBrush(const QBrush& brush) |
|
56 | 54 | { |
|
57 | 55 | mBrush = brush; |
|
58 | 56 | } |
|
59 | 57 | |
|
60 | 58 | const QBrush& QBarSet::brush() const |
|
61 | 59 | { |
|
62 | 60 | return mBrush; |
|
63 | 61 | } |
|
64 | 62 | |
|
65 | void QBarSet::enableFloatingValues(bool enabled) | |
|
66 | { | |
|
67 | qDebug() << "QBarSet::enableFloatingValues"; | |
|
68 | mFloatingValuesEnabled = enabled; | |
|
69 | } | |
|
70 | ||
|
71 | void QBarSet::enableToolTip(bool enabled) | |
|
72 | { | |
|
73 | qDebug() << "QBarSet::enableToolTip"; | |
|
74 | mToolTipEnabled = enabled; | |
|
75 | } | |
|
76 | ||
|
77 | void QBarSet::enableSeparators(bool enabled) | |
|
78 | { | |
|
79 | qDebug() << "QBarSet::enableSeparators"; | |
|
80 | mSeparatorsEnabled = enabled; | |
|
81 | } | |
|
82 | ||
|
83 | 63 | void QBarSet::barClicked() |
|
84 | 64 | { |
|
85 | 65 | // qDebug() << "QBarset::barClicked" << this; |
|
86 | 66 | // Some bar of this set has been clicked |
|
87 | 67 | // TODO: What happens then? |
|
88 | 68 | emit clicked(); // Notify that set has been clicked |
|
89 | 69 | } |
|
90 | 70 | |
|
91 | 71 | void QBarSet::barHoverEntered(QPoint pos) |
|
92 | 72 | { |
|
93 | if (mToolTipEnabled) { | |
|
94 | emit showToolTip(pos, mName); | |
|
95 | } | |
|
96 | // Emit signal to user of charts | |
|
73 | emit showToolTip(pos, mName); | |
|
97 | 74 | emit hoverEnter(pos); |
|
98 | 75 | } |
|
99 | 76 | |
|
100 | 77 | void QBarSet::barHoverLeaved() |
|
101 | 78 | { |
|
102 | 79 | // qDebug() << "QBarset::barHoverLeaved" << this; |
|
103 | 80 | // if (mToolTipEnabled) { |
|
104 | 81 | // TODO: do what? |
|
105 | 82 | // } |
|
106 | 83 | // Emit signal to user of charts |
|
107 | 84 | emit hoverLeave(); |
|
108 | 85 | } |
|
109 | 86 | |
|
110 | 87 | #include "moc_qbarset.cpp" |
|
111 | 88 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,63 +1,60 | |||
|
1 | 1 | #ifndef QBARSET_H |
|
2 | 2 | #define QBARSET_H |
|
3 | 3 | |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <QPen> |
|
6 | 6 | #include <QBrush> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject |
|
11 | 11 | { |
|
12 | 12 | Q_OBJECT |
|
13 | 13 | public: |
|
14 | 14 | QBarSet(QString name, QObject *parent = 0); |
|
15 | 15 | |
|
16 | 16 | void setName(QString name); |
|
17 | 17 | QString name(); |
|
18 | 18 | QBarSet& operator << (const qreal &value); // appends new value to set |
|
19 | 19 | |
|
20 | 20 | int count(); // count of values in set |
|
21 | 21 | qreal valueAt(int index); // for modifying individual values |
|
22 | 22 | void setValue(int index, qreal value); // setter for individual value |
|
23 | 23 | |
|
24 | 24 | void setPen(const QPen& pen); |
|
25 | 25 | const QPen& pen() const; |
|
26 | 26 | |
|
27 | 27 | void setBrush(const QBrush& brush); |
|
28 | 28 | const QBrush& brush() const; |
|
29 | 29 | |
|
30 | 30 | Q_SIGNALS: |
|
31 | void clicked(); // Clicked and hover signals exposed to user | |
|
31 | void clicked(); // Clicked and hover signals exposed to user | |
|
32 | 32 | void hoverEnter(QPoint pos); |
|
33 | 33 | void hoverLeave(); |
|
34 | void toggleFloatingValues(); // Private signal, TODO: move to private impl | |
|
35 | void showToolTip(QPoint pos, QString tip); // Private signal, TODO: move to private impl | |
|
36 | 34 | |
|
37 | public Q_SLOTS: | |
|
38 | void enableFloatingValues(bool enabled); // enables floating values on top of bars | |
|
39 | void enableToolTip(bool enabled); // enables tooltips | |
|
40 | void enableSeparators(bool enabled); // enables separators between categories | |
|
35 | // TODO: Expose this to user or not? | |
|
36 | // TODO: TO PIMPL ---> | |
|
37 | void toggleFloatingValues(); | |
|
38 | void showToolTip(QPoint pos, QString tip); // Private signal | |
|
39 | // <--- TO PIMPL | |
|
41 | 40 | |
|
42 | // TODO: these slots belong to private implementation. | |
|
43 | // These are for single bars to notify set about internal events | |
|
41 | public Q_SLOTS: | |
|
42 | // These are for internal communication | |
|
43 | // TODO: TO PIMPL ---> | |
|
44 | 44 | void barClicked(); |
|
45 | 45 | void barHoverEntered(QPoint pos); |
|
46 | 46 | void barHoverLeaved(); |
|
47 | // <--- TO PIMPL | |
|
47 | 48 | |
|
48 | 49 | private: |
|
49 | 50 | |
|
50 | 51 | QString mName; |
|
51 | 52 | QList<qreal> mValues; |
|
52 | 53 | QPen mPen; |
|
53 | 54 | QBrush mBrush; |
|
54 | 55 | |
|
55 | // TODO: to pimpl | |
|
56 | bool mFloatingValuesEnabled; | |
|
57 | bool mToolTipEnabled; | |
|
58 | bool mSeparatorsEnabled; | |
|
59 | 56 | }; |
|
60 | 57 | |
|
61 | 58 | QTCOMMERCIALCHART_END_NAMESPACE |
|
62 | 59 | |
|
63 | 60 | #endif // QBARSET_H |
@@ -1,44 +1,45 | |||
|
1 | 1 | #include "separator_p.h" |
|
2 | 2 | #include <QDebug> |
|
3 | 3 | #include <QPainter> |
|
4 | 4 | |
|
5 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 6 | |
|
7 | 7 | Separator::Separator(QGraphicsItem *parent) |
|
8 | 8 | : ChartItem(parent) |
|
9 | 9 | { |
|
10 | 10 | } |
|
11 | 11 | |
|
12 | 12 | void Separator::setPos(qreal x, qreal y) |
|
13 | 13 | { |
|
14 | 14 | mXpos = x; |
|
15 | 15 | mYpos = y; |
|
16 | 16 | } |
|
17 | 17 | |
|
18 | 18 | void Separator::setColor(QColor color) |
|
19 | 19 | { |
|
20 | 20 | mColor = color; |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | void Separator::setSize(const QSizeF &size) |
|
24 | 24 | { |
|
25 | 25 | mWidth = size.width(); |
|
26 | 26 | mHeight = size.height(); |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
30 | 30 | { |
|
31 | // TODO: real pen | |
|
32 | QPen pen(mColor); | |
|
33 | painter->setPen(pen); | |
|
34 | painter->drawLine(mXpos,mYpos,mXpos,mHeight); | |
|
31 | if (isVisible()) { | |
|
32 | QPen pen(mColor); | |
|
33 | painter->setPen(pen); | |
|
34 | painter->drawLine(mXpos,mYpos,mXpos,mHeight); | |
|
35 | } | |
|
35 | 36 | } |
|
36 | 37 | |
|
37 | 38 | QRectF Separator::boundingRect() const |
|
38 | 39 | { |
|
39 | 40 | QRectF r(mXpos,mYpos,mWidth,mHeight); |
|
40 | 41 | return r; |
|
41 | 42 | } |
|
42 | 43 | |
|
43 | 44 | |
|
44 | 45 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,369 +1,372 | |||
|
1 | 1 | #include "mainwidget.h" |
|
2 | 2 | #include "dataseriedialog.h" |
|
3 | 3 | #include "qchartseries.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | 5 | #include "qscatterseries.h" |
|
6 | 6 | #include <qlinechartseries.h> |
|
7 | 7 | #include <qbarset.h> |
|
8 | 8 | #include <qbarcategory.h> |
|
9 | 9 | #include <qbarchartseries.h> |
|
10 | 10 | #include <qstackedbarchartseries.h> |
|
11 | 11 | #include <qpercentbarchartseries.h> |
|
12 | 12 | #include <QPushButton> |
|
13 | 13 | #include <QComboBox> |
|
14 | 14 | #include <QSpinBox> |
|
15 | 15 | #include <QCheckBox> |
|
16 | 16 | #include <QGridLayout> |
|
17 | 17 | #include <QHBoxLayout> |
|
18 | 18 | #include <QLabel> |
|
19 | 19 | #include <QSpacerItem> |
|
20 | 20 | #include <QMessageBox> |
|
21 | 21 | #include <cmath> |
|
22 | 22 | #include <QDebug> |
|
23 | 23 | #include <QStandardItemModel> |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | MainWidget::MainWidget(QWidget *parent) : |
|
29 | 29 | QWidget(parent) |
|
30 | 30 | { |
|
31 | 31 | m_chartWidget = new QChartView(this); |
|
32 | 32 | m_chartWidget->setRubberBandPolicy(QChartView::HorizonalRubberBand); |
|
33 | 33 | |
|
34 | 34 | // Grid layout for the controls for configuring the chart widget |
|
35 | 35 | QGridLayout *grid = new QGridLayout(); |
|
36 | 36 | QPushButton *addSeriesButton = new QPushButton("Add series"); |
|
37 | 37 | connect(addSeriesButton, SIGNAL(clicked()), this, SLOT(addSeries())); |
|
38 | 38 | grid->addWidget(addSeriesButton, 0, 1); |
|
39 | 39 | initBackroundCombo(grid); |
|
40 | 40 | initScaleControls(grid); |
|
41 | 41 | initThemeCombo(grid); |
|
42 | 42 | QCheckBox *zoomCheckBox = new QCheckBox("Drag'n drop Zoom"); |
|
43 | 43 | connect(zoomCheckBox, SIGNAL(toggled(bool)), m_chartWidget, SLOT(setZoomEnabled(bool))); |
|
44 | 44 | zoomCheckBox->setChecked(true); |
|
45 | 45 | grid->addWidget(zoomCheckBox, grid->rowCount(), 0); |
|
46 | 46 | // add row with empty label to make all the other rows static |
|
47 | 47 | grid->addWidget(new QLabel(""), grid->rowCount(), 0); |
|
48 | 48 | grid->setRowStretch(grid->rowCount() - 1, 1); |
|
49 | 49 | |
|
50 | 50 | // Another grid layout as a main layout |
|
51 | 51 | QGridLayout *mainLayout = new QGridLayout(); |
|
52 | 52 | mainLayout->addLayout(grid, 0, 0); |
|
53 | 53 | |
|
54 | 54 | // Init series type specific controls |
|
55 | 55 | initPieControls(); |
|
56 | 56 | mainLayout->addLayout(m_pieLayout, 2, 0); |
|
57 | 57 | // Scatter series specific settings |
|
58 | 58 | // m_scatterLayout = new QGridLayout(); |
|
59 | 59 | // m_scatterLayout->addWidget(new QLabel("scatter"), 0, 0); |
|
60 | 60 | // m_scatterLayout->setEnabled(false); |
|
61 | 61 | // mainLayout->addLayout(m_scatterLayout, 1, 0); |
|
62 | 62 | |
|
63 | 63 | // Add layouts and the chart widget to the main layout |
|
64 | 64 | mainLayout->addWidget(m_chartWidget, 0, 1, 3, 1); |
|
65 | 65 | setLayout(mainLayout); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | // Combo box for selecting the chart's background |
|
69 | 69 | void MainWidget::initBackroundCombo(QGridLayout *grid) |
|
70 | 70 | { |
|
71 | 71 | QComboBox *backgroundCombo = new QComboBox(this); |
|
72 | 72 | backgroundCombo->addItem("Color"); |
|
73 | 73 | backgroundCombo->addItem("Gradient"); |
|
74 | 74 | backgroundCombo->addItem("Image"); |
|
75 | 75 | connect(backgroundCombo, SIGNAL(currentIndexChanged(int)), |
|
76 | 76 | this, SLOT(backgroundChanged(int))); |
|
77 | 77 | |
|
78 | 78 | grid->addWidget(new QLabel("Background:"), grid->rowCount(), 0); |
|
79 | 79 | grid->addWidget(backgroundCombo, grid->rowCount() - 1, 1); |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | // Scale related controls (auto-scale vs. manual min-max values) |
|
83 | 83 | void MainWidget::initScaleControls(QGridLayout *grid) |
|
84 | 84 | { |
|
85 | 85 | m_autoScaleCheck = new QCheckBox("Automatic scaling"); |
|
86 | 86 | connect(m_autoScaleCheck, SIGNAL(stateChanged(int)), this, SLOT(autoScaleChanged(int))); |
|
87 | 87 | // Allow setting also non-sense values (like -2147483648 and 2147483647) |
|
88 | 88 | m_xMinSpin = new QSpinBox(); |
|
89 | 89 | m_xMinSpin->setMinimum(INT_MIN); |
|
90 | 90 | m_xMinSpin->setMaximum(INT_MAX); |
|
91 | 91 | m_xMinSpin->setValue(0); |
|
92 | 92 | connect(m_xMinSpin, SIGNAL(valueChanged(int)), this, SLOT(xMinChanged(int))); |
|
93 | 93 | m_xMaxSpin = new QSpinBox(); |
|
94 | 94 | m_xMaxSpin->setMinimum(INT_MIN); |
|
95 | 95 | m_xMaxSpin->setMaximum(INT_MAX); |
|
96 | 96 | m_xMaxSpin->setValue(10); |
|
97 | 97 | connect(m_xMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(xMaxChanged(int))); |
|
98 | 98 | m_yMinSpin = new QSpinBox(); |
|
99 | 99 | m_yMinSpin->setMinimum(INT_MIN); |
|
100 | 100 | m_yMinSpin->setMaximum(INT_MAX); |
|
101 | 101 | m_yMinSpin->setValue(0); |
|
102 | 102 | connect(m_yMinSpin, SIGNAL(valueChanged(int)), this, SLOT(yMinChanged(int))); |
|
103 | 103 | m_yMaxSpin = new QSpinBox(); |
|
104 | 104 | m_yMaxSpin->setMinimum(INT_MIN); |
|
105 | 105 | m_yMaxSpin->setMaximum(INT_MAX); |
|
106 | 106 | m_yMaxSpin->setValue(10); |
|
107 | 107 | connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int))); |
|
108 | 108 | |
|
109 | 109 | grid->addWidget(m_autoScaleCheck, grid->rowCount(), 0); |
|
110 | 110 | grid->addWidget(new QLabel("x min:"), grid->rowCount(), 0); |
|
111 | 111 | grid->addWidget(m_xMinSpin, grid->rowCount() - 1, 1); |
|
112 | 112 | grid->addWidget(new QLabel("x max:"), grid->rowCount(), 0); |
|
113 | 113 | grid->addWidget(m_xMaxSpin, grid->rowCount() - 1, 1); |
|
114 | 114 | grid->addWidget(new QLabel("y min:"), grid->rowCount(), 0); |
|
115 | 115 | grid->addWidget(m_yMinSpin, grid->rowCount() - 1, 1); |
|
116 | 116 | grid->addWidget(new QLabel("y max:"), grid->rowCount(), 0); |
|
117 | 117 | grid->addWidget(m_yMaxSpin, grid->rowCount() - 1, 1); |
|
118 | 118 | |
|
119 | 119 | m_autoScaleCheck->setChecked(true); |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | // Combo box for selecting theme |
|
123 | 123 | void MainWidget::initThemeCombo(QGridLayout *grid) |
|
124 | 124 | { |
|
125 | 125 | QComboBox *chartTheme = new QComboBox(); |
|
126 | 126 | chartTheme->addItem("Default"); |
|
127 | 127 | chartTheme->addItem("Vanilla"); |
|
128 | 128 | chartTheme->addItem("Icy"); |
|
129 | 129 | chartTheme->addItem("Grayscale"); |
|
130 | 130 | chartTheme->addItem("Scientific"); |
|
131 | 131 | chartTheme->addItem("Unnamed1"); |
|
132 | 132 | connect(chartTheme, SIGNAL(currentIndexChanged(int)), |
|
133 | 133 | this, SLOT(changeChartTheme(int))); |
|
134 | 134 | grid->addWidget(new QLabel("Chart theme:"), 8, 0); |
|
135 | 135 | grid->addWidget(chartTheme, 8, 1); |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | void MainWidget::initPieControls() |
|
139 | 139 | { |
|
140 | 140 | // Pie series specific settings |
|
141 | 141 | // Pie size factory |
|
142 | 142 | QDoubleSpinBox *pieSizeSpin = new QDoubleSpinBox(); |
|
143 | 143 | pieSizeSpin->setMinimum(LONG_MIN); |
|
144 | 144 | pieSizeSpin->setMaximum(LONG_MAX); |
|
145 | 145 | pieSizeSpin->setValue(1.0); |
|
146 | 146 | pieSizeSpin->setSingleStep(0.1); |
|
147 | 147 | connect(pieSizeSpin, SIGNAL(valueChanged(double)), this, SLOT(setPieSizeFactor(double))); |
|
148 | 148 | // Pie position |
|
149 | 149 | QComboBox *piePosCombo = new QComboBox(this); |
|
150 | 150 | piePosCombo->addItem("Maximized"); |
|
151 | 151 | piePosCombo->addItem("Top left"); |
|
152 | 152 | piePosCombo->addItem("Top right"); |
|
153 | 153 | piePosCombo->addItem("Bottom left"); |
|
154 | 154 | piePosCombo->addItem("Bottom right"); |
|
155 | 155 | connect(piePosCombo, SIGNAL(currentIndexChanged(int)), |
|
156 | 156 | this, SLOT(setPiePosition(int))); |
|
157 | 157 | m_pieLayout = new QGridLayout(); |
|
158 | 158 | m_pieLayout->setEnabled(false); |
|
159 | 159 | m_pieLayout->addWidget(new QLabel("Pie size factor"), 0, 0); |
|
160 | 160 | m_pieLayout->addWidget(pieSizeSpin, 0, 1); |
|
161 | 161 | m_pieLayout->addWidget(new QLabel("Pie position"), 1, 0); |
|
162 | 162 | m_pieLayout->addWidget(piePosCombo, 1, 1); |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | void MainWidget::addSeries() |
|
166 | 166 | { |
|
167 | 167 | DataSerieDialog dialog(m_defaultSeriesName, this); |
|
168 | 168 | connect(&dialog, SIGNAL(accepted(QString, int, int, QString, bool)), |
|
169 | 169 | this, SLOT(addSeries(QString, int, int, QString, bool))); |
|
170 | 170 | dialog.exec(); |
|
171 | 171 | } |
|
172 | 172 | |
|
173 | 173 | QList<RealList> MainWidget::generateTestData(int columnCount, int rowCount, QString dataCharacteristics) |
|
174 | 174 | { |
|
175 | 175 | // TODO: dataCharacteristics |
|
176 | 176 | QList<RealList> testData; |
|
177 | 177 | for (int j(0); j < columnCount; j++) { |
|
178 | 178 | QList <qreal> newColumn; |
|
179 | 179 | for (int i(0); i < rowCount; i++) { |
|
180 | 180 | if (dataCharacteristics == "Sin") { |
|
181 | 181 | newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100)); |
|
182 | 182 | } else if (dataCharacteristics == "Sin + random") { |
|
183 | 183 | newColumn.append(abs(sin(3.14159265358979 / 50 * i) * 100) + (rand() % 5)); |
|
184 | 184 | } else if (dataCharacteristics == "Random") { |
|
185 | 185 | newColumn.append(rand() % 5); |
|
186 | 186 | } else if (dataCharacteristics == "Linear") { |
|
187 | 187 | //newColumn.append(i * (j + 1.0)); |
|
188 | 188 | // TODO: temporary hack to make pie work; prevent zero values: |
|
189 | 189 | newColumn.append(i * (j + 1.0) + 0.1); |
|
190 | 190 | } else { // "constant" |
|
191 | 191 | newColumn.append((j + 1.0)); |
|
192 | 192 | } |
|
193 | 193 | } |
|
194 | 194 | testData.append(newColumn); |
|
195 | 195 | } |
|
196 | 196 | return testData; |
|
197 | 197 | } |
|
198 | 198 | |
|
199 | 199 | QStringList MainWidget::generateLabels(int count) |
|
200 | 200 | { |
|
201 | 201 | QStringList result; |
|
202 | 202 | for (int i(0); i < count; i++) |
|
203 | 203 | result.append("label" + QString::number(i)); |
|
204 | 204 | return result; |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QString dataCharacteristics, bool labelsEnabled) |
|
208 | 208 | { |
|
209 | 209 | qDebug() << "addSeries: " << seriesName |
|
210 | 210 | << " columnCount: " << columnCount |
|
211 | 211 | << " rowCount: " << rowCount |
|
212 | 212 | << " dataCharacteristics: " << dataCharacteristics |
|
213 | 213 | << " labels enabled: " << labelsEnabled; |
|
214 | 214 | m_defaultSeriesName = seriesName; |
|
215 | 215 | |
|
216 | 216 | QList<RealList> data = generateTestData(columnCount, rowCount, dataCharacteristics); |
|
217 | 217 | |
|
218 | 218 | // Line series and scatter series use similar data |
|
219 | 219 | if (seriesName.contains("line", Qt::CaseInsensitive)) { |
|
220 | 220 | for (int j(0); j < data.count(); j ++) { |
|
221 | 221 | QList<qreal> column = data.at(j); |
|
222 | 222 | QLineChartSeries *series = new QLineChartSeries(); |
|
223 | 223 | for (int i(0); i < column.count(); i++) { |
|
224 | 224 | series->add(i, column.at(i)); |
|
225 | 225 | } |
|
226 | 226 | m_chartWidget->addSeries(series); |
|
227 | 227 | setCurrentSeries(series); |
|
228 | 228 | } |
|
229 | 229 | } else if (seriesName.contains("scatter", Qt::CaseInsensitive)) { |
|
230 | 230 | for (int j(0); j < data.count(); j++) { |
|
231 | 231 | QList<qreal> column = data.at(j); |
|
232 | 232 | QScatterSeries *series = new QScatterSeries(); |
|
233 | 233 | for (int i(0); i < column.count(); i++) { |
|
234 | 234 | (*series) << QPointF(i, column.at(i)); |
|
235 | 235 | } |
|
236 | 236 | m_chartWidget->addSeries(series); |
|
237 | 237 | setCurrentSeries(series); |
|
238 | 238 | } |
|
239 | 239 | } else if (seriesName.contains("pie", Qt::CaseInsensitive)) { |
|
240 | 240 | QStringList labels = generateLabels(rowCount); |
|
241 | 241 | for (int j(0); j < data.count(); j++) { |
|
242 | 242 | QPieSeries *series = new QPieSeries(); |
|
243 | 243 | QList<qreal> column = data.at(j); |
|
244 | 244 | for (int i(0); i < column.count(); i++) { |
|
245 | 245 | series->add(column.at(i), labels.at(i)); |
|
246 | 246 | } |
|
247 | 247 | m_chartWidget->addSeries(series); |
|
248 | 248 | setCurrentSeries(series); |
|
249 | 249 | } |
|
250 | 250 | } else if (seriesName == "Bar" |
|
251 | 251 | || seriesName == "Stacked bar" |
|
252 | 252 | || seriesName == "Percent bar") { |
|
253 | 253 | // TODO: replace QBarCategory with QStringList? |
|
254 | 254 | QBarCategory *category = new QBarCategory; |
|
255 | 255 | QStringList labels = generateLabels(rowCount); |
|
256 | 256 | foreach(QString label, labels) |
|
257 | 257 | *category << label; |
|
258 | 258 | QBarChartSeries* series = 0; |
|
259 | 259 | if (seriesName == "Bar") |
|
260 | 260 | series = new QBarChartSeries(category, this); |
|
261 | 261 | else if (seriesName == "Stacked bar") |
|
262 | 262 | series = new QStackedBarChartSeries(category, this); |
|
263 | 263 | else |
|
264 | 264 | series = new QPercentBarChartSeries(category, this); |
|
265 | 265 | |
|
266 | 266 | for (int j(0); j < data.count(); j++) { |
|
267 | 267 | QList<qreal> column = data.at(j); |
|
268 | 268 | QBarSet *set = new QBarSet("set" + QString::number(j)); |
|
269 | 269 | for (int i(0); i < column.count(); i++) { |
|
270 | 270 | *set << column.at(i); |
|
271 | 271 | } |
|
272 | 272 | series->addBarSet(set); |
|
273 | 273 | } |
|
274 | series->enableFloatingValues(); | |
|
275 | series->enableToolTip(); | |
|
276 | series->enableSeparators(false); | |
|
274 | 277 | m_chartWidget->addSeries(series); |
|
275 | 278 | setCurrentSeries(series); |
|
276 | 279 | } |
|
277 | 280 | |
|
278 | 281 | // TODO: spline and area |
|
279 | 282 | } |
|
280 | 283 | |
|
281 | 284 | void MainWidget::setCurrentSeries(QChartSeries *series) |
|
282 | 285 | { |
|
283 | 286 | if (series) { |
|
284 | 287 | m_currentSeries = series; |
|
285 | 288 | switch (m_currentSeries->type()) { |
|
286 | 289 | case QChartSeries::SeriesTypeLine: |
|
287 | 290 | break; |
|
288 | 291 | case QChartSeries::SeriesTypeScatter: |
|
289 | 292 | break; |
|
290 | 293 | case QChartSeries::SeriesTypePie: |
|
291 | 294 | break; |
|
292 | 295 | case QChartSeries::SeriesTypeBar: |
|
293 | 296 | qDebug() << "setCurrentSeries (bar)"; |
|
294 | 297 | break; |
|
295 | 298 | case QChartSeries::SeriesTypeStackedBar: |
|
296 | 299 | qDebug() << "setCurrentSeries (Stackedbar)"; |
|
297 | 300 | break; |
|
298 | 301 | case QChartSeries::SeriesTypePercentBar: |
|
299 | 302 | qDebug() << "setCurrentSeries (Percentbar)"; |
|
300 | 303 | break; |
|
301 | 304 | default: |
|
302 | 305 | Q_ASSERT(false); |
|
303 | 306 | break; |
|
304 | 307 | } |
|
305 | 308 | } |
|
306 | 309 | } |
|
307 | 310 | |
|
308 | 311 | void MainWidget::backgroundChanged(int itemIndex) |
|
309 | 312 | { |
|
310 | 313 | qDebug() << "backgroundChanged: " << itemIndex; |
|
311 | 314 | } |
|
312 | 315 | |
|
313 | 316 | void MainWidget::autoScaleChanged(int value) |
|
314 | 317 | { |
|
315 | 318 | if (value) { |
|
316 | 319 | // TODO: enable auto scaling |
|
317 | 320 | } else { |
|
318 | 321 | // TODO: set scaling manually (and disable auto scaling) |
|
319 | 322 | } |
|
320 | 323 | |
|
321 | 324 | m_xMinSpin->setEnabled(!value); |
|
322 | 325 | m_xMaxSpin->setEnabled(!value); |
|
323 | 326 | m_yMinSpin->setEnabled(!value); |
|
324 | 327 | m_yMaxSpin->setEnabled(!value); |
|
325 | 328 | } |
|
326 | 329 | |
|
327 | 330 | void MainWidget::xMinChanged(int value) |
|
328 | 331 | { |
|
329 | 332 | qDebug() << "xMinChanged: " << value; |
|
330 | 333 | } |
|
331 | 334 | |
|
332 | 335 | void MainWidget::xMaxChanged(int value) |
|
333 | 336 | { |
|
334 | 337 | qDebug() << "xMaxChanged: " << value; |
|
335 | 338 | } |
|
336 | 339 | |
|
337 | 340 | void MainWidget::yMinChanged(int value) |
|
338 | 341 | { |
|
339 | 342 | qDebug() << "yMinChanged: " << value; |
|
340 | 343 | } |
|
341 | 344 | |
|
342 | 345 | void MainWidget::yMaxChanged(int value) |
|
343 | 346 | { |
|
344 | 347 | qDebug() << "yMaxChanged: " << value; |
|
345 | 348 | } |
|
346 | 349 | |
|
347 | 350 | void MainWidget::changeChartTheme(int themeIndex) |
|
348 | 351 | { |
|
349 | 352 | qDebug() << "changeChartTheme: " << themeIndex; |
|
350 | 353 | m_chartWidget->setChartTheme((QChart::ChartTheme) themeIndex); |
|
351 | 354 | //TODO: remove this hack. This is just to make it so that theme change is seen immediately. |
|
352 | 355 | QSize s = size(); |
|
353 | 356 | s.setWidth(s.width()+1); |
|
354 | 357 | resize(s); |
|
355 | 358 | } |
|
356 | 359 | |
|
357 | 360 | void MainWidget::setPieSizeFactor(double size) |
|
358 | 361 | { |
|
359 | 362 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); |
|
360 | 363 | if (pie) |
|
361 | 364 | pie->setSizeFactor(qreal(size)); |
|
362 | 365 | } |
|
363 | 366 | |
|
364 | 367 | void MainWidget::setPiePosition(int position) |
|
365 | 368 | { |
|
366 | 369 | QPieSeries *pie = qobject_cast<QPieSeries *>(m_currentSeries); |
|
367 | 370 | if (pie) |
|
368 | 371 | pie->setPosition((QPieSeries::PiePosition) position); |
|
369 | 372 | } |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
|
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