QmlDesigner: Extract material browser ctx menus into separate components

Change-Id: I43305567f5224ed8015bcb507c7ee27e268d4d67
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
Mahmoud Badri
2022-10-18 16:26:16 +03:00
parent d3e83f601e
commit e55ea179c4
3 changed files with 234 additions and 163 deletions

View File

@@ -24,7 +24,6 @@
****************************************************************************/ ****************************************************************************/
import QtQuick 2.15 import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuickDesignerTheme 1.0 import QtQuickDesignerTheme 1.0
import HelperWidgets 2.0 import HelperWidgets 2.0
import StudioControls 1.0 as StudioControls import StudioControls 1.0 as StudioControls
@@ -36,12 +35,7 @@ Item {
readonly property int cellWidth: 100 readonly property int cellWidth: 100
readonly property int cellHeight: 120 readonly property int cellHeight: 120
property var currentMaterial: null property var currMaterialItem: null
property int currentMaterialIdx: 0
property var currentBundleMaterial: null
property int copiedMaterialInternalId: -1
property var matSectionsModel: []
// Called also from C++ to close context menu on focus out // Called also from C++ to close context menu on focus out
function closeContextMenu() function closeContextMenu()
@@ -78,8 +72,7 @@ Item {
if (!materialBrowserModel.hasMaterialRoot && materialBrowserModel.hasQuick3DImport if (!materialBrowserModel.hasMaterialRoot && materialBrowserModel.hasQuick3DImport
&& (!materialBrowserBundleModel.matBundleExists || mouse.y < userMatsSecBottom)) { && (!materialBrowserBundleModel.matBundleExists || mouse.y < userMatsSecBottom)) {
root.currentMaterial = null ctxMenu.popupMenu()
ctxMenu.popup()
} }
} }
} }
@@ -89,11 +82,23 @@ Item {
function onSelectedIndexChanged() { function onSelectedIndexChanged() {
// commit rename upon changing selection // commit rename upon changing selection
var item = gridRepeater.itemAt(currentMaterialIdx); if (root.currMaterialItem)
if (item) root.currMaterialItem.commitRename();
item.commitRename();
currentMaterialIdx = materialBrowserModel.selectedIndex; root.currMaterialItem = gridRepeater.itemAt(materialBrowserModel.selectedIndex);
}
}
MaterialBrowserContextMenu {
id: ctxMenu
}
MaterialBundleContextMenu {
id: ctxMenuBundle
onUnimport: (bundleMat) => {
unimportBundleMaterialDialog.targetBundleMaterial = bundleMat
unimportBundleMaterialDialog.open()
} }
} }
@@ -101,152 +106,6 @@ Item {
id: unimportBundleMaterialDialog id: unimportBundleMaterialDialog
} }
StudioControls.Menu {
id: ctxMenu
closePolicy: StudioControls.Menu.CloseOnEscape | StudioControls.Menu.CloseOnPressOutside
StudioControls.MenuItem {
text: qsTr("Apply to selected (replace)")
enabled: root.currentMaterial && materialBrowserModel.hasModelSelection
onTriggered: materialBrowserModel.applyToSelected(root.currentMaterial.materialInternalId, false)
}
StudioControls.MenuItem {
text: qsTr("Apply to selected (add)")
enabled: root.currentMaterial && materialBrowserModel.hasModelSelection
onTriggered: materialBrowserModel.applyToSelected(root.currentMaterial.materialInternalId, true)
}
StudioControls.MenuSeparator {
height: StudioTheme.Values.border
}
StudioControls.Menu {
title: qsTr("Copy properties")
enabled: root.currentMaterial
width: parent.width
onAboutToShow: {
if (root.currentMaterial.hasDynamicProperties)
root.matSectionsModel = ["All", "Custom"];
else
root.matSectionsModel = ["All"];
switch (root.currentMaterial.materialType) {
case "DefaultMaterial":
root.matSectionsModel = root.matSectionsModel.concat(materialBrowserModel.defaultMaterialSections);
break;
case "PrincipledMaterial":
root.matSectionsModel = root.matSectionsModel.concat(materialBrowserModel.principledMaterialSections);
break;
case "CustomMaterial":
root.matSectionsModel = root.matSectionsModel.concat(materialBrowserModel.customMaterialSections);
break;
}
}
Repeater {
model: root.matSectionsModel
StudioControls.MenuItem {
text: modelData
enabled: root.currentMaterial
onTriggered: {
root.copiedMaterialInternalId = root.currentMaterial.materialInternalId
materialBrowserModel.copyMaterialProperties(root.currentMaterialIdx, modelData)
}
}
}
}
StudioControls.MenuItem {
text: qsTr("Paste properties")
enabled: root.currentMaterial
&& root.copiedMaterialInternalId !== root.currentMaterial.materialInternalId
&& root.currentMaterial.materialType === materialBrowserModel.copiedMaterialType
&& materialBrowserModel.isCopiedMaterialValid()
onTriggered: materialBrowserModel.pasteMaterialProperties(root.currentMaterialIdx)
}
StudioControls.MenuSeparator {
height: StudioTheme.Values.border
}
StudioControls.MenuItem {
text: qsTr("Duplicate")
enabled: root.currentMaterial
onTriggered: materialBrowserModel.duplicateMaterial(root.currentMaterialIdx)
}
StudioControls.MenuItem {
text: qsTr("Rename")
enabled: root.currentMaterial
onTriggered: {
var item = gridRepeater.itemAt(root.currentMaterialIdx);
if (item)
item.startRename();
}
}
StudioControls.MenuItem {
text: qsTr("Delete")
enabled: root.currentMaterial
onTriggered: materialBrowserModel.deleteMaterial(root.currentMaterialIdx)
}
StudioControls.MenuSeparator {}
StudioControls.MenuItem {
text: qsTr("Create New Material")
onTriggered: materialBrowserModel.addNewMaterial()
}
}
StudioControls.Menu {
id: ctxMenuBundle
closePolicy: StudioControls.Menu.CloseOnEscape | StudioControls.Menu.CloseOnPressOutside
StudioControls.MenuItem {
text: qsTr("Apply to selected (replace)")
enabled: root.currentBundleMaterial && materialBrowserModel.hasModelSelection
onTriggered: materialBrowserBundleModel.applyToSelected(root.currentBundleMaterial, false)
}
StudioControls.MenuItem {
text: qsTr("Apply to selected (add)")
enabled: root.currentBundleMaterial && materialBrowserModel.hasModelSelection
onTriggered: materialBrowserBundleModel.applyToSelected(root.currentBundleMaterial, true)
}
StudioControls.MenuSeparator {}
StudioControls.MenuItem {
enabled: !materialBrowserBundleModel.importerRunning
text: qsTr("Add an instance to project")
onTriggered: {
materialBrowserBundleModel.addToProject(root.currentBundleMaterial)
}
}
StudioControls.MenuItem {
enabled: !materialBrowserBundleModel.importerRunning && root.currentBundleMaterial.bundleMaterialImported
text: qsTr("Remove from project")
onTriggered: {
unimportBundleMaterialDialog.targetBundleMaterial = root.currentBundleMaterial
unimportBundleMaterialDialog.open()
}
}
}
Column { Column {
id: col id: col
y: 5 y: 5
@@ -347,8 +206,7 @@ Item {
height: root.cellHeight height: root.cellHeight
onShowContextMenu: { onShowContextMenu: {
root.currentMaterial = model ctxMenu.popupMenu(this, model)
ctxMenu.popup()
} }
} }
} }
@@ -410,8 +268,7 @@ Item {
height: root.cellHeight height: root.cellHeight
onShowContextMenu: { onShowContextMenu: {
root.currentBundleMaterial = modelData ctxMenuBundle.popupMenu(modelData)
ctxMenuBundle.popup()
} }
} }
} }

