From 23bf915495bcbba30c874b0951b4d23df0f5acf3 2014-04-04 05:41:30 From: Titta Heikkala Date: 2014-04-04 05:41:30 Subject: [PATCH] Add possibility to set markers in legend in reverse order Added new property for legend which defines whether the markers in legend are shown in reverse order or not. Task-number: QTRD-2512 Change-Id: Ia6e6551be74a183c9f544d7cb9fff679150f4d7e Reviewed-by: Miikka Heikkinen --- diff --git a/plugins/quick2/plugins.qmltypes b/plugins/quick2/plugins.qmltypes index cb0719b..8f715cb 100644 --- a/plugins/quick2/plugins.qmltypes +++ b/plugins/quick2/plugins.qmltypes @@ -1831,6 +1831,7 @@ Module { Property { name: "borderColor"; type: "QColor" } Property { name: "font"; type: "QFont" } Property { name: "labelColor"; type: "QColor" } + Property { name: "reverseMarkers"; type: "bool" } Signal { name: "backgroundVisibleChanged" Parameter { name: "visible"; type: "bool" } @@ -1851,6 +1852,10 @@ Module { name: "labelColorChanged" Parameter { name: "color"; type: "QColor" } } + Signal { + name: "reverseMarkersChanged" + Parameter { name: "reverseMarkers"; type: "bool" } + } } Component { name: "QtCommercialChart::QLineSeries"; prototype: "QtCommercialChart::QXYSeries" } Component { diff --git a/src/legend/legendlayout.cpp b/src/legend/legendlayout.cpp index dfabb2c..aae18e2 100644 --- a/src/legend/legendlayout.cpp +++ b/src/legend/legendlayout.cpp @@ -204,7 +204,14 @@ void LegendLayout::setAttachedGeometry(const QRectF &rect) } QPointF point(0,0); - foreach (QLegendMarker *marker, m_legend->d_ptr->markers()) { + + int markerCount = m_legend->d_ptr->markers().count(); + for (int i = 0; i < markerCount; i++) { + QLegendMarker *marker; + if (m_legend->d_ptr->m_reverseMarkers) + marker = m_legend->d_ptr->markers().at(markerCount - 1 - i); + else + marker = m_legend->d_ptr->markers().at(i); LegendMarkerItem *item = marker->d_ptr->item(); if (item->isVisible()) { QRectF itemRect = geometry; @@ -238,7 +245,13 @@ void LegendLayout::setAttachedGeometry(const QRectF &rect) case Qt::AlignLeft: case Qt::AlignRight: { QPointF point(0,0); - foreach (QLegendMarker *marker, m_legend->d_ptr->markers()) { + int markerCount = m_legend->d_ptr->markers().count(); + for (int i = 0; i < markerCount; i++) { + QLegendMarker *marker; + if (m_legend->d_ptr->m_reverseMarkers) + marker = m_legend->d_ptr->markers().at(markerCount - 1 - i); + else + marker = m_legend->d_ptr->markers().at(i); LegendMarkerItem *item = marker->d_ptr->item(); if (item->isVisible()) { item->setGeometry(geometry); diff --git a/src/legend/qlegend.cpp b/src/legend/qlegend.cpp index 199409e..0cba1bd 100644 --- a/src/legend/qlegend.cpp +++ b/src/legend/qlegend.cpp @@ -129,6 +129,15 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! + \property QLegend::reverseMarkers + Whether reverse order is used for the markers in legend or not. False by default. +*/ +/*! + \qmlproperty bool Legend::reverseMarkers + Whether reverse order is used for the markers in legend or not. False by default. +*/ + +/*! \fn void QLegend::backgroundVisibleChanged(bool) The visibility of the legend background changed to \a visible. */ @@ -391,6 +400,20 @@ QList QLegend::markers(QAbstractSeries *series) const return d_ptr->markers(series); } +bool QLegend::reverseMarkers() +{ + return d_ptr->m_reverseMarkers; +} + +void QLegend::setReverseMarkers(bool reverseMarkers) +{ + if (d_ptr->m_reverseMarkers != reverseMarkers) { + d_ptr->m_reverseMarkers = reverseMarkers; + layout()->invalidate(); + emit reverseMarkersChanged(reverseMarkers); + } +} + /*! \internal \a event see QGraphicsWidget for details */ @@ -425,7 +448,8 @@ QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend m_labelBrush(QBrush()), m_diameter(5), m_attachedToChart(true), - m_backgroundVisible(false) + m_backgroundVisible(false), + m_reverseMarkers(false) { m_items->setHandlesChildEvents(false); } diff --git a/src/legend/qlegend.h b/src/legend/qlegend.h index 81fc9d1..995a03f 100644 --- a/src/legend/qlegend.h +++ b/src/legend/qlegend.h @@ -42,6 +42,7 @@ class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged) + Q_PROPERTY(bool reverseMarkers READ reverseMarkers WRITE setReverseMarkers NOTIFY reverseMarkersChanged) private: explicit QLegend(QChart *chart); @@ -81,6 +82,9 @@ public: QList markers(QAbstractSeries *series = 0) const; + bool reverseMarkers(); + void setReverseMarkers(bool reverseMarkers = true); + protected: void hideEvent(QHideEvent *event); void showEvent(QShowEvent *event); @@ -91,6 +95,7 @@ Q_SIGNALS: void borderColorChanged(QColor color); void fontChanged(QFont font); void labelColorChanged(QColor color); + void reverseMarkersChanged(bool reverseMarkers); private: QScopedPointer d_ptr; diff --git a/src/legend/qlegend_p.h b/src/legend/qlegend_p.h index 0a23f46..6d7ce8e 100644 --- a/src/legend/qlegend_p.h +++ b/src/legend/qlegend_p.h @@ -82,6 +82,7 @@ private: qreal m_diameter; bool m_attachedToChart; bool m_backgroundVisible; + bool m_reverseMarkers; QList m_markers; QList m_series; diff --git a/tests/auto/qchart/tst_qchart.cpp b/tests/auto/qchart/tst_qchart.cpp index 6035fd7..c0a8eee 100644 --- a/tests/auto/qchart/tst_qchart.cpp +++ b/tests/auto/qchart/tst_qchart.cpp @@ -420,6 +420,7 @@ void tst_QChart::legend() { QLegend *legend = m_chart->legend(); QVERIFY(legend); + QVERIFY(!m_chart->legend()->reverseMarkers()); // Colors related signals QSignalSpy colorSpy(legend, SIGNAL(colorChanged(QColor))); @@ -456,6 +457,13 @@ void tst_QChart::legend() f.setBold(!f.bold()); legend->setFont(f); QCOMPARE(fontSpy.count(), 1); + + // reverseMarkersChanged + QSignalSpy reverseMarkersSpy(legend, SIGNAL(reverseMarkersChanged(bool))); + QCOMPARE(reverseMarkersSpy.count(), 0); + legend->setReverseMarkers(); + QCOMPARE(reverseMarkersSpy.count(), 1); + QVERIFY(legend->reverseMarkers()); } void tst_QChart::plotArea_data() diff --git a/tests/auto/qml-qtquicktest/tst_chartview.qml b/tests/auto/qml-qtquicktest/tst_chartview.qml index 11e4e19..bcfb605 100644 --- a/tests/auto/qml-qtquicktest/tst_chartview.qml +++ b/tests/auto/qml-qtquicktest/tst_chartview.qml @@ -55,6 +55,7 @@ Rectangle { compare(chartView.legend.backgroundVisible, false, "ChartView.legend.backgroundVisible"); verify(chartView.legend.borderColor != undefined, "ChartView.legend.borderColor"); verify(chartView.legend.color != undefined, "ChartView.legend.color"); + compare(chartView.legend.reverseMarkers, false, "ChartView.legend.reverseMarkers"); // Legend font compare(chartView.legend.font.bold, false, "ChartView.legend.font.bold"); compare(chartView.legend.font.capitalization, Font.MixedCase, "ChartView.legend.font.capitalization"); diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml index 983b2d2..e8cc095 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml @@ -52,6 +52,8 @@ ChartView { legend.onColorChanged: console.log("legend.onColorChanged: " + color); legend.onBorderColorChanged: console.log("legend.onBorderColorChanged: " + color); legend.onLabelColorChanged: console.log("legend.onLabelColorChanged: " + color); + legend.onReverseMarkersChanged: console.log("legend.onReverseMarkersChanged: " + + chart.legend.reverseMarkers) margins.onTopChanged: console.log("chart.margins.onTopChanged: " + top ); margins.onBottomChanged: console.log("chart.margins.onBottomChanged: " + bottom); margins.onLeftChanged: console.log("chart.margins.onLeftChanged: " + left); diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorLegend.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorLegend.qml index 25d8275..c4db0e3 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorLegend.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditorLegend.qml @@ -65,6 +65,10 @@ Row { text: "legend right" onClicked: chartLegend.alignment = Qt.AlignRight; } + Button { + text: "legend use reverse order" + onClicked: chartLegend.reverseMarkers = !chartLegend.reverseMarkers; + } } FontEditor { diff --git a/tests/quick2chartproperties/qml/quick2chartproperties/Chart.qml b/tests/quick2chartproperties/qml/quick2chartproperties/Chart.qml index a4ba0f2..0dc1721 100644 --- a/tests/quick2chartproperties/qml/quick2chartproperties/Chart.qml +++ b/tests/quick2chartproperties/qml/quick2chartproperties/Chart.qml @@ -52,6 +52,8 @@ ChartView { legend.onColorChanged: console.log("legend.onColorChanged: " + color); legend.onBorderColorChanged: console.log("legend.onBorderColorChanged: " + color); legend.onLabelColorChanged: console.log("legend.onLabelColorChanged: " + color); + legend.onReverseMarkersChanged: console.log("legend.onReverseMarkersChanged: " + + chart.legend.reverseMarkers) margins.onTopChanged: console.log("chart.margins.onTopChanged: " + top ); margins.onBottomChanged: console.log("chart.margins.onBottomChanged: " + bottom); margins.onLeftChanged: console.log("chart.margins.onLeftChanged: " + left); diff --git a/tests/quick2chartproperties/qml/quick2chartproperties/ChartEditorLegend.qml b/tests/quick2chartproperties/qml/quick2chartproperties/ChartEditorLegend.qml index be22da9..063cbf6 100644 --- a/tests/quick2chartproperties/qml/quick2chartproperties/ChartEditorLegend.qml +++ b/tests/quick2chartproperties/qml/quick2chartproperties/ChartEditorLegend.qml @@ -65,6 +65,10 @@ Row { text: "legend right" onClicked: chartLegend.alignment = Qt.AlignRight; } + Button { + text: "legend use reverse order" + onClicked: chartLegend.reverseMarkers = !chartLegend.reverseMarkers; + } } FontEditor {