qt/mlbasemodel: Add 'getData' function for QModelIndex
[vlc.git] / modules / gui / qt / medialibrary / mlbasemodel.hpp
blob2c54681a4574ce34c7fe83a16d5782da2f140cfb
1 /*****************************************************************************
2 * Copyright (C) 2019 VLC authors and VideoLAN
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * ( at your option ) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17 *****************************************************************************/
19 #ifndef MLBASEMODEL_HPP
20 #define MLBASEMODEL_HPP
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "vlc_common.h"
28 #include <memory>
29 #include <QObject>
30 #include <QAbstractListModel>
31 #include "vlc_media_library.h"
32 #include "mlqmltypes.hpp"
33 #include "medialib.hpp"
34 #include <memory>
35 #include "mlevent.hpp"
36 #include "mlqueryparams.hpp"
37 #include "util/listcacheloader.hpp"
39 template <typename T>
40 class ListCache;
42 class MediaLib;
44 class MLBaseModel : public QAbstractListModel
46 Q_OBJECT
48 Q_PROPERTY(MLItemId parentId READ parentId WRITE setParentId NOTIFY parentIdChanged
49 RESET unsetParentId)
51 Q_PROPERTY(MediaLib * ml READ ml WRITE setMl)
53 Q_PROPERTY(QString searchPattern READ searchPattern WRITE setSearchPattern)
55 Q_PROPERTY(Qt::SortOrder sortOrder READ getSortOrder WRITE setSortOder NOTIFY sortOrderChanged)
57 Q_PROPERTY(QString sortCriteria READ getSortCriteria WRITE setSortCriteria
58 NOTIFY sortCriteriaChanged RESET unsetSortCriteria)
60 Q_PROPERTY(unsigned int count READ getCount NOTIFY countChanged)
62 public:
63 explicit MLBaseModel(QObject *parent = nullptr);
65 virtual ~MLBaseModel();
67 public: // Interface
68 Q_INVOKABLE void sortByColumn(QByteArray name, Qt::SortOrder order);
70 Q_INVOKABLE virtual QVariant getIdForIndex(QVariant index) const;
72 Q_INVOKABLE virtual QVariantList getIdsForIndexes(const QVariantList & indexes) const;
73 Q_INVOKABLE virtual QVariantList getIdsForIndexes(const QModelIndexList & indexes) const;
75 Q_INVOKABLE virtual QVariantList getItemsForIndexes(const QModelIndexList & indexes) const;
77 Q_INVOKABLE QMap<QString, QVariant> getDataAt(const QModelIndex & index);
78 Q_INVOKABLE QMap<QString, QVariant> getDataAt(int idx);
80 signals:
81 void parentIdChanged();
82 void resetRequested();
83 void sortOrderChanged();
84 void sortCriteriaChanged();
85 void countChanged(unsigned int) const;
87 protected slots:
88 void onResetRequested();
89 void onLocalSizeAboutToBeChanged(size_t size);
90 void onLocalSizeChanged(size_t size);
91 void onLocalDataChanged(size_t index, size_t count);
93 private:
94 static void onVlcMlEvent( void* data, const vlc_ml_event_t* event );
96 protected:
97 virtual void clear();
98 virtual vlc_ml_sorting_criteria_t roleToCriteria(int role) const = 0;
99 static QString getFirstSymbol(QString str);
100 virtual vlc_ml_sorting_criteria_t nameToCriteria(QByteArray) const {
101 return VLC_ML_SORTING_DEFAULT;
103 virtual QByteArray criteriaToName(vlc_ml_sorting_criteria_t ) const
105 return "";
108 void validateCache() const;
109 void invalidateCache();
110 MLItem* item(int signedidx) const;
111 virtual void onVlcMlEvent( const MLEvent &event );
113 virtual ListCacheLoader<std::unique_ptr<MLItem>> *createLoader() const = 0;
115 virtual void thumbnailUpdated( int ) {}
117 /* Data loader for the cache */
118 struct BaseLoader : public ListCacheLoader<std::unique_ptr<MLItem>>
120 BaseLoader(vlc_medialibrary_t *ml, MLItemId parent, QString searchPattern,
121 vlc_ml_sorting_criteria_t sort, bool sort_desc);
122 BaseLoader(const MLBaseModel &model);
124 MLQueryParams getParams(size_t index = 0, size_t count = 0) const;
126 protected:
127 vlc_medialibrary_t *m_ml;
128 MLItemId m_parent;
129 QString m_searchPattern;
130 vlc_ml_sorting_criteria_t m_sort;
131 bool m_sort_desc;
134 public:
135 MLItemId parentId() const;
136 void setParentId(MLItemId parentId);
137 void unsetParentId();
139 MediaLib* ml() const;
140 void setMl(MediaLib* ml);
142 const QString& searchPattern() const;
143 void setSearchPattern( const QString& pattern );
145 Qt::SortOrder getSortOrder() const;
146 void setSortOder(Qt::SortOrder order);
147 const QString getSortCriteria() const;
148 void setSortCriteria(const QString& criteria);
149 void unsetSortCriteria();
151 int rowCount(const QModelIndex &parent = {}) const;
152 virtual unsigned int getCount() const;
154 protected:
155 MLItemId m_parent;
157 vlc_medialibrary_t* m_ml = nullptr;
158 MediaLib* m_mediaLib = nullptr;
159 QString m_search_pattern;
160 vlc_ml_sorting_criteria_t m_sort = VLC_ML_SORTING_DEFAULT;
161 bool m_sort_desc = false;
163 std::unique_ptr<vlc_ml_event_callback_t,
164 std::function<void(vlc_ml_event_callback_t*)>> m_ml_event_handle;
165 bool m_need_reset = false;
167 mutable std::unique_ptr<ListCache<std::unique_ptr<MLItem>>> m_cache;
170 #endif // MLBASEMODEL_HPP