From 6cfc1dcc2969e0e522aa5777eef97e0eadc967e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomi=20Korpip=C3=A4=C3=A4?= Date: Wed, 11 Dec 2013 13:12:18 +0200 Subject: Multiseries support for theme colors and gradients, part 2 Task-number: QTRD-2611 Change-Id: I280f3b7c22a212ed46813bd28c639d255107f9f3 Reviewed-by: Miikka Heikkinen --- src/datavisualizationqml2/declarativetheme.cpp | 210 +++++++++++++++++++------ 1 file changed, 164 insertions(+), 46 deletions(-) (limited to 'src/datavisualizationqml2/declarativetheme.cpp') diff --git a/src/datavisualizationqml2/declarativetheme.cpp b/src/datavisualizationqml2/declarativetheme.cpp index 263fa62e..6c804397 100644 --- a/src/datavisualizationqml2/declarativetheme.cpp +++ b/src/datavisualizationqml2/declarativetheme.cpp @@ -22,9 +22,12 @@ QT_DATAVISUALIZATION_BEGIN_NAMESPACE DeclarativeTheme3D::DeclarativeTheme3D(QObject *parent) : Q3DTheme(parent), + m_colors(QList()), m_gradients(QList()), m_singleHLGradient(0), - m_multiHLGradient(0) + m_multiHLGradient(0), + m_dummyGradients(false), + m_dummyColors(false) { } @@ -34,8 +37,8 @@ DeclarativeTheme3D::~DeclarativeTheme3D() QQmlListProperty DeclarativeTheme3D::seriesChildren() { - return QQmlListProperty(this, this, &DeclarativeTheme3D::appendSeriesChildren - , 0, 0, 0); + return QQmlListProperty(this, this, &DeclarativeTheme3D::appendSeriesChildren, + 0, 0, 0); } void DeclarativeTheme3D::appendSeriesChildren(QQmlListProperty *list, QObject *element) @@ -45,6 +48,44 @@ void DeclarativeTheme3D::appendSeriesChildren(QQmlListProperty *list, Q // Nothing to do, seriesChildren is there only to enable scoping gradient items in Theme3D item. } +void DeclarativeTheme3D::handleBaseColorUpdate() +{ + int colorCount = m_colors.size(); + int changed = 0; + // Check which one changed + DeclarativeColor *color = qobject_cast(QObject::sender()); + for (int i = 0; i < colorCount; i++) { + if (color == m_colors.at(i)) { + changed = i; + break; + } + } + // Update the changed one from the list + QList list = Q3DTheme::baseColors(); + list[changed] = m_colors.at(changed)->color(); + // Set the changed list + Q3DTheme::setBaseColors(list); +} + +void DeclarativeTheme3D::handleBaseGradientUpdate() +{ + int gradientCount = m_gradients.size(); + int changed = 0; + // Check which one changed + ColorGradient *gradient = qobject_cast(QObject::sender()); + for (int i = 0; i < gradientCount; i++) { + if (gradient == m_gradients.at(i)) { + changed = i; + break; + } + } + // Update the changed one from the list + QList list = Q3DTheme::baseGradients(); + list[changed] = convertGradient(gradient); + // Set the changed list + Q3DTheme::setBaseGradients(list); +} + void DeclarativeTheme3D::handleSingleHLGradientUpdate() { if (m_singleHLGradient) @@ -145,70 +186,147 @@ QLinearGradient DeclarativeTheme3D::convertGradient(ColorGradient *gradient) return newGradient; } -//void DeclarativeTheme3D::addColor(const DeclarativeColor &color) -//{ -// QList list = Q3DTheme::baseColors(); -// list.append(color); -// Q3DTheme::setBaseColors(list); -//} +ColorGradient *DeclarativeTheme3D::convertGradient(const QLinearGradient &gradient) +{ + ColorGradient *newGradient = new ColorGradient(this); + QGradientStops stops = gradient.stops(); + ColorGradientStop *qmlstop; + + // Convert stops + for (int i = 0; i < stops.size(); i++) { + qmlstop = new ColorGradientStop(newGradient); + qmlstop->setColor(stops.at(i).second); + qmlstop->setPosition(stops.at(i).first); + newGradient->m_stops.append(qmlstop); + } + + return newGradient; +} + +void DeclarativeTheme3D::addColor(DeclarativeColor *color) +{ + clearDummyColors(); + m_colors.append(color); + connect(color, &DeclarativeColor::colorChanged, + this, &DeclarativeTheme3D::handleBaseColorUpdate); + QList list = Q3DTheme::baseColors(); + list.append(color->color()); + Q3DTheme::setBaseColors(list); +} -//QList DeclarativeTheme3D::colorList() const -//{ -// return Q3DTheme::baseColors(); -//} +QList DeclarativeTheme3D::colorList() +{ + if (m_colors.isEmpty()) { + // Create dummy ThemeColors from theme's gradients + m_dummyColors = true; + QList list = Q3DTheme::baseColors(); + foreach (QColor item, list) { + DeclarativeColor *color = new DeclarativeColor(this); + color->setColor(item); + m_colors.append(color); + connect(color, &DeclarativeColor::colorChanged, + this, &DeclarativeTheme3D::handleBaseColorUpdate); + } + } + return m_colors; +} + +void DeclarativeTheme3D::clearColors() +{ + clearDummyColors(); + foreach (DeclarativeColor *item, m_colors) + disconnect(item, 0, this, 0); + m_colors.clear(); + Q3DTheme::setBaseColors(QList()); +} -//void DeclarativeTheme3D::clearColors() -//{ -// Q3DTheme::setBaseColors(QList()); -//} +void DeclarativeTheme3D::clearDummyColors() +{ + if (m_dummyColors) { + foreach (DeclarativeColor *item, m_colors) + delete item; + m_colors.clear(); + m_dummyColors = false; + } +} void DeclarativeTheme3D::addGradient(ColorGradient *gradient) { + clearDummyGradients(); m_gradients.append(gradient); + connect(gradient, &ColorGradient::updated, + this, &DeclarativeTheme3D::handleBaseGradientUpdate); QList list = Q3DTheme::baseGradients(); list.append(convertGradient(gradient)); Q3DTheme::setBaseGradients(list); } -QList DeclarativeTheme3D::gradientList() const +QList DeclarativeTheme3D::gradientList() { + if (m_gradients.isEmpty()) { + // Create dummy ColorGradients from theme's gradients + m_dummyGradients = true; + QList list = Q3DTheme::baseGradients(); + foreach (QLinearGradient item, list) { + ColorGradient *gradient = convertGradient(item); + m_gradients.append(gradient); + connect(gradient, &ColorGradient::updated, + this, &DeclarativeTheme3D::handleBaseGradientUpdate); + } + } + return m_gradients; } void DeclarativeTheme3D::clearGradients() { + clearDummyGradients(); + foreach (ColorGradient *item, m_gradients) + disconnect(item, 0, this, 0); m_gradients.clear(); Q3DTheme::setBaseGradients(QList()); } -//QQmlListProperty DeclarativeTheme3D::baseColors() -//{ -// return QQmlListProperty(this, this, -// &DeclarativeTheme3D::appendBaseColorsFunc, -// &DeclarativeTheme3D::countBaseColorsFunc, -// &DeclarativeTheme3D::atBaseColorsFunc, -// &DeclarativeTheme3D::clearBaseColorsFunc); -//} - -//void DeclarativeTheme3D::appendBaseColorsFunc(QQmlListProperty *list, DeclarativeColor *color) -//{ -// reinterpret_cast(list->data)->addColor(*color); -//} - -//int DeclarativeTheme3D::countBaseColorsFunc(QQmlListProperty *list) -//{ -// return reinterpret_cast(list->data)->colorList().size(); -//} - -//DeclarativeColor *DeclarativeTheme3D::atBaseColorsFunc(QQmlListProperty *list, int index) -//{ -// return &(reinterpret_cast(list->data)->colorList()[index]); -//} - -//void DeclarativeTheme3D::clearBaseColorsFunc(QQmlListProperty *list) -//{ -// reinterpret_cast(list->data)->clearGradients(); -//} +void DeclarativeTheme3D::clearDummyGradients() +{ + if (m_dummyGradients) { + foreach (ColorGradient *item, m_gradients) + delete item; + m_gradients.clear(); + m_dummyGradients = false; + } +} + +QQmlListProperty DeclarativeTheme3D::baseColors() +{ + return QQmlListProperty(this, this, + &DeclarativeTheme3D::appendBaseColorsFunc, + &DeclarativeTheme3D::countBaseColorsFunc, + &DeclarativeTheme3D::atBaseColorsFunc, + &DeclarativeTheme3D::clearBaseColorsFunc); +} + +void DeclarativeTheme3D::appendBaseColorsFunc(QQmlListProperty *list, + DeclarativeColor *color) +{ + reinterpret_cast(list->data)->addColor(color); +} + +int DeclarativeTheme3D::countBaseColorsFunc(QQmlListProperty *list) +{ + return reinterpret_cast(list->data)->colorList().size(); +} + +DeclarativeColor *DeclarativeTheme3D::atBaseColorsFunc(QQmlListProperty *list, + int index) +{ + return reinterpret_cast(list->data)->colorList().at(index); +} + +void DeclarativeTheme3D::clearBaseColorsFunc(QQmlListProperty *list) +{ + reinterpret_cast(list->data)->clearColors(); +} QQmlListProperty DeclarativeTheme3D::baseGradients() { -- cgit v1.2.3