Warning, /plasma/latte-dock/declarativeimports/components/TextField.qml is written in an unsupported language. File is not indexed.

0001 /*
0002 *  Copyright 2019  Michail Vourlakos <mvourlakos@gmail.com>
0003 *
0004 *  This file is part of Latte-Dock
0005 *
0006 *  Latte-Dock is free software; you can redistribute it and/or
0007 *  modify it under the terms of the GNU General Public License as
0008 *  published by the Free Software Foundation; either version 2 of
0009 *  the License, or (at your option) any later version.
0010 *
0011 *  Latte-Dock is distributed in the hope that it will be useful,
0012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 *  GNU General Public License for more details.
0015 *
0016 *  You should have received a copy of the GNU General Public License
0017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 import QtQuick 2.0
0020 import QtQuick.Layouts 1.3
0021 import QtQuick.Controls 1.4
0022 
0023 import org.kde.plasma.components 2.0 as PlasmaComponents
0024 
0025 PlasmaComponents.TextField {
0026     id: textField
0027 
0028     validator: IntValidator {
0029         bottom: minValue
0030         top: maxValue
0031     }
0032 
0033     onTextChanged: {
0034         if (text.trim() === minValue.toString())
0035             text = ""
0036     }
0037 
0038     font.italic: true
0039     inputMethodHints: Qt.ImhDigitsOnly
0040     placeholderText: i18n("none")
0041     horizontalAlignment: Text.AlignLeft
0042 
0043     width: internalContent.width + theme.mSize(theme.defaultFont).width * 3.5
0044 
0045     readonly property int value: text === "" ? minValue : parseInt(text)
0046     property int step: 100
0047     property int minValue: 0
0048     property int maxValue: 3000
0049 
0050     function increment() {
0051         var val = text === "" ? minValue : parseInt(text)
0052         text = Math.min(val + step, maxValue).toString()
0053     }
0054 
0055     function decrement() {
0056         var val = text === "" ? minValue : parseInt(text)
0057         val = Math.max(val - step, minValue)
0058         text = val === minValue ? "" : val.toString()
0059     }
0060 
0061     RowLayout {
0062         id: internalContent
0063         spacing: 0
0064         anchors.top: parent.top
0065         anchors.bottom: parent.bottom
0066         anchors.right: parent.right
0067 
0068         LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
0069         LayoutMirroring.childrenInherit: true
0070 
0071         PlasmaComponents.Label {
0072             Layout.alignment: Qt.AlignVCenter
0073             color: textField.textColor
0074             text: i18n("ms.")
0075             font.italic: true
0076             opacity: value === 0 ? 0 : 0.6
0077         }
0078         PlasmaComponents.Button {
0079             id: downButton
0080 
0081             Layout.fillHeight: true
0082             Layout.preferredWidth: height
0083             Layout.maximumWidth: height
0084             Layout.leftMargin: Qt.application.layoutDirection === Qt.RightToLeft ? 0 : 0.7 * theme.mSize(theme.defaultFont).width
0085             Layout.rightMargin: Qt.application.layoutDirection === Qt.RightToLeft ? 0.7 * theme.mSize(theme.defaultFont).width : 0
0086 
0087             text: "-"
0088             onClicked: decrement()
0089         }
0090         PlasmaComponents.Button {
0091             id: upButton
0092 
0093             Layout.fillHeight: true
0094             Layout.preferredWidth: height
0095             Layout.maximumWidth: height
0096             text: "+"
0097             onClicked: increment()
0098         }
0099     }
0100 
0101     Timer {
0102         id: holdPressed
0103         running: upButton.pressed || downButton.pressed
0104         interval: 200
0105         repeat: true
0106 
0107         onRunningChanged: {
0108             if (!running)
0109                 interval = 200
0110         }
0111 
0112         onTriggered: {
0113             if (interval === 200)
0114                 interval = 150
0115             else if (upButton.pressed)
0116                 increment()
0117             else
0118                 decrement()
0119         }
0120     }
0121 
0122     MouseArea {
0123         anchors.fill: parent
0124         acceptedButtons: Qt.MiddleButton
0125 
0126         onWheel: {
0127             var angle = wheel.angleDelta.y / 8
0128 
0129             if (angle > 0) {
0130                 increment()
0131             } else if (angle < 0) {
0132                 decrement()
0133             }
0134         }
0135     }
0136 }