Changeset View
Changeset View
Standalone View
Standalone View
kcms/nightcolor/package/contents/ui/TimeField.qml
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | Copyright 2017 Roman Gilg <subdiff@gmail.com> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or modify | ||||
5 | it under the terms of the GNU General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or | ||||
7 | (at your option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | GNU General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License | ||||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
16 | *********************************************************************/ | ||||
17 | import QtQuick 2.1 | ||||
18 | import QtQuick.Layouts 1.1 | ||||
19 | import QtQuick.Controls 1.4 as Controls | ||||
20 | | ||||
21 | Controls.TextField { | ||||
22 | id: field | ||||
23 | | ||||
24 | property date backend | ||||
25 | horizontalAlignment: TextInput.AlignHCenter | ||||
26 | | ||||
27 | onBackendChanged: readIn() | ||||
28 | | ||||
29 | function readIn() { | ||||
30 | if (!backend) { | ||||
31 | return; | ||||
32 | } | ||||
33 | | ||||
34 | var hours = backend.getHours(); | ||||
35 | var minutes = backend.getMinutes(); | ||||
36 | if (hours < 10) { | ||||
37 | hours = "0" + hours; | ||||
38 | } | ||||
39 | if (minutes < 10) { | ||||
40 | minutes = "0" + minutes; | ||||
41 | } | ||||
42 | | ||||
43 | text = hours + ":" + minutes; | ||||
44 | } | ||||
45 | | ||||
46 | function submit() { | ||||
47 | if (text.length != 5) { | ||||
48 | return; | ||||
49 | } | ||||
50 | var hours = text.slice(0, 2); | ||||
51 | var minutes = text.slice(3, 5); | ||||
52 | | ||||
53 | var date = new Date('1970-01-01'); | ||||
54 | date.setHours(hours); | ||||
55 | date.setMinutes(minutes); | ||||
56 | | ||||
57 | backend = date; | ||||
58 | } | ||||
59 | | ||||
60 | inputMethodHints: Qt.ImhPreferNumbers | ||||
61 | validator: RegExpValidator { regExp: /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/ } | ||||
62 | | ||||
63 | onEditingFinished: submit() | ||||
64 | } | ||||
65 | |