Warning, /pim/kube/views/calendar/qml/TimeSelector.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  Copyright (C) 2017 Michael Bohlender, <bohlender@kolabsys.com>
0003  *  Copyright (C) 2018 Christian Mollekopf, <mollekopf@kolabsys.com>
0004  *
0005  *  This program is free software; you can redistribute it and/or modify
0006  *  it under the terms of the GNU General Public License as published by
0007  *  the Free Software Foundation; either version 2 of the License, or
0008  *  (at your option) any later version.
0009  *
0010  *  This program is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0013  *  GNU General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU General Public License along
0016  *  with this program; if not, write to the Free Software Foundation, Inc.,
0017  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018  */
0019 
0020 import QtQuick 2.7
0021 import QtQuick.Layouts 1.2
0022 import org.kube.framework 1.0 as Kube
0023 import Qt.labs.calendar 1.0
0024 
0025 import "dateutils.js" as DateUtils
0026 
0027 Kube.ComboBox {
0028     id: root
0029 
0030     property int delta: 15
0031     property date dateTime
0032     property var notBefore: new Date(0)
0033     property var notBeforeRounded: DateUtils.roundToMinutes(notBefore, delta)
0034 
0035 
0036     onNotBeforeRoundedChanged: {
0037         root.setNotBefore(notBeforeRounded)
0038     }
0039 
0040     function generateTimes(start, delta) {
0041         var d = new Date(2000, 1, 1, start.getHours(), start.getMinutes(), start.getSeconds())
0042         var list = []
0043         while (d.getDate() == 1) {
0044             list.push(dateToString(d))
0045             d = DateUtils.addMinutesToDate(d, delta)
0046         }
0047         return list
0048     }
0049 
0050     function setNotBefore(notBefore) {
0051         availableTimes = generateTimes(DateUtils.sameDay(notBefore, root.dateTime) ? notBefore : new Date(2000, 1, 1, 0, 0, 0), root.delta)
0052         model = availableTimes
0053         currentIndex = findCurrentIndex(root.dateTime, root.delta)
0054         if (currentIndex >= 0) {
0055             setTimeFromIndex(currentIndex)
0056         } else {
0057             currentIndex = 0
0058             setTimeFromIndex(currentIndex)
0059         }
0060     }
0061 
0062     property var availableTimes: null
0063 
0064     function dateToString(date) {
0065         return date.toLocaleTimeString(Qt.locale(), "hh:mm")
0066     }
0067 
0068     function findCurrentIndex(date, delta) {
0069         var s = dateToString(DateUtils.roundToMinutes(date, delta))
0070         //Find does not reliably work if we just reset the model
0071         return availableTimes.indexOf(s)
0072         // return find(s)
0073     }
0074 
0075     function setTimeFromIndex(index) {
0076         var date = Date.fromLocaleTimeString(Qt.locale(), availableTimes[index], "hh:mm")
0077         //Intermediate variable is necessary for binding to be updated
0078         var newDate = root.dateTime
0079         newDate.setHours(date.getHours(), date.getMinutes())
0080         root.dateTime = newDate
0081     }
0082 
0083     Component.onCompleted: {
0084         setNotBefore(root.notBeforeRounded)
0085     }
0086 
0087     onActivated: {
0088         setTimeFromIndex(index)
0089     }
0090 
0091     model: availableTimes
0092 }