|
| 1 | +// Copyright (c) 2024 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +import QtQuick 2.15 |
| 6 | +import QtQuick.Controls 2.15 |
| 7 | + |
| 8 | +TextInput { |
| 9 | + id: root |
| 10 | + required property string parentState |
| 11 | + property string description: "" |
| 12 | + property bool filled: false |
| 13 | + property int descriptionSize: 18 |
| 14 | + property color textColor: root.filled ? Theme.color.neutral9 : Theme.color.neutral5 |
| 15 | + // Expose a property to indicate validity, initial value will be true (no error message displayed) |
| 16 | + property bool validInput: true |
| 17 | + enabled: true |
| 18 | + state: root.parentState |
| 19 | + validator: RegExpValidator { regExp: /[0-9.:]*/ } // Allow only digits, dots, and colons |
| 20 | + |
| 21 | + maximumLength: 21 |
| 22 | + |
| 23 | + states: [ |
| 24 | + State { |
| 25 | + name: "ACTIVE" |
| 26 | + PropertyChanges { target: root; textColor: Theme.color.orange } |
| 27 | + }, |
| 28 | + State { |
| 29 | + name: "HOVER" |
| 30 | + PropertyChanges { |
| 31 | + target: root |
| 32 | + textColor: root.filled ? Theme.color.orangeLight1 : Theme.color.neutral5 |
| 33 | + } |
| 34 | + }, |
| 35 | + State { |
| 36 | + name: "DISABLED" |
| 37 | + PropertyChanges { |
| 38 | + target: root |
| 39 | + enabled: false |
| 40 | + textColor: Theme.color.neutral4 |
| 41 | + } |
| 42 | + } |
| 43 | + ] |
| 44 | + |
| 45 | + font.family: "Inter" |
| 46 | + font.styleName: "Regular" |
| 47 | + font.pixelSize: root.descriptionSize |
| 48 | + color: root.textColor |
| 49 | + text: root.description |
| 50 | + horizontalAlignment: Text.AlignRight |
| 51 | + wrapMode: Text.WordWrap |
| 52 | + |
| 53 | + Behavior on color { |
| 54 | + ColorAnimation { duration: 150 } |
| 55 | + } |
| 56 | + |
| 57 | + function isValidIPPort(input) |
| 58 | + { |
| 59 | + var parts = input.split(":"); |
| 60 | + if (parts.length !== 2) return false; |
| 61 | + if (parts[1].length === 0) return false; // port part is empty |
| 62 | + var ipAddress = parts[0]; |
| 63 | + var ipAddressParts = ipAddress.split("."); |
| 64 | + if (ipAddressParts.length !== 4) return false; |
| 65 | + for (var i = 0; (i < ipAddressParts.length); i++) { |
| 66 | + if (ipAddressParts[i].length === 0) return false; // ip group number part is empty |
| 67 | + if (parseInt(ipAddressParts[i]) > 255) return false; |
| 68 | + } |
| 69 | + var port = parseInt(parts[1]); |
| 70 | + if (port < 1 || port > 65535) return false; |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + // Connections element to ensure validation on editing finished |
| 75 | + Connections { |
| 76 | + target: root |
| 77 | + onEditingFinished: { |
| 78 | + // Validate the input whenever editing is finished |
| 79 | + validInput = isValidIPPort(root.text); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments