Warning, /libraries/kirigami-addons/src/dateandtime/private/TumblerTimePicker.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2021 Han Young <hanyoung@protonmail.com>
0002 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0003 // SPDX-License-Identifier: LGPL-2.1-or-later
0004 
0005 import QtQuick 2.15
0006 import QtQuick.Controls 2.15
0007 import org.kde.kirigami 2.20 as Kirigami
0008 import QtQuick.Layouts 1.15
0009 import org.kde.kirigamiaddons.dateandtime 0.1
0010 
0011 /**
0012  * A large time picker
0013  * Represented as a clock provides a very visual way for a user
0014  * to set and visulise a time being chosen
0015  */
0016 RowLayout {
0017     id: root
0018 
0019     /**
0020      * This property holds the current hours selected. This is a number between 0 and 23.
0021      */
0022     property int hours
0023 
0024     /**
0025      * This property holds the current minutes selected. This is a number between 0 and 59.
0026      */
0027     property int minutes
0028 
0029     property bool _pm: false
0030 
0031     property bool _init: false
0032 
0033     readonly property bool _isAmPm: Qt.locale().timeFormat().includes("AP")
0034 
0035     implicitHeight: Kirigami.Units.gridUnit * 5
0036     implicitWidth: Kirigami.Units.gridUnit * 10
0037 
0038     Component.onCompleted: {
0039         hoursTumbler.currentIndex = (_isAmPm && hours > 12 ? hours - 12 : hours);
0040         minutesTumbler.currentIndex = minutes;
0041         if (_isAmPm) {
0042             amPmTumbler.currentIndex = hours > 12 ? 1 : 0;
0043         }
0044 
0045         // Avoid initialisation bug where thumbler are by default initialised
0046         // to currentIndex 0
0047         _init = true;
0048     }
0049 
0050     function formatText(count, modelData) {
0051         var data = count === 12 && modelData === 0 ? 12 : modelData;
0052         return data.toString().length < 2 ? "0" + data : data;
0053     }
0054 
0055     FontMetrics {
0056         id: fontMetrics
0057     }
0058 
0059     Component {
0060         id: delegateComponent
0061         Label {
0062             text: formatText(Tumbler.tumbler.count, modelData)
0063             opacity: 1.0 - Math.abs(Tumbler.displacement) / (Tumbler.tumbler.visibleItemCount / 2)
0064             horizontalAlignment: Text.AlignHCenter
0065             verticalAlignment: Text.AlignVCenter
0066             font.pixelSize: fontMetrics.font.pixelSize * 1.25
0067         }
0068     }
0069 
0070     Item {
0071         Layout.fillWidth: true
0072     }
0073 
0074     Tumbler {
0075         id: hoursTumbler
0076         Layout.preferredHeight: Kirigami.Units.gridUnit * 10
0077         model: _isAmPm ? 12 : 24
0078         delegate: delegateComponent
0079         visibleItemCount: 5
0080         onCurrentIndexChanged: if (_init) {
0081             console.error(_isAmPm, _pm, currentIndex)
0082             hours = currentIndex + (_isAmPm && _pm ? 12 : 0)
0083         }
0084     }
0085 
0086     Label {
0087         Layout.alignment: Qt.AlignCenter
0088         text: i18ndc("kirigami-addons", "Time separator", ":")
0089         font.pointSize: Kirigami.Theme.defaultFont.pointSize * 1.3
0090     }
0091 
0092     Tumbler {
0093         id: minutesTumbler
0094         Layout.preferredHeight: Kirigami.Units.gridUnit * 10
0095         model: 60
0096         delegate: delegateComponent
0097         visibleItemCount: 5
0098         onCurrentIndexChanged: if (_init) {
0099             minutes = currentIndex;
0100         }
0101     }
0102 
0103     Tumbler {
0104         id: amPmTumbler
0105         visible: _isAmPm
0106         Layout.preferredHeight: Kirigami.Units.gridUnit * 10
0107         model: [Qt.locale().amText, Qt.locale().pmText]
0108         delegate: delegateComponent
0109         visibleItemCount: 5
0110         onCurrentIndexChanged: if (_isAmPm && _init) {
0111             _pm = currentIndex;
0112             hours = (hours + 12) % 24;
0113         }
0114     }
0115 
0116     Item {
0117         Layout.fillWidth: true
0118     }
0119 }