MauiKit File Browsing

TagsBar.qml
1/*
2 * Copyright 2018 Camilo Higuita <milo.h@aol.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20import QtQuick
21import QtQuick.Controls
22
23import org.mauikit.controls as Maui
24
25import "private" as Private
26
27/**
28 * @inherit QtQuick.Item
29 * @brief A bar to list, add or remove tags for a given set of files.
30 * @see TagsList::urls
31 * @see list
32 *
33 * The retrieved file tags can be restricted to only tags created/associated by the app itself or by any app, this can be tweaked via the `list.strict` property.
34 * @see list
35 *
36 * @image html tagsbar.png "Example using TagsBar and FileBrowser controls"
37 *
38 * @code
39 * Maui.Page
40 * {
41 * id: _tagPreviewPage
42 * Maui.Controls.showCSD: true
43 * anchors.fill: parent
44 *
45 * property var fileItem //A map object representing some properties from a file, such as its name, icon, url, etc.
46 *
47 * Maui.Holder
48 * {
49 * anchors.fill: parent
50 * emojiSize: 100
51 * imageSource: _tagPreviewPage.fileItem.thumbnail
52 * title: _tagPreviewPage.fileItem.name
53 * }
54 *
55 * footer: FB.TagsBar
56 * {
57 * list.strict: false
58 * list.urls: [_tagPreviewPage.fileItem.url]
59 * width: parent.width
60 *
61 * onTagsEdited: (tags) => list.updateToUrls(tags)
62 * }
63 * }
64 * }
65 * @endcode
66 *
67 * <a href="https://invent.kde.org/maui/mauikit-filebrowser/examples/TagsBar.qml">You can find a more complete example at this link.</a>
68 */
69Item
70{
71 id: control
72
73 focus: true
74 implicitHeight: Maui.Style.toolBarHeight + Maui.Style.space.tiny
75
76 /**
77 * @brief An alias to the flickable element listing the tag buttons.
78 * It is exposed to fine tune more of this control properties.
79 * @property Taglist TagsBar::listView
80 */
81 readonly property alias listView : tagsList
82
83 /**
84 * @brief The total amount of tag elements
85 * @property int TagsBar::count
86 */
87 readonly property alias count : tagsList.count
89 /**
90 * @brief Whether the bar is in edit mode or not. This can be also triggered by the user using the attached action buttons in the right side of the bar.
91 * In edit mode the bar exposes a text field, where all the tag elements are plain text divided by comma. The text can be edited to remove tags or add more.
92 * @see allowEditMode
93 * By default this is set to `false`.
94 */
95 readonly property bool editMode : _stackView.depth === 2
96
97 /**
98 * @brief Whether the bar exposes to the user the action buttons that allow to go into edit mode, or to remove the tag elements manually.
99 * By default this is set to `false`
100 */
101 property bool allowEditMode : false
102
103 /**
104 * @see TagList::list
105 * @brief To associate a one or a group of file URLs, use `list.urls`, or to disable the strict mode use `list.strict: false`, etc. Read more about the available properties in the TagsListModel documentation page.
106 * @property TagsListModel TagsBar::list
107 */
108 readonly property alias list : tagsList.list
109
110 /**
111 * Emitted when the close/dismiss button of a tag element has been clicked.
112 * @param index the index position of the tag element
113 *
114 * @note To retrieve information of the tag given the index position, use the alias property function`list.get(index)`
115 */
116 signal tagRemovedClicked(int index)
118 /**
119 * Emitted when a tag element has been clicked.
120 * @param tag the name of the tag element
121 */
122 signal tagClicked(string tag)
123
124 /**
125 * @brief Emitted when the tags have been manually edited by the user via the text field input.
126 * @param tags the list of tags entered in the text field.
127 */
128 signal tagsEdited(var tags)
129
130 onAllowEditModeChanged:
132 if(!control.allowEditMode)
133 control.closeEditMode()
134 }
135
136 Connections
137 {
138 target: tagsList.list
139 onUrlsChanged: closeEditMode()
140 }
141
142 StackView
143 {
144 id: _stackView
145 anchors.fill: parent
146
147 initialItem: Private.TagList
148 {
149 id: tagsList
150
151 showPlaceHolder: allowEditMode
152 showDeleteIcon: allowEditMode
153
154 onTagRemoved: (index) => tagRemovedClicked(index)
155
156 onTagClicked: (index) => control.tagClicked(tagsList.list.get(index).tag)
157
158 onAreaClicked:
159 {
160 if(allowEditMode)
161 {
162 goEditMode()
163 }
164 }
165
166 Item
167 {
168 enabled: visible
169 visible: Maui.Handy.isMobile
170 anchors.fill: parent
171 TapHandler
172 {
173 onTapped: goEditMode()
174 }
175 }
176 }
177
178 Component
179 {
180 id: _editComponent
181 Maui.TextField
182 {
183 id: editTagsEntry
184
185 focus: true
186
187 activeFocusOnPress : true
188
189 text: list.tags.join(",")
190
191 StackView.onActivated:
192 {
193 editTagsEntry.forceActiveFocus()
194 }
195
196 onAccepted:
197 {
198 control.tagsEdited(getTags())
199 control.closeEditMode()
200 }
201
202 actions: Action
203 {
204 icon.name: "checkbox"
205 onTriggered: editTagsEntry.accepted()
206 }
207
208 background: Rectangle
209 {
210 color: "transparent"
211 }
212
213 function getTags()
214 {
215 if(!editTagsEntry.text.length > 0)
216 {
217 return
218 }
219
220 var tags = []
221 if(editTagsEntry.text.trim().length > 0)
222 {
223 var list = editTagsEntry.text.split(",")
224
225 if(list.length > 0)
226 {
227 for(var i in list)
228 {
229 tags.push(list[i].trim())
230
231 }
232 }
233 }
234
235 return tags
236 }
237 }
238 }
239 }
240
241 /**
242 * @brief Force the bar to go into editing mode.
243 */
244 function goEditMode()
245 {
246 control.forceActiveFocus()
247 if(control.allowEditMode)
248 _stackView.push(_editComponent)
249 }
250
251 /**
252 * @brief Force to exit the editing mode.
253 */
254 function closeEditMode()
255 {
256 if(_stackView.depth === 2)
257 _stackView.pop()
258 }
259}
KIOCORE_EXPORT QStringList list(const QString &fileClass)
qsizetype length() const const
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.