| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
// Copyright 2019 Juan Palacios <[email protected]> |
| 3 |
|
| 4 |
import QtQuick 2.15 |
| 5 |
import QtQuick.Controls 2.15 |
| 6 |
import QtQuick.Layouts 1.15 |
| 7 |
import "Style.js" as Style |
| 8 |
|
| 9 |
Dialog { |
| 10 |
id: dlg |
| 11 |
|
| 12 |
property string name |
| 13 |
property string exe |
| 14 |
property url icon |
| 15 |
property url defaultIcon |
| 16 |
property bool hasCustomIcon |
| 17 |
property bool forceAutomaticActivation: false |
| 18 |
|
| 19 |
property var newInfoAction: function(name, exe, icon) {} |
| 20 |
|
| 21 |
signal updateProfileNameUsed(string profileName) |
| 22 |
property bool profileNameUsed: false |
| 23 |
|
| 24 |
signal updateExecutableNameUsed(string executableName) |
| 25 |
property bool executableNameUsed: false |
| 26 |
|
| 27 |
focus: true |
| 28 |
modal: true |
| 29 |
standardButtons: Dialog.Ok | Dialog.Cancel |
| 30 |
closePolicy: Popup.CloseOnEscape |
| 31 |
|
| 32 |
// center in parent |
| 33 |
x: (parent.width - width) / 2 |
| 34 |
y: (parent.height - height) / 2 |
| 35 |
|
| 36 |
spacing: 10 |
| 37 |
implicitWidth: 360 |
| 38 |
|
| 39 |
QtObject { // private stuff |
| 40 |
id: p |
| 41 |
|
| 42 |
property url originalIcon |
| 43 |
property url selectedIcon |
| 44 |
|
| 45 |
property bool nameOK: false |
| 46 |
property bool exeOK: false |
| 47 |
property bool iconOK: false |
| 48 |
|
| 49 |
function validProfileName(name) { |
| 50 |
return name && name.trim().length > 0 |
| 51 |
} |
| 52 |
|
| 53 |
function validFileName(name) { |
| 54 |
return name && name.trim().length > 0 && |
| 55 |
!/\\|\/|\||\x00|\*|`|;|:|'|"/.test(name) |
| 56 |
} |
| 57 |
|
| 58 |
function validateName() { |
| 59 |
nameOK = activationCb.currentIndex === 0 ? validProfileName(name) : |
| 60 |
validFileName(name) |
| 61 |
return nameOK |
| 62 |
} |
| 63 |
|
| 64 |
function validateExe() { |
| 65 |
exeOK = activationCb.currentIndex === 0 ? validFileName(exe) : |
| 66 |
true |
| 67 |
return exeOK |
| 68 |
} |
| 69 |
|
| 70 |
function updateOKButtonState() { |
| 71 |
dlg.footer.standardButton(Dialog.Ok).enabled = nameOK && exeOK && iconOK |
| 72 |
} |
| 73 |
|
| 74 |
function resetState() { |
| 75 |
newInfoAction = function(name, exe, icon) {} |
| 76 |
originalIcon = "" |
| 77 |
selectedIcon = "" |
| 78 |
forceAutomaticActivation = false |
| 79 |
activationCb.currentIndex = -1 |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
onAboutToShow: { |
| 84 |
activationCb.currentIndex = forceAutomaticActivation ? 0 : |
| 85 |
exe.length > 0 ? 0 : 1 |
| 86 |
customizeIconCb.checked = hasCustomIcon |
| 87 |
p.originalIcon = icon |
| 88 |
} |
| 89 |
onClosed: p.resetState() |
| 90 |
onAccepted: newInfoAction(dlg.name, activationCb.currentIndex === 0 ? dlg.exe : "", dlg.icon) |
| 91 |
|
| 92 |
onNameChanged: { |
| 93 |
if (name !== nameTf.text) |
| 94 |
nameTf.text = name |
| 95 |
|
| 96 |
if (p.validateName()) { |
| 97 |
updateProfileNameUsed(name) |
| 98 |
p.nameOK &= !profileNameUsed |
| 99 |
} |
| 100 |
|
| 101 |
p.updateOKButtonState() |
| 102 |
} |
| 103 |
|
| 104 |
onExeChanged: { |
| 105 |
if (exe !== exeTf.text) |
| 106 |
exeTf.text = exe |
| 107 |
|
| 108 |
if (p.validateExe()) { |
| 109 |
updateExecutableNameUsed(exe) |
| 110 |
p.exeOK &= !executableNameUsed |
| 111 |
} |
| 112 |
|
| 113 |
p.updateOKButtonState() |
| 114 |
} |
| 115 |
|
| 116 |
onIconChanged: { |
| 117 |
if (icon !== icnBtn.path) |
| 118 |
icnBtn.path = icon |
| 119 |
|
| 120 |
p.iconOK = icon.toString().length > 0 |
| 121 |
p.updateOKButtonState() |
| 122 |
} |
| 123 |
|
| 124 |
GridLayout { |
| 125 |
anchors.fill: parent |
| 126 |
columns: 2 |
| 127 |
columnSpacing: 10 |
| 128 |
rowSpacing: 10 |
| 129 |
|
| 130 |
Label { |
| 131 |
text: qsTr("Name:") |
| 132 |
Layout.alignment: Qt.AlignRight |
| 133 |
} |
| 134 |
|
| 135 |
TextField { |
| 136 |
id: nameTf |
| 137 |
Layout.alignment: Qt.AlignLeft |
| 138 |
Layout.fillWidth: true |
| 139 |
selectByMouse: true |
| 140 |
hoverEnabled: Style.g_hover |
| 141 |
placeholderText: qsTr("Profile name") |
| 142 |
|
| 143 |
onTextEdited: dlg.name = text |
| 144 |
} |
| 145 |
|
| 146 |
Label { |
| 147 |
text: qsTr("Activation:") |
| 148 |
Layout.alignment: Qt.AlignRight |
| 149 |
} |
| 150 |
|
| 151 |
CComboBox { |
| 152 |
id: activationCb |
| 153 |
Layout.alignment: Qt.AlignLeft |
| 154 |
Layout.fillWidth: true |
| 155 |
hoverEnabled: Style.g_hover |
| 156 |
|
| 157 |
model: ListModel { |
| 158 |
ListElement { text: qsTr("Automatic") } |
| 159 |
ListElement { text: qsTr("Manual") } |
| 160 |
} |
| 161 |
|
| 162 |
onCurrentIndexChanged: { |
| 163 |
p.validateName() |
| 164 |
p.validateExe() |
| 165 |
p.updateOKButtonState() |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
Label { |
| 170 |
text: qsTr("Executable:") |
| 171 |
Layout.alignment: Qt.AlignRight |
| 172 |
visible: activationCb.currentIndex === 0 |
| 173 |
} |
| 174 |
|
| 175 |
RowLayout { |
| 176 |
visible: activationCb.currentIndex === 0 |
| 177 |
Layout.alignment: Qt.AlignLeft |
| 178 |
Layout.fillWidth: true |
| 179 |
spacing: 5 |
| 180 |
|
| 181 |
TextField { |
| 182 |
id: exeTf |
| 183 |
selectByMouse: true |
| 184 |
hoverEnabled: Style.g_hover |
| 185 |
placeholderText: qsTr("Executable name") |
| 186 |
Layout.fillWidth: true |
| 187 |
|
| 188 |
onTextEdited: dlg.exe = text |
| 189 |
} |
| 190 |
|
| 191 |
Button { |
| 192 |
text: Style.g_icon.MORE |
| 193 |
font.pointSize: Style.g_text.icon_size |
| 194 |
implicitWidth: 25 |
| 195 |
implicitHeight: exeTf.height |
| 196 |
spacing: 0 |
| 197 |
padding: 0 |
| 198 |
hoverEnabled: Style.g_hover |
| 199 |
|
| 200 |
NativeFileDialog { |
| 201 |
id: exeFDialog |
| 202 |
title: qsTr("Select an executable file") |
| 203 |
|
| 204 |
onAccepted: { |
| 205 |
var fileName = exeFDialog.fileName() |
| 206 |
exeTf.text = fileName |
| 207 |
dlg.exe = fileName |
| 208 |
} |
| 209 |
} |
| 210 |
onClicked: exeFDialog.open() |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
Label { |
| 215 |
text: qsTr("Icon:") |
| 216 |
Layout.alignment: Qt.AlignRight |
| 217 |
} |
| 218 |
|
| 219 |
Button { |
| 220 |
id: icnBtn |
| 221 |
Layout.alignment: Qt.AlignLeft |
| 222 |
|
| 223 |
enabled: customizeIconCb.checked |
| 224 |
property alias path: icnBtnImage.source |
| 225 |
|
| 226 |
hoverEnabled: Style.g_hover |
| 227 |
|
| 228 |
contentItem: Item { |
| 229 |
implicitWidth: icnBtnImage.width |
| 230 |
implicitHeight: icnBtnImage.height |
| 231 |
|
| 232 |
Image { |
| 233 |
id: icnBtnImage |
| 234 |
anchors.centerIn: parent |
| 235 |
fillMode: Image.PreserveAspectFit |
| 236 |
width: Style.g_icon.size |
| 237 |
height: Style.g_icon.size |
| 238 |
sourceSize.width: Style.g_icon.source_size |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
NativeFileDialog { |
| 243 |
id: icnFDialog |
| 244 |
title: qsTr("Select an icon") |
| 245 |
nameFilters: [qsTr("Images") + "(*.png *.bmp *.jpg *.jpeg *.ico *.icns *.svg)"] |
| 246 |
|
| 247 |
onAccepted: { |
| 248 |
icnBtnImage.source = file |
| 249 |
dlg.icon = file |
| 250 |
p.selectedIcon = file |
| 251 |
} |
| 252 |
} |
| 253 |
onClicked: icnFDialog.open() |
| 254 |
} |
| 255 |
|
| 256 |
CheckBox { |
| 257 |
id: customizeIconCb |
| 258 |
Layout.alignment: Qt.AlignLeft |
| 259 |
Layout.row: activationCb.currentIndex === 0 ? 4 : 3 |
| 260 |
Layout.column: 1 |
| 261 |
|
| 262 |
text: qsTr("Customize icon") |
| 263 |
|
| 264 |
hoverEnabled: Style.g_hover |
| 265 |
|
| 266 |
leftPadding: 0 |
| 267 |
rightPadding: 0 |
| 268 |
topPadding: 0 |
| 269 |
bottomPadding: 0 |
| 270 |
|
| 271 |
onToggled: { |
| 272 |
var customIcon = dlg.hasCustomIcon ? p.originalIcon : "" |
| 273 |
var customIconToShow = p.selectedIcon.toString().length > 0 ? p.selectedIcon : customIcon |
| 274 |
dlg.icon = checked ? customIconToShow : dlg.defaultIcon |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
Component.onCompleted: { |
| 280 |
p.resetState() |
| 281 |
p.updateOKButtonState() |
| 282 |
|
| 283 |
dlg.footer.standardButton(Dialog.Ok).hoverEnabled = Style.g_hover |
| 284 |
dlg.footer.standardButton(Dialog.Cancel).hoverEnabled = Style.g_hover |
| 285 |
} |
| 286 |
} |