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 onActiveFocusChanged:
197 {
198 if(!editTagsEntry.activeFocus)
199 {
200 closeEditMode()
201 }
202 }
203
204 onAccepted:
205 {
206 control.tagsEdited(getTags())
207 control.closeEditMode()
208 }
209
210 actions: Action
211 {
212 icon.name: "checkbox"
213 onTriggered: editTagsEntry.accepted()
214 }
215
216 background: Rectangle
217 {
218 color: "transparent"
219 }
220
221 function getTags()
222 {
223 if(!editTagsEntry.text.length > 0)
224 {
225 return
226 }
227
228 var tags = []
229 if(editTagsEntry.text.trim().length > 0)
230 {
231 var list = editTagsEntry.text.split(",")
232
233 if(list.length > 0)
234 {
235 for(var i in list)
236 {
237 tags.push(list[i].trim())
238
239 }
240 }
241 }
242
243 return tags
244 }
245 }
246 }
247 }
248
249 /**
250 * @brief Force the bar to go into editing mode.
251 */
252 function goEditMode()
253 {
254 control.forceActiveFocus()
255 if(control.allowEditMode)
256 _stackView.push(_editComponent)
257 }
258
259 /**
260 * @brief Force to exit the editing mode.
261 */
262 function closeEditMode()
263 {
264 if(_stackView.depth === 2)
265 _stackView.pop()
266 }
267}
void tagClicked(string tag)
Emitted when a tag element has been clicked.
alias listView
An alias to the flickable element listing the tag buttons.
Definition TagsBar.qml:74
alias count
The total amount of tag elements.
Definition TagsBar.qml:80
void tagRemovedClicked(int index)
Emitted when the close/dismiss button of a tag element has been clicked.
bool editMode
Whether the bar is in edit mode or not.
Definition TagsBar.qml:88
void tagsEdited(var tags)
Emitted when the tags have been manually edited by the user via the text field input.
alias list
To associate a one or a group of file URLs, use list.urls, or to disable the strict mode use list....
Definition TagsBar.qml:100
bool allowEditMode
Whether the bar exposes to the user the action buttons that allow to go into edit mode,...
Definition TagsBar.qml:93
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-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:49:45 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.