##// END OF EJS Templates
tablemodelchart: added coloring to TableView
Marek Rosa -
r849:52126f8b50e7
parent child
Show More
@@ -21,6 +21,8
21 #include "customtablemodel.h"
21 #include "customtablemodel.h"
22 #include <QVector>
22 #include <QVector>
23 #include <QTime>
23 #include <QTime>
24 #include <QRect>
25 #include <QColor>
24
26
25 CustomTableModel::CustomTableModel(QObject *parent) :
27 CustomTableModel::CustomTableModel(QObject *parent) :
26 QAbstractTableModel(parent)
28 QAbstractTableModel(parent)
@@ -44,18 +46,18 CustomTableModel::CustomTableModel(QObject *parent) :
44 for (int i = 0; i < 6; i++)
46 for (int i = 0; i < 6; i++)
45 {
47 {
46 QVector<qreal>* dataVec = new QVector<qreal>(6);
48 QVector<qreal>* dataVec = new QVector<qreal>(6);
47 QVector<QColor>* colorVec = new QVector<QColor>(6);
49 // QVector<QColor>* colorVec = new QVector<QColor>(6);
48 for (int k = 0; k < dataVec->size(); k++)
50 for (int k = 0; k < dataVec->size(); k++)
49 {
51 {
50 if (k%2 == 0)
52 if (k%2 == 0)
51 dataVec->replace(k, i * 50 + qrand()%20);
53 dataVec->replace(k, i * 50 + qrand()%20);
52 else
54 else
53 dataVec->replace(k, qrand()%100);
55 dataVec->replace(k, qrand()%100);
54 colorVec->replace(k, QColor(Qt::white));
56 // colorVec->replace(k, QColor(Qt::white));
55 }
57 }
56 m_data.append(dataVec);
58 m_data.append(dataVec);
57 m_labels.append(QString("Row: %1").arg((i + 1)));
59 m_labels.append(QString("Row: %1").arg((i + 1)));
58 m_rowsColors.append(colorVec);
60 // m_rowsColors.append(colorVec);
59 }
61 }
60 }
62 }
61
63
@@ -137,7 +139,13 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
137 }
139 }
138 else if (role == Qt::BackgroundRole)
140 else if (role == Qt::BackgroundRole)
139 {
141 {
140 return m_rowsColors[index.row()]->at(index.column());
142 QRect rect;
143 foreach(rect, m_mapping)
144 if(rect.contains(index.column(), index.row()))
145 return QColor(m_mapping.key(rect));
146
147 // cell not mapped return white color
148 return QColor(Qt::white);
141 }
149 }
142 return QVariant();
150 return QVariant();
143 }
151 }
@@ -166,11 +174,11 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & val
166 emit dataChanged(index, index);
174 emit dataChanged(index, index);
167 return true;
175 return true;
168 }
176 }
169 else if (role == Qt::BackgroundRole)
177 // else if (role == Qt::BackgroundRole)
170 {
178 // {
171 m_rowsColors[index.row()]->replace(index.column(), value.value<QColor>());
179 // m_rowsColors[index.row()]->replace(index.column(), value.value<QColor>());
172 return true;
180 // return true;
173 }
181 // }
174 return false;
182 return false;
175 }
183 }
176
184
@@ -230,7 +238,7 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & pare
230 }
238 }
231 m_data.insert(i, dataVec);
239 m_data.insert(i, dataVec);
232 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
240 m_labels.insert(i,(QString("Row: %1").arg(i + 1)));
233 m_rowsColors.insert(i, colorVec);
241 // m_rowsColors.insert(i, colorVec);
234 }
242 }
235 endInsertRows();
243 endInsertRows();
236 return true;
244 return true;
@@ -256,3 +264,13 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & pare
256 endRemoveRows();
264 endRemoveRows();
257 return true;
265 return true;
258 }
266 }
267
268 void CustomTableModel::addMapping(QString color, QRect area)
269 {
270 m_mapping.insert(color, area);
271 }
272
273 void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom)
274 {
275 addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom)));
276 }
@@ -24,7 +24,9
24 #include <QAbstractTableModel>
24 #include <QAbstractTableModel>
25 #include <QPointF>
25 #include <QPointF>
26 #include <QStringList>
26 #include <QStringList>
27 #include <QColor>
27 //#include <QColor>
28 #include <QHash>
29 #include <QRect>
28
30
29 class CustomTableModel : public QAbstractTableModel
31 class CustomTableModel : public QAbstractTableModel
30 {
32 {
@@ -41,12 +43,17 public:
41 bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
43 bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
42 bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
44 bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );
43
45
46 void addMapping(QString color, QRect area);
47 void addMapping(QString color, int left, int top, int right, int bottom);
48 void clearMapping() { m_mapping.clear(); }
49
44 //signals:
50 //signals:
45
51
46 //public slots:
52 //public slots:
47 private:
53 private:
48 QList<QVector<qreal>* > m_data;
54 QList<QVector<qreal> * > m_data;
49 QList<QVector<QColor>* > m_rowsColors;
55 // QList<QVector<QColor> * > m_rowsColors;
56 QHash<QString, QRect> m_mapping;
50 QList<QPointF> m_points;
57 QList<QPointF> m_points;
51 QStringList m_labels;
58 QStringList m_labels;
52
59
@@ -22,13 +22,15
22 #include <QGridLayout>
22 #include <QGridLayout>
23 #include <QTableView>
23 #include <QTableView>
24 #include <QStyledItemDelegate>
24 #include <QStyledItemDelegate>
25 #include "qlineseries.h"
25 #include <QLineSeries>
26 #include "qsplineseries.h"
26 #include <QSplineSeries>
27 #include "qscatterseries.h"
27 #include <QScatterSeries>
28 #include "customtablemodel.h"
28 #include "customtablemodel.h"
29 #include "qpieseries.h"
29 #include <QPieSeries>
30 #include "qareaseries.h"
30 #include <QPieSlice>
31 #include "qbarseries.h"
31 #include <QAreaSeries>
32 #include <QBarSeries>
33 #include <QBarSet>
32 #include <QPushButton>
34 #include <QPushButton>
33 #include <QRadioButton>
35 #include <QRadioButton>
34 #include <QSpinBox>
36 #include <QSpinBox>
@@ -52,19 +54,6 TableWidget::TableWidget(QWidget *parent)
52 m_chartView->setRenderHint(QPainter::Antialiasing);
54 m_chartView->setRenderHint(QPainter::Antialiasing);
53 m_chartView->setMinimumSize(640, 480);
55 m_chartView->setMinimumSize(640, 480);
54
56
55 // create
56 // QLineSeries* series = new QLineSeries;
57 // QSplineSeries* series = new QSplineSeries;
58 // QScatterSeries* series = new QScatterSeries;
59 // series->setModel(m_model);
60 // series->setModelMapping(0,1, Qt::Vertical);
61
62 // QPieSeries* pieSeries = new QPieSeries;
63 // pieSeries->setModel(model);
64 // pieSeries
65
66 // chartView->addSeries(series);
67
68 // add, remove data buttons
57 // add, remove data buttons
69 QPushButton* addRowAboveButton = new QPushButton("Add row above");
58 QPushButton* addRowAboveButton = new QPushButton("Add row above");
70 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
59 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
@@ -125,80 +114,61 TableWidget::TableWidget(QWidget *parent)
125
114
126 void TableWidget::addRowAbove()
115 void TableWidget::addRowAbove()
127 {
116 {
128 // m_model->insertRow(m_model->rowCount());
129 // m_model->insertRow(tableView->currentIndex().row());
130 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
117 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
131
118
132 }
119 }
133
120
134 void TableWidget::addRowBelow()
121 void TableWidget::addRowBelow()
135 {
122 {
136 // m_model->insertRow(m_model->rowCount());
137 // m_model->insertRow(tableView->currentIndex().row() + 1);
138 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
123 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
139
124
140 }
125 }
141
126
142 void TableWidget::removeRow()
127 void TableWidget::removeRow()
143 {
128 {
144 // m_model->removeRow(m_model->rowCount() - 1);
145 // m_model->removeRow(tableView->currentIndex().row());
146 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
129 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
147 }
130 }
148
131
149 void TableWidget::updateChartType()
132 void TableWidget::updateChartType()
150 {
133 {
151 m_chart->removeAllSeries();
134 m_chart->removeAllSeries();
135 m_model->clearMapping();
152
136
153 if (m_lineRadioButton->isChecked())
137 QString seriesColorHex = "#000000";
154 {
155 QPen pen;
138 QPen pen;
156 pen.setWidth(2);
139 pen.setWidth(2);
157
140
158 QColor seriesColor("#8FBC8F");
141 if (m_lineRadioButton->isChecked())
142 {
143
144 m_chart->axisX()->setRange(0, 500);
145 m_chart->axisY()->setRange(0, 120);
159
146
160 // series 1
147 // series 1
161 m_series = new QLineSeries;
148 m_series = new QLineSeries;
162 m_series->setModel(m_model);
149 m_series->setModel(m_model);
163 m_series->setModelMapping(0,1, Qt::Vertical);
150 m_series->setModelMapping(0,1, Qt::Vertical);
164 m_series->setModelMappingShift(3, 3);
151 m_series->setModelMappingShift(1, 4);
165 // series->setModelMapping(0,1, Qt::Horizontal);
166
167 pen.setColor(seriesColor);
168 m_series->setPen(pen);
169 m_chart->addSeries(m_series);
152 m_chart->addSeries(m_series);
170 for (int i = 1; i <=4; i++)
153 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
171 {
154 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
172 m_model->setData(m_model->index(i, 0), seriesColor , Qt::BackgroundRole);
173 m_model->setData(m_model->index(i, 1), seriesColor , Qt::BackgroundRole);
174 }
175 // tableView->setsetStyleSheet("QTableView { border: 2px solid red }");
176
177 seriesColor = QColor("#1E90FF");
178
155
179 // series 2
156 // series 2
180 m_series = new QLineSeries;
157 m_series = new QLineSeries;
181 m_series->setModel(m_model);
158 m_series->setModel(m_model);
182 m_series->setModelMapping(2,3, Qt::Vertical);
159 m_series->setModelMapping(2,3, Qt::Vertical);
183 // series->setModelMapping(2,3, Qt::Horizontal);
160 m_chart->addSeries(m_series);
184 pen.setColor(seriesColor);
161 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
185 m_series->setPen(pen);
162 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
186 // m_chart->addSeries(m_series);
187
188 m_chart->axisX()->setRange(0, 500);
189 m_chart->axisY()->setRange(0, 120);
190
163
191 for (int i = 0; i < m_model->rowCount(); i++)
164 // series 3
192 {
165 m_series = new QLineSeries;
193 m_model->setData(m_model->index(i, 2), seriesColor , Qt::BackgroundRole);
166 m_series->setModel(m_model);
194 m_model->setData(m_model->index(i, 3), seriesColor , Qt::BackgroundRole);
167 m_series->setModelMapping(4,5, Qt::Vertical);
195 }
168 m_series->setModelMappingShift(2, 0);
196 // // series 3
169 m_chart->addSeries(m_series);
197 // series = new QLineSeries;
170 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
198 // series->setModel(m_model);
171 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
199 // series->setModelMapping(4,5, Qt::Vertical);
200 //// series->setModelMapping(4,5, Qt::Horizontal);
201 // chartView->addSeries(series);
202 }
172 }
203 else if (m_splineRadioButton->isChecked())
173 else if (m_splineRadioButton->isChecked())
204 {
174 {
@@ -209,6 +179,8 void TableWidget::updateChartType()
209 m_series->setModelMappingShift(1, 4);
179 m_series->setModelMappingShift(1, 4);
210 // series->setModelMapping(0,1, Qt::Horizontal);
180 // series->setModelMapping(0,1, Qt::Horizontal);
211 m_chart->addSeries(m_series);
181 m_chart->addSeries(m_series);
182 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
183 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
212
184
213 // series 2
185 // series 2
214 m_series = new QSplineSeries;
186 m_series = new QSplineSeries;
@@ -217,14 +189,18 void TableWidget::updateChartType()
217 m_series->setModelMappingShift(0, 0);
189 m_series->setModelMappingShift(0, 0);
218 // series->setModelMapping(2,3, Qt::Horizontal);
190 // series->setModelMapping(2,3, Qt::Horizontal);
219 m_chart->addSeries(m_series);
191 m_chart->addSeries(m_series);
192 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
193 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
220
194
221 // series 3
195 // series 3
222 m_series = new QSplineSeries;
196 m_series = new QSplineSeries;
223 m_series->setModel(m_model);
197 m_series->setModel(m_model);
224 m_series->setModelMapping(4,5, Qt::Vertical);
198 m_series->setModelMapping(4,5, Qt::Vertical);
225 m_series->setModelMappingShift(0, 0);
199 m_series->setModelMappingShift(2, 0);
226 // series->setModelMapping(4,5, Qt::Horizontal);
200 // series->setModelMapping(4,5, Qt::Horizontal);
227 m_chart->addSeries(m_series);
201 m_chart->addSeries(m_series);
202 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
203 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
228 }
204 }
229 else if (m_scatterRadioButton->isChecked())
205 else if (m_scatterRadioButton->isChecked())
230 {
206 {
@@ -232,22 +208,32 void TableWidget::updateChartType()
232 m_series = new QScatterSeries;
208 m_series = new QScatterSeries;
233 m_series->setModel(m_model);
209 m_series->setModel(m_model);
234 m_series->setModelMapping(0,1, Qt::Vertical);
210 m_series->setModelMapping(0,1, Qt::Vertical);
211 m_series->setModelMappingShift(2, 0);
235 // series->setModelMapping(0,1, Qt::Horizontal);
212 // series->setModelMapping(0,1, Qt::Horizontal);
236 m_chart->addSeries(m_series);
213 m_chart->addSeries(m_series);
237
214
215 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
216 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
217
238 // series 2
218 // series 2
239 m_series = new QScatterSeries;
219 m_series = new QScatterSeries;
240 m_series->setModel(m_model);
220 m_series->setModel(m_model);
241 m_series->setModelMapping(2,3, Qt::Vertical);
221 m_series->setModelMapping(2,3, Qt::Vertical);
222 m_series->setModelMappingShift(1, 6);
242 // series->setModelMapping(2,3, Qt::Horizontal);
223 // series->setModelMapping(2,3, Qt::Horizontal);
243 m_chart->addSeries(m_series);
224 m_chart->addSeries(m_series);
244
225
226 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
227 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
228
245 // series 3
229 // series 3
246 m_series = new QScatterSeries;
230 m_series = new QScatterSeries;
247 m_series->setModel(m_model);
231 m_series->setModel(m_model);
248 m_series->setModelMapping(4,5, Qt::Vertical);
232 m_series->setModelMapping(4,5, Qt::Vertical);
249 // series->setModelMapping(4,5, Qt::Horizontal);
233 // series->setModelMapping(4,5, Qt::Horizontal);
250 m_chart->addSeries(m_series);
234 m_chart->addSeries(m_series);
235 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
236 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
251 }
237 }
252 else if (m_pieRadioButton->isChecked())
238 else if (m_pieRadioButton->isChecked())
253 {
239 {
@@ -258,7 +244,10 void TableWidget::updateChartType()
258 pieSeries->setLabelsVisible(true);
244 pieSeries->setLabelsVisible(true);
259 pieSeries->setPieSize(0.4);
245 pieSeries->setPieSize(0.4);
260 pieSeries->setPiePosition(0.2, 0.35);
246 pieSeries->setPiePosition(0.2, 0.35);
247
261 m_chart->addSeries(pieSeries);
248 m_chart->addSeries(pieSeries);
249 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
250 m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000));
262
251
263 // pie 2
252 // pie 2
264 pieSeries = new QPieSeries;
253 pieSeries = new QPieSeries;
@@ -268,6 +257,8 void TableWidget::updateChartType()
268 pieSeries->setPieSize(0.4);
257 pieSeries->setPieSize(0.4);
269 pieSeries->setPiePosition(0.8, 0.35);
258 pieSeries->setPiePosition(0.8, 0.35);
270 m_chart->addSeries(pieSeries);
259 m_chart->addSeries(pieSeries);
260 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
261 m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000));
271
262
272 // pie 3
263 // pie 3
273 pieSeries = new QPieSeries;
264 pieSeries = new QPieSeries;
@@ -277,6 +268,8 void TableWidget::updateChartType()
277 pieSeries->setPieSize(0.4);
268 pieSeries->setPieSize(0.4);
278 pieSeries->setPiePosition(0.5, 0.65);
269 pieSeries->setPiePosition(0.5, 0.65);
279 m_chart->addSeries(pieSeries);
270 m_chart->addSeries(pieSeries);
271 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
272 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
280 }
273 }
281 else if (m_areaRadioButton->isChecked())
274 else if (m_areaRadioButton->isChecked())
282 {
275 {
@@ -296,11 +289,15 void TableWidget::updateChartType()
296 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
289 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
297 barSeries->setToolTipEnabled(true);
290 barSeries->setToolTipEnabled(true);
298 m_chart->addSeries(barSeries);
291 m_chart->addSeries(barSeries);
292 for (int i = 0; i < barSeries->barsetCount(); i++) {
293 seriesColorHex = "#" + QString::number(barSeries->barsetAt(i)->brush().color().rgb(), 16).right(6).toUpper();
294 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
295 }
299 }
296 }
300
297
301 // series->setModel(m_model);
298 // repaint table view colors
302 // series->setModelMapping(0,1, Qt::Vertical);
299 m_tableView->repaint();
303 // chartView->addSeries(series);
300 m_tableView->setFocus();
304 }
301 }
305
302
306 TableWidget::~TableWidget()
303 TableWidget::~TableWidget()
General Comments 0
You need to be logged in to leave comments. Login now