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
88
89 /**
90 * @brief Whether the bar should be 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 property bool editMode : false
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)
117
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)
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 Loader
131 {
132 anchors.fill: parent
133 active: control.editMode
134 visible: active
135 asynchronous: true
136
137 sourceComponent: Maui.TextField
138 {
139 id: editTagsEntry
140
141 focus: true
142
143 activeFocusOnPress : true
144
145 text: list.tags.join(",")
146
147 Component.onCompleted:
148 {
149 editTagsEntry.forceActiveFocus()
150 }
151
152 onAccepted:
153 {
154 control.tagsEdited(getTags())
155 control.closeEditMode()
156 }
157
158 actions: Action
159 {
160 icon.name: "checkbox"
161 onTriggered: editTagsEntry.accepted()
162 }
163
164 background: Rectangle
165 {
166 color: "transparent"
167 }
168
169 function getTags()
170 {
171 if(!editTagsEntry.text.length > 0)
172 {
173 return
174 }
175
176 var tags = []
177 if(editTagsEntry.text.trim().length > 0)
178 {
179 var list = editTagsEntry.text.split(",")
180
181 if(list.length > 0)
182 {
183 for(var i in list)
184 {
185 tags.push(list[i].trim())
186
187 }
188 }
189 }
190
191 return tags
192 }
193 }
194 }
195
196 Private.TagList
197 {
198 id: tagsList
199 anchors.fill: parent
200
201 visible: !control.editMode
202
203 showPlaceHolder: allowEditMode
204 showDeleteIcon: allowEditMode
205
206 onTagRemoved: (index) => tagRemovedClicked(index)
207
208 onTagClicked: (index) => control.tagClicked(tagsList.list.get(index).tag)
209
210 onAreaClicked:
211 {
212 if(allowEditMode)
213 {
214 goEditMode()
215 }
216 }
217 }
218
219 /**
220 * @brief Force the bar to go into editing mode.
221 */
222 function goEditMode()
223 {
224 control.forceActiveFocus()
225 control.editMode = true
226 }
227
228 /**
229 * @brief Force to exit the editing mode.
230 */
231 function closeEditMode()
232 {
233 control.editMode = false
234 }
235}
QList< T > & fill(parameter_type value, qsizetype size)
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 Fri Nov 22 2024 12:04:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.