WIP: Refactor Materials management page to use new Managers

This commit is contained in:
Lipu Fei 2018-02-15 15:05:37 +01:00
parent aed0c124e9
commit 1e0a078af8
6 changed files with 1264 additions and 332 deletions

View file

@ -1,147 +1,61 @@
//Copyright (c) 2017 Ultimaker B.V.
//Cura is released under the terms of the LGPLv3 or higher.
// Copyright (c) 2018 Ultimaker B.V.
// Uranium is released under the terms of the LGPLv3 or higher.
import QtQuick 2.1
import QtQuick.Controls 1.1
import QtQuick.Dialogs 1.2
import QtQuick 2.8
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import UM 1.2 as UM
import Cura 1.0 as Cura
UM.ManagementPage
Item
{
id: base;
id: base
property var resetEnabled: false // Keep PreferencesDialog happy
title: catalog.i18nc("@title:tab", "Materials");
UM.I18nCatalog { id: catalog; name: "cura"; }
Component.onCompleted:
{
// Workaround to make sure all of the items are visible
objectList.positionViewAtBeginning();
Cura.NewMaterialsModel {
id: materialsModel
}
model: Cura.MaterialsModel
{
filter:
{
var result = { "type": "material", "approximate_diameter": Math.round(materialDiameterProvider.properties.value).toString() }
if(Cura.MachineManager.filterMaterialsByMachine)
{
result.definition = Cura.MachineManager.activeQualityDefinitionId;
if(Cura.MachineManager.hasVariants)
{
result.variant_name = Cura.MachineManager.activeQualityVariantName;
}
}
else
{
result.definition = "fdmprinter";
result.compatible = true; //NB: Only checks for compatibility in global version of material, but we don't have machine-specific materials anyway.
}
return result
Label {
id: titleLabel
anchors {
top: parent.top
left: parent.left
right: parent.right
margins: 5 * screenScaleFactor
}
sectionProperty: "brand"
font.pointSize: 18
text: catalog.i18nc("@title:tab", "Materials")
}
delegate: Rectangle
Row // Button Row
{
width: objectList.width;
height: childrenRect.height;
color: isCurrentItem ? palette.highlight : index % 2 ? palette.base : palette.alternateBase
property bool isCurrentItem: ListView.isCurrentItem
Row
{
spacing: (UM.Theme.getSize("default_margin").width / 2) | 0
anchors.left: parent.left
anchors.leftMargin: UM.Theme.getSize("default_margin").width
anchors.right: parent.right
Rectangle
{
width: Math.round(parent.height * 0.8)
height: Math.round(parent.height * 0.8)
color: model.metadata.color_code
border.color: isCurrentItem ? palette.highlightedText : palette.text;
anchors.verticalCenter: parent.verticalCenter
}
Label
{
width: Math.round((parent.width * 0.3))
text: model.metadata.material
elide: Text.ElideRight
font.italic: model.id == activeId
color: isCurrentItem ? palette.highlightedText : palette.text;
}
Label
{
text: (model.name != model.metadata.material) ? model.name : ""
elide: Text.ElideRight
font.italic: model.id == activeId
color: isCurrentItem ? palette.highlightedText : palette.text;
}
id: buttonRow
anchors {
left: parent.left
right: parent.right
top: titleLabel.bottom
}
MouseArea
{
anchors.fill: parent;
onClicked:
{
forceActiveFocus();
if(!parent.ListView.isCurrentItem)
{
parent.ListView.view.currentIndex = index;
base.itemActivated();
}
}
}
}
activeId: Cura.MachineManager.activeMaterialId
activeIndex: getIndexById(activeId)
function getIndexById(material_id)
{
for(var i = 0; i < model.rowCount(); i++) {
if (model.getItem(i).id == material_id) {
return i;
}
}
return -1;
}
scrollviewCaption:
{
var caption = catalog.i18nc("@action:label", "Printer") + ": " + Cura.MachineManager.activeMachineName;
if (Cura.MachineManager.hasVariants)
{
caption += ", " + Cura.MachineManager.activeDefinitionVariantsName + ": " + Cura.MachineManager.activeVariantName;
}
return caption;
}
detailsVisible: true
section.property: "section"
section.delegate: Label
{
text: section
font.bold: true
anchors.left: parent.left;
anchors.leftMargin: UM.Theme.getSize("default_lining").width;
}
buttons: [
height: childrenRect.height
// Activate button
Button {
text: catalog.i18nc("@action:button", "Activate")
iconName: "list-activate";
enabled: base.currentItem != null && base.currentItem.id != Cura.MachineManager.activeMaterialId && Cura.MachineManager.hasMaterials
iconName: "list-activate"
//enabled: base.currentItem != null && base.currentItem.id != Cura.MachineManager.activeMaterialId && Cura.MachineManager.hasMaterials
enabled: true // TODO
onClicked: {
forceActiveFocus()
Cura.MachineManager.setActiveMaterial(base.currentItem.id)
currentItem = base.model.getItem(base.objectList.currentIndex) // Refresh the current item.
}
},
}
// Create button
Button {
@ -149,31 +63,32 @@ UM.ManagementPage
iconName: "list-add"
onClicked: {
forceActiveFocus()
Cura.ContainerManager.createMaterial()
// TODO
}
},
}
// Duplicate button
Button {
text: catalog.i18nc("@action:button", "Duplicate");
iconName: "list-add";
enabled: base.currentItem != null
iconName: "list-add"
enabled: true //TODO
onClicked: {
forceActiveFocus()
Cura.ContainerManager.duplicateOriginalMaterial(base.currentItem.id)
// TODO
}
},
}
// Remove button
Button {
text: catalog.i18nc("@action:button", "Remove")
iconName: "list-remove"
enabled: base.currentItem != null && !base.currentItem.readOnly && !Cura.ContainerManager.isContainerUsed(base.currentItem.id)
//enabled: base.currentItem != null && !base.currentItem.readOnly && !Cura.ContainerManager.isContainerUsed(base.currentItem.id)
enabled: true // TODO
onClicked: {
forceActiveFocus()
confirmDialog.open()
// TODO
}
},
}
// Import button
Button {
@ -181,10 +96,10 @@ UM.ManagementPage
iconName: "document-import"
onClicked: {
forceActiveFocus()
importDialog.open()
// TODO
}
visible: true
},
}
// Export button
Button {
@ -192,221 +107,262 @@ UM.ManagementPage
iconName: "document-export"
onClicked: {
forceActiveFocus()
exportDialog.open()
// TODO
}
enabled: currentItem != null
}
]
}
Item {
visible: base.currentItem != null
anchors.fill: parent
id: contentsItem
anchors {
top: titleLabel.bottom
left: parent.left
right: parent.right
bottom: parent.bottom
margins: 5 * screenScaleFactor
bottomMargin: 0
}
clip: true
}
Item
{
anchors {
top: buttonRow.bottom
topMargin: UM.Theme.getSize("default_margin").height
left: parent.left
right: parent.right
bottom: parent.bottom
}
SystemPalette { id: palette }
Label
{
id: captionLabel
anchors {
top: parent.top
left: parent.left
}
text: "TODO"
width: materialScrollView.width
elide: Text.ElideRight
}
ScrollView
{
id: materialScrollView
anchors {
top: captionLabel.visible ? captionLabel.bottom : parent.top
topMargin: captionLabel.visible ? UM.Theme.getSize("default_margin").height : 0
bottom: parent.bottom
left: parent.left
}
Rectangle {
parent: viewport
anchors.fill: parent
color: palette.light
}
width: true ? (parent.width * 0.4) | 0 : parent.width
ListView
{
id: materialListView
model: materialsModel
section.property: "brand"
section.criteria: ViewSection.FullString
section.delegate: Rectangle
{
width: materialScrollView.width
height: childrenRect.height
color: palette.light
Label
{
anchors.left: parent.left
anchors.leftMargin: UM.Theme.getSize("default_lining").width
text: section
font.bold: true
color: palette.text
}
}
delegate: Rectangle
{
width: materialScrollView.width
height: childrenRect.height
color: ListView.isCurrentItem ? palette.highlight : (model.index % 2) ? palette.base : palette.alternateBase
Row
{
spacing: (UM.Theme.getSize("default_margin").width / 2) | 0
anchors.left: parent.left
anchors.leftMargin: UM.Theme.getSize("default_margin").width
anchors.right: parent.right
Rectangle
{
width: Math.floor(parent.height * 0.8)
height: Math.floor(parent.height * 0.8)
color: model.color_code
border.color: parent.ListView.isCurrentItem ? palette.highlightedText : palette.text;
anchors.verticalCenter: parent.verticalCenter
}
Label
{
width: Math.floor((parent.width * 0.3))
text: model.material
elide: Text.ElideRight
font.italic: {
const extruder_position = Cura.ExtruderManager.activeExtruderIndex;
const root_material_id = Cura.MachineManager.currentRootMaterialId[extruder_position];
return model.root_material_id == root_material_id; // TODO
}
color: parent.ListView.isCurrentItem ? palette.highlightedText : palette.text;
}
Label
{
text: (model.name != model.material) ? model.name : ""
elide: Text.ElideRight
font.italic: {
const extruder_position = Cura.ExtruderManager.activeExtruderIndex;
const root_material_id = Cura.MachineManager.currentRootMaterialId[extruder_position];
return model.root_material_id == root_material_id; // TODO
}
color: parent.ListView.isCurrentItem ? palette.highlightedText : palette.text;
}
}
MouseArea
{
anchors.fill: parent
onClicked: {
parent.ListView.view.currentIndex = model.index;
}
}
}
onCurrentIndexChanged:
{
var model = materialsModel.getItem(currentIndex);
materialDetailsView.containerId = model.container_id;
detailsPanel.updateMaterialPropertiesObject();
}
}
}
Item
{
id: profileName
id: detailsPanel
width: parent.width;
height: childrenRect.height
Label { text: materialProperties.name; font: UM.Theme.getFont("large"); }
}
MaterialView
{
anchors
{
left: parent.left
right: parent.right
top: profileName.bottom
topMargin: UM.Theme.getSize("default_margin").height
anchors {
left: materialScrollView.right
leftMargin: UM.Theme.getSize("default_margin").width
top: parent.top
bottom: parent.bottom
right: parent.right
}
editingEnabled: base.currentItem != null && !base.currentItem.readOnly
properties: materialProperties
containerId: base.currentItem != null ? base.currentItem.id : ""
property alias pane: base
}
QtObject
{
id: materialProperties
property string guid: "00000000-0000-0000-0000-000000000000"
property string name: "Unknown";
property string profile_type: "Unknown";
property string supplier: "Unknown";
property string material_type: "Unknown";
property string color_name: "Yellow";
property color color_code: "yellow";
property real density: 0.0;
property real diameter: 0.0;
property string approximate_diameter: "0";
property real spool_cost: 0.0;
property real spool_weight: 0.0;
property real spool_length: 0.0;
property real cost_per_meter: 0.0;
property string description: "";
property string adhesion_info: "";
}
UM.ConfirmRemoveDialog
{
id: confirmDialog
object: base.currentItem != null ? base.currentItem.name : ""
onYes:
function updateMaterialPropertiesObject()
{
// A material container can actually be multiple items, so we need to find (and remove) all of them.
var base_file = Cura.ContainerManager.getContainerMetaDataEntry(base.currentItem.id, "base_file")
if(base_file == "")
{
base_file = base.currentItem.id
}
var guid = Cura.ContainerManager.getContainerMetaDataEntry(base.currentItem.id, "GUID")
// remove base container first, it otherwise triggers loading the base file while removing other containers
var base_containers = Cura.ContainerManager.findInstanceContainers({"GUID": guid, "id": base_file, "base_file": base_file, "type": "material"})
for(var i in base_containers)
{
Cura.ContainerManager.removeContainer(base_containers[i]);
}
var containers = Cura.ContainerManager.findInstanceContainers({"GUID": guid, "base_file": base_file, "type": "material"})
for(var i in containers)
{
Cura.ContainerManager.removeContainer(containers[i]);
}
if(base.objectList.currentIndex > 0)
{
base.objectList.currentIndex--;
}
currentItem = base.model.getItem(base.objectList.currentIndex) // Refresh the current item.
}
}
var currentItem = materialsModel.getItem(materialListView.currentIndex);
FileDialog
{
id: importDialog;
title: catalog.i18nc("@title:window", "Import Material");
selectExisting: true;
nameFilters: Cura.ContainerManager.getContainerNameFilters("material")
folder: CuraApplication.getDefaultPath("dialog_material_path")
onAccepted:
{
var result = Cura.ContainerManager.importMaterialContainer(fileUrl)
materialProperties.name = currentItem.name;
materialProperties.guid = currentItem.guid;
messageDialog.title = catalog.i18nc("@title:window", "Import Material")
messageDialog.text = catalog.i18nc("@info:status Don't translate the XML tags <filename> or <message>!", "Could not import material <filename>%1</filename>: <message>%2</message>").arg(fileUrl).arg(result.message)
if(result.status == "success")
materialProperties.supplier = currentItem.brand ? currentItem.brand : "Unknown";
materialProperties.material_type = currentItem.material ? currentItem.material : "Unknown";
materialProperties.color_name = currentItem.color_name ? currentItem.color_name : "Yellow";
materialProperties.color_code = currentItem.color_code ? currentItem.color_code : "yellow";
materialProperties.description = currentItem.description ? currentItem.description : "";
materialProperties.adhesion_info = currentItem.adhesion_info ? currentItem.adhesion_info : "";
if(currentItem.properties != undefined && currentItem.properties != null)
{
messageDialog.icon = StandardIcon.Information
messageDialog.text = catalog.i18nc("@info:status Don't translate the XML tag <filename>!", "Successfully imported material <filename>%1</filename>").arg(fileUrl)
}
else if(result.status == "duplicate")
{
messageDialog.icon = StandardIcon.Warning
materialProperties.density = currentItem.density ? currentItem.density : 0.0;
materialProperties.diameter = currentItem.diameter ? currentItem.diameter : 0.0;
materialProperties.approximate_diameter = currentItem.approximate_diameter ? currentItem.approximate_diameter : "0";
}
else
{
messageDialog.icon = StandardIcon.Critical
materialProperties.density = 0.0;
materialProperties.diameter = 0.0;
materialProperties.approximate_diameter = "0";
}
messageDialog.open()
CuraApplication.setDefaultPath("dialog_material_path", folder)
}
}
FileDialog
{
id: exportDialog;
title: catalog.i18nc("@title:window", "Export Material");
selectExisting: false;
nameFilters: Cura.ContainerManager.getContainerNameFilters("material")
folder: CuraApplication.getDefaultPath("dialog_material_path")
onAccepted:
Item
{
if(base.currentItem.metadata.base_file)
anchors.fill: parent
Item // Material title Label
{
var result = Cura.ContainerManager.exportContainer(base.currentItem.metadata.base_file, selectedNameFilter, fileUrl)
}
else
{
var result = Cura.ContainerManager.exportContainer(base.currentItem.id, selectedNameFilter, fileUrl)
id: profileName
width: parent.width
height: childrenRect.height
Label {
text: materialProperties.name
font: UM.Theme.getFont("large")
}
}
messageDialog.title = catalog.i18nc("@title:window", "Export Material")
if(result.status == "error")
MaterialView // Material detailed information view below the title Label
{
messageDialog.icon = StandardIcon.Critical
messageDialog.text = catalog.i18nc("@info:status Don't translate the XML tags <filename> and <message>!", "Failed to export material to <filename>%1</filename>: <message>%2</message>").arg(fileUrl).arg(result.message)
messageDialog.open()
id: materialDetailsView
anchors
{
left: parent.left
right: parent.right
top: profileName.bottom
topMargin: UM.Theme.getSize("default_margin").height
bottom: parent.bottom
}
editingEnabled: base.currentItem != null && !base.currentItem.readOnly
properties: materialProperties
containerId: base.currentItem != null ? base.currentItem.id : ""
property alias pane: base
}
else if(result.status == "success")
QtObject
{
messageDialog.icon = StandardIcon.Information
messageDialog.text = catalog.i18nc("@info:status Don't translate the XML tag <filename>!", "Successfully exported material to <filename>%1</filename>").arg(result.path)
messageDialog.open()
id: materialProperties
property string guid: "00000000-0000-0000-0000-000000000000"
property string name: "Unknown";
property string profile_type: "Unknown";
property string supplier: "Unknown";
property string material_type: "Unknown";
property string color_name: "Yellow";
property color color_code: "yellow";
property real density: 0.0;
property real diameter: 0.0;
property string approximate_diameter: "0";
property real spool_cost: 0.0;
property real spool_weight: 0.0;
property real spool_length: 0.0;
property real cost_per_meter: 0.0;
property string description: "";
property string adhesion_info: "";
}
CuraApplication.setDefaultPath("dialog_material_path", folder)
}
}
MessageDialog
{
id: messageDialog
}
UM.SettingPropertyProvider
{
id: materialDiameterProvider
containerStackId: Cura.ExtruderManager.activeExtruderStackId
key: "material_diameter"
watchedProperties: [ "value" ]
storeIndex: 5
}
UM.I18nCatalog { id: catalog; name: "cura"; }
SystemPalette { id: palette }
}
onCurrentItemChanged:
{
if(currentItem == null)
{
return
}
materialProperties.name = currentItem.name;
materialProperties.guid = Cura.ContainerManager.getContainerMetaDataEntry(base.currentItem.id, "GUID");
if(currentItem.metadata != undefined && currentItem.metadata != null)
{
materialProperties.supplier = currentItem.metadata.brand ? currentItem.metadata.brand : "Unknown";
materialProperties.material_type = currentItem.metadata.material ? currentItem.metadata.material : "Unknown";
materialProperties.color_name = currentItem.metadata.color_name ? currentItem.metadata.color_name : "Yellow";
materialProperties.color_code = currentItem.metadata.color_code ? currentItem.metadata.color_code : "yellow";
materialProperties.description = currentItem.metadata.description ? currentItem.metadata.description : "";
materialProperties.adhesion_info = currentItem.metadata.adhesion_info ? currentItem.metadata.adhesion_info : "";
if(currentItem.metadata.properties != undefined && currentItem.metadata.properties != null)
{
materialProperties.density = currentItem.metadata.properties.density ? currentItem.metadata.properties.density : 0.0;
materialProperties.diameter = currentItem.metadata.properties.diameter ? currentItem.metadata.properties.diameter : 0.0;
materialProperties.approximate_diameter = currentItem.metadata.approximate_diameter ? currentItem.metadata.approximate_diameter : "0";
}
else
{
materialProperties.density = 0.0;
materialProperties.diameter = 0.0;
materialProperties.approximate_diameter = "0";
}
}
}
}