From a58216ed7654bf1f3a825f5f1dc1d3da1c9a88f1 2013-06-04 06:07:45 From: Miikka Heikkinen Date: 2013-06-04 06:07:45 Subject: [PATCH] Added API to set chart background roundness Task-number: QTRD-1631 Change-Id: I8ac70b5ed157211b90aa1c9ef3d168bca78fb27c Reviewed-by: Tomi Korpipää --- diff --git a/plugins/declarative/declarativechart.cpp b/plugins/declarative/declarativechart.cpp index bb62e07..a50a6df 100644 --- a/plugins/declarative/declarativechart.cpp +++ b/plugins/declarative/declarativechart.cpp @@ -115,6 +115,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! + \qmlproperty real ChartView::backgroundRoundness + The diameter of the rounding cirle at the corners of the chart background. +*/ + +/*! \qmlproperty color ChartView::plotAreaColor The color of the background of the chart's plot area. By default plot area background uses chart's background color. @@ -706,6 +711,19 @@ bool DeclarativeChart::dropShadowEnabled() return m_chart->isDropShadowEnabled(); } +qreal DeclarativeChart::backgroundRoundness() const +{ + return m_chart->backgroundRoundness(); +} + +void DeclarativeChart::setBackgroundRoundness(qreal diameter) +{ + if (m_chart->backgroundRoundness() != diameter) { + m_chart->setBackgroundRoundness(diameter); + emit backgroundRoundnessChanged(diameter); + } +} + qreal DeclarativeChart::topMargin() { qWarning() << "ChartView.topMargin is deprecated. Use margins instead."; diff --git a/plugins/declarative/declarativechart.h b/plugins/declarative/declarativechart.h index 2717ee0..f24a7a9 100644 --- a/plugins/declarative/declarativechart.h +++ b/plugins/declarative/declarativechart.h @@ -53,6 +53,7 @@ class DeclarativeChart : public QDECLARATIVE_PAINTED_ITEM Q_PROPERTY(int count READ count) Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged) Q_PROPERTY(bool dropShadowEnabled READ dropShadowEnabled WRITE setDropShadowEnabled NOTIFY dropShadowEnabledChanged) + Q_PROPERTY(qreal backgroundRoundness READ backgroundRoundness WRITE setBackgroundRoundness NOTIFY backgroundRoundnessChanged REVISION 3) Q_PROPERTY(qreal topMargin READ topMargin) Q_PROPERTY(qreal bottomMargin READ bottomMargin) Q_PROPERTY(qreal leftMargin READ leftMargin) @@ -143,6 +144,8 @@ public: int count(); void setDropShadowEnabled(bool enabled); bool dropShadowEnabled(); + Q_REVISION(3) qreal backgroundRoundness() const; + Q_REVISION(3) void setBackgroundRoundness(qreal diameter); // Margins & plotArea qreal topMargin(); @@ -191,6 +194,7 @@ Q_SIGNALS: void seriesAdded(QAbstractSeries *series); void seriesRemoved(QAbstractSeries *series); Q_REVISION(3) void plotAreaColorChanged(); + Q_REVISION(3) void backgroundRoundnessChanged(qreal diameter); private Q_SLOTS: void changeMinimumMargins(int top, int bottom, int left, int right); diff --git a/src/chartbackground.cpp b/src/chartbackground.cpp index 4e540d7..7b9e6d1 100644 --- a/src/chartbackground.cpp +++ b/src/chartbackground.cpp @@ -29,7 +29,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE ChartBackground::ChartBackground(QGraphicsItem *parent) : QGraphicsRectItem(parent), - m_diameter(15), + m_diameter(5), m_dropShadow(0) { } @@ -69,7 +69,7 @@ void ChartBackground::paint(QPainter *painter, const QStyleOptionGraphicsItem *o painter->save(); painter->setPen(pen()); painter->setBrush(brush()); - painter->drawRoundRect(rect(), roundness(rect().width()), roundness(rect().height())); + painter->drawRoundedRect(rect(), m_diameter, m_diameter); #ifndef QT_NO_DEBUG painter->setPen(Qt::black); QFont font; @@ -81,21 +81,15 @@ void ChartBackground::paint(QPainter *painter, const QStyleOptionGraphicsItem *o painter->restore(); } -int ChartBackground::roundness(qreal size) const -{ - if (qFuzzyCompare(size, 0)) - return 0; - return 100 * m_diameter / int(size); -} - -int ChartBackground::diameter() const +qreal ChartBackground::diameter() const { return m_diameter; } -void ChartBackground::setDiameter(int diameter) +void ChartBackground::setDiameter(qreal diameter) { m_diameter = diameter; + update(); } QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartbackground_p.h b/src/chartbackground_p.h index a01a614..cb89a01 100644 --- a/src/chartbackground_p.h +++ b/src/chartbackground_p.h @@ -42,8 +42,8 @@ public: ChartBackground(QGraphicsItem *parent = 0); ~ChartBackground(); - void setDiameter(int diameter); - int diameter() const; + void setDiameter(qreal diameter); + qreal diameter() const; void setDropShadowEnabled(bool enabled); bool isDropShadowEnabled() { return m_dropShadow != 0; } @@ -51,10 +51,7 @@ protected: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); private: - int roundness(qreal size) const; - -private: - int m_diameter; + qreal m_diameter; QGraphicsDropShadowEffect *m_dropShadow; }; diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 7d5fb5a..947557b 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -227,6 +227,20 @@ QPen ChartPresenter::backgroundPen() const return m_background->pen(); } +void ChartPresenter::setBackgroundRoundness(qreal diameter) +{ + createBackgroundItem(); + m_background->setDiameter(diameter); + m_layout->invalidate(); +} + +qreal ChartPresenter::backgroundRoundness() const +{ + if (!m_background) + return 0; + return m_background->diameter(); +} + void ChartPresenter::setPlotAreaBackgroundBrush(const QBrush &brush) { createPlotAreaBackgroundItem(); diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index dcfcf66..49ba55d 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -102,6 +102,9 @@ public: void setBackgroundPen(const QPen &pen); QPen backgroundPen() const; + void setBackgroundRoundness(qreal diameter); + qreal backgroundRoundness() const; + void setPlotAreaBackgroundBrush(const QBrush &brush); QBrush plotAreaBackgroundBrush() const; diff --git a/src/qchart.cpp b/src/qchart.cpp index 6eed3a5..a538419 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -97,6 +97,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! + \property QChart::backgroundRoundness + The diameter of the rounding cirle at the corners of the chart background. + */ + +/*! \property QChart::minimumMargins Minimum margins between the plot area (axes) and the edge of the chart widget. This property is deprecated; use margins property instead. @@ -594,6 +599,16 @@ bool QChart::isDropShadowEnabled() const return d_ptr->m_presenter->isBackgroundDropShadowEnabled(); } +void QChart::setBackgroundRoundness(qreal diameter) +{ + d_ptr->m_presenter->setBackgroundRoundness(diameter); +} + +qreal QChart::backgroundRoundness() const +{ + return d_ptr->m_presenter->backgroundRoundness(); +} + /*! Returns all series that are added to the chart. diff --git a/src/qchart.h b/src/qchart.h index 77a287a..3a645c9 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -43,6 +43,7 @@ class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget Q_PROPERTY(QString title READ title WRITE setTitle) Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible) Q_PROPERTY(bool dropShadowEnabled READ isDropShadowEnabled WRITE setDropShadowEnabled) + Q_PROPERTY(qreal backgroundRoundness READ backgroundRoundness WRITE setBackgroundRoundness) Q_PROPERTY(QChart::AnimationOptions animationOptions READ animationOptions WRITE setAnimationOptions) Q_PROPERTY(QMargins minimumMargins READ minimumMargins WRITE setMinimumMargins) Q_PROPERTY(QMargins margins READ margins WRITE setMargins) @@ -119,6 +120,8 @@ public: void setDropShadowEnabled(bool enabled = true); bool isDropShadowEnabled() const; + void setBackgroundRoundness(qreal diameter); + qreal backgroundRoundness() const; void setAnimationOptions(AnimationOptions options); AnimationOptions animationOptions() const; diff --git a/tests/auto/qchart/tst_qchart.cpp b/tests/auto/qchart/tst_qchart.cpp index cbc81b4..ea846ef 100644 --- a/tests/auto/qchart/tst_qchart.cpp +++ b/tests/auto/qchart/tst_qchart.cpp @@ -109,6 +109,7 @@ private slots: void createDefaultAxesForLineSeries_data(); void createDefaultAxesForLineSeries(); void axisPolarOrientation(); + void backgroundRoundness(); private: void createTestData(); @@ -1028,6 +1029,14 @@ void tst_QChart::axisPolarOrientation() QCOMPARE(chart.axisPolarOrientation(yAxes[0]), QPolarChart::PolarOrientationRadial); } +void tst_QChart::backgroundRoundness() +{ + createTestData(); + m_chart->createDefaultAxes(); + m_chart->setBackgroundRoundness(100.0); + QVERIFY(m_chart->backgroundRoundness() == 100.0); +} + QTEST_MAIN(tst_QChart) #include "tst_qchart.moc" diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml index 6e2c97e..626615a 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml @@ -42,6 +42,7 @@ ChartView { onTitleColorChanged: console.log("chart.onTitleColorChanged: " + color); onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + chart.backgroundColor); onDropShadowEnabledChanged: console.log("chart.onDropShadowEnabledChanged: " + enabled); + onBackgroundRoundnessChanged: console.log("chart.onBackgroundRoundnessChanged: " + diameter); onSeriesAdded: console.log("chart.onSeriesAdded: " + series.name); onSeriesRemoved: console.log("chart.onSeriesRemoved: " + series.name); onPlotAreaColorChanged: console.log("chart.plotAreaColorChanged: " + chart.plotAreaColor); diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorProperties.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorProperties.qml index fdced78..814bd71 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorProperties.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorProperties.qml @@ -58,6 +58,10 @@ Flow { onClicked: chart.dropShadowEnabled = !chart.dropShadowEnabled; } Button { + text: "roundness" + onClicked: chart.backgroundRoundness++; + } + Button { text: "zoom +" onClicked: chart.zoom(2); } diff --git a/tests/quick2chartproperties/qml/quick2chartproperties/Chart.qml b/tests/quick2chartproperties/qml/quick2chartproperties/Chart.qml index a639638..f415dac 100644 --- a/tests/quick2chartproperties/qml/quick2chartproperties/Chart.qml +++ b/tests/quick2chartproperties/qml/quick2chartproperties/Chart.qml @@ -42,6 +42,7 @@ ChartView { onTitleColorChanged: console.log("chart.onTitleColorChanged: " + color); onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + chart.backgroundColor); onDropShadowEnabledChanged: console.log("chart.onDropShadowEnabledChanged: " + enabled); + onBackgroundRoundnessChanged: console.log("chart.onBackgroundRoundnessChanged: " + diameter); onSeriesAdded: console.log("chart.onSeriesAdded: " + series.name); onSeriesRemoved: console.log("chart.onSeriesRemoved: " + series.name); onPlotAreaColorChanged: console.log("chart.plotAreaColorChanged: " + chart.plotAreaColor); diff --git a/tests/quick2chartproperties/qml/quick2chartproperties/ChartEditorProperties.qml b/tests/quick2chartproperties/qml/quick2chartproperties/ChartEditorProperties.qml index 1bc14b4..7ce214e 100644 --- a/tests/quick2chartproperties/qml/quick2chartproperties/ChartEditorProperties.qml +++ b/tests/quick2chartproperties/qml/quick2chartproperties/ChartEditorProperties.qml @@ -58,6 +58,10 @@ Flow { onClicked: chart.dropShadowEnabled = !chart.dropShadowEnabled; } Button { + text: "roundness" + onClicked: chart.backgroundRoundness++; + } + Button { text: "zoom +" onClicked: chart.zoom(2); }