From 0c92c8aaf7708a410d508cd0d5100608c8933712 Mon Sep 17 00:00:00 2001 From: Henning Gruendl Date: Wed, 5 Jun 2024 18:46:12 +0200 Subject: [PATCH] QmlDesigner: Show canvas background in states view * Forward the auxiliary property formeditorColorProperty to the states editor delegate * Add functionality to enabled/disable certain colors in the form editor canvas background combo box * Disable context image property in the combo box when it isn't set * When context image is set as the canvas background make the states background transparent * Fix Item as root being rendered with white background in Qt5NodeInstanceServer::grabRenderControl Task-number: QDS-12914 Change-Id: Ie2850986f4a54c6e03f33527d8308d01ba16884e Reviewed-by: Thomas Hartmann --- .../qmldesigner/stateseditor/Main.qml | 2 ++ .../stateseditor/StateThumbnail.qml | 4 +++- .../formeditor/backgroundaction.cpp | 12 +++++++++++ .../components/formeditor/backgroundaction.h | 1 + .../components/formeditor/formeditorview.cpp | 6 ++---- .../formeditor/formeditorwidget.cpp | 19 +++++++++--------- .../stateseditor/stateseditormodel.cpp | 15 ++++++++++++++ .../stateseditor/stateseditormodel.h | 8 ++++++++ .../stateseditor/stateseditorview.cpp | 20 +++++++++++++++++++ .../stateseditor/stateseditorview.h | 6 +++--- src/plugins/qmldesigner/documentmanager.cpp | 3 ++- .../instances/qt5nodeinstanceserver.cpp | 9 ++++++--- 12 files changed, 83 insertions(+), 22 deletions(-) diff --git a/share/qtcreator/qmldesigner/stateseditor/Main.qml b/share/qtcreator/qmldesigner/stateseditor/Main.qml index 8774f3a02b2..9184c870ac7 100644 --- a/share/qtcreator/qmldesigner/stateseditor/Main.qml +++ b/share/qtcreator/qmldesigner/stateseditor/Main.qml @@ -562,6 +562,7 @@ Rectangle { isChecked: root.currentStateInternalId === 0 thumbnailImageSource: StatesEditorBackend.statesEditorModel.baseState.stateImageSource ?? "" // TODO Get rid of the QVariantMap isTiny: root.tinyMode + backgroundColor: StatesEditorBackend.statesEditorModel.backgroundColor onFocusSignal: root.currentStateInternalId = 0 onDefaultClicked: StatesEditorBackend.statesEditorModel.resetDefaultState() @@ -830,6 +831,7 @@ Rectangle { visualIndex: delegateRoot.visualIndex internalNodeId: delegateRoot.internalNodeId isTiny: root.tinyMode + backgroundColor: StatesEditorBackend.statesEditorModel.backgroundColor hasExtend: delegateRoot.hasExtend extendString: delegateRoot.extendString diff --git a/share/qtcreator/qmldesigner/stateseditor/StateThumbnail.qml b/share/qtcreator/qmldesigner/stateseditor/StateThumbnail.qml index 781ddc4601f..703bc3e7ed0 100644 --- a/share/qtcreator/qmldesigner/stateseditor/StateThumbnail.qml +++ b/share/qtcreator/qmldesigner/stateseditor/StateThumbnail.qml @@ -65,6 +65,8 @@ Item { property int internalNodeId + property color backgroundColor: "transparent" + signal focusSignal signal defaultClicked signal clone @@ -241,7 +243,7 @@ Item { (stateBackground.innerHeight - thumbnailImage.paintedHeight) / 2) - StudioTheme.Values.border width: Math.round(thumbnailImage.paintedWidth) + 2 * StudioTheme.Values.border height: Math.round(thumbnailImage.paintedHeight) + 2 * StudioTheme.Values.border - color: "transparent" + color: root.backgroundColor border.width: StudioTheme.Values.border border.color: StudioTheme.Values.themeStatePreviewOutline } diff --git a/src/plugins/qmldesigner/components/formeditor/backgroundaction.cpp b/src/plugins/qmldesigner/components/formeditor/backgroundaction.cpp index 9d9139cf017..4a4bce6dd71 100644 --- a/src/plugins/qmldesigner/components/formeditor/backgroundaction.cpp +++ b/src/plugins/qmldesigner/components/formeditor/backgroundaction.cpp @@ -10,6 +10,8 @@ #include #include +#include + namespace QmlDesigner { BackgroundAction::BackgroundAction(QObject *parent) : @@ -21,7 +23,17 @@ void BackgroundAction::setColor(const QColor &color) { if (m_comboBox) m_comboBox->setCurrentIndex(colors().indexOf(color)); +} +void BackgroundAction::setColorEnabled(const QColor &color, bool enable) +{ + if (!m_comboBox) + return; + + QStandardItemModel *model = qobject_cast(m_comboBox->model()); + if (QStandardItem *item = model->item(colors().indexOf(color))) + item->setFlags(enable ? item->flags() | Qt::ItemIsEnabled + : item->flags() & ~Qt::ItemIsEnabled); } QIcon iconForColor(const QColor &color) { diff --git a/src/plugins/qmldesigner/components/formeditor/backgroundaction.h b/src/plugins/qmldesigner/components/formeditor/backgroundaction.h index c6eeb212fe4..604a9aae720 100644 --- a/src/plugins/qmldesigner/components/formeditor/backgroundaction.h +++ b/src/plugins/qmldesigner/components/formeditor/backgroundaction.h @@ -20,6 +20,7 @@ public: explicit BackgroundAction(QObject *parent); void setColor(const QColor &color); + void setColorEnabled(const QColor &color, bool enable); signals: void backgroundChanged(const QColor &color); diff --git a/src/plugins/qmldesigner/components/formeditor/formeditorview.cpp b/src/plugins/qmldesigner/components/formeditor/formeditorview.cpp index eaf935d4c9d..54b98720c64 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditorview.cpp +++ b/src/plugins/qmldesigner/components/formeditor/formeditorview.cpp @@ -1000,10 +1000,8 @@ void FormEditorView::setupRootItemSize() formEditorWidget()->setRootItemRect(rootRect); formEditorWidget()->centerScene(); - auto contextImage = rootModelNode().auxiliaryData(contextImageProperty); - - if (contextImage) - m_formEditorWidget->setBackgoundImage(contextImage.value().value()); + if (auto contextImage = rootModelNode().auxiliaryData(contextImageProperty)) + formEditorWidget()->setBackgoundImage(contextImage.value().value()); } } diff --git a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp index 46c7a5cc8f4..89f65febd6c 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp +++ b/src/plugins/qmldesigner/components/formeditor/formeditorwidget.cpp @@ -326,17 +326,12 @@ void FormEditorWidget::changeRootItemHeight(const QString &heighText) void FormEditorWidget::changeBackgound(const QColor &color) { - if (color.alpha() == 0) { + if (color.alpha() == 0) m_graphicsView->activateCheckboardBackground(); - if (m_formEditorView->rootModelNode().hasAuxiliaryData(formeditorColorProperty)) { - m_formEditorView->rootModelNode().setAuxiliaryDataWithoutLock(formeditorColorProperty, - {}); - } - } else { + else m_graphicsView->activateColoredBackground(color); - m_formEditorView->rootModelNode().setAuxiliaryDataWithoutLock(formeditorColorProperty, - color); - } + + m_formEditorView->rootModelNode().setAuxiliaryDataWithoutLock(formeditorColorProperty, color); } void FormEditorWidget::registerActionAsCommand( @@ -397,8 +392,12 @@ void FormEditorWidget::updateActions() m_backgroundAction->setColor(Qt::transparent); } - if (m_formEditorView->rootModelNode().hasAuxiliaryData(contextImageProperty)) + if (m_formEditorView->rootModelNode().hasAuxiliaryData(contextImageProperty)) { + m_backgroundAction->setColorEnabled(BackgroundAction::ContextImage, true); m_backgroundAction->setColor(BackgroundAction::ContextImage); + } else { + m_backgroundAction->setColorEnabled(BackgroundAction::ContextImage, false); + } } else { m_rootWidthAction->clearLineEditText(); diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.cpp b/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.cpp index 33351baf5ea..afdeb95d3ad 100644 --- a/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.cpp +++ b/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.cpp @@ -462,6 +462,21 @@ void StatesEditorModel::setCanAddNewStates(bool b) emit canAddNewStatesChanged(); } +QColor StatesEditorModel::backgroundColor() const +{ + return m_backgrounColor; +} + +void StatesEditorModel::setBackgroundColor(const QColor &c) +{ + if (c == m_backgrounColor) + return; + + m_backgrounColor = c; + + emit backgroundColorChanged(); +} + bool StatesEditorModel::isMCUs() const { return DesignerMcuManager::instance().isMCUProject(); diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.h b/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.h index 12d66e29bf2..48212f5eb03 100644 --- a/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.h +++ b/src/plugins/qmldesigner/components/stateseditor/stateseditormodel.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include namespace QmlDesigner { @@ -16,6 +17,8 @@ class StatesEditorModel : public QAbstractListModel Q_PROPERTY(bool canAddNewStates READ canAddNewStates WRITE setCanAddNewStates NOTIFY canAddNewStatesChanged) + Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY + backgroundColorChanged FINAL) enum { StateNameRole = Qt::DisplayRole, @@ -93,6 +96,9 @@ public: bool canAddNewStates() const; void setCanAddNewStates(bool b); + QColor backgroundColor() const; + void setBackgroundColor(const QColor &c); + bool isMCUs() const; signals: @@ -104,6 +110,7 @@ signals: void activeStateGroupIndexChanged(); void stateGroupsChanged(); void canAddNewStatesChanged(); + void backgroundColorChanged(); void isMCUsChanged(); private: @@ -111,6 +118,7 @@ private: bool m_hasExtend; QStringList m_extendedStates; bool m_canAddNewStates = false; + QColor m_backgrounColor = Qt::transparent; }; } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp index 0731d438f4f..dd8a030eaa6 100644 --- a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp +++ b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.cpp @@ -14,6 +14,8 @@ #include +#include +#include #include #include #include @@ -719,6 +721,13 @@ void StatesEditorView::modelAttached(Model *model) resetModel(); resetStateGroups(); + // Initially set background color from auxiliary data + if (rootModelNode().hasAuxiliaryData(formeditorColorProperty)) { + QColor color = rootModelNode().auxiliaryDataWithDefault(formeditorColorProperty).value(); + m_statesEditorModel->setBackgroundColor( + color == BackgroundAction::ContextImage ? Qt::transparent : color); + } + emit m_statesEditorModel->activeStateGroupChanged(); emit m_statesEditorModel->activeStateGroupIndexChanged(); } @@ -880,6 +889,17 @@ void StatesEditorView::variantPropertiesChanged(const QList &pr } } +void StatesEditorView::auxiliaryDataChanged(const ModelNode &, + AuxiliaryDataKeyView key, + const QVariant &data) +{ + if (key == formeditorColorProperty) { + QColor color = data.value(); + m_statesEditorModel->setBackgroundColor( + color == BackgroundAction::ContextImage ? Qt::transparent : color); + } +} + void StatesEditorView::customNotification(const AbstractView * /*view*/, const QString &identifier, const QList & /*nodeList*/, diff --git a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.h b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.h index 502e5d2ac6f..919518683ea 100644 --- a/src/plugins/qmldesigner/components/stateseditor/stateseditorview.h +++ b/src/plugins/qmldesigner/components/stateseditor/stateseditorview.h @@ -64,15 +64,15 @@ public: PropertyChangeFlags propertyChange) override; void variantPropertiesChanged(const QList &propertyList, PropertyChangeFlags propertyChange) override; - + void auxiliaryDataChanged(const ModelNode &node, + AuxiliaryDataKeyView key, + const QVariant &data) override; void customNotification(const AbstractView *view, const QString &identifier, const QList &nodeList, const QList &data) override; void rewriterBeginTransaction() override; void rewriterEndTransaction() override; - - // AbstractView void currentStateChanged(const ModelNode &node) override; void instancesPreviewImageChanged(const QVector &nodeList) override; diff --git a/src/plugins/qmldesigner/documentmanager.cpp b/src/plugins/qmldesigner/documentmanager.cpp index 92d80680a94..8592fc6c342 100644 --- a/src/plugins/qmldesigner/documentmanager.cpp +++ b/src/plugins/qmldesigner/documentmanager.cpp @@ -4,6 +4,7 @@ #include "documentmanager.h" #include "qmldesignerplugin.h" +#include #include #include #include @@ -295,7 +296,7 @@ bool DocumentManager::goIntoComponent(const ModelNode &modelNode) ModelNode rootModelNode = designDocument()->rewriterView()->rootModelNode(); applyProperties(rootModelNode, oldProperties); - rootModelNode.setAuxiliaryData(AuxiliaryDataType::Temporary, "contextImage", image); + rootModelNode.setAuxiliaryData(contextImageProperty, image); return true; } diff --git a/src/tools/qml2puppet/qml2puppet/instances/qt5nodeinstanceserver.cpp b/src/tools/qml2puppet/qml2puppet/instances/qt5nodeinstanceserver.cpp index 765b52321bf..32a901f8d1e 100644 --- a/src/tools/qml2puppet/qml2puppet/instances/qt5nodeinstanceserver.cpp +++ b/src/tools/qml2puppet/qml2puppet/instances/qt5nodeinstanceserver.cpp @@ -89,6 +89,7 @@ void Qt5NodeInstanceServer::initializeView() m_viewData.renderControl = new QQuickRenderControl; m_viewData.window = new QQuickWindow(m_viewData.renderControl); + m_viewData.window->setColor(Qt::transparent); setPipelineCacheConfig(m_viewData.window); m_viewData.renderControl->initialize(); m_qmlEngine = new QQmlEngine; @@ -379,9 +380,11 @@ QImage Qt5NodeInstanceServer::grabRenderControl([[maybe_unused]] RenderViewData QRhiReadbackResult readResult; readResult.completed = [&] { readCompleted = true; - QImage wrapperImage(reinterpret_cast(readResult.data.constData()), - readResult.pixelSize.width(), readResult.pixelSize.height(), - QImage::Format_RGBA8888_Premultiplied); + QImage wrapperImage( + reinterpret_cast(readResult.data.constData()), + readResult.pixelSize.width(), + readResult.pixelSize.height(), + QImage::Format_RGBA8888_Premultiplied); if (viewData.rhi->isYUpInFramebuffer()) renderImage = wrapperImage.mirrored(); else