1/*
2 *  SPDX-FileCopyrightText: 2012 Sebastian Gottfried <sebastiangottfried@web.de>
3 *
4 *  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7import QtQuick 2.9
8import ktouch 1.0
9
10import "../common"
11
12Item {
13    id: root
14
15    Preferences {
16        id: preferences
17    }
18
19    property real scaleFactor: Math.pow(2, keyboardLayoutEditor.zoomLevel / 2)
20    property KeyboardLayout layout: keyboardLayoutEditor.keyboardLayout
21    property int lastZIndex: 0
22
23    width: keyContainer.width + 40
24    height: keyContainer.height + 40
25
26    MouseArea {
27        anchors.fill: parent
28        onPressed: {
29            if (mouse.button == Qt.LeftButton) {
30                keyboardLayoutEditor.selectedKey = null
31                mouse.accepted = true
32            }
33        }
34    }
35
36    LineGrid {
37        id: keyContainer
38
39        anchors.centerIn: parent
40        width: Math.round(layout.width * scaleFactor)
41        height: Math.round(layout.height * scaleFactor)
42
43        lineDistance: 10.0 * scaleFactor
44        color: "#121286"
45        backgroundColor: "#cccccc"
46
47        Repeater {
48            id: keys
49            model: layout.isValid? layout.keyCount: 0
50
51            KeyItem {
52                property bool manipulated: false
53                id: keyItem
54                keyboardLayout: layout;
55                keyIndex: index
56                isHighlighted: keyItem.key == keyboardLayoutEditor.selectedKey
57                animateHighlight: false
58                opacity: manipulated? 0.7: 1.0
59                horizontalScaleFactor: scaleFactor
60                verticalScaleFactor: scaleFactor
61
62                MouseArea {
63                    anchors.fill: parent
64                    cursorShape: keyItem.manipulated? Qt.SizeAllCursor: Qt.ArrowCursor
65                    onPressed: {
66                        if (mouse.button == Qt.LeftButton) {
67                            keyboardLayoutEditor.selectedKey = layout.key(index)
68                            root.lastZIndex++
69                            keyItem.z = root.lastZIndex
70                        }
71                    }
72                    drag {
73                        target: !keyboardLayoutEditor.readOnly? keyItem: undefined
74                        axis: Drag.XandYAxis
75                        minimumX: 0
76                        maximumX: keyContainer.width - keyItem.width
77                        minimumY: 0
78                        maximumY: keyContainer.height - keyItem.height
79                        onActiveChanged: {
80                            keyItem.manipulated = drag.active
81                            if (!drag.active) {
82                                var left = 10 * Math.round(keyItem.x / scaleFactor / 10)
83                                var top = 10 * Math.round(keyItem.y / scaleFactor / 10)
84                                keyboardLayoutEditor.setKeyGeometry(keyIndex, left, top, keyItem.key.width, keyItem.key.height)
85                            }
86                        }
87                    }
88                }
89
90                Behavior on opacity {
91                    NumberAnimation { duration: 150 }
92                }
93            }
94        }
95
96        SelectionRectangle {
97            keyboardLayout: layout;
98            target: keyboardLayoutEditor.selectedKey
99            z: root.lastZIndex + 1
100            interactive: !keyboardLayoutEditor.readOnly
101        }
102    }
103}
104