From 2a8b34ac7965d62c3aedb686b53245e80041fc1d Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Tue, 27 Feb 2018 14:11:49 +0100 Subject: [PATCH] added list delegate for simpler list editing --- examples/mvvmcore/SampleCore/settings.xml | 10 ++++++ src/imports/mvvmquick/OverviewListView.qml | 14 ++++++++- .../icons/ic_chevron_right_white_24px.svg | 4 +++ src/imports/mvvmquick/qtmvvmquick_plugin.qrc | 1 + src/imports/mvvmquick/settingsentrymodel.cpp | 2 +- src/mvvmquick/BoolDelegate.qml | 14 +++++++-- src/mvvmquick/ListDelegate.qml | 31 +++++++++++++++++++ src/mvvmquick/MsgDelegate.qml | 2 +- src/mvvmquick/RadioListEdit.qml | 27 ++++++++++++++++ src/mvvmquick/SwitchDelegate.qml | 14 +++++++-- src/mvvmquick/inputviewfactory.cpp | 10 +++--- src/mvvmquick/qtmvvmquick_module.qrc | 2 ++ 12 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 src/imports/mvvmquick/icons/ic_chevron_right_white_24px.svg create mode 100644 src/mvvmquick/ListDelegate.qml create mode 100644 src/mvvmquick/RadioListEdit.qml diff --git a/examples/mvvmcore/SampleCore/settings.xml b/examples/mvvmcore/SampleCore/settings.xml index c1dc4ee..02dbc86 100644 --- a/examples/mvvmcore/SampleCore/settings.xml +++ b/examples/mvvmcore/SampleCore/settings.xml @@ -89,5 +89,15 @@ type="int" title="Enter a &number" default="42" /> + + true + + Text 1 + Text 2 + + diff --git a/src/imports/mvvmquick/OverviewListView.qml b/src/imports/mvvmquick/OverviewListView.qml index f43fec6..c880015 100644 --- a/src/imports/mvvmquick/OverviewListView.qml +++ b/src/imports/mvvmquick/OverviewListView.qml @@ -24,7 +24,7 @@ ListView { contentItem: GridLayout { id: grid rows: 2 - columns: 2 + columns: 3 columnSpacing: 14 TintIcon { @@ -59,6 +59,18 @@ ListView { wrapMode: Text.WordWrap Layout.fillWidth: true } + + TintIcon { + id: openIcon + source: "image://svg/de/skycoder42/qtmvvm/quick/icons/ic_chevron_right" + Layout.row: 0 + Layout.column: 2 + Layout.rowSpan: 2 + Layout.fillHeight: true + Layout.preferredWidth: iconSize.width + Layout.preferredHeight: iconSize.height + Layout.alignment: Qt.AlignVCenter | Qt.AlignRight + } } } } diff --git a/src/imports/mvvmquick/icons/ic_chevron_right_white_24px.svg b/src/imports/mvvmquick/icons/ic_chevron_right_white_24px.svg new file mode 100644 index 0000000..bbb978a --- /dev/null +++ b/src/imports/mvvmquick/icons/ic_chevron_right_white_24px.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/imports/mvvmquick/qtmvvmquick_plugin.qrc b/src/imports/mvvmquick/qtmvvmquick_plugin.qrc index 8ee5735..06f4725 100644 --- a/src/imports/mvvmquick/qtmvvmquick_plugin.qrc +++ b/src/imports/mvvmquick/qtmvvmquick_plugin.qrc @@ -7,6 +7,7 @@ icons/ic_close_white_24px.svg icons/ic_search_white_24px.svg icons/ic_settings_backup_restore_white_24px.svg + icons/ic_chevron_right_white_24px.svg FileDialog.qml diff --git a/src/imports/mvvmquick/settingsentrymodel.cpp b/src/imports/mvvmquick/settingsentrymodel.cpp index c66e7ac..d7a2008 100644 --- a/src/imports/mvvmquick/settingsentrymodel.cpp +++ b/src/imports/mvvmquick/settingsentrymodel.cpp @@ -92,7 +92,7 @@ QHash SettingsEntryModel::roleNames() const {TitleRole, "title"}, {ToolTipRole, "tooltip"}, {DelegateUrlRole, "delegateUrl"}, - {SettingsValueRole, "settingsValue"}, + {SettingsValueRole, "inputValue"}, {PropertiesRole, "properties"} }; } diff --git a/src/mvvmquick/BoolDelegate.qml b/src/mvvmquick/BoolDelegate.qml index aaff24a..9e64824 100644 --- a/src/mvvmquick/BoolDelegate.qml +++ b/src/mvvmquick/BoolDelegate.qml @@ -7,8 +7,18 @@ CheckDelegate { text: title - Component.onCompleted: checked = settingsValue; //TODO buggy, use binding? - onCheckedChanged: settingsValue = checked; + function asBool(value) { + if(typeof value == "string") + return value === "true"; + else + return Boolean(value); + } + + Component.onCompleted: checked = asBool(inputValue) + onCheckedChanged: { + if(asBool(inputValue) !== checked) + inputValue = checked; + } contentItem: GridLayout { columns: 2 diff --git a/src/mvvmquick/ListDelegate.qml b/src/mvvmquick/ListDelegate.qml new file mode 100644 index 0000000..a548db6 --- /dev/null +++ b/src/mvvmquick/ListDelegate.qml @@ -0,0 +1,31 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 +import QtQuick.Layouts 1.3 + +ItemDelegate { + id: _listDelegate + + text: title + + signal showInput(string key, string title, string type, var defaultValue, var properties); + + contentItem: ColumnLayout { + Label { + id: _titleLabel + text: _listDelegate.text + font.bold: true + elide: Label.ElideRight + Layout.fillWidth: true + } + + Label { + id: _textLabel + visible: tooltip + text: tooltip + wrapMode: Text.WordWrap + Layout.fillWidth: true + } + } + + onClicked: showInput(key, title, "radiolist", inputValue, properties) +} diff --git a/src/mvvmquick/MsgDelegate.qml b/src/mvvmquick/MsgDelegate.qml index 87a5d36..43d354b 100644 --- a/src/mvvmquick/MsgDelegate.qml +++ b/src/mvvmquick/MsgDelegate.qml @@ -27,5 +27,5 @@ ItemDelegate { } } - onClicked: showInput(key, title, type, settingsValue, properties) + onClicked: showInput(key, title, type, inputValue, properties) } diff --git a/src/mvvmquick/RadioListEdit.qml b/src/mvvmquick/RadioListEdit.qml new file mode 100644 index 0000000..84d89ae --- /dev/null +++ b/src/mvvmquick/RadioListEdit.qml @@ -0,0 +1,27 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +ListView { + id: _edit + property var inputValue + property alias listElements: _edit.model + readonly property bool isExtended: Boolean(model[0] && model[0].name) + + implicitHeight: dummyDelegate.height * model.length + + ScrollBar.vertical: ScrollBar {} + + delegate: RadioDelegate { + width: parent.width + text: isExtended ? _edit.model[index].name : modelData + + checked: (isExtended ? _edit.model[index].value : modelData) == inputValue + onClicked: inputValue = (isExtended ? _edit.model[index].value : modelData) + } + + RadioDelegate { + id:dummyDelegate + visible: false + text: "dummy" + } +} diff --git a/src/mvvmquick/SwitchDelegate.qml b/src/mvvmquick/SwitchDelegate.qml index 396a553..c5251b0 100644 --- a/src/mvvmquick/SwitchDelegate.qml +++ b/src/mvvmquick/SwitchDelegate.qml @@ -7,8 +7,18 @@ Controls.SwitchDelegate { text: title - Component.onCompleted: checked = settingsValue; - onCheckedChanged: settingsValue = checked; + function asBool(value) { + if(typeof value == "string") + return value === "true"; + else + return Boolean(value); + } + + Component.onCompleted: checked = asBool(inputValue) + onCheckedChanged: { + if(asBool(inputValue) !== checked) + inputValue = checked; + } contentItem: GridLayout { columns: 2 diff --git a/src/mvvmquick/inputviewfactory.cpp b/src/mvvmquick/inputviewfactory.cpp index 9da13ff..7312fe9 100644 --- a/src/mvvmquick/inputviewfactory.cpp +++ b/src/mvvmquick/inputviewfactory.cpp @@ -67,6 +67,8 @@ QUrl InputViewFactory::getInputUrl(const QByteArray &type, const QVariantMap &vi return QStringLiteral("qrc:/qtmvvm/inputs/UrlField.qml"); else if(type == "selection" || type == "list") return QStringLiteral("qrc:/qtmvvm/inputs/ListEdit.qml"); + else if(type == "radiolist") + return QStringLiteral("qrc:/qtmvvm/inputs/RadioListEdit.qml"); else { logCritical() << "Failed to find any input view for input type:" << type; return QUrl(); @@ -83,10 +85,10 @@ QUrl InputViewFactory::getDelegate(const QByteArray &type, const QVariantMap &vi else if(type == QMetaType::typeName(QMetaType::Bool)) return QStringLiteral("qrc:/qtmvvm/delegates/BoolDelegate.qml"); else if(type == "switch") - return QStringLiteral("qrc:/qtmvvm/inputs/SwitchDelegate.qml"); - //TODO add -// else if(type == "selection" || type == "list") -// return QStringLiteral("qrc:/qtmvvm/inputs/ListDelegate.qml"); + return QStringLiteral("qrc:/qtmvvm/delegates/SwitchDelegate.qml"); + else if((type == "selection" || type == "list") && + !viewProperties.value(QStringLiteral("editable"), false).toBool()) + return QStringLiteral("qrc:/qtmvvm/delegates/ListDelegate.qml"); else return QStringLiteral("qrc:/qtmvvm/delegates/MsgDelegate.qml"); } diff --git a/src/mvvmquick/qtmvvmquick_module.qrc b/src/mvvmquick/qtmvvmquick_module.qrc index 1efe95e..bb7821f 100644 --- a/src/mvvmquick/qtmvvmquick_module.qrc +++ b/src/mvvmquick/qtmvvmquick_module.qrc @@ -8,11 +8,13 @@ FontEdit.qml UrlField.qml Switch.qml + RadioListEdit.qml BoolDelegate.qml MsgDelegate.qml SwitchDelegate.qml + ListDelegate.qml SettingsView.qml