MauiKit Controls

mauilist.h
1/*
2 * <one line to give the program's name and a brief idea of what it does.>
3 * Copyright (C) 2019 camilo <chiguitar@unal.edu.co>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <QObject>
22 #include <QQmlEngine>
23#include "fmh.h"
24
25#include "mauikit_export.h"
26
27#include <QQmlParserStatus>
28
29/**
30 * @brief MauiList class.
31 *
32 * A helper class for easily setting up a list model to be feed into MauiModel::list, and to be used by the browsing views in Mauikit controls.
33 *
34 * @warning This method of setting up a data model is very limited by the lingo supported by the FMH::MODEL_KEY dictionary.
35 * So only consider using this class if your data model structure is based on only string text and can be represented by the FMH::MODEL_KEY dictionary entries.
36 * @see FMH::MODEL_KEY
37 *
38 * This inherits from the QQmlParserStatus class, so t can be aware of its creation status in the QML engine. This is useful to lazy-loading parts when needed, as when the component presenting the data model is ready and loaded.
39 *
40 * The list generated by sub-classing MauiList is meant to be used as the list for the MauiModel class, exposed to QML as the `BaseModel` type. The MauiModel supports features, such as filtering and sorting.
41 * @see MauiModel
42 *
43 * @image html Misc/listbrowser_data.png
44 *
45 * @section example Example
46 * This is a simple example of a MauiList based data model.
47 *
48 * First wee need to setup the MauiList data. In the example below the data is manually added. The data must be modeled using the FMH::MODEL_LIST, which is an array list of FMH::MODEL, which is a map of key pairs, where the key must be a FMH::MODEL_KEY enum type, and the value a string text.
49 *
50 * @code
51 * #pragma once
52 *
53 * #include <QObject>
54 * #include <MauiKit4/Core/mauilist.h>
55 *
56 * class PlantsList : public MauiList
57 * {
58 * Q_OBJECT
59 * public:
60 * PlantsList(QObject *parent = nullptr);
61 *
62 * // QQmlParserStatus interface
63 * void componentComplete();
64 *
65 * // MauiList interface
66 * const FMH::MODEL_LIST &items() const;
67 *
68 * private:
69 * FMH::MODEL_LIST m_list;
70 *
71 * //Here we have our own custom raw data and turn it into a FMH::MODEL_LIST
72 * FMH::MODEL_LIST getData() const;
73 * };
74 * @endcode
75 *
76 *
77 * In this example the data is only retrieved once the QML engine has completely loaded the component using the MauiList, for this we override the `componentComplete()` virtual method, and there we inform the MauiModel with the signals that the list data is ready.
78 *
79 * @code
80 * #include "plantslist.h"
81 *
82 * PlantsList::PlantsList(QObject *parent) : MauiList(parent)
83 * {
84 *
85 * }
86 *
87 * void PlantsList::componentComplete()
88 * {
89 * Q_EMIT preListChanged();
90 * m_list = getData();
91 * Q_EMIT postListChanged();
92 * Q_EMIT countChanged();
93 * }
94 *
95 * const FMH::MODEL_LIST &PlantsList::items() const
96 * {
97 * return m_list;
98 * }
99 *
100 * FMH::MODEL_LIST PlantsList::getData() const
101 * {
102 * FMH::MODEL_LIST data;
103 *
104 * data << FMH::MODEL {{FMH::MODEL_KEY::TITLE, QStringLiteral("Acanthaceae")}, {FMH::MODEL_KEY::CATEGORY, QStringLiteral("Acanthus")}};
105 *
106 * data << FMH::MODEL {{FMH::MODEL_KEY::TITLE, QStringLiteral("Agavaceae")}, {FMH::MODEL_KEY::CATEGORY, QStringLiteral("Agave")}};
107 *
108 * data << FMH::MODEL {{FMH::MODEL_KEY::TITLE, QStringLiteral("Bixaceae")}, {FMH::MODEL_KEY::CATEGORY, QStringLiteral("Annatto")}};
109 *
110 * data << FMH::MODEL {{FMH::MODEL_KEY::TITLE, QStringLiteral("Asteraceae")}, {FMH::MODEL_KEY::CATEGORY, QStringLiteral("Aster")}};
111 *
112 * data << FMH::MODEL {{FMH::MODEL_KEY::TITLE, QStringLiteral("Leguminaceae")}, {FMH::MODEL_KEY::CATEGORY, QStringLiteral("Bean")}};
113 *
114 * return data;
115 * }
116 *
117 * @endcode
118 *
119 * Now we register our own custom PlanstList class to the QML engine as a type.
120 *
121 * @code
122 * #include <MauiKit3/Core/mauiapp.h>
123 * #include "plantslist.h"
124 *
125 * int main(int argc, char *argv[])
126 * {
127 * ...
128 *
129 * qmlRegisterType<PlantsList>("org.maui.demo", 1, 0, "PlantsList"); //Here we register it.
130 *
131 * engine.load(url);
132 *
133 * return app.exec();
134 * }
135 * @endcode
136 *
137 * And finally, we can consume the data list by hooking it up to the MauiModel exposed type `BaseModel`.
138 *
139 * @code
140 * import org.mauikit.controls as Maui
141 *
142 * import org.maui.demo as Demo
143 *
144 * Maui.ApplicationWindow
145 * {
146 * id: root
147 *
148 * Maui.Page
149 * {
150 * id: _page
151 * anchors.fill: parent
152 * Maui.Controls.showCSD: true
153 * headBar.forceCenterMiddleContent: true
154 *
155 * Maui.ListBrowser
156 * {
157 * anchors.fill: parent
158 * model: Maui.BaseModel
159 * {
160 * list: Demo.PlantsList
161 * {
162 *
163 * }
164 * }
165 *
166 * delegate: Maui.ListBrowserDelegate
167 * {
168 * width: ListView.view.width
169 * label1.text: model.title
170 * label2.text: model.category
171 * }
172 * }
173 * }
174 * }
175 * @endcode
176 *
177 * <a href="https://invent.kde.org/maui/mauikit/-/blob/qt6-2/examples/mauilist/">You can find a more complete example at this link.</a>
178 */
179class MAUIKIT_EXPORT MauiList : public QObject, public QQmlParserStatus
180{
181
182 Q_OBJECT
183 QML_ANONYMOUS
185
186 Q_DISABLE_COPY(MauiList)
187
188 /**
189 * The total amount of elements in the list.
190 * @note This needs to be setup manually, as in emitting the signal when new items are appended or removed, etc.
191 */
192 Q_PROPERTY(int count READ getCount NOTIFY countChanged FINAL)
193
194public:
195 /**
196 * @brief Default constructor. The usage of this class is meant to be via inheritance by sub-classing it.
197 */
198 explicit MauiList(QObject *parent = nullptr);
199
200 /**
201 * @brief The modeled data represented by a FMH::MODEL_LIST.
202 * @note The data must be modeled using the FMH::MODEL_LIST, which is an array list of FMH::MODEL elements, which is a map of key pairs, where the key must be a FMH::MODEL_KEY enum type, and the value a string text.
203 *
204 * @return The data model.
205 */
206 virtual const FMH::MODEL_LIST &items() const = 0;
207
208 /**
209 * @brief See the Qt documentation on the QQmlParserStatus.
210 */
211 virtual void classBegin() override {}
212
213 /**
214 * @brief See the Qt documentation on the QQmlParserStatus.
215 */
216 virtual void componentComplete() override {}
217
218 /**
219 * @brief This function is called once the MauiList has been hooked to the MauiModel, using the MauiModel::list property.
220 */
221 virtual void modelHooked() {}
222
223 int getCount() const;
224
225 /**
226 * @brief Request to get an item in the list, the item is represented as a FMH::MODEL key pair value.
227 * @param index the position of the item to retrieve
228 * @return The found item/map in the list at the index. If not item is found at the given index, then an empty map is returned.
229 */
230 FMH::MODEL getItem(const int &index) const;
231
232public Q_SLOTS:
233 /**
234 * @brief Request to get an item in the list, the item is represented as a QVariantMap for easy consumption within the QML scope. This function is exposed to be invoked from QML.
235 * @param index the position of the item to retrieve
236 * @return The found item/map in the list at the index. If not item is found at the given index, then an empty map is returned.
237 */
238 QVariantMap get(const int &index) const;
239
240protected:
241 /**
242 * @brief Whether an item with a given key-par value exists in the list.
243 * @param key the FMH::MODEL_KEY to look for
244 * @param value the value associated with the key to look for
245 * @return an exact match exists or not
246 */
247 bool exists(const FMH::MODEL_KEY &key, const QString &value) const;
248
249 /**
250 * @brief The index number of an item in the list of a given key-par value.
251 * @param key the FMH::MODEL_KEY to look for
252 * @param value the value associated with the key to look for
253 * @return the found index or `-1` if not found
254 */
255 int indexOf(const FMH::MODEL_KEY &key, const QString &value) const;
256
257Q_SIGNALS:
258 /**
259 * @brief This signal should be emitted by the implementation before appending a new item to the list data model.
260 */
262
263 /**
264 * @brief This signal should be emitted by the implementation before appending a multiple new items to the list data model.
265 * @param count the total amount of new items that will be added
266 */
267 void preItemsAppended(uint count);
268
269 /**
270 * @brief This signal should be emitted by the implementation after one or multiple new items have finished being added into the list data model.
271 */
273
274 /**
275 * @brief This signal should be emitted by the implementation before a new item has been inserted at a given index to the list data model.
276 * @param index the position index where the new item was inserted at
277 */
278 void preItemAppendedAt(int index);
279
280 /**
281 * @brief This signal should be emitted by the implementation before an item has been removed at the given index position.
282 * @param index the index position of the element that will be removed
283 */
284 void preItemRemoved(int index);
285
286 /**
287 * @brief This signal should be emitted by the implementation after an item has been successfully removed from the list data model.
288 */
290
291 /**
292 * @brief This signal should be emitted by the implementation when changes have been done in the list data model.
293 * @param index the index position of the item that was modified in the list data model
294 * @param roles the keys that were modified, the key values can be FMH::MODEL_KEY
295 */
296 void updateModel(int index, QVector<int> roles);
297
298 /**
299 * @brief This signal should be emitted by the implementation before the list data model has been assigned or populated.
300 */
302
303 /**
304 * @brief This signal should be emitted by the implementation after the list data model is set and done.
305 */
307
308 /**
309 * @brief This signal should be emitted by the implementation when an item has been moved from one index position to another.
310 * @param index the original index position of the item in the list data model
311 * @param to the new index destination of the item
312 */
313 void itemMoved(int index, int to);
314
315 /**
316 * @brief This signal should be emitted by the implementation when the number of elements in the list data model varies. For example when an item is removed or added.
317 */
319};
MauiList class.
Definition mauilist.h:180
virtual void modelHooked()
This function is called once the MauiList has been hooked to the MauiModel, using the MauiModel::list...
Definition mauilist.h:221
void preItemAppendedAt(int index)
This signal should be emitted by the implementation before a new item has been inserted at a given in...
void preItemAppended()
This signal should be emitted by the implementation before appending a new item to the list data mode...
void countChanged()
This signal should be emitted by the implementation when the number of elements in the list data mode...
void updateModel(int index, QVector< int > roles)
This signal should be emitted by the implementation when changes have been done in the list data mode...
void postItemAppended()
This signal should be emitted by the implementation after one or multiple new items have finished bei...
void preItemRemoved(int index)
This signal should be emitted by the implementation before an item has been removed at the given inde...
void preItemsAppended(uint count)
This signal should be emitted by the implementation before appending a multiple new items to the list...
void itemMoved(int index, int to)
This signal should be emitted by the implementation when an item has been moved from one index positi...
virtual void componentComplete() override
See the Qt documentation on the QQmlParserStatus.
Definition mauilist.h:216
void preListChanged()
This signal should be emitted by the implementation before the list data model has been assigned or p...
void postListChanged()
This signal should be emitted by the implementation after the list data model is set and done.
void postItemRemoved()
This signal should be emitted by the implementation after an item has been successfully removed from ...
A set of helpers for managing the key-value model data type.
Definition fmh.cpp:7
MODEL_KEY
The MODEL_KEY enum values.
Definition fmh.h:64
Q_INTERFACES(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:56:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.