1/**************************************************************************
2 **                                                                      **
3 ** Copyright (C) 2018 Lukas Spies                                       **
4 ** Contact: http://photoqt.org                                          **
5 **                                                                      **
6 ** This file is part of PhotoQt.                                        **
7 **                                                                      **
8 ** PhotoQt is free software: you can redistribute it and/or modify      **
9 ** it under the terms of the GNU General Public License as published by **
10 ** the Free Software Foundation, either version 2 of the License, or    **
11 ** (at your option) any later version.                                  **
12 **                                                                      **
13 ** PhotoQt is distributed in the hope that it will be useful,           **
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of       **
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        **
16 ** GNU General Public License for more details.                         **
17 **                                                                      **
18 ** You should have received a copy of the GNU General Public License    **
19 ** along with PhotoQt. If not, see <http://www.gnu.org/licenses/>.      **
20 **                                                                      **
21 **************************************************************************/
22
23import QtQuick 2.5
24
25import "../../../elements"
26
27Item {
28
29    id: setTop
30
31    height: Math.max(childrenRect.height,5)
32    Behavior on height { NumberAnimation { duration: variables.animationSpeed/2 } }
33
34    property bool external: shortcutscontainer.external
35
36    clip: true
37
38    ListView {
39
40        id: listview
41
42        x: 3
43        y: 3
44        width: parent.width-6
45        height: count*(elementHeight+spacing)
46
47        spacing: 6
48
49        interactive: false
50
51        property int elementHeight: 24
52
53        model: ListModel { }
54
55        delegate: Rectangle {
56
57            id: ele
58
59            width: listview.width-6
60            height: listview.elementHeight
61            Behavior on height { NumberAnimation { duration: variables.animationSpeed/5 } }
62
63            radius: 3
64            clip: true
65
66            Behavior on x { NumberAnimation { duration: variables.animationSpeed } }
67            onXChanged: {
68                if(x <= -ele.width)
69                    listview.model.remove(index)
70            }
71
72            // Change color when hovered
73            property bool hovered: false
74            color: hovered ? colour.tiles_inactive : colour.tiles_disabled
75            Behavior on color { ColorAnimation { duration: variables.animationSpeed/2 } }
76
77            property bool hotForShortcutDetection: false
78
79            // quit or not (only visible for external shortcuts)
80            Item {
81
82                id: closeitem
83
84                anchors {
85                    left: parent.left
86                    leftMargin: 5
87                    top: parent.top
88                    topMargin: 2
89                    bottom: parent.bottom
90                    bottomMargin: 2
91                }
92                width: visible ? theclose.width : 0
93
94                visible: external
95
96                Text{
97
98                    id: theclose
99
100                    anchors.left: parent.left
101                    anchors.top: parent.top
102                    anchors.bottom: parent.bottom
103
104                    color: close=="1" ? "white" : "grey"
105
106                    //: Shortcuts: KEEP THIS STRING SHORT! It is displayed for external shortcuts as an option to quit PhotoQt after executing shortcut
107                    text: em.pty+qsTr("quit") + "  "
108
109                    ToolTip {
110
111                        anchors.fill: parent
112                        cursorShape: Qt.PointingHandCursor
113                        text: close=="1"
114                                ? em.pty+qsTr("Quit PhotoQt when executing shortcut")
115                                : em.pty+qsTr("Keep PhotoQt running when executing shortcut")
116                        onClicked:
117                            close = (close=="1" ? "0" : "1")
118
119                    }
120                }
121
122            }
123
124            // What shortcut this is
125            Item {
126
127                anchors {
128                    left: closeitem.right
129                    leftMargin: 3
130                    top: parent.top
131                    topMargin: 2
132                    bottom: parent.bottom
133                    bottomMargin: 2
134                    right: deleteItem.left
135                    rightMargin: 3
136                }
137
138                MouseArea {
139
140                    anchors.fill: parent
141
142                    hoverEnabled: true
143                    cursorShape: Qt.PointingHandCursor
144
145                    onEntered: ele.hovered = true
146                    onExited: ele.hovered = false
147                    onClicked: {
148                        hotForShortcutDetection = true
149                        detectshortcut.show()
150                    }
151
152                }
153
154                Text {
155                    id: thetitle
156                    anchors.fill: parent
157                    anchors.rightMargin: parent.width/2
158                    visible: !external
159                    color: colour.tiles_text_active
160                    elide: Text.ElideRight
161                    text: desc
162                }
163                CustomLineEdit {
164                    id: externalCommand
165                    anchors.fill: parent
166                    anchors.rightMargin: parent.width/2+closeitem.width
167                    visible: external
168                    text: desc
169                    //: Shortcuts: This is the command/executable to be executed (external shortcut)
170                    emptyMessage: em.pty+qsTr("The command goes here")
171                    onTextEdited:
172                        updateExternalString.restart()
173                }
174                Timer {
175                    id: updateExternalString
176                    interval: 250
177                    running: false
178                    repeat: false
179                    onTriggered: {
180                        if(external)
181                            listview.model.set(index, {"cmd" : externalCommand.getText()})
182                    }
183                }
184
185                Text {
186                    id: thekey
187                    anchors.fill: parent
188                    anchors.leftMargin: parent.width/2-closeitem.width/2
189                    color: colour.tiles_text_active
190                    elide: Text.ElideRight
191                    text: strings.translateShortcut(key)
192                    Component.onCompleted: {
193                        if(text == "...") {
194                            ele.hotForShortcutDetection = true
195                            detectshortcut.show()
196                        }
197                    }
198                }
199
200            }
201
202            Image {
203
204                id: deleteItem
205
206                property bool hovered: false
207
208                anchors {
209                    right: parent.right
210                    top: parent.top
211                    bottom: parent.bottom
212                    margins: 3
213                }
214                width: ele.height-6
215
216                opacity: hovered ? 1 : 0.3
217                Behavior on opacity { NumberAnimation { duration: 200 } }
218
219                source: "qrc:/img/settings/shortcuts/deleteshortcut.png"
220
221                ToolTip {
222                    anchors.fill: parent
223                    text: em.pty+qsTr("Delete shortcut")
224                    hoverEnabled: true
225                    cursorShape: Qt.PointingHandCursor
226                    onEntered: {
227                        ele.hovered = true
228                        deleteItem.hovered = true
229                    }
230                    onExited: {
231                        ele.hovered = false
232                        deleteItem.hovered = false
233                    }
234                    onClicked:
235                        ele.deleteThisShortcut()
236                }
237
238            }
239
240
241            Connections {
242                target: detectshortcut
243                onAbortedShortcutDetection: {
244                    if(ele.hotForShortcutDetection) {
245                        ele.hotForShortcutDetection = false
246                        if(thekey.text == "...")
247                            ele.deleteThisShortcut()
248                    }
249                }
250                onGotNewShortcut: {
251                    if(ele.hotForShortcutDetection) {
252                        ele.hotForShortcutDetection = false
253                        thekey.text = strings.translateShortcut(sh)
254                        listview.model.set(index, {"key" : sh})
255                    }
256                }
257            }
258
259            function deleteThisShortcut() {
260                ele.x = -ele.width-50
261            }
262
263        }
264
265    }
266
267    function setData(sh) {
268
269        listview.model.clear()
270
271        for(var i = 0; i < sh.length; ++i)
272            listview.model.append({"desc" : sh[i][0], "key" : sh[i][1], "close" : sh[i][2], "cmd" : sh[i][3]})
273
274    }
275
276    function addShortcut(dat) {
277
278        verboseMessage("SettingsManager/Shortcuts/Set", "addShortcut(): " + dat)
279
280        listview.model.append({"desc" : dat[1], "key" : "...", "close" : "0", "cmd" : dat[0]})
281
282    }
283
284    function saveData() {
285
286        var ret = []
287
288        for(var i = 0; i < listview.model.count; ++i) {
289            var item = listview.model.get(i)
290            ret.push(item.key)
291            ret.push(item.close)
292            ret.push(item.cmd)
293        }
294
295        return ret
296
297    }
298
299}
300