Cura/resources/qml/MachineSettings/ComboBoxWithOptions.qml
2019-03-18 09:24:20 +01:00

120 lines
3.4 KiB
QML

// Copyright (c) 2019 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.3 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 string tooltipText: propertyProvider.properties.description
// callback functions
property var afterOnActivateFunction: dummy_func
property var forceUpdateOnChangeFunction: dummy_func
// a dummy function for default property values
function dummy_func() {}
UM.SettingPropertyProvider
{
id: propertyProvider
watchedProperties: [ "value", "options", "description" ]
}
Row
{
spacing: UM.Theme.getSize("default_margin").width
Label
{
id: fieldLabel
anchors.verticalCenter: comboBox.verticalCenter
visible: text != ""
font: UM.Theme.getFont("medium")
renderType: Text.NativeRendering
}
ListModel
{
id: optionsModel
Component.onCompleted:
{
// Options come in as a string-representation of an OrderedDict
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("', '")
optionsModel.append({text: option[1], value: option[0]})
}
}
}
}
CuraComboBox
{
id: comboBox
width: comboBoxWithOptions.controlWidth
height: comboBoxWithOptions.controlHeight
model: optionsModel
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:
{
if(propertyProvider.properties.value != model.get(index).value)
{
propertyProvider.setPropertyValue("value", model.get(index).value)
forceUpdateOnChangeFunction()
afterOnActivateFunction()
}
}
}
}
}