QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
TimeEdit.qml
1 import QtQuick 2.10
2 import QtQuick.Controls 2.3
3 import QtQuick.Layouts 1.3
4 
12 RowLayout {
13  id: _edit
14  spacing: 0
15 
27  property date time: new Date()
28  onTimeChanged: {
29  if(!_p.skipChange)
30  _p.forceTime = time
31  }
32 
42  property bool showHours: true
52  property bool showMinutes: true
62  property bool showSeconds: false
63 
78  property bool use24HourFormat: {
79  var fmStr = Qt.locale().timeFormat(Locale.LongFormat);
80  var isApPm = false;
81  ["A", "AP", "a", "ap"].forEach(function(text){
82  if(fmStr.indexOf(text) !== -1)
83  isApPm = true;
84  });
85  return !isApPm;
86  }
87 
98  readonly property int hours: showHours ? (_hourTumbler.currentIndex + (!use24HourFormat && _amPmTumbler.currentIndex === 1 ? 12 : 0)) : 0
109  readonly property int minutes: showMinutes ? _minuteTumbler.currentIndex : 0
120  readonly property int seconds: showSeconds ? _secondsTumbler.currentIndex : 0
121 
122  QtObject {
123  id: _p
124  property date forceTime: new Date()
125  property bool skipChange: false
126 
127  function recalcTime() {
128  if(_p.forceTime.getHours() !== hours ||
129  _p.forceTime.getMinutes() !== minutes ||
130  _p.forceTime.getSeconds() !== seconds) {
131  _p.skipChange = true;
132  time = new Date(0, 0, 0, hours, minutes, seconds);
133  _p.skipChange = false;
134  }
135  }
136  }
137 
138  Item {
139  id: _spacer1
140  Layout.fillHeight: true
141  Layout.fillWidth: true
142  Layout.preferredWidth: 0
143  }
144 
145  TimeTumbler {
146  id: _hourTumbler
147  visible: showHours
148  model: {
149  var model = new Array(use24HourFormat ? 24 : 12);
150  for(var i = 0; i < model.length; i++)
151  model[i] = i.toString();
152  if(!use24HourFormat)
153  model[0] = 12;
154  return model;
155  }
156  currentIndex: use24HourFormat ? _p.forceTime.getHours() : (_p.forceTime.getHours() % 12)
157  onCurrentIndexChanged: Qt.callLater(_p.recalcTime)
158  }
159 
160  Label {
161  text: qsTr(":")
162  Layout.minimumWidth: implicitWidth
163  visible: showHours && showMinutes
164  }
165 
166  TimeTumbler {
167  id: _minuteTumbler
168  visible: showMinutes
169  model: {
170  var mod = new Array(60)
171  for(var i = 0; i < mod.length; i++) {
172  var data = i.toString();
173  mod[i] = data.length < 2 ? Qt.locale().zeroDigit + data : data;
174  }
175  return mod;
176  }
177  currentIndex: _p.forceTime.getMinutes()
178  onCurrentIndexChanged: Qt.callLater(_p.recalcTime)
179  }
180 
181  Label {
182  text: qsTr(":")
183  Layout.minimumWidth: implicitWidth
184  visible: showSeconds && (showMinutes || showHours)
185  }
186 
187  TimeTumbler {
188  id: _secondsTumbler
189  visible: showSeconds
190  model: {
191  var mod = new Array(60)
192  for(var i = 0; i < mod.length; i++) {
193  var data = i.toString();
194  mod[i] = data.length < 2 ? Qt.locale().zeroDigit + data : data;
195  }
196  return mod;
197  }
198  currentIndex: _p.forceTime.getSeconds()
199  onCurrentIndexChanged: Qt.callLater(_p.recalcTime)
200  }
201 
202  TimeTumbler {
203  id: _amPmTumbler
204  visible: !use24HourFormat && showHours
205  model: [
206  Qt.locale().amText,
207  Qt.locale().pmText,
208  ]
209  currentIndex: _p.forceTime.getHours() >= 12 ? 1 : 0
210  onCurrentIndexChanged: Qt.callLater(_p.recalcTime)
211  }
212 
213  Item {
214  id: _spacer2
215  Layout.fillHeight: true
216  Layout.fillWidth: true
217  Layout.preferredWidth: 0
218  }
219 }