Warning, /plasma/powerdevil/kcmodule/profiles/ui/TimeDelaySpinBox.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *  SPDX-FileCopyrightText: 2023 Jakob Petsovits <jpetso@petsovits.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 import QtQuick
0008 import QtQuick.Controls as QQC2
0009 import org.kde.kirigami as Kirigami
0010 
0011 QQC2.SpinBox {
0012     id: root
0013 
0014     readonly property string minutesSuffixes: i18nc("List of recognized strings for 'minutes' in a time delay expression such as 'after 10 min'", "m|min|mins|minutes")
0015     readonly property string secondsSuffixes: i18nc("List of recognized strings for 'seconds' in a time delay expression such as 'after 10 sec'", "s|sec|secs|seconds")
0016     readonly property var extractor: new RegExp(i18nc("Validator/extractor regular expression for a time delay number and unit, from e.g. 'after 10 min'. Uses recognized strings for minutes and seconds as %1 and %2.",
0017                                                       "[^\\d]*(\\d+)\\s*(%1|%2)\\s*", minutesSuffixes, secondsSuffixes))
0018 
0019     editable: true
0020     validator: RegularExpressionValidator {
0021         regularExpression: extractor
0022     }
0023 
0024     textFromValue: function(value, locale) {
0025         if (stepSize == 60 && value % 60 == 0) {
0026             return i18np("after %1 min", "after %1 min", value / 60);
0027         }
0028         return i18np("after %1 sec", "after %1 sec", value);
0029     }
0030 
0031     valueFromText: function(text, locale) {
0032         const match = text.match(root.extractor);
0033         if (match[1]) {
0034             const multiplier = minutesSuffixes.split("|").includes(match[2]) ? 60 : 1;
0035             return Number.fromLocaleString(locale, match[1]) * multiplier;
0036         }
0037         return root.value; // unchanged
0038     }
0039 
0040     TextMetrics {
0041         id: metricsMax
0042         text: textFromValue(to, root.locale)
0043     }
0044     TextMetrics {
0045         id: metricsAlmostMax
0046         text: textFromValue(to - stepSize, root.locale)
0047     }
0048     implicitWidth: Math.max(metricsMax.advanceWidth, metricsAlmostMax.advanceWidth) + Kirigami.Units.gridUnit * 2
0049 }