QIviSearchAndBrowseModel Class
The QIviSearchAndBrowseModel is a generic model which can be used to search, browse, filter and sort data. More...
Header: | #include <QIviSearchAndBrowseModel> |
qmake: | QT += ivicore |
Instantiated By: | SearchAndBrowseModel |
Inherits: | QIviPagingModel |
Public Types
enum | NavigationType { InModelNavigation, OutOfModelNavigation } |
enum | Roles { CanGoForwardRole } |
Properties
- availableContentTypes : const QStringList
- canGoBack : const bool
- contentType : QString
- query : QString
Public Functions
QIviSearchAndBrowseModel(QObject *parent = nullptr) | |
QStringList | availableContentTypes() const |
bool | canGoBack() const |
bool | canGoForward(int i) const |
QString | contentType() const |
void | goBack() |
QIviSearchAndBrowseModel * | goForward(int i, QIviSearchAndBrowseModel::NavigationType navigationType) |
QIviPendingReply<int> | indexOf(const QVariant &variant) |
QIviPendingReply<void> | insert(int index, const QVariant &variant) |
QIviPendingReply<void> | move(int cur_index, int new_index) |
QString | query() const |
QIviPendingReply<void> | remove(int index) |
void | setContentType(const QString &contentType) |
void | setQuery(const QString &query) |
Reimplemented Public Functions
virtual QVariant | data(const QModelIndex &index, int role) const override |
virtual QHash<int, QByteArray> | roleNames() const override |
Signals
void | availableContentTypesChanged(const QStringList &availableContentTypes) |
void | canGoBackChanged(bool canGoBack) |
void | contentTypeChanged(const QString &contentType) |
void | queryChanged(const QString &query) |
Reimplemented Protected Functions
virtual void | clearServiceObject() override |
virtual void | connectToServiceObject(QIviServiceObject *serviceObject) override |
Detailed Description
The QIviSearchAndBrowseModel should be used directly or as a base class whenever a lot of data needs to be presented in a ListView.
The model is built upon the basic principle of filtering and sorting the data already where they are created instead of retrieving everything and sort or filter it locally. In addition the QIviSearchAndBrowseModel only fetches the data it really needs and can it can be configured how this can be done.
The backend filling the model with data needs to implement the QIviSearchAndBrowseModelInterface class.
Setting it up
The QIviSearchAndBrowseModel is using QtIviCore's Dynamic Backend System and is derived from QIviAbstractFeatureListModel. Other than most "QtIvi Feature classes", the QIviSearchAndBrowseModel doesn't automatically connect to available backends.
The easiest approach to set it up, is to connect to the same backend used by another feature. E.g. for connecting to the media backend, use the instance from the mediaplayer feature:
QIviMediaPlayer *player = new QIviMediaPlayer(); player->startAutoDiscovery(); QIviSearchAndBrowseModel *model = new QIviSearchAndBrowseModel(); model->setServiceObject(player->serviceObject());
Content Types
Once the model is connected to a backend, the contentType needs to be selected. All possible content types can be queried from the availableContentTypes property. As the name already suggests, this property selects what type of content should be shown in the model. For the mediaplayer example, the available content types could be "track", "album" and "artist".
Filtering and Sorting
One of the main use case of the QIviSearchAndBrowseModel is to provide a powerful way of filtering and sorting the content of the underlying data model. As explained above, the filtering and sorting is supposed to happen where the data is produced. To make this work across multiple backends the Qt IVI Query Language was invented.
The query property is used to sort the content of the model: e.g. by setting the string "[/name]", the content will be sorted by name in ascending order.
For filtering, the same property is used but without the brackets e.g. "name='Example Item'" for only showing items which have the 'name' property set to 'Example Item'.
Filtering and sorting can also be combined in one string and the filter part can also be more complex. More on that can be found in the detailed Qt IVI Query Language Documentation.
Browsing
In addition to filtering and sorting, the QIviSearchAndBrowseModel also supports browsing through a hierarchy of different content types. The easiest way to explain this is to look at the existing media example.
When implementing a library view of all available media files, you might want to provide a way for the user to browse through the media database and select a song. You might also want to provide several staring points and from there limit the results. E.g.
- Artist -> Album -> Track
- Album -> Track
- Track
This can be achieved by defining a complex filter query which takes the previously selected item into account. That is the most powerful way of doing it, as the developer/designer can define the browsing order and it can easily be changed. The downside of this is that the backend needs to support this way of filtering and sorting as well, which is not always be the case. A good example here is a DLNA backend, where the server already defines a fixed browsing order.
The QIviSearchAndBrowseModel provides the following methods for browsing:
- canGoForward()
- goForward()
- canGoBack()
- goBack()
Navigation Types
The QIviSearchAndBrowseModel supports two navigation types when browsing through the available data: for most use cases the simple InModelNavigation type is sufficient. By using this, the content type of the current model instance changes when navigating and the model is reset to show the new data. The other navigation type is OutOfModelNavigation and leaves the current model instance as it is. Instead the goForward() method returns a new model instance which contains the new data. This is especially useful when several views need to be open at the same time. E.g. when used inside a QML StackView.
QIviSearchAndBrowseModel *artistModel = new QIviSearchAndBrowseModel(); model->setContentType("artist"); //Returns a new instance of QIviSearchAndBrowseModel which contains all albums from the artist at index '0' QIviSearchAndBrowseModel *albumModel = artistModel->goForward(0, QIviSearchAndBrowseModel::OutOfModelNavigation);
Note: Please also see the QIviPagingModel documentation for how the data loading works and the Models section for more information about all models in QtIvi.
Member Type Documentation
enum QIviSearchAndBrowseModel::NavigationType
Constant | Value | Description |
---|---|---|
QIviSearchAndBrowseModel::InModelNavigation | 0 | The new content will be loaded into this model and the existing model data will be reset |
QIviSearchAndBrowseModel::OutOfModelNavigation | 1 | A new model will be returned which loads the new content. The model data of this model will not be changed and can still be used. |
enum QIviSearchAndBrowseModel::Roles
Constant | Value | Description |
---|---|---|
QIviSearchAndBrowseModel::CanGoForwardRole | QIviPagingModel::LastRole + 1 | True if this item can be used to go one level forward and display the next set of items. See also goForward() |
See also QIviPagingModel::Roles.
Property Documentation
availableContentTypes : const QStringList
Holds all the available content types
Access functions:
QStringList | availableContentTypes() const |
Notifier signal:
void | availableContentTypesChanged(const QStringList &availableContentTypes) |
See also contentType.
canGoBack : const bool
Holds whether the goBack() function can be used to return to the previous content.
See Browsing for more information.
Access functions:
bool | canGoBack() const |
Notifier signal:
void | canGoBackChanged(bool canGoBack) |
contentType : QString
Holds the current type of content displayed in this model.
Note: When changing this property the content will be reset.
Access functions:
QString | contentType() const |
void | setContentType(const QString &contentType) |
Notifier signal:
void | contentTypeChanged(const QString &contentType) |
See also availableContentTypes.
query : QString
Holds the current query used for filtering and sorting the current content of the model.
Note: When changing this property the content will be reset.
See Qt IVI Query Language for more information.
Access functions:
QString | query() const |
void | setQuery(const QString &query) |
Notifier signal:
void | queryChanged(const QString &query) |
See also FilteringAndSorting.
Member Function Documentation
QIviSearchAndBrowseModel::QIviSearchAndBrowseModel(QObject *parent = nullptr)
Constructs a QIviSearchAndBrowseModel.
The parent argument is passed on to the QIviAbstractFeatureListModel base class.
bool QIviSearchAndBrowseModel::canGoForward(int i) const
Returns true when the item at index i can be used to show the next set of elements.
See also Browsing for more information.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[override virtual protected]
void QIviSearchAndBrowseModel::clearServiceObject()
Reimplements: QIviPagingModel::clearServiceObject().
[override virtual protected]
void QIviSearchAndBrowseModel::connectToServiceObject(QIviServiceObject *serviceObject)
Reimplements: QIviPagingModel::connectToServiceObject(QIviServiceObject *serviceObject).
[override virtual]
QVariant QIviSearchAndBrowseModel::data(const QModelIndex &index, int role) const
Reimplements: QIviPagingModel::data(const QModelIndex &index, int role) const.
void QIviSearchAndBrowseModel::goBack()
Goes one level back in the navigation history.
See also Browsing for more information.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
QIviSearchAndBrowseModel *QIviSearchAndBrowseModel::goForward(int i, QIviSearchAndBrowseModel::NavigationType navigationType)
Returns true when the item at index i can be used to show the next set of elements.
Uses the item at index i and shows the next set of items. The navigationType can be used to control whether the new data should be shown in this model instance or whether a new instance should be created and returned. If a instance is returned, this instance is owned by the caller.
Note: Whether the OutOfModelNavigation navigation type is supported is decided by the backend.
See also Browsing for more information.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
QIviPendingReply<int> QIviSearchAndBrowseModel::indexOf(const QVariant &variant)
Determines the index of variant in this model.
The result is returned as a QIviPendingReply.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
QIviPendingReply<void> QIviSearchAndBrowseModel::insert(int index, const QVariant &variant)
Insert the variant at the position index.
If the backend doesn't accept the provided item, this operation will end in a no op.
The returned QIviPendingReply notifies about when the action has been done or whether it failed.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
QIviPendingReply<void> QIviSearchAndBrowseModel::move(int cur_index, int new_index)
Moves the item at position cur_index to the new position new_index.
The returned QIviPendingReply notifies about when the action has been done or whether it failed.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
QIviPendingReply<void> QIviSearchAndBrowseModel::remove(int index)
Removes the item at position index.
The returned QIviPendingReply notifies about when the action has been done or whether it failed.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[override virtual]
QHash<int, QByteArray> QIviSearchAndBrowseModel::roleNames() const
Reimplements: QIviPagingModel::roleNames() const.
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.