From 0b7cf651b670d51c213cae8f4638b97b1a5011f8 2012-06-25 07:52:41 From: Tero Ahola Date: 2012-06-25 07:52:41 Subject: [PATCH] Added property definitions to QChart --- diff --git a/examples/legend/mainwidget.cpp b/examples/legend/mainwidget.cpp index 81468e8..52f8b9a 100644 --- a/examples/legend/mainwidget.cpp +++ b/examples/legend/mainwidget.cpp @@ -139,7 +139,6 @@ void MainWidget::showLegendSpinbox() { m_legendSettings->setVisible(true); QRectF chartViewRect = m_chartView->rect(); - QRectF legendRect = m_chart->legend()->boundingRect(); m_legendPosX->setMinimum(0); m_legendPosX->setMaximum(chartViewRect.width()); diff --git a/plugins/declarative/declarativechart.cpp b/plugins/declarative/declarativechart.cpp index 8bbe6aa..d6448bb 100644 --- a/plugins/declarative/declarativechart.cpp +++ b/plugins/declarative/declarativechart.cpp @@ -107,6 +107,30 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! + \qmlproperty real ChartView::topMargin + The space between the top of chart view and the top of the plot area. The title (if non-empty) is drawn on top margin + area of the chart view. Top margin area is also used by legend, if aligned to top. +*/ + +/*! + \qmlproperty real ChartView::bottomMargin + The space between the bottom of chart view and the bottom of the plot area. The bottom margin area may be used by + legend (if aligned to bottom), x-axis, x-axis labels and x-axis tick marks. +*/ + +/*! + \qmlproperty real ChartView::leftMargin + The space between the left side of chart view and the left side of the plot area. The left margin area may be used by + legend (if aligned to left), y-axis, y-axis labels and y-axis tick marks. +*/ + +/*! + \qmlproperty real ChartView::rightMargin + The space between the right side of chart view and the right side of the plot area. The right margin area may be used + by legend (if aligned to right). +*/ + +/*! \qmlmethod AbstractSeries ChartView::series(int index) Returns the series with \a index on the chart. This allows you to loop through the series of a chart together with the count property of the chart. @@ -167,6 +191,22 @@ DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) { setFlag(QGraphicsItem::ItemHasNoContents, false); // m_chart->axisX()->setNiceNumbersEnabled(false); + m_chartMargins = m_chart->margins(); + connect(m_chart, SIGNAL(marginsChanged(QRectF)), this, SLOT(handleMarginsChanged(QRectF))); +} + +void DeclarativeChart::handleMarginsChanged(QRectF newMargins) +{ + if (m_chartMargins.top() != newMargins.top()) + topMarginChanged(m_chart->margins().top()); + if (m_chartMargins.bottom() != newMargins.bottom()) + bottomMarginChanged(m_chart->margins().bottom()); + if (m_chartMargins.left() != newMargins.left()) + leftMarginChanged(m_chart->margins().left()); + if (m_chartMargins.right() != newMargins.right()) + rightMarginChanged(m_chart->margins().right()); + + m_chartMargins = m_chart->margins(); } DeclarativeChart::~DeclarativeChart() @@ -302,7 +342,7 @@ void DeclarativeChart::setTitleColor(QColor color) if (color != b.color()) { b.setColor(color); m_chart->setTitleBrush(b); - emit titleColorChanged(); + emit titleColorChanged(color); } } @@ -355,6 +395,26 @@ bool DeclarativeChart::dropShadowEnabled() return m_chart->isDropShadowEnabled(); } +qreal DeclarativeChart::topMargin() +{ + return m_chart->margins().top(); +} + +qreal DeclarativeChart::bottomMargin() +{ + return m_chart->margins().bottom(); +} + +qreal DeclarativeChart::leftMargin() +{ + return m_chart->margins().left(); +} + +qreal DeclarativeChart::rightMargin() +{ + return m_chart->margins().right(); +} + void DeclarativeChart::zoom(qreal factor) { m_chart->zoom(factor); diff --git a/plugins/declarative/declarativechart.h b/plugins/declarative/declarativechart.h index f257325..feeab72 100644 --- a/plugins/declarative/declarativechart.h +++ b/plugins/declarative/declarativechart.h @@ -47,6 +47,10 @@ class DeclarativeChart : public QDeclarativeItem 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 topMargin READ topMargin NOTIFY topMarginChanged) + Q_PROPERTY(qreal bottomMargin READ bottomMargin NOTIFY bottomMarginChanged) + Q_PROPERTY(qreal leftMargin READ leftMargin NOTIFY leftMarginChanged) + Q_PROPERTY(qreal rightMargin READ rightMargin NOTIFY rightMarginChanged) Q_ENUMS(Animation) Q_ENUMS(Theme) Q_ENUMS(SeriesType) @@ -112,6 +116,10 @@ public: int count(); void setDropShadowEnabled(bool enabled); bool dropShadowEnabled(); + qreal topMargin(); + qreal bottomMargin(); + qreal leftMargin(); + qreal rightMargin(); public: Q_INVOKABLE QAbstractSeries *series(int index); @@ -126,14 +134,22 @@ public: Q_SIGNALS: void axisLabelsChanged(); - void titleColorChanged(); + void titleColorChanged(QColor color); void backgroundColorChanged(); void dropShadowEnabledChanged(bool enabled); + void topMarginChanged(qreal margin); + void bottomMarginChanged(qreal margin); + void leftMarginChanged(qreal margin); + void rightMarginChanged(qreal margin); -public: +public Q_SLOTS: + void handleMarginsChanged(QRectF newMargins); + +private: // Extending QChart with DeclarativeChart is not possible because QObject does not support // multi inheritance, so we now have a QChart as a member instead QChart *m_chart; + QRectF m_chartMargins; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 5695cc6..eae233c 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -300,6 +300,8 @@ void ChartPresenter::updateLayout() { if (!m_rect.isValid()) return; + QRectF oldChargMargins = m_chartMargins; + // recalculate title size QSize titleSize; @@ -389,6 +391,8 @@ void ChartPresenter::updateLayout() emit geometryChanged(m_chartRect); } + if (oldChargMargins != m_chartMargins) + emit marginsChanged(m_chartMargins); } void ChartPresenter::createChartBackgroundItem() diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 19eca9f..fdae5e7 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -122,6 +122,7 @@ private Q_SLOTS: Q_SIGNALS: void geometryChanged(const QRectF& rect); void animationsFinished(); + void marginsChanged(QRectF margins); private: QChart* m_chart; diff --git a/src/qchart.cpp b/src/qchart.cpp index 00c7f29..9b70e8e 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -76,6 +76,7 @@ QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget( d_ptr->createConnections(); d_ptr->m_legend = new LegendScroller(this); d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); + connect(d_ptr->m_presenter, SIGNAL(marginsChanged(QRectF)), this, SIGNAL(marginsChanged(QRectF))); } /*! diff --git a/src/qchart.h b/src/qchart.h index cbd2836..581b475 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -37,6 +37,12 @@ struct QChartPrivate; class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget { Q_OBJECT + Q_PROPERTY(QChart::ChartTheme theme READ theme WRITE setTheme) + 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(QChart::AnimationOptions animationOptions READ animationOptions WRITE setAnimationOptions) + Q_PROPERTY(QRectF margins READ margins NOTIFY marginsChanged) Q_ENUMS(ChartTheme) Q_ENUMS(AnimationOption) @@ -107,6 +113,9 @@ public: QLegend* legend() const; QRectF margins() const; +Q_SIGNALS: + void marginsChanged(QRectF newMargins); + protected: void resizeEvent(QGraphicsSceneResizeEvent *event); diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml index c34b294..7cb7c5f 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml @@ -38,10 +38,26 @@ ChartView { XyPoint { x: 4.1; y: 3.3 } } - onVisibleChanged: console.log("chart.onVisibleChanged: " + series.visible); - onTitleColorChanged: console.log("chart.onTitleColorChanged: " + series.titleColor); + onVisibleChanged: console.log("chart.onVisibleChanged: " + visible); + onTitleColorChanged: console.log("chart.onTitleColorChanged: " + color); onBackgroundColorChanged: console.log("chart.onBackgroundColorChanged: " + series.backgroundColor); onDropShadowEnabledChanged: console.log("chart.onDropShadowEnabledChanged: " + enabled); + onTopMarginChanged: { + console.log("chart.onTopMarginChanged: " + margin); + marginVisualizer.opacity = 1.0; + } + onBottomMarginChanged: { + console.log("chart.onBottomMarginChanged: " + margin); + marginVisualizer.opacity = 1.0; + } + onLeftMarginChanged: { + console.log("chart.onLeftMarginChanged: " + margin); + marginVisualizer.opacity = 1.0; + } + onRightMarginChanged: { + console.log("chart.onRightMarginChanged: " + margin); + marginVisualizer.opacity = 1.0; + } legend.onVisibleChanged: console.log("legend.onVisibleChanged: " + series.legend.visible); legend.onBackgroundVisibleChanged: console.log("legend.onBackgroundVisibleChanged: " + visible); @@ -69,4 +85,21 @@ ChartView { axisY.onShadesBorderColorChanged: console.log("axisY.onShadesBorderColorChanged: " + color); axisY.onMinChanged: console.log("axisY.onMinChanged: " + min); axisY.onMaxChanged: console.log("axisY.onMaxChanged: " + max); + + + Rectangle { + id: marginVisualizer + color: "transparent" + border.color: "red" + anchors.fill: parent + anchors.topMargin: parent.topMargin + anchors.bottomMargin: parent.bottomMargin + anchors.leftMargin: parent.leftMargin + anchors.rightMargin: parent.rightMargin + opacity: 0.0 + onOpacityChanged: if (opacity == 1.0) opacity = 0.0; + Behavior on opacity { + NumberAnimation { duration: 800 } + } + } }