From bf7c8a356b51a3913a73347fcfecf28dd42179ef 2012-06-14 11:56:47 From: Tero Ahola Date: 2012-06-14 11:56:47 Subject: [PATCH] QML ChartView scrolling, zooming, drop shadow --- diff --git a/plugins/declarative/declarativechart.cpp b/plugins/declarative/declarativechart.cpp index b6d49cc..4acc974 100644 --- a/plugins/declarative/declarativechart.cpp +++ b/plugins/declarative/declarativechart.cpp @@ -220,6 +220,71 @@ int DeclarativeChart::count() return m_chart->series().count(); } +void DeclarativeChart::setDropShadowEnabled(bool enabled) +{ + if (enabled != m_chart->isBackgroundDropShadowEnabled()) { + m_chart->setBackgroundDropShadowEnabled(enabled); + dropShadowEnabledChanged(enabled); + } +} + +bool DeclarativeChart::dropShadowEnabled() +{ + return m_chart->isBackgroundDropShadowEnabled(); +} + +void DeclarativeChart::zoom(qreal factor) +{ + m_chart->zoom(factor); +} + +void DeclarativeChart::scrollLeft(qreal pixels) +{ + m_chart->scroll(QPointF(pixels, 0)); +} + +void DeclarativeChart::scrollRight(qreal pixels) +{ + m_chart->scroll(QPointF(-pixels, 0)); +} + +void DeclarativeChart::scrollUp(qreal pixels) +{ + m_chart->scroll(QPointF(0, pixels)); +} + +void DeclarativeChart::scrollDown(qreal pixels) +{ + m_chart->scroll(QPointF(0, -pixels)); +} + +//void DeclarativeChart::scrollLeft(qreal ticks) +//{ +// m_chart->scroll(QPointF(ticksToPixels(m_chart->axisX(), ticks), 0)); +//} + +//void DeclarativeChart::scrollRight(qreal ticks) +//{ +// m_chart->scroll(QPointF(-ticksToPixels(m_chart->axisX(), ticks), 0)); +//} + +//void DeclarativeChart::scrollUp(qreal ticks) +//{ +// m_chart->scroll(QPointF(0, ticksToPixels(m_chart->axisY(), ticks))); +//} + +//void DeclarativeChart::scrollDown(qreal ticks) +//{ +// m_chart->scroll(QPointF(0, -ticksToPixels(m_chart->axisY(), ticks))); +//} + +//qreal DeclarativeChart::ticksToPixels(QAxis *axis, qreal ticks) +//{ +// qreal tickCount = axis->max() - axis->min(); +// qreal tickPixels = (m_chart->size().width() - m_chart->margins().width() * 2) / tickCount; +// return tickPixels * ticks; +//} + QAbstractSeries *DeclarativeChart::series(int index) { if (index < m_chart->series().count()) { diff --git a/plugins/declarative/declarativechart.h b/plugins/declarative/declarativechart.h index a02adee..bd7cae1 100644 --- a/plugins/declarative/declarativechart.h +++ b/plugins/declarative/declarativechart.h @@ -37,14 +37,15 @@ class DeclarativeChart : public QDeclarativeItem Q_PROPERTY(Theme theme READ theme WRITE setTheme NOTIFY themeChanged) Q_PROPERTY(Animation animationOptions READ animationOptions WRITE setAnimationOptions NOTIFY animationOptionsChanged) Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) + Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged) Q_PROPERTY(QAxis *axisX READ axisX) Q_PROPERTY(QAxis *axisY READ axisY) Q_PROPERTY(QLegend *legend READ legend) // TODO: how to define axis labels? This is not very convenient Q_PROPERTY(QVariantList axisXLabels READ axisXLabels WRITE setAxisXLabels NOTIFY axisLabelsChanged) Q_PROPERTY(int count READ count) - Q_PROPERTY(QColor titleColor READ titleColor WRITE setTitleColor NOTIFY titleColorChanged) Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged) + Q_PROPERTY(bool dropShadowEnabled READ dropShadowEnabled WRITE setDropShadowEnabled NOTIFY dropShadowEnabledChanged) Q_ENUMS(Animation) Q_ENUMS(Theme) Q_ENUMS(SeriesType) @@ -107,6 +108,13 @@ public: void setBackgroundColor(QColor color); QColor backgroundColor(); int count(); + void setDropShadowEnabled(bool enabled); + bool dropShadowEnabled(); + Q_INVOKABLE void zoom(qreal factor); + Q_INVOKABLE void scrollLeft(qreal pixels); + Q_INVOKABLE void scrollRight(qreal pixels); + Q_INVOKABLE void scrollUp(qreal pixels); + Q_INVOKABLE void scrollDown(qreal pixels); Q_INVOKABLE QAbstractSeries *series(int index); Q_INVOKABLE QAbstractSeries *series(QString seriesName); Q_INVOKABLE QAbstractSeries *createSeries(DeclarativeChart::SeriesType type, QString name = ""); @@ -118,6 +126,7 @@ Q_SIGNALS: void axisLabelsChanged(); void titleColorChanged(); void backgroundColorChanged(); + void dropShadowEnabledChanged(bool enabled); public: // Extending QChart with DeclarativeChart is not possible because QObject does not support diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml index 6002ceb..6457f4a 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml @@ -43,6 +43,7 @@ Flow { onAnimationOptionsChanged: console.log("chart.onAnimationOptionsChanged: " + series.animationOptions); onTitleColorChanged: console.log("chart.onTitleColorChanged: " + series.titleColor); onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + series.backgroundColor); + onDropShadowEnabledChanged: console.log("chart.onDropShadowEnabledChanged: " + enabled); } Connections { @@ -116,6 +117,34 @@ Flow { onClicked: series.backgroundColor = main.nextColor(); } Button { + text: "drop shadow enabled" + onClicked: series.dropShadowEnabled = !series.dropShadowEnabled; + } + Button { + text: "zoom +" + onClicked: series.zoom(2); + } + Button { + text: "zoom -" + onClicked: series.zoom(0.5); + } + Button { + text: "scroll left" + onClicked: series.scrollLeft(10); + } + Button { + text: "scroll right" + onClicked: series.scrollRight(10); + } + Button { + text: "scroll up" + onClicked: series.scrollUp(10); + } + Button { + text: "scroll down" + onClicked: series.scrollDown(10); + } + Button { text: "legend visible" onClicked: series.legend.visible = !series.legend.visible; }