diff --git a/examples/tablemodelchart/customtablemodel.cpp b/examples/tablemodelchart/customtablemodel.cpp index f4516ff..91548f1 100644 --- a/examples/tablemodelchart/customtablemodel.cpp +++ b/examples/tablemodelchart/customtablemodel.cpp @@ -21,6 +21,8 @@ #include "customtablemodel.h" #include #include +#include +#include CustomTableModel::CustomTableModel(QObject *parent) : QAbstractTableModel(parent) @@ -44,18 +46,18 @@ CustomTableModel::CustomTableModel(QObject *parent) : for (int i = 0; i < 6; i++) { QVector* dataVec = new QVector(6); - QVector* colorVec = new QVector(6); +// QVector* colorVec = new QVector(6); for (int k = 0; k < dataVec->size(); k++) { if (k%2 == 0) dataVec->replace(k, i * 50 + qrand()%20); else dataVec->replace(k, qrand()%100); - colorVec->replace(k, QColor(Qt::white)); +// colorVec->replace(k, QColor(Qt::white)); } m_data.append(dataVec); m_labels.append(QString("Row: %1").arg((i + 1))); - m_rowsColors.append(colorVec); +// m_rowsColors.append(colorVec); } } @@ -137,7 +139,13 @@ QVariant CustomTableModel::data(const QModelIndex & index, int role) const } else if (role == Qt::BackgroundRole) { - return m_rowsColors[index.row()]->at(index.column()); + QRect rect; + foreach(rect, m_mapping) + if(rect.contains(index.column(), index.row())) + return QColor(m_mapping.key(rect)); + + // cell not mapped return white color + return QColor(Qt::white); } return QVariant(); } @@ -166,11 +174,11 @@ bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & val emit dataChanged(index, index); return true; } - else if (role == Qt::BackgroundRole) - { - m_rowsColors[index.row()]->replace(index.column(), value.value()); - return true; - } +// else if (role == Qt::BackgroundRole) +// { +// m_rowsColors[index.row()]->replace(index.column(), value.value()); +// return true; +// } return false; } @@ -230,7 +238,7 @@ bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & pare } m_data.insert(i, dataVec); m_labels.insert(i,(QString("Row: %1").arg(i + 1))); - m_rowsColors.insert(i, colorVec); +// m_rowsColors.insert(i, colorVec); } endInsertRows(); return true; @@ -256,3 +264,13 @@ bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & pare endRemoveRows(); return true; } + +void CustomTableModel::addMapping(QString color, QRect area) +{ + m_mapping.insert(color, area); +} + +void CustomTableModel::addMapping(QString color, int left, int top, int right, int bottom) +{ + addMapping(color, QRect(QPoint(left, top), QPoint(right, bottom))); +} diff --git a/examples/tablemodelchart/customtablemodel.h b/examples/tablemodelchart/customtablemodel.h index 1cf484f..4d5e80b 100644 --- a/examples/tablemodelchart/customtablemodel.h +++ b/examples/tablemodelchart/customtablemodel.h @@ -24,7 +24,9 @@ #include #include #include -#include +//#include +#include +#include class CustomTableModel : public QAbstractTableModel { @@ -40,13 +42,18 @@ public: Qt::ItemFlags flags ( const QModelIndex & index ) const; bool insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() ); + + void addMapping(QString color, QRect area); + void addMapping(QString color, int left, int top, int right, int bottom); + void clearMapping() { m_mapping.clear(); } //signals: //public slots: private: - QList* > m_data; - QList* > m_rowsColors; + QList * > m_data; +// QList * > m_rowsColors; + QHash m_mapping; QList m_points; QStringList m_labels; diff --git a/examples/tablemodelchart/tablewidget.cpp b/examples/tablemodelchart/tablewidget.cpp index 374e8d8..13cee84 100644 --- a/examples/tablemodelchart/tablewidget.cpp +++ b/examples/tablemodelchart/tablewidget.cpp @@ -22,13 +22,15 @@ #include #include #include -#include "qlineseries.h" -#include "qsplineseries.h" -#include "qscatterseries.h" +#include +#include +#include #include "customtablemodel.h" -#include "qpieseries.h" -#include "qareaseries.h" -#include "qbarseries.h" +#include +#include +#include +#include +#include #include #include #include @@ -45,26 +47,13 @@ TableWidget::TableWidget(QWidget *parent) m_tableView = new QTableView; m_tableView->setModel(m_model); m_tableView->setMinimumHeight(240); -// tableView->setMinimumSize(340, 480); -// tableView->setItemDelegate(new QStyledItemDelegate); + // tableView->setMinimumSize(340, 480); + // tableView->setItemDelegate(new QStyledItemDelegate); m_chart = new QChart; m_chartView = new QChartView(m_chart); m_chartView->setRenderHint(QPainter::Antialiasing); m_chartView->setMinimumSize(640, 480); - // create -// QLineSeries* series = new QLineSeries; -// QSplineSeries* series = new QSplineSeries; -// QScatterSeries* series = new QScatterSeries; -// series->setModel(m_model); -// series->setModelMapping(0,1, Qt::Vertical); - -// QPieSeries* pieSeries = new QPieSeries; -// pieSeries->setModel(model); -// pieSeries - -// chartView->addSeries(series); - // add, remove data buttons QPushButton* addRowAboveButton = new QPushButton("Add row above"); connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove())); @@ -119,86 +108,67 @@ TableWidget::TableWidget(QWidget *parent) mainLayout->addLayout(radioLayout, 2, 1); mainLayout->addWidget(m_tableView, 1, 0); mainLayout->addWidget(m_chartView, 2, 0); - setLayout(mainLayout); + setLayout(mainLayout); m_lineRadioButton->setFocus(); } void TableWidget::addRowAbove() { -// m_model->insertRow(m_model->rowCount()); -// m_model->insertRow(tableView->currentIndex().row()); m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value()); } void TableWidget::addRowBelow() { -// m_model->insertRow(m_model->rowCount()); -// m_model->insertRow(tableView->currentIndex().row() + 1); m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value()); } void TableWidget::removeRow() { -// m_model->removeRow(m_model->rowCount() - 1); -// m_model->removeRow(tableView->currentIndex().row()); m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value())); } void TableWidget::updateChartType() { m_chart->removeAllSeries(); + m_model->clearMapping(); + + QString seriesColorHex = "#000000"; + QPen pen; + pen.setWidth(2); if (m_lineRadioButton->isChecked()) { - QPen pen; - pen.setWidth(2); - QColor seriesColor("#8FBC8F"); + m_chart->axisX()->setRange(0, 500); + m_chart->axisY()->setRange(0, 120); // series 1 m_series = new QLineSeries; m_series->setModel(m_model); m_series->setModelMapping(0,1, Qt::Vertical); - m_series->setModelMappingShift(3, 3); -// series->setModelMapping(0,1, Qt::Horizontal); - - pen.setColor(seriesColor); - m_series->setPen(pen); + m_series->setModelMappingShift(1, 4); m_chart->addSeries(m_series); - for (int i = 1; i <=4; i++) - { - m_model->setData(m_model->index(i, 0), seriesColor , Qt::BackgroundRole); - m_model->setData(m_model->index(i, 1), seriesColor , Qt::BackgroundRole); - } -// tableView->setsetStyleSheet("QTableView { border: 2px solid red }"); - - seriesColor = QColor("#1E90FF"); + seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4)); // series 2 m_series = new QLineSeries; m_series->setModel(m_model); m_series->setModelMapping(2,3, Qt::Vertical); -// series->setModelMapping(2,3, Qt::Horizontal); - pen.setColor(seriesColor); - m_series->setPen(pen); -// m_chart->addSeries(m_series); - - m_chart->axisX()->setRange(0, 500); - m_chart->axisY()->setRange(0, 120); + m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000)); - for (int i = 0; i < m_model->rowCount(); i++) - { - m_model->setData(m_model->index(i, 2), seriesColor , Qt::BackgroundRole); - m_model->setData(m_model->index(i, 3), seriesColor , Qt::BackgroundRole); - } -// // series 3 -// series = new QLineSeries; -// series->setModel(m_model); -// series->setModelMapping(4,5, Qt::Vertical); -//// series->setModelMapping(4,5, Qt::Horizontal); -// chartView->addSeries(series); + // series 3 + m_series = new QLineSeries; + m_series->setModel(m_model); + m_series->setModelMapping(4,5, Qt::Vertical); + m_series->setModelMappingShift(2, 0); + m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000)); } else if (m_splineRadioButton->isChecked()) { @@ -207,24 +177,30 @@ void TableWidget::updateChartType() m_series->setModel(m_model); m_series->setModelMapping(0,1, Qt::Vertical); m_series->setModelMappingShift(1, 4); -// series->setModelMapping(0,1, Qt::Horizontal); + // series->setModelMapping(0,1, Qt::Horizontal); m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4)); // series 2 m_series = new QSplineSeries; m_series->setModel(m_model); m_series->setModelMapping(2,3, Qt::Vertical); m_series->setModelMappingShift(0, 0); -// series->setModelMapping(2,3, Qt::Horizontal); + // series->setModelMapping(2,3, Qt::Horizontal); m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000)); // series 3 m_series = new QSplineSeries; m_series->setModel(m_model); m_series->setModelMapping(4,5, Qt::Vertical); - m_series->setModelMappingShift(0, 0); -// series->setModelMapping(4,5, Qt::Horizontal); + m_series->setModelMappingShift(2, 0); + // series->setModelMapping(4,5, Qt::Horizontal); m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000)); } else if (m_scatterRadioButton->isChecked()) { @@ -232,22 +208,32 @@ void TableWidget::updateChartType() m_series = new QScatterSeries; m_series->setModel(m_model); m_series->setModelMapping(0,1, Qt::Vertical); -// series->setModelMapping(0,1, Qt::Horizontal); + m_series->setModelMappingShift(2, 0); + // series->setModelMapping(0,1, Qt::Horizontal); m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000)); + // series 2 m_series = new QScatterSeries; m_series->setModel(m_model); m_series->setModelMapping(2,3, Qt::Vertical); -// series->setModelMapping(2,3, Qt::Horizontal); + m_series->setModelMappingShift(1, 6); + // series->setModelMapping(2,3, Qt::Horizontal); m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6)); + // series 3 m_series = new QScatterSeries; m_series->setModel(m_model); m_series->setModelMapping(4,5, Qt::Vertical); -// series->setModelMapping(4,5, Qt::Horizontal); + // series->setModelMapping(4,5, Qt::Horizontal); m_chart->addSeries(m_series); + seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000)); } else if (m_pieRadioButton->isChecked()) { @@ -258,7 +244,10 @@ void TableWidget::updateChartType() pieSeries->setLabelsVisible(true); pieSeries->setPieSize(0.4); pieSeries->setPiePosition(0.2, 0.35); + m_chart->addSeries(pieSeries); + seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000)); // pie 2 pieSeries = new QPieSeries; @@ -268,6 +257,8 @@ void TableWidget::updateChartType() pieSeries->setPieSize(0.4); pieSeries->setPiePosition(0.8, 0.35); m_chart->addSeries(pieSeries); + seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000)); // pie 3 pieSeries = new QPieSeries; @@ -277,6 +268,8 @@ void TableWidget::updateChartType() pieSeries->setPieSize(0.4); pieSeries->setPiePosition(0.5, 0.65); m_chart->addSeries(pieSeries); + seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000)); } else if (m_areaRadioButton->isChecked()) { @@ -296,11 +289,15 @@ void TableWidget::updateChartType() barSeries->setModelMapping(5, 2, 4, Qt::Vertical); barSeries->setToolTipEnabled(true); m_chart->addSeries(barSeries); + for (int i = 0; i < barSeries->barsetCount(); i++) { + seriesColorHex = "#" + QString::number(barSeries->barsetAt(i)->brush().color().rgb(), 16).right(6).toUpper(); + m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000)); + } } -// series->setModel(m_model); -// series->setModelMapping(0,1, Qt::Vertical); -// chartView->addSeries(series); + // repaint table view colors + m_tableView->repaint(); + m_tableView->setFocus(); } TableWidget::~TableWidget()