View File

@@ -0,0 +1,140 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
import QtQuick
import HelperWidgets
import StudioControls as StudioControls
import StudioTheme as StudioTheme
StudioControls.Menu {
id: root
property var targetMaterial: null
property var targetItem: null
property int copiedMaterialInternalId: -1
property var matSectionsModel: []
function popupMenu(targetItem = null, targetMaterial = null)
{
this.targetItem = targetItem
this.targetMaterial = targetMaterial
popup()
}
closePolicy: StudioControls.Menu.CloseOnEscape | StudioControls.Menu.CloseOnPressOutside
StudioControls.MenuItem {
text: qsTr("Apply to selected (replace)")
enabled: root.targetMaterial && materialBrowserModel.hasModelSelection
onTriggered: materialBrowserModel.applyToSelected(root.targetMaterial.materialInternalId, false)
}
StudioControls.MenuItem {
text: qsTr("Apply to selected (add)")
enabled: root.targetMaterial && materialBrowserModel.hasModelSelection
onTriggered: materialBrowserModel.applyToSelected(root.targetMaterial.materialInternalId, true)
}
StudioControls.MenuSeparator {}
StudioControls.Menu {
title: qsTr("Copy properties")
enabled: root.targetMaterial
width: parent.width
onAboutToShow: {
if (root.targetMaterial.hasDynamicProperties)
root.matSectionsModel = ["All", "Custom"];
else
root.matSectionsModel = ["All"];
switch (root.targetMaterial.materialType) {
case "DefaultMaterial":
root.matSectionsModel = root.matSectionsModel.concat(materialBrowserModel.defaultMaterialSections);
break;
case "PrincipledMaterial":
root.matSectionsModel = root.matSectionsModel.concat(materialBrowserModel.principledMaterialSections);
break;
case "CustomMaterial":
root.matSectionsModel = root.matSectionsModel.concat(materialBrowserModel.customMaterialSections);
break;
}
}
Repeater {
model: root.matSectionsModel
StudioControls.MenuItem {
text: modelData
enabled: root.targetMaterial
onTriggered: {
root.copiedMaterialInternalId = root.targetMaterial.materialInternalId
materialBrowserModel.copyMaterialProperties(materialBrowserModel.selectedIndex, modelData)
}
}
}
}
StudioControls.MenuItem {
text: qsTr("Paste properties")
enabled: root.targetMaterial
&& root.copiedMaterialInternalId !== root.targetMaterial.materialInternalId
&& root.targetMaterial.materialType === materialBrowserModel.copiedMaterialType
&& materialBrowserModel.isCopiedMaterialValid()
onTriggered: materialBrowserModel.pasteMaterialProperties(materialBrowserModel.selectedIndex)
}
StudioControls.MenuSeparator {}
StudioControls.MenuItem {
text: qsTr("Duplicate")
enabled: root.targetMaterial
onTriggered: materialBrowserModel.duplicateMaterial(materialBrowserModel.selectedIndex)
}
StudioControls.MenuItem {
text: qsTr("Rename")
enabled: root.targetItem
onTriggered: root.targetItem.startRename();
}
StudioControls.MenuItem {
text: qsTr("Delete")
enabled: root.targetMaterial
onTriggered: materialBrowserModel.deleteMaterial(materialBrowserModel.selectedIndex)
}
StudioControls.MenuSeparator {}
StudioControls.MenuItem {
text: qsTr("Create New Material")
onTriggered: materialBrowserModel.addNewMaterial()
}
}

