1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
// Copyright (c) 2020 Ultimaker B.V.
// Cura is released under the terms of the LGPLv3 or higher.
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import UM 1.5 as UM
import Cura 1.1 as Cura
import "../Widgets"
//
// ComboBox with dropdown options in the Machine Settings dialog.
//
UM.TooltipArea
{
id: comboBoxWithOptions
UM.I18nCatalog { id: catalog; name: "cura"; }
height: childrenRect.height
width: childrenRect.width
text: tooltipText
property int controlWidth: UM.Theme.getSize("setting_control").width
property int controlHeight: UM.Theme.getSize("setting_control").height
property alias containerStackId: propertyProvider.containerStackId
property alias settingKey: propertyProvider.key
property alias settingStoreIndex: propertyProvider.storeIndex
property alias labelText: fieldLabel.text
property alias labelFont: fieldLabel.font
property alias labelWidth: fieldLabel.width
property alias optionModel: comboBox.model
property string tooltipText: propertyProvider.properties.description ? propertyProvider.properties.description : ""
// callback functions
property var forceUpdateOnChangeFunction: dummy_func
property var afterOnEditingFinishedFunction: dummy_func
property var setValueFunction: null
// a dummy function for default property values
function dummy_func() {}
UM.SettingPropertyProvider
{
id: propertyProvider
watchedProperties: [ "value", "options", "description" ]
}
UM.Label
{
id: fieldLabel
anchors.left: parent.left
anchors.verticalCenter: comboBox.verticalCenter
visible: text != ""
font: UM.Theme.getFont("medium")
}
ListModel
{
id: defaultOptionsModel
function updateModel()
{
clear()
// Options come in as a string-representation of an OrderedDict
if(propertyProvider.properties.options)
{
var options = propertyProvider.properties.options.match(/^OrderedDict\(\[\((.*)\)\]\)$/);
if(options)
{
options = options[1].split("), (");
for(var i = 0; i < options.length; i++)
{
var option = options[i].substring(1, options[i].length - 1).split("', '");
append({ text: option[1], value: option[0] });
}
}
}
}
Component.onCompleted: updateModel()
}
// Remake the model when the model is bound to a different container stack
Connections
{
target: propertyProvider
function onContainerStackChanged() { defaultOptionsModel.updateModel() }
function onIsValueUsedChanged() { defaultOptionsModel.updateModel() }
}
Cura.ComboBox
{
id: comboBox
anchors.left: fieldLabel.right
anchors.leftMargin: UM.Theme.getSize("default_margin").width
width: comboBoxWithOptions.controlWidth
height: comboBoxWithOptions.controlHeight
model: defaultOptionsModel
textRole: "text"
currentIndex:
{
var currentValue = propertyProvider.properties.value
var index = 0
for (var i = 0; i < model.count; i++)
{
if (model.get(i).value == currentValue)
{
index = i
break
}
}
return index
}
onActivated:
{
var newValue = model.get(index).value
if (propertyProvider.properties.value != newValue)
{
if (setValueFunction !== null)
{
setValueFunction(newValue)
}
else
{
propertyProvider.setPropertyValue("value", newValue)
}
forceUpdateOnChangeFunction()
afterOnEditingFinishedFunction()
}
}
}
}
|