@@ -58,10 +58,12 TableWidget::TableWidget(QWidget *parent) | |||
|
58 | 58 | lineRadioButton = new QRadioButton("Line"); |
|
59 | 59 | splineRadioButton = new QRadioButton("Spline"); |
|
60 | 60 | scatterRadioButton = new QRadioButton("Scatter"); |
|
61 | pieRadioButton = new QRadioButton("Pie"); | |
|
61 | 62 | |
|
62 | 63 | connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); |
|
63 | 64 | connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); |
|
64 | 65 | connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); |
|
66 | connect(pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType())); | |
|
65 | 67 | lineRadioButton->setChecked(true); |
|
66 | 68 | |
|
67 | 69 | // radio buttons layout |
@@ -69,6 +71,7 TableWidget::TableWidget(QWidget *parent) | |||
|
69 | 71 | radioLayout->addWidget(lineRadioButton); |
|
70 | 72 | radioLayout->addWidget(splineRadioButton); |
|
71 | 73 | radioLayout->addWidget(scatterRadioButton); |
|
74 | radioLayout->addWidget(pieRadioButton); | |
|
72 | 75 | |
|
73 | 76 | // create main layout |
|
74 | 77 | QGridLayout* mainLayout = new QGridLayout; |
@@ -107,8 +110,17 void TableWidget::updateChartType() | |||
|
107 | 110 | series = new QLineSeries; |
|
108 | 111 | else if (splineRadioButton->isChecked()) |
|
109 | 112 | series = new QSplineSeries; |
|
110 | else | |
|
113 | else if (scatterRadioButton->isChecked()) | |
|
111 | 114 | series = new QScatterSeries; |
|
115 | else if (pieRadioButton->isChecked()) | |
|
116 | { | |
|
117 | QPieSeries* pieSeries = new QPieSeries; | |
|
118 | pieSeries->setModel(m_model); | |
|
119 | pieSeries->setModelMapping(0,2, Qt::Vertical); | |
|
120 | pieSeries->setLabelsVisible(true); | |
|
121 | chartView->addSeries(pieSeries); | |
|
122 | return; | |
|
123 | } | |
|
112 | 124 | |
|
113 | 125 | series->setModel(m_model); |
|
114 | 126 | series->setModelMapping(0,1, Qt::Vertical); |
@@ -35,6 +35,7 public: | |||
|
35 | 35 | QRadioButton* lineRadioButton; |
|
36 | 36 | QRadioButton* splineRadioButton; |
|
37 | 37 | QRadioButton* scatterRadioButton; |
|
38 | QRadioButton* pieRadioButton; | |
|
38 | 39 | }; |
|
39 | 40 | |
|
40 | 41 | #endif // TABLEWIDGET_H |
@@ -265,7 +265,7 QList<QPieSlice*> QPieSeries::slices() const | |||
|
265 | 265 | void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition) |
|
266 | 266 | { |
|
267 | 267 | if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 || |
|
268 | relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0) | |
|
268 | relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0) | |
|
269 | 269 | return; |
|
270 | 270 | |
|
271 | 271 | if (m_pieRelativeHorPos != relativeHorizontalPosition || m_pieRelativeVerPos != relativeVerticalPosition) { |
@@ -531,6 +531,71 void QPieSeries::updateDerivativeData() | |||
|
531 | 531 | } |
|
532 | 532 | } |
|
533 | 533 | |
|
534 | bool QPieSeries::setModel(QAbstractItemModel* model) | |
|
535 | { | |
|
536 | // disconnect signals from old model | |
|
537 | if(m_model) | |
|
538 | { | |
|
539 | disconnect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), 0, 0); | |
|
540 | disconnect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), 0, 0); | |
|
541 | disconnect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), 0, 0); | |
|
542 | } | |
|
543 | ||
|
544 | // set new model if not NULL and connect necessary signals from it | |
|
545 | if(model) | |
|
546 | { | |
|
547 | m_model = model; | |
|
548 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); | |
|
549 | connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); | |
|
550 | connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); | |
|
551 | } | |
|
552 | } | |
|
553 | ||
|
554 | void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation) | |
|
555 | { | |
|
556 | m_mapValues = modelValuesLine; | |
|
557 | m_mapLabels = modelLabelsLine; | |
|
558 | m_mapOrientation = orientation; | |
|
559 | ||
|
560 | if (m_model == NULL) | |
|
561 | return; | |
|
562 | ||
|
563 | if (m_mapOrientation == Qt::Vertical) | |
|
564 | for (int i = 0; i < m_model->rowCount(); i++) | |
|
565 | 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()); | |
|
566 | else | |
|
567 | for (int i = 0; i < m_model->columnCount(); i++) | |
|
568 | 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()); | |
|
569 | ||
|
570 | ||
|
571 | } | |
|
572 | ||
|
573 | void QPieSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | |
|
574 | { | |
|
575 | if (m_mapOrientation == Qt::Vertical) | |
|
576 | // slices().at(topLeft.row())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble()); | |
|
577 | if (topLeft.column() == m_mapValues) | |
|
578 | slices().at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | |
|
579 | else if (topLeft.column() == m_mapLabels) | |
|
580 | slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | |
|
581 | else | |
|
582 | // slices().at(topLeft.column())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble()); | |
|
583 | if (topLeft.column() == m_mapValues) | |
|
584 | slices().at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | |
|
585 | else if (topLeft.column() == m_mapLabels) | |
|
586 | slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | |
|
587 | } | |
|
588 | ||
|
589 | void QPieSeries::modelDataAdded(QModelIndex parent, int start, int end) | |
|
590 | { | |
|
591 | // | |
|
592 | } | |
|
593 | ||
|
594 | void QPieSeries::modelDataRemoved(QModelIndex parent, int start, int end) | |
|
595 | { | |
|
596 | remove(slices().at(start)); | |
|
597 | } | |
|
598 | ||
|
534 | 599 | #include "moc_qpieseries.cpp" |
|
535 | 600 | |
|
536 | 601 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -82,6 +82,10 public: | |||
|
82 | 82 | QPieSlice* add(qreal value, QString name); |
|
83 | 83 | void setLabelsVisible(bool visible = true); |
|
84 | 84 | |
|
85 | // data from model | |
|
86 | bool setModel(QAbstractItemModel* model); | |
|
87 | void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical); | |
|
88 | ||
|
85 | 89 | // TODO: find slices? |
|
86 | 90 | // QList<QPieSlice*> findByValue(qreal value); |
|
87 | 91 | // ... |
@@ -105,6 +109,11 private Q_SLOTS: // TODO: should be private and not visible in the interface at | |||
|
105 | 109 | void sliceHoverEnter(); |
|
106 | 110 | void sliceHoverLeave(); |
|
107 | 111 | |
|
112 | // slots for updating pie when data in model changes | |
|
113 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); | |
|
114 | void modelDataAdded(QModelIndex parent, int start, int end); | |
|
115 | void modelDataRemoved(QModelIndex parent, int start, int end); | |
|
116 | ||
|
108 | 117 | private: |
|
109 | 118 | void updateDerivativeData(); |
|
110 | 119 | |
@@ -122,6 +131,11 private: | |||
|
122 | 131 | qreal m_pieStartAngle; |
|
123 | 132 | qreal m_pieEndAngle; |
|
124 | 133 | qreal m_total; |
|
134 | ||
|
135 | // model map | |
|
136 | int m_mapValues; | |
|
137 | int m_mapLabels; | |
|
138 | Qt::Orientation m_mapOrientation; | |
|
125 | 139 | }; |
|
126 | 140 | |
|
127 | 141 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -32,7 +32,7 public: | |||
|
32 | 32 | };*/ |
|
33 | 33 | |
|
34 | 34 | protected: |
|
35 | QSeries(QObject *parent = 0) : QObject(parent) {} | |
|
35 | QSeries(QObject *parent = 0) : QObject(parent) {m_model = NULL;} | |
|
36 | 36 | |
|
37 | 37 | public: |
|
38 | 38 | virtual ~QSeries() {} |
@@ -44,6 +44,9 public: | |||
|
44 | 44 | void setTitle(QString title) { m_title = title; } |
|
45 | 45 | QString title() { return m_title; } |
|
46 | 46 | |
|
47 | protected: | |
|
48 | QAbstractItemModel* m_model; | |
|
49 | ||
|
47 | 50 | private: |
|
48 | 51 | QString m_title; |
|
49 | 52 | }; |
@@ -50,7 +50,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
50 | 50 | */ |
|
51 | 51 | QXYSeries::QXYSeries(QObject* parent):QSeries(parent) |
|
52 | 52 | { |
|
53 | m_model = NULL; | |
|
54 | 53 | m_mapX = -1; |
|
55 | 54 | m_mapY = -1; |
|
56 | 55 | m_mapOrientation = Qt::Vertical; |
@@ -268,7 +267,10 QXYSeries& QXYSeries::operator<< (const QList<QPointF> points) | |||
|
268 | 267 | |
|
269 | 268 | void QXYSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) |
|
270 | 269 | { |
|
271 | emit pointReplaced(topLeft.row()); | |
|
270 | if (m_mapOrientation == Qt::Vertical) | |
|
271 | emit pointReplaced(topLeft.row()); | |
|
272 | else | |
|
273 | emit pointReplaced(topLeft.column()); | |
|
272 | 274 | } |
|
273 | 275 | |
|
274 | 276 | void QXYSeries::modelDataAdded(QModelIndex parent, int start, int end) |
General Comments 0
You need to be logged in to leave comments.
Login now