LogarithmPlotter/LogarithmPlotter/qml/ListSetting.qml

191 lines
5.9 KiB
QML
Raw Normal View History

import QtQuick 2.12
2020-12-25 23:59:57 +00:00
import QtQuick.Controls 2.12
import QtQml.Models 2.12
2020-12-25 23:59:57 +00:00
Column {
id: control
signal changed()
2020-12-25 23:59:57 +00:00
property string label: ''
2021-03-31 13:58:21 +00:00
property string icon: ''
property bool dictionaryMode: false
2020-12-25 23:59:57 +00:00
property string keyType: "string"
property string valueType: "string"
property string preKeyLabel: ""
property string postKeyLabel: ": "
property var keyRegexp: /^.+$/
property var valueRegexp: /^.+$/
2020-12-25 23:59:57 +00:00
property bool forbidAdding: false
2020-12-25 23:59:57 +00:00
property alias model: repeater.model
2021-03-31 13:58:21 +00:00
Row {
height: 30
width: parent.width;
Icon {
id: iconLabel
anchors.top: parent.top
anchors.topMargin: icon == "" ? 0 : 3
source: control.visible ? control.icon : ""
width: height
height: icon == "" || !visible ? 0 : 24
color: sysPalette.windowText
}
2021-03-14 18:03:58 +00:00
Label {
2020-12-25 23:59:57 +00:00
id: labelItem
height: 30
2020-12-25 23:59:57 +00:00
verticalAlignment: TextInput.AlignVCenter
text: control.label +": "
}
2021-03-31 13:58:21 +00:00
}
2020-12-25 23:59:57 +00:00
Repeater {
id: repeater
width: control.width
2020-12-25 23:59:57 +00:00
model: ListModel {}
2020-12-25 23:59:57 +00:00
Row {
id: defRow
height: addEntryBtn.height
width: parent.width
Text {
id: preKeyText
height: parent.height
verticalAlignment: TextInput.AlignVCenter
color: sysPalette.windowText
text: control.preKeyLabel
}
2020-12-25 23:59:57 +00:00
TextField {
id: keyInput
visible: control.dictionaryMode
height: parent.height
width: visible ? 50 : 0
validator: RegExpValidator {
regExp: control.keyRegexp
}
2020-12-25 23:59:57 +00:00
verticalAlignment: TextInput.AlignVCenter
horizontalAlignment: TextInput.AlignHCenter
color: sysPalette.windowText
text: visible ? control.model.get(index).key : false
selectByMouse: true
onEditingFinished: {
var value = text
if(control.keyType == 'int') {
value = parseInt(value)
if(value.toString()=="NaN")
value = ""
}
if(control.keyType == 'double') {
value = parseFloat(value)
if(value.toString()=="NaN")
value = ""
}
if(value !== "" && valueInput.acceptableInput) {
2020-12-25 23:59:57 +00:00
control.model.setProperty(index, 'key', value)
control.changed()
}
}
}
2020-12-25 23:59:57 +00:00
Text {
id: postKeyText
visible: control.dictionaryMode
height: parent.height
verticalAlignment: TextInput.AlignVCenter
color: sysPalette.windowText
text: control.postKeyLabel
}
2020-12-25 23:59:57 +00:00
TextField {
id: valueInput
height: parent.height
2020-12-27 18:42:03 +00:00
width: parent.width - x - deleteButton.width - 5
2020-12-25 23:59:57 +00:00
validator: RegExpValidator {
regExp: control.valueRegexp
}
2020-12-25 23:59:57 +00:00
verticalAlignment: TextInput.AlignVCenter
horizontalAlignment: TextInput.AlignHCenter
color: sysPalette.windowText
text: visible ? control.model.get(index).val : false
2020-12-25 23:59:57 +00:00
selectByMouse: true
onEditingFinished: {
var value = text
if(control.valueType == 'int') {
value = parseInt(value)
if(value.toString()=="NaN")
value = ""
}
if(control.valueType == 'double') {
value = parseFloat(value)
if(value.toString()=="NaN")
value = ""
}
if(value !== "" && keyInput.acceptableInput) {
control.model.setProperty(index, 'val', value)
2020-12-25 23:59:57 +00:00
control.changed()
}
}
}
2020-12-27 18:42:03 +00:00
Item {
width: 5
height: parent.height
}
Button {
id: deleteButton
width: visible ? parent.height : 0
height: width
icon.source: './icons/delete.svg'
icon.name: 'delete'
visible: !control.forbidAdding
onClicked: {
control.model.remove(index)
}
}
}
}
2020-12-25 23:59:57 +00:00
Button {
id: addEntryBtn
2020-12-25 23:59:57 +00:00
visible: !control.forbidAdding
text: '+ Add Entry'
width: control.width
onClicked: {
control.model.append({
key: control.keyType == 'string' ? '' : model.count,
val: control.valueType == 'string' ? '' : 0
})
}
}
2020-12-25 23:59:57 +00:00
function importModel(importer) {
model.clear()
2020-12-25 23:59:57 +00:00
for(var key in importer)
model.append({
key: control.keyType == 'string' ? key.toString() : parseFloat(key),
val: control.valueType == 'string' ? importer[key].toString() : parseFloat(importer[key])
})
}
2020-12-25 23:59:57 +00:00
function exportModel() {
if(dictionaryMode) {
var ret = {}
for(var i = 0; i < model.count; i++)
ret[model.get(i).key] = model.get(i).val
2020-12-25 23:59:57 +00:00
return ret
} else {
var ret = []
for(var i = 0; i < model.count; i++)
ret.push(model.get(i).val)
return ret
2020-12-25 23:59:57 +00:00
}
}
}