View File

@@ -0,0 +1,74 @@
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
import QtQuick 2.15
import HelperWidgets 2.0
import StudioControls 1.0 as StudioControls
import StudioTheme 1.0 as StudioTheme
StudioControls.Menu {
id: root
property var targetMaterial: null
signal unimport(var bundleMat);
function popupMenu(targetMaterial = null)
{
this.targetMaterial = targetMaterial
popup()
}
closePolicy: StudioControls.Menu.CloseOnEscape | StudioControls.Menu.CloseOnPressOutside
StudioControls.MenuItem {
text: qsTr("Apply to selected (replace)")
enabled: root.targetMaterial && materialBrowserModel.hasModelSelection
onTriggered: materialBrowserBundleModel.applyToSelected(root.targetMaterial, false)
}
StudioControls.MenuItem {
text: qsTr("Apply to selected (add)")
enabled: root.targetMaterial && materialBrowserModel.hasModelSelection
onTriggered: materialBrowserBundleModel.applyToSelected(root.targetMaterial, true)
}
StudioControls.MenuSeparator {}
StudioControls.MenuItem {
enabled: !materialBrowserBundleModel.importerRunning
text: qsTr("Add an instance to project")
onTriggered: {
materialBrowserBundleModel.addToProject(root.targetMaterial)
}
}
StudioControls.MenuItem {
enabled: !materialBrowserBundleModel.importerRunning && root.targetMaterial.bundleMaterialImported
text: qsTr("Remove from project")
onTriggered: root.unimport(root.targetMaterial);
}
}