diff --git a/examples/tablemodelchart/tablewidget.cpp b/examples/tablemodelchart/tablewidget.cpp index 44ec38a..6b7ace5 100644 --- a/examples/tablemodelchart/tablewidget.cpp +++ b/examples/tablemodelchart/tablewidget.cpp @@ -58,10 +58,12 @@ TableWidget::TableWidget(QWidget *parent) lineRadioButton = new QRadioButton("Line"); splineRadioButton = new QRadioButton("Spline"); scatterRadioButton = new QRadioButton("Scatter"); + pieRadioButton = new QRadioButton("Pie"); connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); + connect(pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); lineRadioButton->setChecked(true); // radio buttons layout @@ -69,6 +71,7 @@ TableWidget::TableWidget(QWidget *parent) radioLayout->addWidget(lineRadioButton); radioLayout->addWidget(splineRadioButton); radioLayout->addWidget(scatterRadioButton); + radioLayout->addWidget(pieRadioButton); // create main layout QGridLayout* mainLayout = new QGridLayout; @@ -107,8 +110,17 @@ void TableWidget::updateChartType() series = new QLineSeries; else if (splineRadioButton->isChecked()) series = new QSplineSeries; - else + else if (scatterRadioButton->isChecked()) series = new QScatterSeries; + else if (pieRadioButton->isChecked()) + { + QPieSeries* pieSeries = new QPieSeries; + pieSeries->setModel(m_model); + pieSeries->setModelMapping(0,2, Qt::Vertical); + pieSeries->setLabelsVisible(true); + chartView->addSeries(pieSeries); + return; + } series->setModel(m_model); series->setModelMapping(0,1, Qt::Vertical); diff --git a/examples/tablemodelchart/tablewidget.h b/examples/tablemodelchart/tablewidget.h index 48ce2c9..5421e0b 100644 --- a/examples/tablemodelchart/tablewidget.h +++ b/examples/tablemodelchart/tablewidget.h @@ -35,6 +35,7 @@ public: QRadioButton* lineRadioButton; QRadioButton* splineRadioButton; QRadioButton* scatterRadioButton; + QRadioButton* pieRadioButton; }; #endif // TABLEWIDGET_H diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index c0f311a..2611568 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -265,7 +265,7 @@ QList QPieSeries::slices() const void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition) { if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 || - relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0) + relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0) return; if (m_pieRelativeHorPos != relativeHorizontalPosition || m_pieRelativeVerPos != relativeVerticalPosition) { @@ -531,6 +531,71 @@ void QPieSeries::updateDerivativeData() } } +bool QPieSeries::setModel(QAbstractItemModel* model) +{ + // disconnect signals from old model + if(m_model) + { + disconnect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), 0, 0); + disconnect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), 0, 0); + disconnect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), 0, 0); + } + + // set new model if not NULL and connect necessary signals from it + if(model) + { + m_model = model; + connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); + connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); + connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); + } +} + +void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation) +{ + m_mapValues = modelValuesLine; + m_mapLabels = modelLabelsLine; + m_mapOrientation = orientation; + + if (m_model == NULL) + return; + + if (m_mapOrientation == Qt::Vertical) + for (int i = 0; i < m_model->rowCount(); i++) + add(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString()); + else + for (int i = 0; i < m_model->columnCount(); i++) + add(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString()); + + +} + +void QPieSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) +{ + if (m_mapOrientation == Qt::Vertical) + // slices().at(topLeft.row())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble()); + if (topLeft.column() == m_mapValues) + slices().at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); + else if (topLeft.column() == m_mapLabels) + slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); + else + // slices().at(topLeft.column())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble()); + if (topLeft.column() == m_mapValues) + slices().at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); + else if (topLeft.column() == m_mapLabels) + slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); +} + +void QPieSeries::modelDataAdded(QModelIndex parent, int start, int end) +{ + // +} + +void QPieSeries::modelDataRemoved(QModelIndex parent, int start, int end) +{ + remove(slices().at(start)); +} + #include "moc_qpieseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/piechart/qpieseries.h b/src/piechart/qpieseries.h index e5fe20e..be6ef1a 100644 --- a/src/piechart/qpieseries.h +++ b/src/piechart/qpieseries.h @@ -82,6 +82,10 @@ public: QPieSlice* add(qreal value, QString name); void setLabelsVisible(bool visible = true); + // data from model + bool setModel(QAbstractItemModel* model); + void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical); + // TODO: find slices? // QList findByValue(qreal value); // ... @@ -105,6 +109,11 @@ private Q_SLOTS: // TODO: should be private and not visible in the interface at void sliceHoverEnter(); void sliceHoverLeave(); + // slots for updating pie when data in model changes + void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); + void modelDataAdded(QModelIndex parent, int start, int end); + void modelDataRemoved(QModelIndex parent, int start, int end); + private: void updateDerivativeData(); @@ -122,6 +131,11 @@ private: qreal m_pieStartAngle; qreal m_pieEndAngle; qreal m_total; + + // model map + int m_mapValues; + int m_mapLabels; + Qt::Orientation m_mapOrientation; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qseries.h b/src/qseries.h index 315d260..fb6dd79 100644 --- a/src/qseries.h +++ b/src/qseries.h @@ -32,7 +32,7 @@ public: };*/ protected: - QSeries(QObject *parent = 0) : QObject(parent) {} + QSeries(QObject *parent = 0) : QObject(parent) {m_model = NULL;} public: virtual ~QSeries() {} @@ -44,6 +44,9 @@ public: void setTitle(QString title) { m_title = title; } QString title() { return m_title; } +protected: + QAbstractItemModel* m_model; + private: QString m_title; }; diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index f208419..ff3b7d9 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -50,7 +50,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ QXYSeries::QXYSeries(QObject* parent):QSeries(parent) { - m_model = NULL; m_mapX = -1; m_mapY = -1; m_mapOrientation = Qt::Vertical; @@ -268,7 +267,10 @@ QXYSeries& QXYSeries::operator<< (const QList points) void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) { - emit pointReplaced(topLeft.row()); + if (m_mapOrientation == Qt::Vertical) + emit pointReplaced(topLeft.row()); + else + emit pointReplaced(topLeft.column()); } void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end) diff --git a/src/xychart/qxyseries.h b/src/xychart/qxyseries.h index 6b8c96b..06541ba 100644 --- a/src/xychart/qxyseries.h +++ b/src/xychart/qxyseries.h @@ -64,7 +64,7 @@ protected: QPen m_pen; QBrush m_brush; - QAbstractItemModel* m_model; +// QAbstractItemModel* m_model; int m_mapX; Qt::Orientation m_mapOrientation; int m_mapY;