| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2020 George Florea Bănuș <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
pragma ComponentBehavior: Bound |
| 8 |
|
| 9 |
import QtQml |
| 10 |
import QtQuick |
| 11 |
import QtQuick.Layouts |
| 12 |
import QtQuick.Controls |
| 13 |
|
| 14 |
import org.kde.kirigami as Kirigami |
| 15 |
import org.kde.haruna |
| 16 |
|
| 17 |
SettingsBasePage { |
| 18 |
id: root |
| 19 |
|
| 20 |
required property MouseActionsModel m_mouseActionsModel |
| 21 |
|
| 22 |
signal newMouseActionChanged() |
| 23 |
|
| 24 |
padding: 0 |
| 25 |
|
| 26 |
SelectActionPopup { |
| 27 |
id: selectActionPopup |
| 28 |
} |
| 29 |
|
| 30 |
ColumnLayout { |
| 31 |
id: content |
| 32 |
|
| 33 |
anchors.fill: parent |
| 34 |
spacing: Kirigami.Units.largeSpacing |
| 35 |
|
| 36 |
ListView { |
| 37 |
id: mouseActionsListView |
| 38 |
|
| 39 |
ScrollBar.vertical: ScrollBar { id: scrollBar } |
| 40 |
|
| 41 |
model: root.m_mouseActionsModel |
| 42 |
delegate: ItemDelegate { |
| 43 |
id: delegate |
| 44 |
|
| 45 |
required property mouseAction mouseAction |
| 46 |
required property int index |
| 47 |
required property string actionName |
| 48 |
required property string i18nButton |
| 49 |
required property string i18nModifier |
| 50 |
required property bool isDoubleClick |
| 51 |
readonly property string doubleClickString: i18nc("@label represents a mouse double click, for example `Right x2`", "x2") |
| 52 |
property string dc: delegate.isDoubleClick ? doubleClickString : "" |
| 53 |
|
| 54 |
width: scrollBar.visible ? ListView.view.width - scrollBar.width : ListView.view.width |
| 55 |
highlighted: false |
| 56 |
Kirigami.Theme.useAlternateBackgroundColor: true |
| 57 |
|
| 58 |
contentItem: RowLayout { |
| 59 |
Kirigami.Chip { |
| 60 |
text: delegate.i18nModifier |
| 61 |
visible: text |
| 62 |
closable: false |
| 63 |
checkable: false |
| 64 |
} |
| 65 |
|
| 66 |
Kirigami.Chip { |
| 67 |
text: `${delegate.i18nButton} ${delegate.dc}` |
| 68 |
closable: false |
| 69 |
checkable: false |
| 70 |
} |
| 71 |
|
| 72 |
Label { |
| 73 |
text: HarunaApp.actions[delegate.actionName].text |
| 74 |
elide: Text.ElideRight |
| 75 |
horizontalAlignment: Qt.AlignRight |
| 76 |
Layout.fillWidth: true |
| 77 |
} |
| 78 |
|
| 79 |
ToolButton { |
| 80 |
icon.name: "overflow-menu" |
| 81 |
Layout.preferredHeight: Kirigami.Units.iconSizes.medium |
| 82 |
|
| 83 |
onClicked: { |
| 84 |
delegateMenu.open() |
| 85 |
} |
| 86 |
|
| 87 |
Menu { |
| 88 |
id: delegateMenu |
| 89 |
|
| 90 |
y: parent.height |
| 91 |
|
| 92 |
MenuItem { |
| 93 |
text: i18nc("@label:action change action associated with mouse event", "Change action") |
| 94 |
icon.name: "edit-entry" |
| 95 |
onTriggered: { |
| 96 |
const actionHandler = function (actionName) { |
| 97 |
delegate.mouseAction.actionName = actionName |
| 98 |
root.m_mouseActionsModel.editAction(delegate.mouseAction) |
| 99 |
selectActionPopup.actionSelected.disconnect(actionHandler) |
| 100 |
} |
| 101 |
selectActionPopup.actionSelected.connect(actionHandler) |
| 102 |
selectActionPopup.open() |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
MenuItem { |
| 107 |
text: i18nc("@label:action delete mouse event", "Delete") |
| 108 |
icon.name: "delete" |
| 109 |
onTriggered: { |
| 110 |
root.m_mouseActionsModel.removeAction(delegate.index) |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
Layout.fillWidth: true |
| 119 |
Layout.fillHeight: true |
| 120 |
} |
| 121 |
|
| 122 |
Item { |
| 123 |
Layout.preferredWidth: Kirigami.Units.gridUnit |
| 124 |
Layout.preferredHeight: Kirigami.Units.gridUnit |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
footer: ToolBar { |
| 129 |
RowLayout { |
| 130 |
anchors.fill: parent |
| 131 |
|
| 132 |
ToolButton { |
| 133 |
id: addButton |
| 134 |
|
| 135 |
text: i18nc("@action:intoolbar add a mouse button action", "&Add") |
| 136 |
icon.name: "list-add" |
| 137 |
onClicked: { |
| 138 |
addActionComponent.createObject(root) |
| 139 |
} |
| 140 |
|
| 141 |
Layout.alignment: Qt.AlignRight |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
Component { |
| 147 |
id: addActionComponent |
| 148 |
|
| 149 |
Dialog { |
| 150 |
id: mouseActionDialog |
| 151 |
|
| 152 |
property bool canBeSaved: !noActionSelectedMessage.visible && !actionExistsMessage.visible |
| 153 |
|
| 154 |
function selectAction() { |
| 155 |
const actionHandler = function (actionName) { |
| 156 |
if (mouseActionDialog === null) { |
| 157 |
return |
| 158 |
} |
| 159 |
|
| 160 |
selectedActionLabel.actionName = actionName |
| 161 |
selectedActionLabel.text = HarunaApp.actions[actionName].text |
| 162 |
Q_EMIT: root.newMouseActionChanged() |
| 163 |
|
| 164 |
selectActionPopup.actionSelected.disconnect(actionHandler) |
| 165 |
} |
| 166 |
selectActionPopup.actionSelected.connect(actionHandler) |
| 167 |
selectActionPopup.open() |
| 168 |
} |
| 169 |
|
| 170 |
title: i18nc("@title:window", "Add mouse button action") |
| 171 |
parent: content |
| 172 |
width: Math.min(parent.width, 400) |
| 173 |
height: Math.min(parent.height, 600) |
| 174 |
visible: true |
| 175 |
standardButtons: Dialog.Ok | Dialog.Cancel |
| 176 |
closePolicy: Popup.NoAutoClose | Popup.CloseOnEscape |
| 177 |
modal: true |
| 178 |
|
| 179 |
ColumnLayout { |
| 180 |
width: parent.width |
| 181 |
|
| 182 |
Label { |
| 183 |
text: i18nc("@label:listbox", "Mouse button") |
| 184 |
} |
| 185 |
|
| 186 |
ComboBox { |
| 187 |
id: mouseButtonComboBox |
| 188 |
|
| 189 |
textRole: "text" |
| 190 |
valueRole: "value" |
| 191 |
currentIndex: 0 |
| 192 |
model: ListModel { |
| 193 |
id: mouseButtonsModel |
| 194 |
} |
| 195 |
onActivated: { |
| 196 |
Q_EMIT: root.newMouseActionChanged() |
| 197 |
} |
| 198 |
Component.onCompleted: { |
| 199 |
const left = { |
| 200 |
text: i18nc("@item:listbox left mouse button", "Left"), |
| 201 |
value: "Left" |
| 202 |
} |
| 203 |
mouseButtonsModel.append(left) |
| 204 |
|
| 205 |
const middle = { |
| 206 |
text: i18nc("@item:listbox middle mouse button", "Middle"), |
| 207 |
value: "Middle" |
| 208 |
} |
| 209 |
mouseButtonsModel.append(middle) |
| 210 |
|
| 211 |
const right = { |
| 212 |
text: i18nc("@item:listbox right mouse button", "Right"), |
| 213 |
value: "Right" |
| 214 |
} |
| 215 |
mouseButtonsModel.append(right) |
| 216 |
|
| 217 |
const forward = { |
| 218 |
text: i18nc("@item:listbox forward mouse button", "Forward"), |
| 219 |
value: "Forward" |
| 220 |
} |
| 221 |
mouseButtonsModel.append(forward) |
| 222 |
|
| 223 |
const back = { |
| 224 |
text: i18nc("@item:listbox back mouse button", "Back"), |
| 225 |
value: "Back" |
| 226 |
} |
| 227 |
mouseButtonsModel.append(back) |
| 228 |
|
| 229 |
const scrollUp = { |
| 230 |
text: i18nc("@item:listbox mouse scroll up", "Scroll up"), |
| 231 |
value: "ScrollUp" |
| 232 |
} |
| 233 |
mouseButtonsModel.append(scrollUp) |
| 234 |
|
| 235 |
const scrollDown = { |
| 236 |
text: i18nc("@item:listbox mouse scroll down", "Scroll down"), |
| 237 |
value: "ScrollDown" |
| 238 |
} |
| 239 |
mouseButtonsModel.append(scrollDown) |
| 240 |
} |
| 241 |
Layout.bottomMargin: Kirigami.Units.largeSpacing |
| 242 |
} |
| 243 |
|
| 244 |
Label { |
| 245 |
text: i18nc("@label:listbox", "Modifier key") |
| 246 |
} |
| 247 |
|
| 248 |
ComboBox { |
| 249 |
id: modifierComboBox |
| 250 |
|
| 251 |
textRole: "text" |
| 252 |
valueRole: "value" |
| 253 |
currentIndex: 0 |
| 254 |
model: ListModel { |
| 255 |
id: modifiersModel |
| 256 |
} |
| 257 |
onActivated: { |
| 258 |
Q_EMIT: root.newMouseActionChanged() |
| 259 |
} |
| 260 |
|
| 261 |
Component.onCompleted: { |
| 262 |
const noModifier = { |
| 263 |
text: i18nc("@item:listbox no modifier key", "No modifier"), |
| 264 |
value: "NoModifier" |
| 265 |
} |
| 266 |
modifiersModel.append(noModifier) |
| 267 |
|
| 268 |
const control = { |
| 269 |
text: i18nc("@item:listbox control modifier key", "Control"), |
| 270 |
value: "Control" |
| 271 |
} |
| 272 |
modifiersModel.append(control) |
| 273 |
|
| 274 |
const shift = { |
| 275 |
text: i18nc("@item:listbox shift modifier key", "Shift"), |
| 276 |
value: "Shift" |
| 277 |
} |
| 278 |
modifiersModel.append(shift) |
| 279 |
|
| 280 |
const alt = { |
| 281 |
text: i18nc("@item:listbox alt modifier key", "Alt"), |
| 282 |
value: "Alt" |
| 283 |
} |
| 284 |
modifiersModel.append(alt) |
| 285 |
|
| 286 |
const meta = { |
| 287 |
text: i18nc("@item:listbox meta modifier key", "Meta"), |
| 288 |
value: "Meta" |
| 289 |
} |
| 290 |
modifiersModel.append(meta) |
| 291 |
} |
| 292 |
Layout.bottomMargin: Kirigami.Units.largeSpacing |
| 293 |
} |
| 294 |
|
| 295 |
CheckBox { |
| 296 |
id: isDoubleClickCheckBox |
| 297 |
|
| 298 |
text: i18nc("@label:check whether mouse button action should trigger on double click", |
| 299 |
"Trigger on double click") |
| 300 |
enabled: mouseButtonComboBox.currentIndex < 5 |
| 301 |
onClicked: { |
| 302 |
Q_EMIT: root.newMouseActionChanged() |
| 303 |
} |
| 304 |
Layout.bottomMargin: Kirigami.Units.largeSpacing |
| 305 |
} |
| 306 |
|
| 307 |
RowLayout { |
| 308 |
Label { |
| 309 |
text: i18nc("@label", "Action") |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
RowLayout { |
| 314 |
Layout.fillWidth: true |
| 315 |
|
| 316 |
Button { |
| 317 |
text: i18nc("@action:button select mouse button action", "Select action") |
| 318 |
visible: !selectedActionLabel.text |
| 319 |
onClicked: { |
| 320 |
mouseActionDialog.selectAction() |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
Label { |
| 325 |
id: selectedActionLabel |
| 326 |
|
| 327 |
property string actionName |
| 328 |
font.weight: Font.Bold |
| 329 |
Layout.fillWidth: true |
| 330 |
} |
| 331 |
|
| 332 |
Button { |
| 333 |
text: i18nc("@action:button", "Change action") |
| 334 |
visible: selectedActionLabel.text |
| 335 |
onClicked: { |
| 336 |
mouseActionDialog.selectAction() |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
Kirigami.InlineMessage { |
| 342 |
id: noActionSelectedMessage |
| 343 |
|
| 344 |
text: i18nc("@info", "No action selected") |
| 345 |
type: Kirigami.MessageType.Warning |
| 346 |
visible: selectedActionLabel.text === "" |
| 347 |
|
| 348 |
Layout.fillWidth: true |
| 349 |
Layout.preferredHeight: 50 |
| 350 |
Layout.topMargin: Kirigami.Units.largeSpacing |
| 351 |
} |
| 352 |
|
| 353 |
Kirigami.InlineMessage { |
| 354 |
id: actionExistsMessage |
| 355 |
|
| 356 |
text: { |
| 357 |
const model = root.m_mouseActionsModel |
| 358 |
const button = mouseButtonComboBox.currentValue |
| 359 |
const modifier = modifierComboBox.currentValue |
| 360 |
const isDoubleClick = isDoubleClickCheckBox.checked |
| 361 |
const actionName = model.getAction(model.stringToMouseButton(button), |
| 362 |
model.stringToModifier(modifier), |
| 363 |
isDoubleClickCheckBox.checked) |
| 364 |
if (actionName === "") { |
| 365 |
return "" |
| 366 |
} |
| 367 |
return i18nc("@info", "Mouse button combination is already assigned to “%1”", |
| 368 |
HarunaApp.actions[actionName].text) |
| 369 |
} |
| 370 |
visible: text !== "" |
| 371 |
type: Kirigami.MessageType.Warning |
| 372 |
|
| 373 |
Layout.fillWidth: true |
| 374 |
Layout.preferredHeight: 50 |
| 375 |
Layout.topMargin: Kirigami.Units.largeSpacing |
| 376 |
} |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
Connections { |
| 381 |
target: root |
| 382 |
function onNewMouseActionChanged() { |
| 383 |
const button = mouseButtonComboBox.currentValue |
| 384 |
const modifier = modifierComboBox.currentValue |
| 385 |
const isDoubleClick = isDoubleClickCheckBox.checked |
| 386 |
|
| 387 |
if (root.m_mouseActionsModel.actionExists(button, modifier, isDoubleClick)) { |
| 388 |
actionExistsMessage.visible = true |
| 389 |
} else { |
| 390 |
actionExistsMessage.visible = false |
| 391 |
} |
| 392 |
let okBtn = mouseActionDialog.standardButton(Dialog.Ok) |
| 393 |
okBtn.enabled = mouseActionDialog.canBeSaved |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
onAccepted: { |
| 398 |
root.m_mouseActionsModel.addAction(selectedActionLabel.actionName, |
| 399 |
mouseButtonComboBox.currentValue, |
| 400 |
modifierComboBox.currentValue, |
| 401 |
isDoubleClickCheckBox.checked) |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
} |