diff --git a/src/qchart.cpp b/src/qchart.cpp index 1179bb9..2d50797 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -18,12 +18,14 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QChart::QChart(QGraphicsObject* parent) : QGraphicsObject(parent), - m_axisX(new Axis(this)), - m_axisY(new Axis(this)), - m_grid(new XYGrid(this)), - m_plotDataIndex(0), - m_marginSize(0) + m_axisX(new Axis(this)), + m_axisY(new Axis(this)), + m_grid(new XYGrid(this)), + m_plotDataIndex(0), + m_marginSize(0) { + // TODO: the default theme? + setTheme(QChart::ChartThemeVanilla); // setFlags(QGraphicsItem::ItemClipsChildrenToShape); // set axis m_axisY->rotate(90); @@ -103,6 +105,9 @@ QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) connect(this, SIGNAL(sizeChanged(QRectF)), scatterSeries, SLOT(chartSizeChanged(QRectF))); scatterSeries->d->setParentItem(this); + QColor nextColor = m_themeColors.takeFirst(); + nextColor.setAlpha(150); // TODO: default opacity? + scatterSeries->setMarkerColor(nextColor); return scatterSeries; } case QChartSeries::SeriesTypePie: { @@ -111,6 +116,7 @@ QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) QPieSeries *pieSeries = new QPieSeries(this); connect(this, SIGNAL(sizeChanged(QRectF)), pieSeries, SLOT(chartSizeChanged(QRectF))); + // TODO: how to define the color for all the slices of a pie? return pieSeries; } default: @@ -159,7 +165,39 @@ void QChart::setMargin(int margin) m_marginSize = margin; } -#include "moc_qchart.cpp" +void QChart::setTheme(QChart::ChartTheme theme) +{ + // TODO: define color themes + switch (theme) { + case ChartThemeVanilla: + m_themeColors.append(QColor(255, 238, 174)); + m_themeColors.append(QColor(228, 228, 160)); + m_themeColors.append(QColor(228, 179, 160)); + m_themeColors.append(QColor(180, 151, 18)); + m_themeColors.append(QColor(252, 252, 37)); + break; + case ChartThemeIcy: + m_themeColors.append(QColor(255, 238, 174)); + m_themeColors.append(QColor(228, 228, 160)); + m_themeColors.append(QColor(228, 179, 160)); + m_themeColors.append(QColor(180, 151, 18)); + m_themeColors.append(QColor(252, 252, 37)); + break; + case ChartThemeGrayscale: + m_themeColors.append(QColor(255, 238, 174)); + m_themeColors.append(QColor(228, 228, 160)); + m_themeColors.append(QColor(228, 179, 160)); + m_themeColors.append(QColor(180, 151, 18)); + m_themeColors.append(QColor(252, 252, 37)); + break; + default: + Q_ASSERT(false); + break; + } + // TODO: update coloring of different elements to match the selected theme +} + +#include "moc_qchart.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchart.h b/src/qchart.h index e1d0856..9a70953 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -26,6 +26,13 @@ class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsObject { Q_OBJECT public: + enum ChartTheme { + ChartThemeVanilla = 0, + ChartThemeIcy, + ChartThemeGrayscale + }; + +public: QChart(QGraphicsObject* parent = 0); ~QChart(); @@ -41,6 +48,7 @@ public: virtual void setSize(const QSizeF& rect); void setMargin(int margin); int margin() const; + void setTheme(QChart::ChartTheme theme); signals: void sizeChanged(QRectF rect); @@ -58,6 +66,7 @@ private: QList m_items; int m_plotDataIndex; int m_marginSize; + QList m_themeColors; QList m_BarGroupItems; }; diff --git a/src/qchartwidget.cpp b/src/qchartwidget.cpp index 299e191..ecd3f1b 100644 --- a/src/qchartwidget.cpp +++ b/src/qchartwidget.cpp @@ -46,6 +46,12 @@ QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type) { return m_chart->createSeries(type); } + +void QChartWidget::setTheme(QChart::ChartTheme theme) +{ + m_chart->setTheme(theme); +} + #include "moc_qchartwidget.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartwidget.h b/src/qchartwidget.h index 8f5ea76..b5d2347 100644 --- a/src/qchartwidget.h +++ b/src/qchartwidget.h @@ -28,6 +28,12 @@ public: void addSeries(QChartSeries* series); QChartSeries* createSeries(QChartSeries::QChartSeriesType type); + /*! + * Set color theme for the chart. Themes define harmonic colors for the graphical elements of + * the chart. + */ + void setTheme(QChart::ChartTheme theme); + private: Q_DISABLE_COPY(QChartWidget) QGraphicsScene *m_scene; diff --git a/src/qscatterseries.cpp b/src/qscatterseries.cpp index 75b2ea7..471da73 100644 --- a/src/qscatterseries.cpp +++ b/src/qscatterseries.cpp @@ -12,7 +12,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) : QGraphicsItem(parent), m_scalex(100), // TODO: let the use define the scale (or autoscaled) - m_scaley(100) + m_scaley(100), + m_markerColor(QColor()) { } @@ -41,7 +42,8 @@ void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsI QPen pen = painter->pen(); QBrush brush = pen.brush(); // TODO: The opacity should be user definable... - brush.setColor(QColor(255, 82, 0, 100)); + //brush.setColor(QColor(255, 82, 0, 100)); + brush.setColor(m_markerColor); pen.setBrush(brush); pen.setWidth(4); painter->setPen(pen); @@ -80,6 +82,11 @@ void QScatterSeries::chartSizeChanged(QRectF rect) d->resize(rect); } +void QScatterSeries::setMarkerColor(QColor color) +{ + d->m_markerColor = color; +} + // TODO: //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale) //{ diff --git a/src/qscatterseries.h b/src/qscatterseries.h index 83e3a77..a134b1e 100644 --- a/src/qscatterseries.h +++ b/src/qscatterseries.h @@ -3,6 +3,7 @@ #include "qchartseries.h" #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE class QScatterSeriesPrivate; @@ -21,6 +22,7 @@ public: // from QChartSeries public Q_SLOTS: void chartSizeChanged(QRectF rect); + void setMarkerColor(QColor color); //void chartScaleChanged(qreal xscale, qreal yscale); private: diff --git a/src/qscatterseries_p.h b/src/qscatterseries_p.h index ef8b71e..e39c4af 100644 --- a/src/qscatterseries_p.h +++ b/src/qscatterseries_p.h @@ -26,6 +26,7 @@ public: // from QGraphicsItem qreal m_scaley; QList m_scenex; QList m_sceney; + QColor m_markerColor; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index f73ff63..80aae62 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -58,6 +58,14 @@ MainWidget::MainWidget(QWidget *parent) : m_yMaxSpin->setValue(10); connect(m_yMaxSpin, SIGNAL(valueChanged(int)), this, SLOT(yMaxChanged(int))); + QComboBox *chartTheme = new QComboBox(); + chartTheme->addItem("Vanilla"); + chartTheme->addItem("Icy"); + chartTheme->addItem("Grayscale"); + chartTheme->addItem("Tobedefined"); + connect(chartTheme, SIGNAL(currentIndexChanged(int)), + this, SLOT(changeChartTheme(int))); + QGridLayout *grid = new QGridLayout(); QGridLayout *mainLayout = new QGridLayout(); //grid->addWidget(new QLabel("Add series:"), 0, 0); @@ -73,9 +81,11 @@ MainWidget::MainWidget(QWidget *parent) : grid->addWidget(m_yMinSpin, 6, 1); grid->addWidget(new QLabel("y max:"), 7, 0); grid->addWidget(m_yMaxSpin, 7, 1); + grid->addWidget(new QLabel("Chart theme:"), 8, 0); + grid->addWidget(chartTheme, 8, 1); // add row with empty label to make all the other rows static - grid->addWidget(new QLabel(""), 8, 0); - grid->setRowStretch(8, 1); + grid->addWidget(new QLabel(""), 9, 0); + grid->setRowStretch(9, 1); mainLayout->addLayout(grid, 0, 0); @@ -276,6 +286,12 @@ void MainWidget::yMaxChanged(int value) qDebug() << "yMaxChanged: " << value; } +void MainWidget::changeChartTheme(int themeIndex) +{ + qDebug() << "changeChartTheme: " << themeIndex; + m_chartWidget->setTheme((QChart::ChartTheme) themeIndex); +} + void MainWidget::setPieSizeFactor(double size) { QPieSeries *pie = qobject_cast(m_currentSeries); diff --git a/test/chartwidgettest/mainwidget.h b/test/chartwidgettest/mainwidget.h index 3e5fc39..21cf670 100644 --- a/test/chartwidgettest/mainwidget.h +++ b/test/chartwidgettest/mainwidget.h @@ -30,6 +30,7 @@ private slots: void yMinChanged(int value); void yMaxChanged(int value); void setCurrentSeries(QChartSeries *series); + void changeChartTheme(int themeIndex); void setPieSizeFactor(double margin); private: