@@ -21,6 +21,8 | |||
|
21 | 21 | #include "customtablemodel.h" |
|
22 | 22 | #include <QVector> |
|
23 | 23 | #include <QTime> |
|
24 | #include <QRect> | |
|
25 | #include <QColor> | |
|
24 | 26 | |
|
25 | 27 | CustomTableModel::CustomTableModel(QObject *parent) : |
|
26 | 28 | QAbstractTableModel(parent) |
@@ -44,18 +46,18 CustomTableModel::CustomTableModel(QObject *parent) : | |||
|
44 | 46 | for (int i = 0; i < 6; i++) |
|
45 | 47 | { |
|
46 | 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 | 50 | for (int k = 0; k < dataVec->size(); k++) |
|
49 | 51 | { |
|
50 | 52 | if (k%2 == 0) |
|
51 | 53 | dataVec->replace(k, i * 50 + qrand()%20); |
|
52 | 54 | else |
|
53 | 55 | dataVec->replace(k, qrand()%100); |
|
54 | colorVec->replace(k, QColor(Qt::white)); | |
|
56 | // colorVec->replace(k, QColor(Qt::white)); | |
|
55 | 57 | } |
|
56 | 58 | m_data.append(dataVec); |
|
57 | 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 | 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 | 150 | return QVariant(); |
|
143 | 151 | } |
@@ -166,11 +174,11 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & val | |||
|
166 | 174 | emit dataChanged(index, index); |
|
167 | 175 | return true; |
|
168 | 176 | } |
|
169 | else if (role == Qt::BackgroundRole) | |
|
170 | { | |
|
171 | m_rowsColors[index.row()]->replace(index.column(), value.value<QColor>()); | |
|
172 | return true; | |
|
173 | } | |
|
177 | // else if (role == Qt::BackgroundRole) | |
|
178 | // { | |
|
179 | // m_rowsColors[index.row()]->replace(index.column(), value.value<QColor>()); | |
|
180 | // return true; | |
|
181 | // } | |
|
174 | 182 | return false; |
|
175 | 183 | } |
|
176 | 184 | |
@@ -230,7 +238,7 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & pare | |||
|
230 | 238 | } |
|
231 | 239 | m_data.insert(i, dataVec); |
|
232 | 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 | 243 | endInsertRows(); |
|
236 | 244 | return true; |
@@ -256,3 +264,13 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & pare | |||
|
256 | 264 | endRemoveRows(); |
|
257 | 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 | 24 | #include <QAbstractTableModel> |
|
25 | 25 | #include <QPointF> |
|
26 | 26 | #include <QStringList> |
|
27 | #include <QColor> | |
|
27 | //#include <QColor> | |
|
28 | #include <QHash> | |
|
29 | #include <QRect> | |
|
28 | 30 | |
|
29 | 31 | class CustomTableModel : public QAbstractTableModel |
|
30 | 32 | { |
@@ -40,13 +42,18 public: | |||
|
40 | 42 | Qt::ItemFlags flags ( const QModelIndex & index ) const; |
|
41 | 43 | bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); |
|
42 | 44 | bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); |
|
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(); } | |
|
43 | 49 | |
|
44 | 50 | //signals: |
|
45 | 51 | |
|
46 | 52 | //public slots: |
|
47 | 53 | private: |
|
48 | QList<QVector<qreal>* > m_data; | |
|
49 | QList<QVector<QColor>* > m_rowsColors; | |
|
54 | QList<QVector<qreal> * > m_data; | |
|
55 | // QList<QVector<QColor> * > m_rowsColors; | |
|
56 | QHash<QString, QRect> m_mapping; | |
|
50 | 57 | QList<QPointF> m_points; |
|
51 | 58 | QStringList m_labels; |
|
52 | 59 |
@@ -22,13 +22,15 | |||
|
22 | 22 | #include <QGridLayout> |
|
23 | 23 | #include <QTableView> |
|
24 | 24 | #include <QStyledItemDelegate> |
|
25 |
#include |
|
|
26 |
#include |
|
|
27 |
#include |
|
|
25 | #include <QLineSeries> | |
|
26 | #include <QSplineSeries> | |
|
27 | #include <QScatterSeries> | |
|
28 | 28 | #include "customtablemodel.h" |
|
29 |
#include |
|
|
30 | #include "qareaseries.h" | |
|
31 |
#include |
|
|
29 | #include <QPieSeries> | |
|
30 | #include <QPieSlice> | |
|
31 | #include <QAreaSeries> | |
|
32 | #include <QBarSeries> | |
|
33 | #include <QBarSet> | |
|
32 | 34 | #include <QPushButton> |
|
33 | 35 | #include <QRadioButton> |
|
34 | 36 | #include <QSpinBox> |
@@ -45,26 +47,13 TableWidget::TableWidget(QWidget *parent) | |||
|
45 | 47 | m_tableView = new QTableView; |
|
46 | 48 | m_tableView->setModel(m_model); |
|
47 | 49 | m_tableView->setMinimumHeight(240); |
|
48 | // tableView->setMinimumSize(340, 480); | |
|
49 | // tableView->setItemDelegate(new QStyledItemDelegate); | |
|
50 | // tableView->setMinimumSize(340, 480); | |
|
51 | // tableView->setItemDelegate(new QStyledItemDelegate); | |
|
50 | 52 | m_chart = new QChart; |
|
51 | 53 | m_chartView = new QChartView(m_chart); |
|
52 | 54 | m_chartView->setRenderHint(QPainter::Antialiasing); |
|
53 | 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 | 57 | // add, remove data buttons |
|
69 | 58 | QPushButton* addRowAboveButton = new QPushButton("Add row above"); |
|
70 | 59 | connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove())); |
@@ -119,86 +108,67 TableWidget::TableWidget(QWidget *parent) | |||
|
119 | 108 | mainLayout->addLayout(radioLayout, 2, 1); |
|
120 | 109 | mainLayout->addWidget(m_tableView, 1, 0); |
|
121 | 110 | mainLayout->addWidget(m_chartView, 2, 0); |
|
122 |
setLayout(mainLayout); |
|
|
111 | setLayout(mainLayout); | |
|
123 | 112 | m_lineRadioButton->setFocus(); |
|
124 | 113 | } |
|
125 | 114 | |
|
126 | 115 | void TableWidget::addRowAbove() |
|
127 | 116 | { |
|
128 | // m_model->insertRow(m_model->rowCount()); | |
|
129 | // m_model->insertRow(tableView->currentIndex().row()); | |
|
130 | 117 | m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value()); |
|
131 | 118 | |
|
132 | 119 | } |
|
133 | 120 | |
|
134 | 121 | void TableWidget::addRowBelow() |
|
135 | 122 | { |
|
136 | // m_model->insertRow(m_model->rowCount()); | |
|
137 | // m_model->insertRow(tableView->currentIndex().row() + 1); | |
|
138 | 123 | m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value()); |
|
139 | 124 | |
|
140 | 125 | } |
|
141 | 126 | |
|
142 | 127 | void TableWidget::removeRow() |
|
143 | 128 | { |
|
144 | // m_model->removeRow(m_model->rowCount() - 1); | |
|
145 | // m_model->removeRow(tableView->currentIndex().row()); | |
|
146 | 129 | m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value())); |
|
147 | 130 | } |
|
148 | 131 | |
|
149 | 132 | void TableWidget::updateChartType() |
|
150 | 133 | { |
|
151 | 134 | m_chart->removeAllSeries(); |
|
135 | m_model->clearMapping(); | |
|
136 | ||
|
137 | QString seriesColorHex = "#000000"; | |
|
138 | QPen pen; | |
|
139 | pen.setWidth(2); | |
|
152 | 140 | |
|
153 | 141 | if (m_lineRadioButton->isChecked()) |
|
154 | 142 | { |
|
155 | QPen pen; | |
|
156 | pen.setWidth(2); | |
|
157 | 143 | |
|
158 | QColor seriesColor("#8FBC8F"); | |
|
144 | m_chart->axisX()->setRange(0, 500); | |
|
145 | m_chart->axisY()->setRange(0, 120); | |
|
159 | 146 | |
|
160 | 147 | // series 1 |
|
161 | 148 | m_series = new QLineSeries; |
|
162 | 149 | m_series->setModel(m_model); |
|
163 | 150 | m_series->setModelMapping(0,1, Qt::Vertical); |
|
164 |
m_series->setModelMappingShift( |
|
|
165 | // series->setModelMapping(0,1, Qt::Horizontal); | |
|
166 | ||
|
167 | pen.setColor(seriesColor); | |
|
168 | m_series->setPen(pen); | |
|
151 | m_series->setModelMappingShift(1, 4); | |
|
169 | 152 | m_chart->addSeries(m_series); |
|
170 | for (int i = 1; i <=4; i++) | |
|
171 | { | |
|
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"); | |
|
153 | seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); | |
|
154 | m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4)); | |
|
178 | 155 | |
|
179 | 156 | // series 2 |
|
180 | 157 | m_series = new QLineSeries; |
|
181 | 158 | m_series->setModel(m_model); |
|
182 | 159 | m_series->setModelMapping(2,3, Qt::Vertical); |
|
183 | // series->setModelMapping(2,3, Qt::Horizontal); | |
|
184 | pen.setColor(seriesColor); | |
|
185 | m_series->setPen(pen); | |
|
186 | // m_chart->addSeries(m_series); | |
|
187 | ||
|
188 | m_chart->axisX()->setRange(0, 500); | |
|
189 | m_chart->axisY()->setRange(0, 120); | |
|
160 | m_chart->addSeries(m_series); | |
|
161 | seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); | |
|
162 | m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000)); | |
|
190 | 163 | |
|
191 | for (int i = 0; i < m_model->rowCount(); i++) | |
|
192 | { | |
|
193 | m_model->setData(m_model->index(i, 2), seriesColor , Qt::BackgroundRole); | |
|
194 | m_model->setData(m_model->index(i, 3), seriesColor , Qt::BackgroundRole); | |
|
195 | } | |
|
196 | // // series 3 | |
|
197 | // series = new QLineSeries; | |
|
198 | // series->setModel(m_model); | |
|
199 | // series->setModelMapping(4,5, Qt::Vertical); | |
|
200 | //// series->setModelMapping(4,5, Qt::Horizontal); | |
|
201 | // chartView->addSeries(series); | |
|
164 | // series 3 | |
|
165 | m_series = new QLineSeries; | |
|
166 | m_series->setModel(m_model); | |
|
167 | m_series->setModelMapping(4,5, Qt::Vertical); | |
|
168 | m_series->setModelMappingShift(2, 0); | |
|
169 | m_chart->addSeries(m_series); | |
|
170 | seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); | |
|
171 | m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000)); | |
|
202 | 172 | } |
|
203 | 173 | else if (m_splineRadioButton->isChecked()) |
|
204 | 174 | { |
@@ -207,24 +177,30 void TableWidget::updateChartType() | |||
|
207 | 177 | m_series->setModel(m_model); |
|
208 | 178 | m_series->setModelMapping(0,1, Qt::Vertical); |
|
209 | 179 | m_series->setModelMappingShift(1, 4); |
|
210 | // series->setModelMapping(0,1, Qt::Horizontal); | |
|
180 | // series->setModelMapping(0,1, Qt::Horizontal); | |
|
211 | 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 | 185 | // series 2 |
|
214 | 186 | m_series = new QSplineSeries; |
|
215 | 187 | m_series->setModel(m_model); |
|
216 | 188 | m_series->setModelMapping(2,3, Qt::Vertical); |
|
217 | 189 | m_series->setModelMappingShift(0, 0); |
|
218 | // series->setModelMapping(2,3, Qt::Horizontal); | |
|
190 | // series->setModelMapping(2,3, Qt::Horizontal); | |
|
219 | 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 | 195 | // series 3 |
|
222 | 196 | m_series = new QSplineSeries; |
|
223 | 197 | m_series->setModel(m_model); |
|
224 | 198 | m_series->setModelMapping(4,5, Qt::Vertical); |
|
225 |
m_series->setModelMappingShift( |
|
|
226 | // series->setModelMapping(4,5, Qt::Horizontal); | |
|
199 | m_series->setModelMappingShift(2, 0); | |
|
200 | // series->setModelMapping(4,5, Qt::Horizontal); | |
|
227 | 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 | 205 | else if (m_scatterRadioButton->isChecked()) |
|
230 | 206 | { |
@@ -232,22 +208,32 void TableWidget::updateChartType() | |||
|
232 | 208 | m_series = new QScatterSeries; |
|
233 | 209 | m_series->setModel(m_model); |
|
234 | 210 | m_series->setModelMapping(0,1, Qt::Vertical); |
|
235 |
|
|
|
211 | m_series->setModelMappingShift(2, 0); | |
|
212 | // series->setModelMapping(0,1, Qt::Horizontal); | |
|
236 | 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 | 218 | // series 2 |
|
239 | 219 | m_series = new QScatterSeries; |
|
240 | 220 | m_series->setModel(m_model); |
|
241 | 221 | m_series->setModelMapping(2,3, Qt::Vertical); |
|
242 |
|
|
|
222 | m_series->setModelMappingShift(1, 6); | |
|
223 | // series->setModelMapping(2,3, Qt::Horizontal); | |
|
243 | 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 | 229 | // series 3 |
|
246 | 230 | m_series = new QScatterSeries; |
|
247 | 231 | m_series->setModel(m_model); |
|
248 | 232 | m_series->setModelMapping(4,5, Qt::Vertical); |
|
249 | // series->setModelMapping(4,5, Qt::Horizontal); | |
|
233 | // series->setModelMapping(4,5, Qt::Horizontal); | |
|
250 | 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 | 238 | else if (m_pieRadioButton->isChecked()) |
|
253 | 239 | { |
@@ -258,7 +244,10 void TableWidget::updateChartType() | |||
|
258 | 244 | pieSeries->setLabelsVisible(true); |
|
259 | 245 | pieSeries->setPieSize(0.4); |
|
260 | 246 | pieSeries->setPiePosition(0.2, 0.35); |
|
247 | ||
|
261 | 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 | 252 | // pie 2 |
|
264 | 253 | pieSeries = new QPieSeries; |
@@ -268,6 +257,8 void TableWidget::updateChartType() | |||
|
268 | 257 | pieSeries->setPieSize(0.4); |
|
269 | 258 | pieSeries->setPiePosition(0.8, 0.35); |
|
270 | 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 | 263 | // pie 3 |
|
273 | 264 | pieSeries = new QPieSeries; |
@@ -277,6 +268,8 void TableWidget::updateChartType() | |||
|
277 | 268 | pieSeries->setPieSize(0.4); |
|
278 | 269 | pieSeries->setPiePosition(0.5, 0.65); |
|
279 | 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 | 274 | else if (m_areaRadioButton->isChecked()) |
|
282 | 275 | { |
@@ -296,11 +289,15 void TableWidget::updateChartType() | |||
|
296 | 289 | barSeries->setModelMapping(5, 2, 4, Qt::Vertical); |
|
297 | 290 | barSeries->setToolTipEnabled(true); |
|
298 | 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); | |
|
302 | // series->setModelMapping(0,1, Qt::Vertical); | |
|
303 | // chartView->addSeries(series); | |
|
298 | // repaint table view colors | |
|
299 | m_tableView->repaint(); | |
|
300 | m_tableView->setFocus(); | |
|
304 | 301 | } |
|
305 | 302 | |
|
306 | 303 | TableWidget::~TableWidget() |
General Comments 0
You need to be logged in to leave comments.
Login now