From 7121bc7b05befa9fd4be5e38419358971a8a97f4 Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Sun, 24 Jun 2018 13:33:02 +0200 Subject: [PATCH] added QML time edit --- examples/mvvmcore/SampleCore/settings.xml | 38 ++++++--- src/imports/mvvmquick/TimeEdit.qml | 99 +++++++++++++++++++++++ src/imports/mvvmquick/mvvmquick.pro | 3 +- src/imports/mvvmquick/qmldir | 1 + src/mvvmquick/TimeEdit.qml | 19 +++++ src/mvvmquick/inputviewfactory.cpp | 6 +- src/mvvmquick/qtmvvmquick_module.qrc | 1 + src/mvvmwidgets/inputwidgetfactory.cpp | 10 ++- 8 files changed, 160 insertions(+), 17 deletions(-) create mode 100644 src/imports/mvvmquick/TimeEdit.qml create mode 100644 src/mvvmquick/TimeEdit.qml diff --git a/examples/mvvmcore/SampleCore/settings.xml b/examples/mvvmcore/SampleCore/settings.xml index bb5ea60..e814756 100644 --- a/examples/mvvmcore/SampleCore/settings.xml +++ b/examples/mvvmcore/SampleCore/settings.xml @@ -100,14 +100,6 @@ default="42"> Is set to %n unit(s) - - 0 - 100 - %1 % - - + 0 + 100 + %1 % + + + Selected time: %1 + 1 + + + Selected date: %1 + dd.MM.yyyy + + + Selected date and time: %1 + + diff --git a/src/imports/mvvmquick/TimeEdit.qml b/src/imports/mvvmquick/TimeEdit.qml new file mode 100644 index 0000000..91dd540 --- /dev/null +++ b/src/imports/mvvmquick/TimeEdit.qml @@ -0,0 +1,99 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 +import QtQuick.Layouts 1.3 + +RowLayout { + id: _edit + spacing: 0 + + property date time: new Date() + onTimeChanged: { + if(!_skipChange) + _forceTime = time + } + + property var _forceTime: new Date() + + readonly property bool is24Hours: { + var fmStr = Qt.locale().timeFormat(Locale.LongFormat); + var isApPm = false; + ["A", "AP", "a", "ap"].forEach(function(text){ + if(fmStr.indexOf(text) !== -1) + isApPm = true; + }); + return !isApPm; + } + + readonly property int hours: _hourTumbler.currentIndex + (!is24Hours && _amPmTumbler.currentIndex === 1 ? 12 : 0); + readonly property int minutes: _minuteTumbler.currentIndex; + property bool _skipChange: false + + function recalcTime() { + var hours = _hourTumbler.currentIndex + (!is24Hours && _amPmTumbler.currentIndex === 1 ? 12 : 0); + var minutes = _minuteTumbler.currentIndex; + if(_forceTime.getHours() !== hours || + _forceTime.getMinutes() !== minutes) { + _skipChange = true; + time = new Date(0, 0, 0, hours, minutes); + _skipChange = false; + } + } + + Item { + id: _spacer1 + Layout.fillHeight: true + Layout.fillWidth: true + Layout.preferredWidth: 0 + } + + Tumbler { + id: _hourTumbler + model: { + var model = new Array(is24Hours ? 24 : 12); + for(var i = 0; i < model.length; i++) + model[i] = i.toString(); + if(!is24Hours) + model[0] = 12; + return model; + } + currentIndex: is24Hours ? _forceTime.getHours() : (_forceTime.getHours() % 12) + onCurrentIndexChanged: Qt.callLater(recalcTime) + } + + Label { + text: qsTr(":") + Layout.minimumWidth: implicitWidth + } + + Tumbler { + id: _minuteTumbler + model: { + var mod = new Array(60) + for(var i = 0; i < mod.length; i++) { + var data = i.toString(); + mod[i] = data.length < 2 ? Qt.locale().zeroDigit + data : data; + } + return mod; + } + currentIndex: _forceTime.getMinutes() + onCurrentIndexChanged: Qt.callLater(recalcTime) + } + + Tumbler { + id: _amPmTumbler + visible: !is24Hours + model: [ + Qt.locale().amText, + Qt.locale().pmText, + ] + currentIndex: _forceTime.getHours() >= 12 ? 1 : 0 + onCurrentIndexChanged: Qt.callLater(recalcTime) + } + + Item { + id: _spacer2 + Layout.fillHeight: true + Layout.fillWidth: true + Layout.preferredWidth: 0 + } +} diff --git a/src/imports/mvvmquick/mvvmquick.pro b/src/imports/mvvmquick/mvvmquick.pro index db7e55b..1528544 100644 --- a/src/imports/mvvmquick/mvvmquick.pro +++ b/src/imports/mvvmquick/mvvmquick.pro @@ -53,7 +53,8 @@ QML_FILES += \ SearchBar.qml \ RoundMenuButton.qml \ DecorLabel.qml \ - MsgDelegate.qml + MsgDelegate.qml \ + TimeEdit.qml RESOURCES += \ qtmvvmquick_plugin.qrc diff --git a/src/imports/mvvmquick/qmldir b/src/imports/mvvmquick/qmldir index cf7f2c9..8374717 100644 --- a/src/imports/mvvmquick/qmldir +++ b/src/imports/mvvmquick/qmldir @@ -27,6 +27,7 @@ SearchBar 1.1 SearchBar.qml RoundMenuButton 1.1 RoundMenuButton.qml DecorLabel 1.1 DecorLabel.qml MsgDelegate 1.1 MsgDelegate.qml +TimeEdit 1.1 TimeEdit.qml PresenterProgress 1.0 PresenterProgress.qml PresentingStackView 1.0 PresentingStackView.qml diff --git a/src/mvvmquick/TimeEdit.qml b/src/mvvmquick/TimeEdit.qml new file mode 100644 index 0000000..c5d06fd --- /dev/null +++ b/src/mvvmquick/TimeEdit.qml @@ -0,0 +1,19 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 +import QtQuick.Layouts 1.3 +import de.skycoder42.QtMvvm.Quick 1.1 as QtMvvm + +QtMvvm.TimeEdit { + id: _edit + + property var inputValue: new Date() + + onInputValueChanged: { + var realTime = typeof inputValue == "string" ? new Date("2000-01-01T" + inputValue) : inputValue + if(realTime.getHours() !== time.getHours() || + realTime.getMinutes() !== time.getMinutes()) { + time = realTime + } + } + onTimeChanged: inputValue = time +} diff --git a/src/mvvmquick/inputviewfactory.cpp b/src/mvvmquick/inputviewfactory.cpp index 6f817a6..d3ccd00 100644 --- a/src/mvvmquick/inputviewfactory.cpp +++ b/src/mvvmquick/inputviewfactory.cpp @@ -115,9 +115,9 @@ InputViewFactoryPrivate::InputViewFactoryPrivate() : {QMetaType::typeName(QMetaType::Double), QStringLiteral("qrc:/qtmvvm/inputs/DoubleSpinBox.qml")}, {"number", QStringLiteral("qrc:/qtmvvm/inputs/DoubleSpinBox.qml")}, {"range", QStringLiteral("qrc:/qtmvvm/inputs/Slider.qml")}, -// {QMetaType::typeName(QMetaType::QDate), QStringLiteral("qrc:/qtmvvm/inputs/.qml")}, -// {QMetaType::typeName(QMetaType::QTime), QStringLiteral("qrc:/qtmvvm/inputs/.qml")}, -// {QMetaType::typeName(QMetaType::QDateTime), QStringLiteral("qrc:/qtmvvm/inputs/.qml")}, +// {QMetaType::typeName(QMetaType::QDate), QStringLiteral("qrc:/qtmvvm/inputs/")}, + {QMetaType::typeName(QMetaType::QTime), QStringLiteral("qrc:/qtmvvm/inputs/TimeEdit.qml")}, +// {QMetaType::typeName(QMetaType::QDateTime), QStringLiteral("qrc:/qtmvvm/inputs/")}, // {"date", QStringLiteral("qrc:/qtmvvm/inputs/.qml")}, {QMetaType::typeName(QMetaType::QFont), QStringLiteral("qrc:/qtmvvm/inputs/FontEdit.qml")}, {QMetaType::typeName(QMetaType::QUrl), QStringLiteral("qrc:/qtmvvm/inputs/UrlField.qml")}, diff --git a/src/mvvmquick/qtmvvmquick_module.qrc b/src/mvvmquick/qtmvvmquick_module.qrc index deda855..0284365 100644 --- a/src/mvvmquick/qtmvvmquick_module.qrc +++ b/src/mvvmquick/qtmvvmquick_module.qrc @@ -10,6 +10,7 @@ Switch.qml RadioListEdit.qml Slider.qml + TimeEdit.qml BoolDelegate.qml diff --git a/src/mvvmwidgets/inputwidgetfactory.cpp b/src/mvvmwidgets/inputwidgetfactory.cpp index b511ee3..ae1de18 100644 --- a/src/mvvmwidgets/inputwidgetfactory.cpp +++ b/src/mvvmwidgets/inputwidgetfactory.cpp @@ -54,13 +54,15 @@ QWidget *InputWidgetFactory::createInput(const QByteArray &type, QWidget *parent widget = new QDoubleSpinBox(parent); else if(type == "range") widget = new ToolTipSlider(parent); - else if(type == QMetaType::typeName(QMetaType::QDate)) + else if(type == QMetaType::typeName(QMetaType::QDate)) { widget = new QDateEdit(parent); - else if(type == QMetaType::typeName(QMetaType::QTime)) + static_cast(widget)->setCalendarPopup(true); + } else if(type == QMetaType::typeName(QMetaType::QTime)) widget = new QTimeEdit(parent); - else if(type == QMetaType::typeName(QMetaType::QDateTime) || type == "date") + else if(type == QMetaType::typeName(QMetaType::QDateTime) || type == "date") { widget = new QDateTimeEdit(parent); - else if(type == QMetaType::typeName(QMetaType::QFont)) + static_cast(widget)->setCalendarPopup(true); + } else if(type == QMetaType::typeName(QMetaType::QFont)) widget = new FontComboBox(parent); else if(type == QMetaType::typeName(QMetaType::QKeySequence)) widget = new QKeySequenceEdit(parent);