File indexing completed on 2024-05-12 05:39:50

0001 /***************************************************************************
0002  *  Copyright (C) 2020 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #ifndef CONTENTMODEL_H
0021 #define CONTENTMODEL_H
0022 
0023 #include "controller/view_controller/mediacontrollerbase.h"
0024 #include "media/mediatype.h"
0025 #include <QAbstractListModel>
0026 #include <QSortFilterProxyModel>
0027 #include <core_global.h>
0028 #include <memory>
0029 #include <vector>
0030 
0031 class CORE_EXPORT FilteredContentModel : public QSortFilterProxyModel
0032 {
0033 public:
0034     FilteredContentModel(Core::ContentType type);
0035 
0036     template <class T>
0037     std::vector<T> contentController() const;
0038 
0039 protected:
0040     bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
0041 
0042 private:
0043     Core::ContentType m_type;
0044 };
0045 
0046 class ContentModel : public QAbstractListModel
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051     enum CustomRole
0052     {
0053         NameRole= Qt::UserRole + 1,
0054         TitleRole,
0055         UuidRole,
0056         PathRole,
0057         ContentTypeRole,
0058         ActiveRole,
0059         ModifiedRole,
0060         OwnerIdRole,
0061         ControllerRole
0062     };
0063     Q_ENUM(CustomRole)
0064     explicit ContentModel(QObject* parent= nullptr);
0065     virtual ~ContentModel();
0066 
0067     // Basic functionality:
0068     int rowCount(const QModelIndex& parent= QModelIndex()) const override;
0069 
0070     QVariant data(const QModelIndex& index, int role= Qt::DisplayRole) const override;
0071 
0072     bool appendMedia(MediaControllerBase* media);
0073     bool removeMedia(const QString& uuid);
0074     QString activeMediaId() const;
0075     MediaControllerBase* media(const QString& mediaId);
0076 
0077     std::vector<MediaControllerBase*> controllers() const;
0078 
0079     QHash<int, QByteArray> roleNames() const override;
0080 
0081     void clearData();
0082     int mediaCount(Core::ContentType type);
0083 
0084 signals:
0085     void mediaControllerAdded(MediaControllerBase* newCtrl);
0086 
0087 private:
0088     std::vector<std::unique_ptr<MediaControllerBase>> m_medias;
0089 };
0090 
0091 template <class T>
0092 std::vector<T> FilteredContentModel::contentController() const
0093 {
0094     std::vector<T> vec;
0095     auto size= rowCount();
0096     vec.reserve(size);
0097     for(int i= 0; i < size; ++i)
0098     {
0099         QModelIndex idx= index(i, 0);
0100         auto ctrl= idx.data(ContentModel::ControllerRole).value<MediaControllerBase*>();
0101         auto itemctrl= dynamic_cast<T>(ctrl);
0102         if(nullptr != itemctrl)
0103             vec.push_back(itemctrl);
0104     }
0105     return vec;
0106 }
0107 #endif // CONTENTMODEL_H