diff options
author | Kaj Grönholm <kaj.gronholm@qt.io> | 2024-05-08 08:36:43 +0300 |
---|---|---|
committer | Kaj Grönholm <kaj.gronholm@qt.io> | 2024-05-08 16:24:59 +0300 |
commit | be53d4ba6f4437ad9552da268f755f5e51f60ba4 (patch) | |
tree | ec0d370be7a0e57b89ebb1a42a2f1334def0bf34 | |
parent | 4f5b084cf30f8605595449d09942d4a9fde166c3 (diff) |
Implement custom bars with dynamic properties
Remove BarComponent class and instead use dynamic properties for
custom QML bar components.
Task-number: QTBUG-124507
Change-Id: Ib1c1f520ffa3770f6260b169bc6c748b4c796ab7
Reviewed-by: Tomi Korpipää <tomi.korpipaa@qt.io>
-rw-r--r-- | examples/graphs/2d/testbed/qml/testbed/CustomBars.qml | 6 | ||||
-rw-r--r-- | src/graphs2d/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/graphs2d/barchart/barcomponent.cpp | 158 | ||||
-rw-r--r-- | src/graphs2d/barchart/barcomponent_p.h | 73 | ||||
-rw-r--r-- | src/graphs2d/barchart/qbarseries.cpp | 84 | ||||
-rw-r--r-- | src/graphs2d/qsgrenderer/barsrenderer.cpp | 31 | ||||
-rw-r--r-- | tests/auto/qml2dtest/bars/tst_barseries.qml | 5 |
7 files changed, 111 insertions, 247 deletions
diff --git a/examples/graphs/2d/testbed/qml/testbed/CustomBars.qml b/examples/graphs/2d/testbed/qml/testbed/CustomBars.qml index 8c1241c..c59d842 100644 --- a/examples/graphs/2d/testbed/qml/testbed/CustomBars.qml +++ b/examples/graphs/2d/testbed/qml/testbed/CustomBars.qml @@ -98,8 +98,12 @@ Rectangle { selectable: true barWidth: 0.8 valuesMultiplier: barSeries.showAnimated - barComponent: BarComponent { + barComponent: Item { id: comp + property color barColor + property bool barSelected + property real barValue + property string barLabel anchors.alignWhenCentered: false opacity: barSeries.showAnimated BarImage { diff --git a/src/graphs2d/CMakeLists.txt b/src/graphs2d/CMakeLists.txt index 0833913..3005db6 100644 --- a/src/graphs2d/CMakeLists.txt +++ b/src/graphs2d/CMakeLists.txt @@ -8,7 +8,6 @@ qt_internal_extend_target(Graphs charthelpers_p.h barchart/qbarseries.cpp barchart/qbarseries.h barchart/qbarseries_p.h barchart/qbarset.cpp barchart/qbarset.h barchart/qbarset_p.h - barchart/barcomponent.cpp barchart/barcomponent_p.h barchart/qbarmodelmapper.cpp barchart/qbarmodelmapper.h barchart/qbarmodelmapper_p.h linechart/qlineseries.cpp linechart/qlineseries.h linechart/qlineseries_p.h piechart/qpieseries.cpp piechart/qpieseries.h piechart/qpieseries_p.h diff --git a/src/graphs2d/barchart/barcomponent.cpp b/src/graphs2d/barchart/barcomponent.cpp deleted file mode 100644 index fc7be14..0000000 --- a/src/graphs2d/barchart/barcomponent.cpp +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright (C) 2024 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only - -#include "barcomponent_p.h" - -QT_BEGIN_NAMESPACE - -/*! - \qmltype BarComponent - \inqmlmodule QtGraphs - \ingroup graphs_qml_2D - \inherits QQuickItem - \brief A base element for custom bar components. - - In bar graphs, bars are by default rendered as rectangles with specified fill color, - border color etc. By using this element, it is possible to render the bars using custom - QML elements. - - Here is an example of how to create bars that show \c backgroundImage, overlay the bar - with \c selectionImage once selected and render also the value of the bar into the center - of each bar. - - \badcode - BarSeries { - barComponent: BarComponent { - id: comp - BarImage { - id: backgroundImage - anchors.fill: parent - source: "images/bar_background.png" - } - BarImage { - id: selectionImage - anchors.fill: parent - source: "images/bar_selection.png" - visible: comp.selected - } - Text { - anchors.centerIn: parent - text: comp.barLabel - } - } - } - \endcode -*/ - -BarComponent::BarComponent(QQuickItem *parent) : - QQuickItem(parent) -{ -} - -/*! - \qmlproperty real BarComponent::barValue - The value of the bar. This value comes either from the \l{BarSet::values}. -*/ -qreal BarComponent::barValue() const -{ - return m_barValue; -} - -void BarComponent::setBarValue(qreal value) -{ - if (qFuzzyCompare(m_barValue, value)) - return; - m_barValue = value; - Q_EMIT barValueChanged(); -} - -/*! - \qmlproperty color BarComponent::barColor - The fill color of the bar. This value comes either from the \l SeriesTheme - or from \l{BarSet::color} if the \l BarSet overrides the color. -*/ -QColor BarComponent::barColor() const -{ - return m_barColor; -} - -void BarComponent::setBarColor(const QColor &color) -{ - if (m_barColor == color) - return; - m_barColor = color; - Q_EMIT barColorChanged(); -} - -/*! - \qmlproperty color BarComponent::barBorderColor - The border color of the bar. This value comes either from the \l SeriesTheme - or from \l{BarSet::borderColor} if the \l BarSet overrides the color. -*/ -QColor BarComponent::barBorderColor() const -{ - return m_barBorderColor; -} - -void BarComponent::setBarBorderColor(const QColor &color) -{ - if (m_barBorderColor == color) - return; - m_barBorderColor = color; - Q_EMIT barBorderColorChanged(); -} - -/*! - \qmlproperty real BarComponent::barBorderWidth - The width of the bar border. This value comes either from the \l SeriesTheme - or from \l{BarSet::borderWidth} if the \l BarSet overrides the width. -*/ -qreal BarComponent::barBorderWidth() const -{ - return m_barBorderWidth; -} - -void BarComponent::setBarBorderWidth(qreal width) -{ - if (qFuzzyCompare(m_barBorderWidth, width)) - return; - m_barBorderWidth = width; - Q_EMIT barBorderWidthChanged(); -} - -/*! - \qmlproperty string BarComponent::barLabel - The label of the bar. This value comes either from the \l{BarSet::label}. -*/ -QString BarComponent::barLabel() const -{ - return m_barLabel; -} - -void BarComponent::setBarLabel(const QString &label) -{ - if (m_barLabel == label) - return; - m_barLabel = label; - Q_EMIT barLabelChanged(); -} - -/*! - \qmlproperty bool BarComponent::barSelected - This value is true when the bar is selected, meaning that the bar index - is in \l{BarSet::selectedBars}. -*/ -bool BarComponent::barSelected() const -{ - return m_barSelected; -} - -void BarComponent::setBarSelected(bool selected) -{ - if (m_barSelected == selected) - return; - m_barSelected = selected; - Q_EMIT barSelectedChanged(); -} - -QT_END_NAMESPACE diff --git a/src/graphs2d/barchart/barcomponent_p.h b/src/graphs2d/barchart/barcomponent_p.h deleted file mode 100644 index 697f44c..0000000 --- a/src/graphs2d/barchart/barcomponent_p.h +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (C) 2024 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only - -#ifndef BARCOMPONENT_H -#define BARCOMPONENT_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the QtGraphs API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. - -#include <QtGraphs/qgraphsglobal.h> -#include <QQuickItem> -#include <QColor> -#include <QString> - -QT_BEGIN_NAMESPACE - -class Q_GRAPHS_EXPORT BarComponent : public QQuickItem -{ - Q_OBJECT - Q_PROPERTY(qreal barValue READ barValue NOTIFY barValueChanged) - Q_PROPERTY(QColor barColor READ barColor NOTIFY barColorChanged) - Q_PROPERTY(QColor barBorderColor READ barBorderColor NOTIFY barBorderColorChanged) - Q_PROPERTY(qreal barBorderWidth READ barBorderWidth NOTIFY barBorderWidthChanged) - Q_PROPERTY(QString barLabel READ barLabel NOTIFY barLabelChanged) - Q_PROPERTY(bool barSelected READ barSelected NOTIFY barSelectedChanged) - QML_ELEMENT - -public: - BarComponent(QQuickItem *parent = nullptr); - - qreal barValue() const; - QColor barColor() const; - QColor barBorderColor() const; - qreal barBorderWidth() const; - QString barLabel() const; - bool barSelected() const; - -Q_SIGNALS: - void barValueChanged(); - void barColorChanged(); - void barBorderColorChanged(); - void barBorderWidthChanged(); - void barLabelChanged(); - void barSelectedChanged(); - -private: - friend class BarsRenderer; - void setBarValue(qreal value); - void setBarColor(const QColor &color); - void setBarBorderColor(const QColor &color); - void setBarBorderWidth(qreal width); - void setBarLabel(const QString &label); - void setBarSelected(bool selected); - -private: - qreal m_barValue = 0; - QColor m_barColor; - QColor m_barBorderColor; - qreal m_barBorderWidth = 0; - QString m_barLabel; - bool m_barSelected = false; -}; - -QT_END_NAMESPACE - -#endif // BARCOMPONENT_H diff --git a/src/graphs2d/barchart/qbarseries.cpp b/src/graphs2d/barchart/qbarseries.cpp index a5637f8..458974f 100644 --- a/src/graphs2d/barchart/qbarseries.cpp +++ b/src/graphs2d/barchart/qbarseries.cpp @@ -236,20 +236,100 @@ QT_BEGIN_NAMESPACE Default value is 6. */ + /*! \property QBarSeries::barComponent \brief A custom QML component used for visualizing each of the bars. Instance of this component is created for each of the bar. - To get access into bars properties, use \l BarComponent. When this is not defined, a default rectangle visualization for bars is used. + + The dynamic properties available for this component are: + + \table + \header + \li Type + \li Name + \li Description + \row + \li QColor + \li barColor + \li The fill color of the bar. This value comes either from the \l QGraphsTheme + or from \l{QBarSet::color} if the \l QBarSet overrides the color. + \row + \li QColor + \li barBorderColor + \li The border color of the bar. This value comes either from the \l QGraphsTheme + or from \l{QBarSet::borderColor} if the \l QBarSet overrides the color. + \row + \li qreal + \li barBorderWidth + \li The width of the bar border. This value comes either from the \l QGraphsTheme + or from \l{QBarSet::borderWidth} if the \l QBarSet overrides the width. + \row + \li qreal + \li barValue + \li The value of the bar. This value comes from the \l{QBarSet::values}. + \row + \li QString + \li barLabel + \li The label of the bar. This value comes from the \l{QBarSet::label}. + \row + \li bool + \li barSelected + \li This value is true when the bar is selected, meaning that the bar index + is in \l{QBarSet::selectedBars}. + \endtable + + To use any of these, add property with the defined name into your custom component. + For example \c{"property color barColor"} and \c{"property real barValue"}. */ /*! \qmlproperty Component BarSeries::barComponent A custom QML component used for visualizing each of the bars. Instance of this component is created for each of the bar. - To get access into bars properties, use \l BarComponent. When this is not defined, a default rectangle visualization for bars is used. + + The dynamic properties available for this component are: + + \table + \header + \li Type + \li Name + \li Description + \row + \li color + \li barColor + \li The fill color of the bar. This value comes either from the \l GraphsTheme + or from \l{BarSet::color} if the \l BarSet overrides the color. + \row + \li color + \li barBorderColor + \li The border color of the bar. This value comes either from the \l GraphsTheme + or from \l{BarSet::borderColor} if the \l BarSet overrides the color. + \row + \li real + \li barBorderWidth + \li The width of the bar border. This value comes either from the \l GraphsTheme + or from \l{BarSet::borderWidth} if the \l BarSet overrides the width. + \row + \li real + \li barValue + \li The value of the bar. This value comes from the \l{BarSet::values}. + \row + \li string + \li barLabel + \li The label of the bar. This value comes from the \l{BarSet::label}. + \row + \li bool + \li barSelected + \li This value is true when the bar is selected, meaning that the bar index + is in \l{BarSet::selectedBars}. + \endtable + + To use any of these, add property with the defined name into your custom component. + For example \c{"property color barColor"} and \c{"property real barValue"}. */ + /*! \property QBarSeries::barSets \brief A list of sets added to the series. diff --git a/src/graphs2d/qsgrenderer/barsrenderer.cpp b/src/graphs2d/qsgrenderer/barsrenderer.cpp index 4bdc2fc..e0ea3af 100644 --- a/src/graphs2d/qsgrenderer/barsrenderer.cpp +++ b/src/graphs2d/qsgrenderer/barsrenderer.cpp @@ -6,10 +6,16 @@ #include <private/barsrenderer_p.h> #include <private/qbarseries_p.h> #include <private/qgraphsview_p.h> -#include <private/barcomponent_p.h> QT_BEGIN_NAMESPACE +static const char* TAG_BAR_COLOR = "barColor"; +static const char* TAG_BAR_BORDER_COLOR = "barBorderColor"; +static const char* TAG_BAR_BORDER_WIDTH = "barBorderWidth"; +static const char* TAG_BAR_SELECTED = "barSelected"; +static const char* TAG_BAR_VALUE = "barValue"; +static const char* TAG_BAR_LABEL = "barLabel"; + BarsRenderer::BarsRenderer(QQuickItem *parent) : QQuickItem(parent) { @@ -128,16 +134,19 @@ void BarsRenderer::updateComponents(QBarSeries *series) barItem->setY(d.rect.y()); barItem->setWidth(d.rect.width()); barItem->setHeight(d.rect.height()); - auto barComponent = qobject_cast<BarComponent *>(barItem); - if (barComponent) { - barComponent->setBarColor(d.color); - barComponent->setBarBorderColor(d.borderColor); - barComponent->setBarBorderWidth(d.borderWidth); - barComponent->setBarSelected(d.isSelected); - barComponent->setBarValue(d.value); - barComponent->setBarLabel(d.label); - } - barItem->update(); + // Check for specific dynamic properties + if (barItem->property(TAG_BAR_COLOR).isValid()) + barItem->setProperty(TAG_BAR_COLOR, d.color); + if (barItem->property(TAG_BAR_BORDER_COLOR).isValid()) + barItem->setProperty(TAG_BAR_BORDER_COLOR, d.borderColor); + if (barItem->property(TAG_BAR_BORDER_WIDTH).isValid()) + barItem->setProperty(TAG_BAR_BORDER_WIDTH, d.borderWidth); + if (barItem->property(TAG_BAR_SELECTED).isValid()) + barItem->setProperty(TAG_BAR_SELECTED, d.isSelected); + if (barItem->property(TAG_BAR_VALUE).isValid()) + barItem->setProperty(TAG_BAR_VALUE, d.value); + if (barItem->property(TAG_BAR_LABEL).isValid()) + barItem->setProperty(TAG_BAR_LABEL, d.label); } barIndex++; } diff --git a/tests/auto/qml2dtest/bars/tst_barseries.qml b/tests/auto/qml2dtest/bars/tst_barseries.qml index 2b504f3..73472d6 100644 --- a/tests/auto/qml2dtest/bars/tst_barseries.qml +++ b/tests/auto/qml2dtest/bars/tst_barseries.qml @@ -37,8 +37,11 @@ Item { Component { id: customBarComponent - BarComponent { + Item { id: comp + property color barColor + property color barBorderColor + property real barBorderWidth Rectangle { anchors.fill: parent color: comp.barColor |