1/**
2 * \file StringListEditPage.qml
3 * Page to edit a list of strings.
4 *
5 * \b Project: Kid3
6 * \author Urs Fleisch
7 * \date 21 Feb 2019
8 *
9 * Copyright (C) 2019  Urs Fleisch
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; version 3.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24import QtQuick 2.11
25import QtQuick.Layouts 1.11
26import QtQuick.Controls 2.4
27
28Page {
29  id: page
30
31  property alias model: listView.model
32  property alias currentIndex: listView.currentIndex
33  property alias count: listView.count
34  property Dialog editDialog: textEditDialog
35
36  property var onAddClicked: function() {
37    function modifyIfCompleted(ok) {
38      editDialog.completed.disconnect(modifyIfCompleted)
39      if (ok) {
40        addElement(editDialog.getElement())
41      }
42    }
43
44    editDialog.setElement({name: ""})
45    editDialog.completed.connect(modifyIfCompleted)
46    editDialog.open()
47  }
48
49  property var onEditClicked: function() {
50    var idx = listView.currentIndex
51    if (idx >= 0) {
52      function modifyIfCompleted(ok) {
53        editDialog.completed.disconnect(modifyIfCompleted)
54        if (ok) {
55          listView.model.set(idx, editDialog.getElement())
56        }
57      }
58
59      editDialog.setElement(listView.model.get(idx))
60      editDialog.completed.connect(modifyIfCompleted)
61      editDialog.open()
62    }
63  }
64
65  function addElement(element) {
66    model.append(element)
67    currentIndex = count - 1
68  }
69
70  function setElements(lst) {
71    listView.model.clear()
72    for (var i = 0; i < lst.length; i++) {
73      listView.model.append({"name": lst[i]})
74    }
75  }
76
77  function getElements() {
78    var lst = []
79    for (var i = 0; i < listView.model.count; i++) {
80      lst.push(listView.model.get(i).name)
81    }
82    return lst
83  }
84
85  title: qsTr("Edit")
86
87  Dialog {
88    id: textEditDialog
89
90    signal completed(bool ok)
91
92    function setElement(element) {
93      textLineEdit.text = element.name
94    }
95
96    function getElement() {
97      return {name: textLineEdit.text}
98    }
99
100    modal: true
101    width: Math.min(parent.width, constants.gu(70))
102    x: (parent.width - width) / 2
103    y: 0
104    standardButtons: Dialog.Ok | Dialog.Cancel
105
106    TextField {
107      id: textLineEdit
108      width: parent.width
109      selectByMouse: true
110    }
111
112    onAccepted: completed(true)
113    onRejected: completed(false)
114  }
115
116  header: ToolBar {
117    IconButton {
118      id: prevButton
119      anchors.left: parent.left
120      anchors.verticalCenter: parent.verticalCenter
121      iconName: "go-previous"
122      color: titleLabel.color
123      width: visible ? height : 0
124      visible: page.StackView.view && page.StackView.view.depth > 1
125      onClicked: page.StackView.view.pop()
126    }
127    Label {
128      id: titleLabel
129      anchors.left: prevButton.right
130      anchors.right: parent.right
131      anchors.verticalCenter: parent.verticalCenter
132      clip: true
133      text: page.title
134    }
135  }
136
137  RowLayout {
138    anchors {
139      fill: parent
140      margins: constants.margins
141    }
142    ListView {
143      id: listView
144      Layout.fillWidth: true
145      Layout.fillHeight: true
146      clip: true
147      model: ListModel {}
148      delegate: Standard {
149        text: name
150        highlighted: ListView.view.currentIndex === index
151        onClicked: ListView.view.currentIndex = index
152        background: Rectangle {
153          color: highlighted ? constants.highlightColor : "transparent"
154        }
155      }
156    }
157    ColumnLayout {
158      Layout.alignment: Qt.AlignTop
159      Label {
160        id: invisibleLabel
161        visible: false
162      }
163      IconButton {
164        iconName: "add"
165        color: invisibleLabel.color
166        onClicked: onAddClicked()
167      }
168      IconButton {
169        iconName: "go-up"
170        color: invisibleLabel.color
171        onClicked: {
172          var idx = listView.currentIndex
173          if (idx > 0) {
174            listView.model.move(idx, idx - 1, 1)
175            listView.currentIndex = idx - 1
176          }
177        }
178      }
179      IconButton {
180        iconName: "go-down"
181        color: invisibleLabel.color
182        onClicked: {
183          var idx = listView.currentIndex
184          if (idx >= 0 && idx < listView.model.count - 1) {
185            listView.model.move(idx, idx + 1, 1)
186            listView.currentIndex = idx + 1
187          }
188        }
189      }
190      IconButton {
191        iconName: "edit"
192        color: invisibleLabel.color
193        onClicked: onEditClicked()
194      }
195      IconButton {
196        iconName: "remove"
197        color: invisibleLabel.color
198        onClicked: {
199          var idx = listView.currentIndex
200          if (idx >= 0) {
201            listView.model.remove(idx, 1)
202          }
203        }
204      }
205    }
206  }
207}
208