/*SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>SPDX-License-Identifier: GPL-2.0-or-later*/import QtQuick 2.15import QtQuick.Layouts 1.1import QtQuick.Controls 2.3 as QQC2import org.kde.kirigami 2.19 as Kirigamiimport org.kde.plasma.tablet.kcm 1.1import org.kde.kcmutilsimport org.kde.kquickcontrols 2.0SimpleKCM {id: rootConfigModule.buttons: ConfigModule.Default | ConfigModule.ApplyimplicitWidth: Kirigami.Units.gridUnit * 38implicitHeight: Kirigami.Units.gridUnit * 35// So it doesn't scroll while draggingflickable.interactive: Kirigami.Settings.hasTransientTouchInputheader: Kirigami.InlineMessage {Layout.fillWidth: truetype: Kirigami.MessageType.Informationvisible: combo.count === 0text: i18nd("kcm_tablet", "No drawing tablets found.")}Kirigami.FormLayout {id: formenabled: combo.count > 0QQC2.ComboBox {id: comboKirigami.FormData.label: i18ndc("kcm_tablet", "@label:listbox The device we are configuring", "Device:")model: kcm.toolsModelonCountChanged: if (count > 0 && currentIndex < 0) {currentIndex = 0;}function reloadOutputView() {const initialOutputArea = form.device.outputArea;if (initialOutputArea === Qt.rect(0, 0, 1, 1)) {outputAreaCombo.currentIndex = 0;} else if (initialOutputArea.x === 0 && initialOutputArea.y === 0 && initialOutputArea.width === 1) {outputAreaCombo.currentIndex = 1;} else {outputAreaCombo.currentIndex = 2;}keepAspectRatio.checked = tabletItem.aspectRatio === (form.device.size.width / form.device.size.height)outputAreaView.resetOutputArea(outputAreaCombo.currentIndex, initialOutputArea)}onCurrentIndexChanged: {parent.device = kcm.toolsModel.deviceAt(combo.currentIndex)reloadOutputView()}Connections {target: kcmfunction onSettingsRestored() {outputAreaView.changed = falsecombo.reloadOutputView()}}}property QtObject device: nullQQC2.ComboBox {id: outputsComboKirigami.FormData.label: i18nd("kcm_tablet", "Target display:")model: OutputsModel {id: outputsModel}enabled: count > 2 //It's only interesting when there's more than 1 screencurrentIndex: {if (count === 0) {return -1}outputsModel.rowForOutputName(parent.device.outputName)}textRole: "display"onActivated: {parent.device.outputName = outputsModel.outputNameAt(currentIndex)}}QQC2.ComboBox {Kirigami.FormData.label: i18nd("kcm_tablet", "Orientation:")model: OrientationsModel {id: orientationsModel}enabled: parent.device && parent.device.supportsOrientationcurrentIndex: orientationsModel.rowForOrientation(parent.device.orientation)textRole: "display"onActivated: {parent.device.orientation = orientationsModel.orientationAt(currentIndex)}}QQC2.CheckBox {Kirigami.FormData.label: i18nd("kcm_tablet", "Left-handed mode:")enabled: parent.device && parent.device.supportsLeftHandedchecked: parent.device && parent.device.leftHandedonCheckedChanged: {parent.device.leftHanded = checked}}QQC2.ComboBox {id: outputAreaComboLayout.fillWidth: trueKirigami.FormData.label: i18nd("kcm_tablet", "Area:")model: OutputsFittingModel {}onActivated: {outputAreaView.changed = truekeepAspectRatio.checked = trueoutputAreaView.resetOutputArea(index, index === 0 ? Qt.rect(0,0, 1,1) : Qt.rect(0, 0, 1, outputItem.aspectRatio/tabletItem.aspectRatio))}}// Display fit demoItem {id: outputAreaViewfunction resetOutputArea(mode, outputArea) {if (mode === 0) {tabletItem.x = 0tabletItem.y = 0tabletItem.width = Qt.binding(() => outputItem.width);tabletItem.height = Qt.binding(() => outputItem.height);} else {tabletItem.x = Qt.binding(() => outputArea.x * outputItem.width)tabletItem.y = Qt.binding(() => outputArea.y * outputItem.height)tabletItem.width = Qt.binding(() => tabletSizeHandle.x);tabletItem.height = Qt.binding(() => tabletSizeHandle.y);tabletSizeHandle.x = Qt.binding(() => outputArea.width * outputItem.width)if (keepAspectRatio.checked) {tabletSizeHandle.y = Qt.binding(() => tabletSizeHandle.x / tabletItem.aspectRatio);} else {tabletSizeHandle.y = Qt.binding(() => outputArea.height * outputItem.height)}}}readonly property rect outputAreaSetting: Qt.rect(tabletItem.x/outputItem.width, tabletItem.y/outputItem.height,tabletItem.width/outputItem.width, tabletItem.height/outputItem.height)property bool changed: falseonOutputAreaSettingChanged: {if (form.device && changed) {form.device.outputArea = outputAreaSetting}}Layout.fillWidth: trueLayout.preferredHeight: Math.max(outputItem.height, tabletItem.height)enabled: parent.deviceOutput {id: outputItemreadonly property size outputPhysicalSize: outputsModel.data(outputsModel.index(outputsCombo.currentIndex, 0), Qt.UserRole + 1)readonly property size outputSize: outputsModel.data(outputsModel.index(outputsCombo.currentIndex, 0), Qt.UserRole + 2)readonly property real aspectRatio: outputPhysicalSize.width / outputPhysicalSize.heightwidth: parent.width * 0.7height: width / aspectRatio}Rectangle {id: tabletItemcolor: Kirigami.Theme.activeBackgroundColoropacity: 0.8readonly property real aspectRatio: outputAreaCombo.currentIndex === 0 ? outputItem.aspectRatio : form.device.size.width / form.device.size.heightwidth: tabletSizeHandle.xheight: tabletSizeHandle.yDragHandler {cursorShape: Qt.ClosedHandCursortarget: parentenabled: outputAreaCombo.currentIndex >= 2onActiveChanged: { outputAreaView.changed = true }xAxis.minimum: 0xAxis.maximum: outputItem.width - tabletItem.widthyAxis.minimum: 0yAxis.maximum: outputItem.height - tabletItem.height}TapHandler {gesturePolicy: TapHandler.WithinBounds}QQC2.Button {id: tabletSizeHandlex: outputItem.widthy: outputItem.width / parent.aspectRatiovisible: outputAreaCombo.currentIndex >= 2icon.name: "transform-move"display: QQC2.AbstractButton.IconOnlytext: i18nd("kcm_tablet", "Resize the tablet area")QQC2.ToolTip {text: tabletSizeHandle.textvisible: parent.hovereddelay: Kirigami.Units.toolTipDelay}DragHandler {cursorShape: Qt.SizeFDiagCursortarget: parentonActiveChanged: { outputAreaView.changed = true }xAxis.minimum: 10xAxis.maximum: outputItem.width - tabletItem.xyAxis.minimum: keepAspectRatio.checked ? (tabletItem.width / tabletItem.aspectRatio) : 10yAxis.maximum: keepAspectRatio.checked ? (tabletItem.width / tabletItem.aspectRatio) : outputItem.height - tabletItem.y}}}}QQC2.CheckBox {id: keepAspectRatiotext: i18ndc("kcm_tablet", "@option:check", "Lock aspect ratio")visible: outputAreaCombo.currentIndex >= 2checked: trueonToggled: {outputAreaView.resetOutputArea(outputAreaCombo.currentIndex, form.device.outputArea)}}QQC2.Label {text: i18ndc("kcm_tablet", "tablet area position - size", "%1,%2 - %3×%4", String(Math.floor(outputAreaView.outputAreaSetting.x * outputItem.outputSize.width)), String(Math.floor(outputAreaView.outputAreaSetting.y * outputItem.outputSize.height)), String(Math.floor(outputAreaView.outputAreaSetting.width * outputItem.outputSize.width)), String(Math.floor(outputAreaView.outputAreaSetting.height * outputItem.outputSize.height)))}Repeater {model: [{ value: 0x14b, text: i18nd("kcm_tablet", "Tool Button 1") },{ value: 0x14c, text: i18nd("kcm_tablet", "Tool Button 2") },{ value: 0x149, text: i18nd("kcm_tablet", "Tool Button 3") }] // BTN_STYLUS, BTN_STYLUS2, BTN_STYLUS3delegate: KeySequenceItem {id: seqKirigami.FormData.label: (pressed ? "<b>" : "") + modelData.text + (pressed ? "</b>" : "")property bool pressed: falseConnections {target: tabletEventsfunction onToolButtonReceived(hardware_serial_hi, hardware_serial_lo, button, pressed) {if (button !== modelData.value) {return;}seq.pressed = pressed}}keySequence: kcm.toolButtonMapping(form.device.name, modelData.value)Connections {target: kcmfunction onSettingsRestored() {seq.keySequence = kcm.toolButtonMapping(form.device.name, modelData.value)}}showCancelButton: truemodifierlessAllowed: truemodifierOnlyAllowed: truemultiKeyShortcutsAllowed: falsecheckForConflictsAgainst: ShortcutType.NoneonCaptureFinished: {kcm.assignToolButtonMapping(form.device.name, modelData.value, keySequence)}}}Kirigami.Separator {Layout.fillWidth: true}property QtObject padDevice: nullQQC2.ComboBox {Kirigami.FormData.label: i18ndc("kcm_tablet", "@label:listbox The pad we are configuring", "Pad:")model: kcm.padsModelonCurrentIndexChanged: {parent.padDevice = kcm.padsModel.deviceAt(currentIndex)}onCountChanged: if (count > 0 && currentIndex < 0) {currentIndex = 0;}enabled: count > 0displayText: enabled ? currentText : i18n("None")}TabletEvents {id: tabletEventsanchors.fill: parentonPadButtonsChanged: {if (!path.endsWith(form.padDevice.sysName)) {return;}buttonsRepeater.model = buttonCount}}Repeater {id: buttonsRepeatermodel: tabletEvents.padButtonsdelegate: KeySequenceItem {id: seqKirigami.FormData.label: (pressed ? "<b>" : "") + i18nd("kcm_tablet", "Button %1:", modelData + 1) + (pressed ? "</b>" : "")property bool pressed: falseConnections {target: tabletEventsfunction onPadButtonReceived(path, button, pressed) {if (button !== modelData || !path.endsWith(form.padDevice.sysName)) {return;}seq.pressed = pressed}}keySequence: kcm.padButtonMapping(form.padDevice.name, modelData)Connections {target: kcmfunction onSettingsRestored() {seq.keySequence = kcm.padButtonMapping(form.padDevice.name, modelData)}}showCancelButton: truemodifierlessAllowed: truemodifierOnlyAllowed: truemultiKeyShortcutsAllowed: falsecheckForConflictsAgainst: ShortcutType.NoneonCaptureFinished: {kcm.assignPadButtonMapping(form.padDevice.name, modelData, keySequence)}}}}}