##// END OF EJS Templates
Barchart updates on BarSet->SetValue now
Marek Rosa -
r655:7ed6749a4b0d
parent child
Show More
@@ -1,249 +1,249
1 #include "tablewidget.h"
1 #include "tablewidget.h"
2 #include <QGridLayout>
2 #include <QGridLayout>
3 #include <QTableView>
3 #include <QTableView>
4 #include <QStyledItemDelegate>
4 #include <QStyledItemDelegate>
5 #include "qlineseries.h"
5 #include "qlineseries.h"
6 #include "qsplineseries.h"
6 #include "qsplineseries.h"
7 #include "qscatterseries.h"
7 #include "qscatterseries.h"
8 #include "customtablemodel.h"
8 #include "customtablemodel.h"
9 #include "qpieseries.h"
9 #include "qpieseries.h"
10 #include "qareaseries.h"
10 #include "qareaseries.h"
11 #include "qbarseries.h"
11 #include "qbarseries.h"
12 #include <QPushButton>
12 #include <QPushButton>
13 #include <QRadioButton>
13 #include <QRadioButton>
14 #include <QTime>
14 #include <QTime>
15
15
16 TableWidget::TableWidget(QWidget *parent)
16 TableWidget::TableWidget(QWidget *parent)
17 : QWidget(parent)
17 : QWidget(parent)
18 {
18 {
19 setGeometry(100, 100, 1000, 600);
19 setGeometry(100, 100, 1000, 600);
20 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
20 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
21 // create simple model for storing data
21 // create simple model for storing data
22 // user's table data model
22 // user's table data model
23 m_model = new CustomTableModel;
23 m_model = new CustomTableModel;
24 tableView = new QTableView;
24 tableView = new QTableView;
25 tableView->setModel(m_model);
25 tableView->setModel(m_model);
26 tableView->setMinimumHeight(240);
26 tableView->setMinimumHeight(240);
27 // tableView->setMinimumSize(340, 480);
27 // tableView->setMinimumSize(340, 480);
28 // tableView->setItemDelegate(new QStyledItemDelegate);
28 // tableView->setItemDelegate(new QStyledItemDelegate);
29 chartView = new QChartView(this);
29 chartView = new QChartView(this);
30 chartView->setRenderHint(QPainter::Antialiasing);
30 chartView->setRenderHint(QPainter::Antialiasing);
31 chartView->setMinimumSize(640, 480);
31 chartView->setMinimumSize(640, 480);
32
32
33 // create
33 // create
34 // QLineSeries* series = new QLineSeries;
34 // QLineSeries* series = new QLineSeries;
35 // QSplineSeries* series = new QSplineSeries;
35 // QSplineSeries* series = new QSplineSeries;
36 // QScatterSeries* series = new QScatterSeries;
36 // QScatterSeries* series = new QScatterSeries;
37 // series->setModel(m_model);
37 // series->setModel(m_model);
38 // series->setModelMapping(0,1, Qt::Vertical);
38 // series->setModelMapping(0,1, Qt::Vertical);
39
39
40 // QPieSeries* pieSeries = new QPieSeries;
40 // QPieSeries* pieSeries = new QPieSeries;
41 // pieSeries->setModel(model);
41 // pieSeries->setModel(model);
42 // pieSeries
42 // pieSeries
43
43
44 // chartView->addSeries(series);
44 // chartView->addSeries(series);
45
45
46 // add, remove data buttons
46 // add, remove data buttons
47 QPushButton* addRowAboveButton = new QPushButton("Add row above");
47 QPushButton* addRowAboveButton = new QPushButton("Add row above");
48 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
48 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
49
49
50 QPushButton* addRowBelowButton = new QPushButton("Add row below");
50 QPushButton* addRowBelowButton = new QPushButton("Add row below");
51 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
51 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
52
52
53 QPushButton* removeRowButton = new QPushButton("Remove row");
53 QPushButton* removeRowButton = new QPushButton("Remove row");
54 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
54 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
55
55
56 // buttons layout
56 // buttons layout
57 QVBoxLayout* buttonsLayout = new QVBoxLayout;
57 QVBoxLayout* buttonsLayout = new QVBoxLayout;
58 buttonsLayout->addWidget(addRowAboveButton);
58 buttonsLayout->addWidget(addRowAboveButton);
59 buttonsLayout->addWidget(addRowBelowButton);
59 buttonsLayout->addWidget(addRowBelowButton);
60 buttonsLayout->addWidget(removeRowButton);
60 buttonsLayout->addWidget(removeRowButton);
61 buttonsLayout->addStretch();
61 buttonsLayout->addStretch();
62
62
63 // chart type radio buttons
63 // chart type radio buttons
64 lineRadioButton = new QRadioButton("Line");
64 lineRadioButton = new QRadioButton("Line");
65 splineRadioButton = new QRadioButton("Spline");
65 splineRadioButton = new QRadioButton("Spline");
66 scatterRadioButton = new QRadioButton("Scatter");
66 scatterRadioButton = new QRadioButton("Scatter");
67 pieRadioButton = new QRadioButton("Pie");
67 pieRadioButton = new QRadioButton("Pie");
68 areaRadioButton = new QRadioButton("Area");
68 areaRadioButton = new QRadioButton("Area");
69 barRadioButton = new QRadioButton("Bar");
69 barRadioButton = new QRadioButton("Bar");
70
70
71 connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
71 connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
72 connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
72 connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
73 connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
73 connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
74 connect(pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
74 connect(pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
75 connect(areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
75 connect(areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
76 connect(barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
76 connect(barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
77 lineRadioButton->setChecked(true);
77 lineRadioButton->setChecked(true);
78
78
79 // radio buttons layout
79 // radio buttons layout
80 QVBoxLayout* radioLayout = new QVBoxLayout;
80 QVBoxLayout* radioLayout = new QVBoxLayout;
81 radioLayout->addWidget(lineRadioButton);
81 radioLayout->addWidget(lineRadioButton);
82 radioLayout->addWidget(splineRadioButton);
82 radioLayout->addWidget(splineRadioButton);
83 radioLayout->addWidget(scatterRadioButton);
83 radioLayout->addWidget(scatterRadioButton);
84 radioLayout->addWidget(pieRadioButton);
84 radioLayout->addWidget(pieRadioButton);
85 radioLayout->addWidget(areaRadioButton);
85 radioLayout->addWidget(areaRadioButton);
86 radioLayout->addWidget(barRadioButton);
86 radioLayout->addWidget(barRadioButton);
87 radioLayout->addStretch();
87 radioLayout->addStretch();
88
88
89 // create main layout
89 // create main layout
90 QGridLayout* mainLayout = new QGridLayout;
90 QGridLayout* mainLayout = new QGridLayout;
91 mainLayout->addLayout(buttonsLayout, 1, 0);
91 mainLayout->addLayout(buttonsLayout, 1, 0);
92 mainLayout->addLayout(radioLayout, 2, 0);
92 mainLayout->addLayout(radioLayout, 2, 0);
93 mainLayout->addWidget(tableView, 1, 1);
93 mainLayout->addWidget(tableView, 1, 1);
94 mainLayout->addWidget(chartView, 2, 1);
94 mainLayout->addWidget(chartView, 2, 1);
95 setLayout(mainLayout);
95 setLayout(mainLayout);
96 lineRadioButton->setFocus();
96 lineRadioButton->setFocus();
97 }
97 }
98
98
99 void TableWidget::addRowAbove()
99 void TableWidget::addRowAbove()
100 {
100 {
101 // m_model->insertRow(m_model->rowCount());
101 // m_model->insertRow(m_model->rowCount());
102 m_model->insertRow(tableView->currentIndex().row());
102 m_model->insertRow(tableView->currentIndex().row());
103
103
104 }
104 }
105
105
106 void TableWidget::addRowBelow()
106 void TableWidget::addRowBelow()
107 {
107 {
108 // m_model->insertRow(m_model->rowCount());
108 // m_model->insertRow(m_model->rowCount());
109 m_model->insertRow(tableView->currentIndex().row() + 1);
109 m_model->insertRow(tableView->currentIndex().row() + 1);
110
110
111 }
111 }
112
112
113 void TableWidget::removeRow()
113 void TableWidget::removeRow()
114 {
114 {
115 // m_model->removeRow(m_model->rowCount() - 1);
115 // m_model->removeRow(m_model->rowCount() - 1);
116 m_model->removeRow(tableView->currentIndex().row());
116 m_model->removeRow(tableView->currentIndex().row());
117 }
117 }
118
118
119 void TableWidget::updateChartType()
119 void TableWidget::updateChartType()
120 {
120 {
121 chartView->removeAllSeries();
121 chartView->removeAllSeries();
122
122
123 if (lineRadioButton->isChecked())
123 if (lineRadioButton->isChecked())
124 {
124 {
125 // series 1
125 // series 1
126 series = new QLineSeries;
126 series = new QLineSeries;
127 series->setModel(m_model);
127 series->setModel(m_model);
128 series->setModelMapping(0,1, Qt::Vertical);
128 series->setModelMapping(0,1, Qt::Vertical);
129 // series->setModelMapping(0,1, Qt::Horizontal);
129 // series->setModelMapping(0,1, Qt::Horizontal);
130 chartView->addSeries(series);
130 chartView->addSeries(series);
131
131
132 // series 2
132 // series 2
133 series = new QLineSeries;
133 series = new QLineSeries;
134 series->setModel(m_model);
134 series->setModel(m_model);
135 series->setModelMapping(2,3, Qt::Vertical);
135 series->setModelMapping(2,3, Qt::Vertical);
136 // series->setModelMapping(2,3, Qt::Horizontal);
136 // series->setModelMapping(2,3, Qt::Horizontal);
137 chartView->addSeries(series);
137 chartView->addSeries(series);
138
138
139 // series 3
139 // series 3
140 series = new QLineSeries;
140 series = new QLineSeries;
141 series->setModel(m_model);
141 series->setModel(m_model);
142 series->setModelMapping(4,5, Qt::Vertical);
142 series->setModelMapping(4,5, Qt::Vertical);
143 // series->setModelMapping(4,5, Qt::Horizontal);
143 // series->setModelMapping(4,5, Qt::Horizontal);
144 chartView->addSeries(series);
144 chartView->addSeries(series);
145 }
145 }
146 else if (splineRadioButton->isChecked())
146 else if (splineRadioButton->isChecked())
147 {
147 {
148 // series 1
148 // series 1
149 series = new QSplineSeries;
149 series = new QSplineSeries;
150 series->setModel(m_model);
150 series->setModel(m_model);
151 series->setModelMapping(0,1, Qt::Vertical);
151 series->setModelMapping(0,1, Qt::Vertical);
152 // series->setModelMapping(0,1, Qt::Horizontal);
152 // series->setModelMapping(0,1, Qt::Horizontal);
153 chartView->addSeries(series);
153 chartView->addSeries(series);
154
154
155 // series 2
155 // series 2
156 series = new QSplineSeries;
156 series = new QSplineSeries;
157 series->setModel(m_model);
157 series->setModel(m_model);
158 series->setModelMapping(2,3, Qt::Vertical);
158 series->setModelMapping(2,3, Qt::Vertical);
159 // series->setModelMapping(2,3, Qt::Horizontal);
159 // series->setModelMapping(2,3, Qt::Horizontal);
160 chartView->addSeries(series);
160 chartView->addSeries(series);
161
161
162 // series 3
162 // series 3
163 series = new QSplineSeries;
163 series = new QSplineSeries;
164 series->setModel(m_model);
164 series->setModel(m_model);
165 series->setModelMapping(4,5, Qt::Vertical);
165 series->setModelMapping(4,5, Qt::Vertical);
166 // series->setModelMapping(4,5, Qt::Horizontal);
166 // series->setModelMapping(4,5, Qt::Horizontal);
167 chartView->addSeries(series);
167 chartView->addSeries(series);
168 }
168 }
169 else if (scatterRadioButton->isChecked())
169 else if (scatterRadioButton->isChecked())
170 {
170 {
171 // series 1
171 // series 1
172 series = new QScatterSeries;
172 series = new QScatterSeries;
173 series->setModel(m_model);
173 series->setModel(m_model);
174 series->setModelMapping(0,1, Qt::Vertical);
174 series->setModelMapping(0,1, Qt::Vertical);
175 // series->setModelMapping(0,1, Qt::Horizontal);
175 // series->setModelMapping(0,1, Qt::Horizontal);
176 chartView->addSeries(series);
176 chartView->addSeries(series);
177
177
178 // series 2
178 // series 2
179 series = new QScatterSeries;
179 series = new QScatterSeries;
180 series->setModel(m_model);
180 series->setModel(m_model);
181 series->setModelMapping(2,3, Qt::Vertical);
181 series->setModelMapping(2,3, Qt::Vertical);
182 // series->setModelMapping(2,3, Qt::Horizontal);
182 // series->setModelMapping(2,3, Qt::Horizontal);
183 chartView->addSeries(series);
183 chartView->addSeries(series);
184
184
185 // series 3
185 // series 3
186 series = new QScatterSeries;
186 series = new QScatterSeries;
187 series->setModel(m_model);
187 series->setModel(m_model);
188 series->setModelMapping(4,5, Qt::Vertical);
188 series->setModelMapping(4,5, Qt::Vertical);
189 // series->setModelMapping(4,5, Qt::Horizontal);
189 // series->setModelMapping(4,5, Qt::Horizontal);
190 chartView->addSeries(series);
190 chartView->addSeries(series);
191 }
191 }
192 else if (pieRadioButton->isChecked())
192 else if (pieRadioButton->isChecked())
193 {
193 {
194 // pie 1
194 // pie 1
195 QPieSeries* pieSeries = new QPieSeries;
195 QPieSeries* pieSeries = new QPieSeries;
196 pieSeries->setModel(m_model);
196 pieSeries->setModel(m_model);
197 pieSeries->setModelMapping(0,0, Qt::Horizontal);
197 pieSeries->setModelMapping(0,0, Qt::Vertical);
198 pieSeries->setLabelsVisible(true);
198 pieSeries->setLabelsVisible(true);
199 pieSeries->setPieSize(0.4);
199 pieSeries->setPieSize(0.4);
200 pieSeries->setPiePosition(0.2, 0.35);
200 pieSeries->setPiePosition(0.2, 0.35);
201 chartView->addSeries(pieSeries);
201 chartView->addSeries(pieSeries);
202
202
203 // pie 2
203 // pie 2
204 pieSeries = new QPieSeries;
204 pieSeries = new QPieSeries;
205 pieSeries->setModel(m_model);
205 pieSeries->setModel(m_model);
206 pieSeries->setModelMapping(1,1, Qt::Horizontal);
206 pieSeries->setModelMapping(1,1, Qt::Vertical);
207 pieSeries->setLabelsVisible(true);
207 pieSeries->setLabelsVisible(true);
208 pieSeries->setPieSize(0.4);
208 pieSeries->setPieSize(0.4);
209 pieSeries->setPiePosition(0.8, 0.35);
209 pieSeries->setPiePosition(0.8, 0.35);
210 chartView->addSeries(pieSeries);
210 chartView->addSeries(pieSeries);
211
211
212 // pie 3
212 // pie 3
213 pieSeries = new QPieSeries;
213 pieSeries = new QPieSeries;
214 pieSeries->setModel(m_model);
214 pieSeries->setModel(m_model);
215 pieSeries->setModelMapping(2,2, Qt::Horizontal);
215 pieSeries->setModelMapping(2,2, Qt::Vertical);
216 pieSeries->setLabelsVisible(true);
216 pieSeries->setLabelsVisible(true);
217 pieSeries->setPieSize(0.4);
217 pieSeries->setPieSize(0.4);
218 pieSeries->setPiePosition(0.5, 0.65);
218 pieSeries->setPiePosition(0.5, 0.65);
219 chartView->addSeries(pieSeries);
219 chartView->addSeries(pieSeries);
220 }
220 }
221 else if (areaRadioButton->isChecked())
221 else if (areaRadioButton->isChecked())
222 {
222 {
223 QLineSeries* upperLineSeries = new QLineSeries;
223 QLineSeries* upperLineSeries = new QLineSeries;
224 upperLineSeries->setModel(m_model);
224 upperLineSeries->setModel(m_model);
225 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
225 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
226 QLineSeries* lowerLineSeries = new QLineSeries;
226 QLineSeries* lowerLineSeries = new QLineSeries;
227 lowerLineSeries->setModel(m_model);
227 lowerLineSeries->setModel(m_model);
228 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
228 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
229 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
229 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
230 chartView->addSeries(areaSeries);
230 chartView->addSeries(areaSeries);
231 }
231 }
232 else if (barRadioButton->isChecked())
232 else if (barRadioButton->isChecked())
233 {
233 {
234 QBarSeries* barSeries = new QBarSeries(QStringList());
234 QBarSeries* barSeries = new QBarSeries(QStringList());
235 barSeries->setModel(m_model);
235 barSeries->setModel(m_model);
236 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
236 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
237 barSeries->setToolTipEnabled(true);
237 barSeries->setToolTipEnabled(true);
238 chartView->addSeries(barSeries);
238 chartView->addSeries(barSeries);
239 }
239 }
240
240
241 // series->setModel(m_model);
241 // series->setModel(m_model);
242 // series->setModelMapping(0,1, Qt::Vertical);
242 // series->setModelMapping(0,1, Qt::Vertical);
243 // chartView->addSeries(series);
243 // chartView->addSeries(series);
244 }
244 }
245
245
246 TableWidget::~TableWidget()
246 TableWidget::~TableWidget()
247 {
247 {
248
248
249 }
249 }
@@ -1,85 +1,87
1 #include "barpresenter_p.h"
1 #include "barpresenter_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "qbarset.h"
4 #include "qbarset.h"
5 #include <QDebug>
5 #include <QDebug>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) :
9 BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) :
10 BarPresenterBase(series, parent)
10 BarPresenterBase(series, parent)
11 {
11 {
12 connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged()));
12 }
13 }
13
14
14 void BarPresenter::layoutChanged()
15 void BarPresenter::layoutChanged()
15 {
16 {
16 // Scale bars to new layout
17 // Scale bars to new layout
17 // Layout for bars:
18 // Layout for bars:
18 if (mSeries->barsetCount() <= 0) {
19 if (mSeries->barsetCount() <= 0) {
19 qDebug() << "No sets in model!";
20 qDebug() << "No sets in model!";
20 return;
21 return;
21 }
22 }
22
23
23 if (childItems().count() == 0) {
24 if (childItems().count() == 0) {
24 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
25 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
25 return;
26 return;
26 }
27 }
27
28
28 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
29 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
29 int categoryCount = mSeries->categoryCount();
30 int categoryCount = mSeries->categoryCount();
30 int setCount = mSeries->barsetCount();
31 int setCount = mSeries->barsetCount();
31
32
32 qreal tW = mWidth;
33 qreal tW = mWidth;
33 qreal tH = mHeight;
34 qreal tH = mHeight;
34 qreal tM = mSeries->max();
35 qreal tM = mSeries->max();
35 qreal scale = (tH/tM);
36 qreal scale = (tH/tM);
36 qreal tC = categoryCount + 1;
37 qreal tC = categoryCount + 1;
37 qreal categoryWidth = tW/tC;
38 qreal categoryWidth = tW/tC;
38 mBarWidth = categoryWidth / (setCount+1);
39 mBarWidth = categoryWidth / (setCount+1);
39
40
40 int itemIndex(0);
41 int itemIndex(0);
41 for (int category=0; category < categoryCount; category++) {
42 for (int category=0; category < categoryCount; category++) {
42 qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2;
43 qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2;
43 qreal yPos = mHeight;
44 qreal yPos = mHeight;
44 for (int set = 0; set < setCount; set++) {
45 for (int set = 0; set < setCount; set++) {
45 qreal barHeight = mSeries->valueAt(set,category) * scale;
46 qreal barHeight = mSeries->valueAt(set,category) * scale;
46 Bar* bar = mBars.at(itemIndex);
47 Bar* bar = mBars.at(itemIndex);
47
48
48 // TODO: width settable per bar?
49 // TODO: width settable per bar?
49 bar->resize(mBarWidth, barHeight);
50 bar->resize(mBarWidth, barHeight);
50 bar->setBrush(mSeries->barsetAt(set)->brush());
51 bar->setBrush(mSeries->barsetAt(set)->brush());
51 bar->setPos(xPos, yPos-barHeight);
52 bar->setPos(xPos, yPos-barHeight);
52 itemIndex++;
53 itemIndex++;
53 xPos += mBarWidth;
54 xPos += mBarWidth;
54 }
55 }
55 }
56 }
56
57
57 // Position floating values
58 // Position floating values
58 itemIndex = 0;
59 itemIndex = 0;
59 for (int category=0; category < mSeries->categoryCount(); category++) {
60 for (int category=0; category < mSeries->categoryCount(); category++) {
60 qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth;
61 qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth;
61 qreal yPos = mHeight;
62 qreal yPos = mHeight;
62 for (int set=0; set < mSeries->barsetCount(); set++) {
63 for (int set=0; set < mSeries->barsetCount(); set++) {
63 qreal barHeight = mSeries->valueAt(set,category) * scale;
64 qreal barHeight = mSeries->valueAt(set,category) * scale;
64 BarValue* value = mFloatingValues.at(itemIndex);
65 BarValue* value = mFloatingValues.at(itemIndex);
65
66
66 QBarSet* barSet = mSeries->barsetAt(set);
67 QBarSet* barSet = mSeries->barsetAt(set);
67 value->resize(100,50); // TODO: proper layout for this.
68 value->resize(100,50); // TODO: proper layout for this.
68 value->setPos(xPos, yPos-barHeight/2);
69 value->setPos(xPos, yPos-barHeight/2);
69 value->setPen(barSet->floatingValuePen());
70 value->setPen(barSet->floatingValuePen());
70
71
71 if (mSeries->valueAt(set,category) != 0) {
72 if (mSeries->valueAt(set,category) != 0) {
72 value->setValueString(QString::number(mSeries->valueAt(set,category)));
73 value->setValueString(QString::number(mSeries->valueAt(set,category)));
73 } else {
74 } else {
74 value->setValueString(QString(""));
75 value->setValueString(QString(""));
75 }
76 }
76
77
77 itemIndex++;
78 itemIndex++;
78 xPos += mBarWidth;
79 xPos += mBarWidth;
79 }
80 }
80 }
81 }
82 update();
81 }
83 }
82
84
83 #include "moc_barpresenter_p.cpp"
85 #include "moc_barpresenter_p.cpp"
84
86
85 QTCOMMERCIALCHART_END_NAMESPACE
87 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,65
1 #ifndef BARPRESENTERBASE_H
1 #ifndef BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "qbarseries.h"
5 #include "qbarseries.h"
6 #include <QPen>
6 #include <QPen>
7 #include <QBrush>
7 #include <QBrush>
8 #include <QGraphicsItem>
8 #include <QGraphicsItem>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class Bar;
12 class Bar;
13 class BarValue;
13 class BarValue;
14 class QChartAxisCategories;
14 class QChartAxisCategories;
15 class QChart;
15 class QChart;
16
16
17 // Common implemantation of different presenters. Not to be instantiated.
17 // Common implemantation of different presenters. Not to be instantiated.
18 // TODO: combine this with BarPresenter and derive other presenters from it?
18 // TODO: combine this with BarPresenter and derive other presenters from it?
19 class BarPresenterBase : public QObject, public ChartItem
19 class BarPresenterBase : public QObject, public ChartItem
20 {
20 {
21 Q_OBJECT
21 Q_OBJECT
22 public:
22 public:
23 BarPresenterBase(QBarSeries *series, QChart *parent = 0);
23 BarPresenterBase(QBarSeries *series, QChart *parent = 0);
24 virtual ~BarPresenterBase();
24 virtual ~BarPresenterBase();
25
25
26 public:
26 public:
27 // From QGraphicsItem
27 // From QGraphicsItem
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 QRectF boundingRect() const;
29 QRectF boundingRect() const;
30
30
31 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
31 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
32 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
32 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
33 private slots:
33 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
34 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
34
35
35 protected:
36 protected:
36 void initAxisLabels();
37 void initAxisLabels();
37
38
38 public slots:
39 public slots:
39 void handleModelChanged(int index);
40 void handleModelChanged(int index);
40 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
41 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
41 void handleGeometryChanged(const QRectF& size);
42 void handleGeometryChanged(const QRectF& size);
42
43
43 // Internal slots
44 // Internal slots
44 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
45 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
45
46
46 protected:
47 protected:
47
48
48 // TODO: consider these.
49 // TODO: consider these.
49 int mHeight; // Layout spesific
50 int mHeight; // Layout spesific
50 int mWidth;
51 int mWidth;
51 qreal mBarWidth;
52 qreal mBarWidth;
52
53
53 bool mLayoutSet; // True, if component has been laid out.
54 bool mLayoutSet; // True, if component has been laid out.
54
55
55 // Not owned.
56 // Not owned.
56 QBarSeries* mSeries;
57 QBarSeries* mSeries;
57 QList<Bar*> mBars;
58 QList<Bar*> mBars;
58 QList<BarValue*> mFloatingValues;
59 QList<BarValue*> mFloatingValues;
59 QChart* mChart;
60 QChart* mChart;
60 };
61 };
61
62
62 QTCOMMERCIALCHART_END_NAMESPACE
63 QTCOMMERCIALCHART_END_NAMESPACE
63
64
64 #endif // BARPRESENTERBASE_H
65 #endif // BARPRESENTERBASE_H
@@ -1,331 +1,331
1 #include <QDebug>
1 #include <QDebug>
2 #include "qbarseries.h"
2 #include "qbarseries.h"
3 #include "qbarset.h"
3 #include "qbarset.h"
4 #include "barchartmodel_p.h"
4 #include "barchartmodel_p.h"
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 /*!
8 /*!
9 \class QBarSeries
9 \class QBarSeries
10 \brief part of QtCommercial chart API.
10 \brief part of QtCommercial chart API.
11
11
12 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
12 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
13 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
13 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
14 by QStringList.
14 by QStringList.
15
15
16 \mainclass
16 \mainclass
17
17
18 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
18 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
19 */
19 */
20
20
21 /*!
21 /*!
22 \fn virtual QSeriesType QBarSeries::type() const
22 \fn virtual QSeriesType QBarSeries::type() const
23 \brief Returns type of series.
23 \brief Returns type of series.
24 \sa QSeries, QSeriesType
24 \sa QSeries, QSeriesType
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
28 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
29 \brief \internal \a pos \a tip
29 \brief \internal \a pos \a tip
30 */
30 */
31
31
32 /*!
32 /*!
33 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
33 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
34 QBarSeries is QObject which is a child of a \a parent.
34 QBarSeries is QObject which is a child of a \a parent.
35 */
35 */
36 QBarSeries::QBarSeries(QStringList categories, QObject *parent)
36 QBarSeries::QBarSeries(QStringList categories, QObject *parent)
37 : QSeries(parent)
37 : QSeries(parent)
38 ,mModel(new BarChartModel(categories, this))
38 ,mModel(new BarChartModel(categories, this))
39 {
39 {
40 m_model = NULL;
40 m_model = NULL;
41 m_mapCategories = -1;
41 m_mapCategories = -1;
42 m_mapBarBottom = -1;
42 m_mapBarBottom = -1;
43 m_mapBarTop = -1;
43 m_mapBarTop = -1;
44 m_mapOrientation = Qt::Vertical;
44 m_mapOrientation = Qt::Vertical;
45 }
45 }
46
46
47 /*!
47 /*!
48 Adds a set of bars to series. Takes ownership of \a set.
48 Adds a set of bars to series. Takes ownership of \a set.
49 Connects the clicked(QString) and rightClicked(QString) signals
49 Connects the clicked(QString) and rightClicked(QString) signals
50 of \a set to this series
50 of \a set to this series
51 */
51 */
52 void QBarSeries::addBarSet(QBarSet *set)
52 void QBarSeries::addBarSet(QBarSet *set)
53 {
53 {
54 mModel->addBarSet(set);
54 mModel->addBarSet(set);
55 connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
55 connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
56 connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
56 connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
57 connect(set, SIGNAL(changed()), this, SLOT(barsetChanged()));
57 connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
58 }
58 }
59
59
60 /*!
60 /*!
61 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
61 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
62 Disconnects the clicked(QString) and rightClicked(QString) signals
62 Disconnects the clicked(QString) and rightClicked(QString) signals
63 of \a set from this series
63 of \a set from this series
64 */
64 */
65 void QBarSeries::removeBarSet(QBarSet *set)
65 void QBarSeries::removeBarSet(QBarSet *set)
66 {
66 {
67 disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
67 disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
68 disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
68 disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
69 mModel->removeBarSet(set);
69 mModel->removeBarSet(set);
70 }
70 }
71
71
72 /*!
72 /*!
73 Returns number of sets in series.
73 Returns number of sets in series.
74 */
74 */
75 int QBarSeries::barsetCount()
75 int QBarSeries::barsetCount()
76 {
76 {
77 // if(m_model)
77 // if(m_model)
78 // return m_mapBarTop - m_mapBarBottom;
78 // return m_mapBarTop - m_mapBarBottom;
79 // else
79 // else
80 return mModel->barsetCount();
80 return mModel->barsetCount();
81 }
81 }
82
82
83 /*!
83 /*!
84 Returns number of categories in series
84 Returns number of categories in series
85 */
85 */
86 int QBarSeries::categoryCount()
86 int QBarSeries::categoryCount()
87 {
87 {
88 return mModel->categoryCount();
88 return mModel->categoryCount();
89 }
89 }
90
90
91 /*!
91 /*!
92 Returns a list of sets in series. Keeps ownership of sets.
92 Returns a list of sets in series. Keeps ownership of sets.
93 */
93 */
94 QList<QBarSet*> QBarSeries::barSets()
94 QList<QBarSet*> QBarSeries::barSets()
95 {
95 {
96 return mModel->barSets();
96 return mModel->barSets();
97 }
97 }
98
98
99 /*!
99 /*!
100 \internal \a index
100 \internal \a index
101 */
101 */
102 QBarSet* QBarSeries::barsetAt(int index)
102 QBarSet* QBarSeries::barsetAt(int index)
103 {
103 {
104 return mModel->setAt(index);
104 return mModel->setAt(index);
105 }
105 }
106
106
107 /*!
107 /*!
108 \internal \a category
108 \internal \a category
109 */
109 */
110 QString QBarSeries::categoryName(int category)
110 QString QBarSeries::categoryName(int category)
111 {
111 {
112 return mModel->categoryName(category);
112 return mModel->categoryName(category);
113 }
113 }
114
114
115 /*!
115 /*!
116 Enables or disables tooltip depending on parameter \a enabled.
116 Enables or disables tooltip depending on parameter \a enabled.
117 Tooltip shows the name of set, when mouse is hovering on top of bar.
117 Tooltip shows the name of set, when mouse is hovering on top of bar.
118 Calling without parameter \a enabled, enables the tooltip
118 Calling without parameter \a enabled, enables the tooltip
119 */
119 */
120 void QBarSeries::setToolTipEnabled(bool enabled)
120 void QBarSeries::setToolTipEnabled(bool enabled)
121 {
121 {
122 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
122 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
123 if (enabled) {
123 if (enabled) {
124 for (int i=0; i<mModel->barsetCount(); i++) {
124 for (int i=0; i<mModel->barsetCount(); i++) {
125 QBarSet *set = mModel->setAt(i);
125 QBarSet *set = mModel->setAt(i);
126 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
126 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
127 }
127 }
128 } else {
128 } else {
129 for (int i=0; i<mModel->barsetCount(); i++) {
129 for (int i=0; i<mModel->barsetCount(); i++) {
130 QBarSet *set = mModel->setAt(i);
130 QBarSet *set = mModel->setAt(i);
131 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
131 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
132 }
132 }
133 }
133 }
134 }
134 }
135
135
136
136
137 /*!
137 /*!
138 \internal \a category
138 \internal \a category
139 */
139 */
140 void QBarSeries::barsetClicked(QString category)
140 void QBarSeries::barsetClicked(QString category)
141 {
141 {
142 emit clicked(qobject_cast<QBarSet*>(sender()), category);
142 emit clicked(qobject_cast<QBarSet*>(sender()), category);
143 }
143 }
144
144
145 /*!
145 /*!
146 \internal \a category
146 \internal \a category
147 */
147 */
148 void QBarSeries::barsetRightClicked(QString category)
148 void QBarSeries::barsetRightClicked(QString category)
149 {
149 {
150 emit rightClicked(qobject_cast<QBarSet*>(sender()), category);
150 emit rightClicked(qobject_cast<QBarSet*>(sender()), category);
151 }
151 }
152
152
153
153
154 /*!
154 /*!
155 \internal
155 \internal
156 */
156 */
157 qreal QBarSeries::min()
157 qreal QBarSeries::min()
158 {
158 {
159 return mModel->min();
159 return mModel->min();
160 }
160 }
161
161
162 /*!
162 /*!
163 \internal
163 \internal
164 */
164 */
165 qreal QBarSeries::max()
165 qreal QBarSeries::max()
166 {
166 {
167 return mModel->max();
167 return mModel->max();
168 }
168 }
169
169
170 /*!
170 /*!
171 \internal \a set \a category
171 \internal \a set \a category
172 */
172 */
173 qreal QBarSeries::valueAt(int set, int category)
173 qreal QBarSeries::valueAt(int set, int category)
174 {
174 {
175 return mModel->valueAt(set,category);
175 return mModel->valueAt(set,category);
176 }
176 }
177
177
178 /*!
178 /*!
179 \internal \a set \a category
179 \internal \a set \a category
180 */
180 */
181 qreal QBarSeries::percentageAt(int set, int category)
181 qreal QBarSeries::percentageAt(int set, int category)
182 {
182 {
183 return mModel->percentageAt(set,category);
183 return mModel->percentageAt(set,category);
184 }
184 }
185
185
186 /*!
186 /*!
187 \internal \a category
187 \internal \a category
188 */
188 */
189 qreal QBarSeries::categorySum(int category)
189 qreal QBarSeries::categorySum(int category)
190 {
190 {
191 return mModel->categorySum(category);
191 return mModel->categorySum(category);
192 }
192 }
193
193
194 /*!
194 /*!
195 \internal
195 \internal
196 */
196 */
197 qreal QBarSeries::maxCategorySum()
197 qreal QBarSeries::maxCategorySum()
198 {
198 {
199 return mModel->maxCategorySum();
199 return mModel->maxCategorySum();
200 }
200 }
201
201
202 /*!
202 /*!
203 \internal
203 \internal
204 */
204 */
205 BarChartModel& QBarSeries::model()
205 BarChartModel& QBarSeries::model()
206 {
206 {
207 return *mModel;
207 return *mModel;
208 }
208 }
209
209
210 bool QBarSeries::setModel(QAbstractItemModel* model)
210 bool QBarSeries::setModel(QAbstractItemModel* model)
211 {
211 {
212 // disconnect signals from old model
212 // disconnect signals from old model
213 if(m_model)
213 if(m_model)
214 {
214 {
215 disconnect(m_model, 0, this, 0);
215 disconnect(m_model, 0, this, 0);
216 m_mapCategories = -1;
216 m_mapCategories = -1;
217 m_mapBarBottom = -1;
217 m_mapBarBottom = -1;
218 m_mapBarTop = -1;
218 m_mapBarTop = -1;
219 m_mapOrientation = Qt::Vertical;
219 m_mapOrientation = Qt::Vertical;
220 }
220 }
221
221
222 // set new model
222 // set new model
223 if(model)
223 if(model)
224 {
224 {
225 m_model = model;
225 m_model = model;
226 return true;
226 return true;
227 }
227 }
228 else
228 else
229 {
229 {
230 m_model = NULL;
230 m_model = NULL;
231 return false;
231 return false;
232 }
232 }
233 }
233 }
234
234
235 // TODO
235 // TODO
236 void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
236 void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
237 {
237 {
238 if (m_model == NULL)
238 if (m_model == NULL)
239 return;
239 return;
240 m_mapCategories = categories;
240 m_mapCategories = categories;
241 m_mapBarBottom = bottomBoundry;
241 m_mapBarBottom = bottomBoundry;
242 m_mapBarTop = topBoundry;
242 m_mapBarTop = topBoundry;
243 m_mapOrientation = orientation;
243 m_mapOrientation = orientation;
244
244
245 // connect the signals
245 // connect the signals
246 if (m_mapOrientation == Qt::Vertical)
246 if (m_mapOrientation == Qt::Vertical)
247 {
247 {
248 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
248 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
249 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
249 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
250 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
250 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
251 }
251 }
252 else
252 else
253 {
253 {
254 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
254 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
255 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
255 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
256 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
256 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
257 }
257 }
258
258
259
259
260 // create the initial bars
260 // create the initial bars
261 delete mModel;
261 delete mModel;
262 if (m_mapOrientation == Qt::Vertical)
262 if (m_mapOrientation == Qt::Vertical)
263 {
263 {
264 QStringList categories;
264 QStringList categories;
265 for (int k = 0; k < m_model->rowCount(); k++)
265 for (int k = 0; k < m_model->rowCount(); k++)
266 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
266 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
267 mModel = new BarChartModel(categories, this);
267 mModel = new BarChartModel(categories, this);
268
268
269 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
269 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
270 {
270 {
271 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
271 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
272 for(int m = 0; m < m_model->rowCount(); m++)
272 for(int m = 0; m < m_model->rowCount(); m++)
273 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
273 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
274 addBarSet(barSet);
274 addBarSet(barSet);
275 }
275 }
276 }
276 }
277 else
277 else
278 {
278 {
279 QStringList categories;
279 QStringList categories;
280 for (int k = 0; k < m_model->columnCount(); k++)
280 for (int k = 0; k < m_model->columnCount(); k++)
281 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
281 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
282 mModel = new BarChartModel(categories, this);
282 mModel = new BarChartModel(categories, this);
283
283
284 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
284 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
285 {
285 {
286 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
286 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
287 for(int m = 0; m < m_model->columnCount(); m++)
287 for(int m = 0; m < m_model->columnCount(); m++)
288 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
288 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
289 addBarSet(barSet);
289 addBarSet(barSet);
290 }
290 }
291 }
291 }
292 }
292 }
293
293
294 void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
294 void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
295 {
295 {
296 Q_UNUSED(bottomRight)
296 Q_UNUSED(bottomRight)
297
297
298 if (m_mapOrientation == Qt::Vertical)
298 if (m_mapOrientation == Qt::Vertical)
299 {
299 {
300 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop)
300 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop)
301 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
301 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
302 // else if (topLeft.column() == m_mapCategories)
302 // else if (topLeft.column() == m_mapCategories)
303 // slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
303 // slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
304 }
304 }
305 else
305 else
306 {
306 {
307 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop)
307 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop)
308 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
308 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
309 // else if (topLeft.row() == m_mapCategories)
309 // else if (topLeft.row() == m_mapCategories)
310 // slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
310 // slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
311 }
311 }
312 }
312 }
313
313
314 void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int /*start*/, int /*end*/)
314 void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int /*start*/, int /*end*/)
315 {
315 {
316 //
316 //
317 }
317 }
318
318
319 void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int /*start*/, int /*end*/)
319 void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int /*start*/, int /*end*/)
320 {
320 {
321 //
321 //
322 }
322 }
323
323
324 void QBarSeries::barsetChanged()
324 void QBarSeries::barsetChanged()
325 {
325 {
326 // mModel->
326 emit updatedBars();
327 }
327 }
328
328
329 #include "moc_qbarseries.cpp"
329 #include "moc_qbarseries.cpp"
330
330
331 QTCOMMERCIALCHART_END_NAMESPACE
331 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,84 +1,87
1 #ifndef BARSERIES_H
1 #ifndef BARSERIES_H
2 #define BARSERIES_H
2 #define BARSERIES_H
3
3
4 #include "qseries.h"
4 #include "qseries.h"
5 #include <QStringList>
5 #include <QStringList>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class QBarSet;
9 class QBarSet;
10 class BarChartModel;
10 class BarChartModel;
11 class BarCategory;
11 class BarCategory;
12
12
13 // Container for series
13 // Container for series
14 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries
14 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 QBarSeries(QStringList categories, QObject* parent=0);
18 QBarSeries(QStringList categories, QObject* parent=0);
19
19
20 virtual QSeriesType type() const { return QSeries::SeriesTypeBar; }
20 virtual QSeriesType type() const { return QSeries::SeriesTypeBar; }
21
21
22 void addBarSet(QBarSet *set); // Takes ownership of set
22 void addBarSet(QBarSet *set); // Takes ownership of set
23 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
23 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
24 int barsetCount();
24 int barsetCount();
25 int categoryCount();
25 int categoryCount();
26 QList<QBarSet*> barSets();
26 QList<QBarSet*> barSets();
27
27
28 bool setModel(QAbstractItemModel* model);
28 bool setModel(QAbstractItemModel* model);
29 QAbstractItemModel* modelExt() {return m_model;}
29 QAbstractItemModel* modelExt() {return m_model;}
30 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
30 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
31
31
32 public:
32 public:
33 // TODO: Functions below this are not part of api and will be moved
33 // TODO: Functions below this are not part of api and will be moved
34 // to private implementation, when we start using it
34 // to private implementation, when we start using it
35 // TODO: TO PIMPL --->
35 // TODO: TO PIMPL --->
36 QBarSet* barsetAt(int index);
36 QBarSet* barsetAt(int index);
37 QString categoryName(int category);
37 QString categoryName(int category);
38 qreal min();
38 qreal min();
39 qreal max();
39 qreal max();
40 qreal valueAt(int set, int category);
40 qreal valueAt(int set, int category);
41 qreal percentageAt(int set, int category);
41 qreal percentageAt(int set, int category);
42 qreal categorySum(int category);
42 qreal categorySum(int category);
43 qreal maxCategorySum();
43 qreal maxCategorySum();
44 BarChartModel& model();
44 BarChartModel& model();
45 // <--- TO PIMPL
45 // <--- TO PIMPL
46
46
47 signals:
47 signals:
48 //void changed(int index);
48 //void changed(int index);
49 void clicked(QBarSet* barset, QString category); // Up to user of api, what to do with these signals
49 void clicked(QBarSet* barset, QString category); // Up to user of api, what to do with these signals
50 void rightClicked(QBarSet* barset, QString category);
50 void rightClicked(QBarSet* barset, QString category);
51
51
52 //
53 void updatedBars();
54
52 // TODO: internal signals, these to private implementation.
55 // TODO: internal signals, these to private implementation.
53 // TODO: TO PIMPL --->
56 // TODO: TO PIMPL --->
54 void showToolTip(QPoint pos, QString tip);
57 void showToolTip(QPoint pos, QString tip);
55 // <--- TO PIMPL
58 // <--- TO PIMPL
56
59
57 public Q_SLOTS:
60 public Q_SLOTS:
58 void setToolTipEnabled(bool enabled=true); // enables tooltips
61 void setToolTipEnabled(bool enabled=true); // enables tooltips
59
62
60 // TODO: TO PIMPL --->
63 // TODO: TO PIMPL --->
61 void barsetClicked(QString category);
64 void barsetClicked(QString category);
62 void barsetRightClicked(QString category);
65 void barsetRightClicked(QString category);
63 // <--- TO PIMPL
66 // <--- TO PIMPL
64
67
65 private Q_SLOTS:
68 private Q_SLOTS:
66 // slots for updating bars when data in model changes
69 // slots for updating bars when data in model changes
67 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
70 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
68 void modelDataAdded(QModelIndex parent, int start, int end);
71 void modelDataAdded(QModelIndex parent, int start, int end);
69 void modelDataRemoved(QModelIndex parent, int start, int end);
72 void modelDataRemoved(QModelIndex parent, int start, int end);
70 void barsetChanged();
73 void barsetChanged();
71
74
72 protected:
75 protected:
73 BarChartModel* mModel;
76 BarChartModel* mModel;
74
77
75 QAbstractItemModel* m_model;
78 // QAbstractItemModel* m_model;
76 int m_mapCategories;
79 int m_mapCategories;
77 int m_mapBarBottom;
80 int m_mapBarBottom;
78 int m_mapBarTop;
81 int m_mapBarTop;
79 Qt::Orientation m_mapOrientation;
82 Qt::Orientation m_mapOrientation;
80 };
83 };
81
84
82 QTCOMMERCIALCHART_END_NAMESPACE
85 QTCOMMERCIALCHART_END_NAMESPACE
83
86
84 #endif // BARSERIES_H
87 #endif // BARSERIES_H
@@ -1,194 +1,195
1 #include "qbarset.h"
1 #include "qbarset.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QToolTip>
3 #include <QToolTip>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 /*!
7 /*!
8 \class QBarSet
8 \class QBarSet
9 \brief part of QtCommercial chart API.
9 \brief part of QtCommercial chart API.
10
10
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
12 First value of set is assumed to belong to first category, second to second category and so on.
12 First value of set is assumed to belong to first category, second to second category and so on.
13 If set has fewer values than there are categories, then the missing values are assumed to be
13 If set has fewer values than there are categories, then the missing values are assumed to be
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
15
15
16 \mainclass
16 \mainclass
17
17
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
19 */
19 */
20
20
21 /*!
21 /*!
22 \fn void QBarSet::clicked(QString category)
22 \fn void QBarSet::clicked(QString category)
23 \brief signals that set has been clicked
23 \brief signals that set has been clicked
24 Parameter \a category describes on which category was clicked
24 Parameter \a category describes on which category was clicked
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QBarSet::rightClicked(QString category)
28 \fn void QBarSet::rightClicked(QString category)
29 \brief signals that set has been clicked with right mouse button
29 \brief signals that set has been clicked with right mouse button
30 Parameter \a category describes on which category was clicked
30 Parameter \a category describes on which category was clicked
31 */
31 */
32
32
33 /*!
33 /*!
34 \fn void QBarSet::hoverEnter(QPoint pos)
34 \fn void QBarSet::hoverEnter(QPoint pos)
35 \brief signals that mouse has entered over the set at position \a pos.
35 \brief signals that mouse has entered over the set at position \a pos.
36 */
36 */
37
37
38 /*!
38 /*!
39 \fn void QBarSet::hoverLeave()
39 \fn void QBarSet::hoverLeave()
40 \brief signals that mouse has left from the set.
40 \brief signals that mouse has left from the set.
41 */
41 */
42
42
43 /*!
43 /*!
44 \fn void QBarSet::toggleFloatingValues()
44 \fn void QBarSet::toggleFloatingValues()
45 \brief \internal
45 \brief \internal
46 */
46 */
47
47
48 /*!
48 /*!
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
50 \brief \internal \a pos \a tip
50 \brief \internal \a pos \a tip
51 */
51 */
52
52
53
53
54 /*!
54 /*!
55 Constructs QBarSet with a name of \a name and with parent of \a parent
55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 */
56 */
57 QBarSet::QBarSet(QString name, QObject *parent)
57 QBarSet::QBarSet(QString name, QObject *parent)
58 : QObject(parent)
58 : QObject(parent)
59 ,mName(name)
59 ,mName(name)
60 {
60 {
61 }
61 }
62
62
63 /*!
63 /*!
64 Sets new \a name for set.
64 Sets new \a name for set.
65 */
65 */
66 void QBarSet::setName(QString name)
66 void QBarSet::setName(QString name)
67 {
67 {
68 mName = name;
68 mName = name;
69 }
69 }
70
70
71 /*!
71 /*!
72 Returns name of the set.
72 Returns name of the set.
73 */
73 */
74 QString QBarSet::name()
74 QString QBarSet::name()
75 {
75 {
76 return mName;
76 return mName;
77 }
77 }
78
78
79 /*!
79 /*!
80 Appends new value \a value to the end of set.
80 Appends new value \a value to the end of set.
81 */
81 */
82 QBarSet& QBarSet::operator << (const qreal &value)
82 QBarSet& QBarSet::operator << (const qreal &value)
83 {
83 {
84 mValues.append(value);
84 mValues.append(value);
85 emit structureChanged();
85 return *this;
86 return *this;
86 }
87 }
87
88
88 /*!
89 /*!
89 Returns count of values in set.
90 Returns count of values in set.
90 */
91 */
91 int QBarSet::count()
92 int QBarSet::count()
92 {
93 {
93 return mValues.count();
94 return mValues.count();
94 }
95 }
95
96
96 /*!
97 /*!
97 Returns value of set indexed by \a index
98 Returns value of set indexed by \a index
98 */
99 */
99 qreal QBarSet::valueAt(int index)
100 qreal QBarSet::valueAt(int index)
100 {
101 {
101 return mValues.at(index);
102 return mValues.at(index);
102 }
103 }
103
104
104 /*!
105 /*!
105 Sets a new value \a value to set, indexed by \a index
106 Sets a new value \a value to set, indexed by \a index
106 */
107 */
107 void QBarSet::setValue(int index, qreal value)
108 void QBarSet::setValue(int index, qreal value)
108 {
109 {
109 mValues.replace(index,value);
110 mValues.replace(index,value);
110 emit changed();
111 emit valueChanged();
111 }
112 }
112
113
113 /*!
114 /*!
114 Returns total sum of all values in barset.
115 Returns total sum of all values in barset.
115 */
116 */
116 qreal QBarSet::total()
117 qreal QBarSet::total()
117 {
118 {
118 qreal total(0);
119 qreal total(0);
119 for (int i=0; i<mValues.count(); i++) {
120 for (int i=0; i<mValues.count(); i++) {
120 total += mValues.at(i);
121 total += mValues.at(i);
121 }
122 }
122 return total;
123 return total;
123 }
124 }
124
125
125 /*!
126 /*!
126 Sets pen for set. Bars of this set are drawn using \a pen
127 Sets pen for set. Bars of this set are drawn using \a pen
127 */
128 */
128 void QBarSet::setPen(const QPen pen)
129 void QBarSet::setPen(const QPen pen)
129 {
130 {
130 mPen = pen;
131 mPen = pen;
131 emit changed();
132 emit valueChanged();
132 }
133 }
133
134
134 /*!
135 /*!
135 Returns pen of the set.
136 Returns pen of the set.
136 */
137 */
137 QPen QBarSet::pen() const
138 QPen QBarSet::pen() const
138 {
139 {
139 return mPen;
140 return mPen;
140 }
141 }
141
142
142 /*!
143 /*!
143 Sets brush for the set. Bars of this set are drawn using \a brush
144 Sets brush for the set. Bars of this set are drawn using \a brush
144 */
145 */
145 void QBarSet::setBrush(const QBrush brush)
146 void QBarSet::setBrush(const QBrush brush)
146 {
147 {
147 mBrush = brush;
148 mBrush = brush;
148 emit changed();
149 emit valueChanged();
149 }
150 }
150
151
151 /*!
152 /*!
152 Returns brush of the set.
153 Returns brush of the set.
153 */
154 */
154 QBrush QBarSet::brush() const
155 QBrush QBarSet::brush() const
155 {
156 {
156 return mBrush;
157 return mBrush;
157 }
158 }
158
159
159 /*!
160 /*!
160 Sets the pen for floating values that are drawn on top of this set
161 Sets the pen for floating values that are drawn on top of this set
161 */
162 */
162 void QBarSet::setFloatingValuePen(const QPen pen)
163 void QBarSet::setFloatingValuePen(const QPen pen)
163 {
164 {
164 mFloatingValuePen = pen;
165 mFloatingValuePen = pen;
165 }
166 }
166
167
167 /*!
168 /*!
168 Returns the pen for floating values that are drawn on top of this set
169 Returns the pen for floating values that are drawn on top of this set
169 */
170 */
170 QPen QBarSet::floatingValuePen() const
171 QPen QBarSet::floatingValuePen() const
171 {
172 {
172 return mFloatingValuePen;
173 return mFloatingValuePen;
173 }
174 }
174
175
175 /*!
176 /*!
176 \internal \a pos
177 \internal \a pos
177 */
178 */
178 void QBarSet::barHoverEnterEvent(QPoint pos)
179 void QBarSet::barHoverEnterEvent(QPoint pos)
179 {
180 {
180 emit showToolTip(pos, mName);
181 emit showToolTip(pos, mName);
181 emit hoverEnter(pos);
182 emit hoverEnter(pos);
182 }
183 }
183
184
184 /*!
185 /*!
185 \internal
186 \internal
186 */
187 */
187 void QBarSet::barHoverLeaveEvent()
188 void QBarSet::barHoverLeaveEvent()
188 {
189 {
189 // Emit signal to user of charts
190 // Emit signal to user of charts
190 emit hoverLeave();
191 emit hoverLeave();
191 }
192 }
192
193
193 #include "moc_qbarset.cpp"
194 #include "moc_qbarset.cpp"
194 QTCOMMERCIALCHART_END_NAMESPACE
195 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,71 +1,72
1 #ifndef QBARSET_H
1 #ifndef QBARSET_H
2 #define QBARSET_H
2 #define QBARSET_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <QPen>
5 #include <QPen>
6 #include <QBrush>
6 #include <QBrush>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 QBarSet(QString name, QObject *parent = 0);
14 QBarSet(QString name, QObject *parent = 0);
15
15
16 void setName(QString name);
16 void setName(QString name);
17 QString name();
17 QString name();
18 QBarSet& operator << (const qreal &value); // appends new value to set
18 QBarSet& operator << (const qreal &value); // appends new value to set
19
19
20 // TODO: remove indices eventually. Use as internal?
20 // TODO: remove indices eventually. Use as internal?
21 int count(); // count of values in set
21 int count(); // count of values in set
22 qreal valueAt(int index); // for modifying individual values
22 qreal valueAt(int index); // for modifying individual values
23 void setValue(int index, qreal value); // setter for individual value
23 void setValue(int index, qreal value); // setter for individual value
24 qreal total(); // total values in the set
24 qreal total(); // total values in the set
25
25
26 // TODO:
26 // TODO:
27 //qreal value(QString category);
27 //qreal value(QString category);
28 //void setValue(QString category, qreal value);
28 //void setValue(QString category, qreal value);
29
29
30 void setPen(const QPen pen);
30 void setPen(const QPen pen);
31 QPen pen() const;
31 QPen pen() const;
32
32
33 void setBrush(const QBrush brush);
33 void setBrush(const QBrush brush);
34 QBrush brush() const;
34 QBrush brush() const;
35
35
36 void setFloatingValuePen(const QPen pen);
36 void setFloatingValuePen(const QPen pen);
37 QPen floatingValuePen() const;
37 QPen floatingValuePen() const;
38
38
39 Q_SIGNALS:
39 Q_SIGNALS:
40 void clicked(QString category); // Clicked and hover signals exposed to user
40 void clicked(QString category); // Clicked and hover signals exposed to user
41 void rightClicked(QString category);
41 void rightClicked(QString category);
42 void toggleFloatingValues();
42 void toggleFloatingValues();
43
43
44 // TODO: Expose this to user or not?
44 // TODO: Expose this to user or not?
45 // TODO: TO PIMPL --->
45 // TODO: TO PIMPL --->
46 void changed();
46 void structureChanged();
47 void valueChanged();
47 void hoverEnter(QPoint pos);
48 void hoverEnter(QPoint pos);
48 void hoverLeave();
49 void hoverLeave();
49 void showToolTip(QPoint pos, QString tip); // Private signal
50 void showToolTip(QPoint pos, QString tip); // Private signal
50 // <--- TO PIMPL
51 // <--- TO PIMPL
51
52
52 public Q_SLOTS:
53 public Q_SLOTS:
53 // These are for internal communication
54 // These are for internal communication
54 // TODO: TO PIMPL --->
55 // TODO: TO PIMPL --->
55 void barHoverEnterEvent(QPoint pos);
56 void barHoverEnterEvent(QPoint pos);
56 void barHoverLeaveEvent();
57 void barHoverLeaveEvent();
57 // <--- TO PIMPL
58 // <--- TO PIMPL
58
59
59 private:
60 private:
60
61
61 QString mName;
62 QString mName;
62 QList<qreal> mValues; // TODO: replace with map (category, value)
63 QList<qreal> mValues; // TODO: replace with map (category, value)
63 QMap<QString,qreal> mMappedValues;
64 QMap<QString,qreal> mMappedValues;
64 QPen mPen;
65 QPen mPen;
65 QBrush mBrush;
66 QBrush mBrush;
66 QPen mFloatingValuePen;
67 QPen mFloatingValuePen;
67 };
68 };
68
69
69 QTCOMMERCIALCHART_END_NAMESPACE
70 QTCOMMERCIALCHART_END_NAMESPACE
70
71
71 #endif // QBARSET_H
72 #endif // QBARSET_H
General Comments 0
You need to be logged in to leave comments. Login now