File indexing completed on 2024-05-12 05:39:50
0001 /*************************************************************************** 0002 * Copyright (C) 2021 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 MEDIAMODEL_H 0021 #define MEDIAMODEL_H 0022 0023 #include <QAbstractItemModel> 0024 #include <QDateTime> 0025 #include <QFileInfo> 0026 #include <QList> 0027 #include <QPointer> 0028 #include <core_global.h> 0029 #include <memory> 0030 0031 class QUndoCommand; 0032 namespace campaign 0033 { 0034 class Media; 0035 class CORE_EXPORT MediaNode : public QObject 0036 { 0037 Q_OBJECT 0038 Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged) 0039 public: 0040 enum NodeType 0041 { 0042 File, 0043 Directory 0044 }; 0045 MediaNode(NodeType type, const QString& path, const QString& uuid= QString()); 0046 0047 NodeType nodeType() const; 0048 QString uuid() const; 0049 int childrenCount() const; 0050 QString parentPath() const; 0051 QString path() const; 0052 QString name() const; 0053 QVariant size() const; 0054 QDateTime modifiedTime() const; 0055 0056 MediaNode* childAt(int i) const; 0057 void removeChild(int i); 0058 0059 const std::vector<std::unique_ptr<MediaNode>>& children() const; 0060 void addChild(std::unique_ptr<MediaNode> node); 0061 signals: 0062 void pathChanged(); 0063 0064 public slots: 0065 void setPath(const QString& path); 0066 0067 private: 0068 NodeType m_type; 0069 QString m_path; 0070 QFileInfo m_info; 0071 QString m_uuid; 0072 std::vector<std::unique_ptr<MediaNode>> m_children; 0073 }; 0074 0075 class Campaign; 0076 class CORE_EXPORT MediaModel : public QAbstractItemModel 0077 { 0078 Q_OBJECT 0079 public: 0080 enum DataRole 0081 { 0082 Role_Name= Qt::UserRole + 1, 0083 Role_Size, 0084 Role_Type, 0085 Role_AddedDate, 0086 Role_ModifiedDate, 0087 Role_Icon, 0088 Role_Path, 0089 Role_Uuid, 0090 Role_IsDir, 0091 Role_Unknown 0092 }; 0093 explicit MediaModel(Campaign* campaign, QObject* parent= nullptr); 0094 0095 // Header: 0096 QVariant headerData(int section, Qt::Orientation orientation, int role= Qt::DisplayRole) const override; 0097 // Basic functionality: 0098 QModelIndex index(int row, int column, const QModelIndex& parent= QModelIndex()) const override; 0099 QModelIndex parent(const QModelIndex& index) const override; 0100 0101 int rowCount(const QModelIndex& parent= QModelIndex()) const override; 0102 int columnCount(const QModelIndex& parent= QModelIndex()) const override; 0103 0104 QVariant data(const QModelIndex& index, int role= Qt::DisplayRole) const override; 0105 0106 // Editable: 0107 bool setData(const QModelIndex& index, const QVariant& value, int role= Qt::EditRole) override; 0108 0109 Qt::ItemFlags flags(const QModelIndex& index) const override; 0110 0111 void dataChangedFor(MediaNode* node); 0112 0113 void setCampaign(Campaign* campaign); 0114 signals: 0115 void campaignChanged(); 0116 void performCommand(QUndoCommand* cmd); 0117 0118 private slots: 0119 void addMediaNode(Media* media); 0120 void removeMediaNode(const QString& id); 0121 0122 void initDataFromCampaign(); 0123 0124 private: 0125 QPointer<Campaign> m_campaign; 0126 std::unique_ptr<MediaNode> m_root; 0127 QStringList m_headers; 0128 }; 0129 0130 } // namespace campaign 0131 #endif // MEDIAMODEL_H