From aa62563de463f59b264087e1b58764ff372270b6 2012-06-13 13:12:21 From: Tero Ahola Date: 2012-06-13 13:12:21 Subject: [PATCH] Adding legend properties --- diff --git a/doc/src/qml.qdoc b/doc/src/qml.qdoc index 6682bcd..64d9380 100644 --- a/doc/src/qml.qdoc +++ b/doc/src/qml.qdoc @@ -10,6 +10,7 @@
  • ChartView
  • PieSeries
  • Axis
  • +
  • Legend
  • \endraw */ diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index e61c80a..219e183 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -60,10 +60,83 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE \sa QChart */ +/*! + \qmlclass Legend QLegend + \brief Legend is part of QtCommercial Chart QML API. + + Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when + series have been changed. Legend is referenced via ChartView class. For example: + \code + ChartView { + legend.visible: true + legend.alignment: Qt.AlignBottom + // Add a few series... + } + \endcode + + \image examples_percentbarchart_legend.png +*/ + +/*! + \property QLegend::alignment + \brief The alignment of the legend. + + Legend paints on the defined position in the chart. The following alignments are supported: + Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined. +*/ +/*! + \qmlproperty Qt.Alignment Legend::alignment + \brief The alignment of the legend. + + Legend paints on the defined position in the chart. The following alignments are supported: + Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined. +*/ + +/*! + \property QLegend::backgroundVisible + Whether the legend background is visible or not. +*/ +/*! + \qmlproperty bool Legend::backgroundVisible + Whether the legend background is visible or not. +*/ + +/*! + \property QLegend::color + The color of the legend, i.e. the background color. +*/ +/*! + \qmlproperty color Legend::color + The color of the legend, i.e. the background color. +*/ + +/*! + \property QLegend::borderColor + The border color of the legend, i.e. the line color. +*/ +/*! + \qmlproperty color Legend::borderColor + The border color of the legend, i.e. the line color. +*/ /*! - \fn void QLegend::alignmentChanged() - Emitted when the alignment of the legend changes. + \fn void QLegend::alignmentChanged(Qt::Alignment) + Emitted when the \a alignment of the legend changes. +*/ + +/*! + \fn void QLegend::backgroundVisibleChanged(bool) + The visibility of the legend background changed to \a visible. +*/ + +/*! + \fn void QLegend::colorChanged(QColor) + The color of the legend background changed to \a color. +*/ + +/*! + \fn void QLegend::borderColorChanged(QColor) + The border color of the legend background changed to \a color. */ /*! @@ -141,6 +214,21 @@ QBrush QLegend::brush() const return d_ptr->m_brush; } +void QLegend::setColor(QColor color) +{ + QBrush b = d_ptr->m_brush; + if (b.color() != color) { + b.setColor(color); + setBrush(b); + emit colorChanged(color); + } +} + +QColor QLegend::color() +{ + return d_ptr->m_brush.color(); +} + /*! Sets the \a pen of legend. Pen affects the legend borders. */ @@ -161,30 +249,30 @@ QPen QLegend::pen() const return d_ptr->m_pen; } -/*! - \property QLegend::alignment - \brief The alignment of the legend. -*/ +void QLegend::setBorderColor(QColor color) +{ + QPen p = d_ptr->m_pen; + if (p.color() != color) { + p.setColor(color); + setPen(p); + emit borderColorChanged(color); + } +} -/*! - Sets the \a alignment for legend. Legend paints on the defined position in chart. The following alignments are - supported: Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result - is undefined. +QColor QLegend::borderColor() +{ + return d_ptr->m_pen.color(); +} - \sa QLegend::Alignment - */ void QLegend::setAlignment(Qt::Alignment alignment) { if(d_ptr->m_alignment!=alignment) { d_ptr->m_alignment = alignment; d_ptr->updateLayout(); - alignmentChanged(); + alignmentChanged(alignment); } } -/*! - Returns the preferred layout for legend - */ Qt::Alignment QLegend::alignment() const { return d_ptr->m_alignment; @@ -235,10 +323,10 @@ QPointF QLegend::offset() const */ void QLegend::setBackgroundVisible(bool visible) { - if(d_ptr->m_backgroundVisible!=visible) - { - d_ptr->m_backgroundVisible=visible; + if(d_ptr->m_backgroundVisible != visible) { + d_ptr->m_backgroundVisible = visible; update(); + emit backgroundVisibleChanged(visible); } } diff --git a/src/legend/qlegend.h b/src/legend/qlegend.h index 94fc366..d62f925 100644 --- a/src/legend/qlegend.h +++ b/src/legend/qlegend.h @@ -44,6 +44,9 @@ class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget { Q_OBJECT Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged) + Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) + Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged) private: explicit QLegend(QChart *chart); @@ -56,9 +59,13 @@ public: void setBrush(const QBrush &brush); QBrush brush() const; + void setColor(QColor color); + QColor color(); void setPen(const QPen &pen); QPen pen() const; + void setBorderColor(QColor color); + QColor borderColor(); void setAlignment(Qt::Alignment alignment); Qt::Alignment alignment() const; @@ -82,11 +89,14 @@ protected: void showEvent(QShowEvent *event); Q_SIGNALS: - void alignmentChanged(); + void alignmentChanged(Qt::Alignment alignment); + void backgroundVisibleChanged(bool visible); + void colorChanged(QColor color); + void borderColorChanged(QColor color); private: QScopedPointer d_ptr; - Q_DISABLE_COPY(QLegend); + Q_DISABLE_COPY(QLegend) friend class LegendScroller; }; diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml index 069f274..6002ceb 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor.qml @@ -48,8 +48,11 @@ Flow { Connections { id: legendConnections ignoreUnknownSignals: true - onAlignmentChanged: console.log("legend.onAlignmentChanged: " + series.legend.alignment); + onAlignmentChanged: console.log("legend.onAlignmentChanged: " + alignment); onVisibleChanged: console.log("legend.onVisibleChanged: " + series.legend.visible); + onBackgroundVisibleChanged: console.log("legend.onBackgroundVisibleChanged: " + visible); + onColorChanged: console.log("legend.onColorChanged: " + color); + onBorderColorChanged: console.log("legend.onBorderColorChanged: " + color); } Connections { @@ -113,6 +116,22 @@ Flow { onClicked: series.backgroundColor = main.nextColor(); } Button { + text: "legend visible" + onClicked: series.legend.visible = !series.legend.visible; + } + Button { + text: "legend bckgrd visible" + onClicked: series.legend.backgroundVisible = !series.legend.backgroundVisible; + } + Button { + text: "legend color" + onClicked: series.legend.color = main.nextColor(); + } + Button { + text: "legend border color" + onClicked: series.legend.borderColor = main.nextColor(); + } + Button { text: "legend top" onClicked: series.legend.alignment ^= Qt.AlignTop; } @@ -129,10 +148,6 @@ Flow { onClicked: series.legend.alignment ^= Qt.AlignRight; } Button { - text: "legend visible" - onClicked: series.legend.visible = !series.legend.visible; - } - Button { text: "axis X nice nmb" onClicked: series.axisX.niceNumbersEnabled = !series.axisX.niceNumbersEnabled; }