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

0001 /***************************************************************************
0002  *  Copyright (C) 2009 by Renaud Guezennec                             *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   Rolisteam 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 SESSIONITEMMODEL_H
0021 #define SESSIONITEMMODEL_H
0022 
0023 #include <QAbstractItemModel>
0024 #include <QMimeData>
0025 #include <core_global.h>
0026 #include <media/mediatype.h>
0027 #include <memory>
0028 namespace session
0029 {
0030 Q_NAMESPACE
0031 enum SessionItemType
0032 {
0033     Media,
0034     Person,
0035     Chapter
0036 };
0037 Q_ENUM_NS(SessionItemType)
0038 
0039 class CORE_EXPORT SessionData
0040 {
0041 public:
0042     SessionData();
0043 
0044     void setUuid(const QString& uuid);
0045     QString uuid() const;
0046 
0047     void setName(const QString& name);
0048     QString name() const;
0049 
0050     void setPath(const QString& path);
0051     QString path() const;
0052 
0053     void setData(const QVariant& data);
0054     QVariant data() const;
0055 
0056     SessionItemType type() const;
0057     void setType(SessionItemType type);
0058 
0059 private:
0060     QString m_name;
0061     QString m_uuid;
0062     QString m_path;
0063     QVariant m_data;
0064     SessionItemType m_type= Chapter;
0065 };
0066 
0067 class CORE_EXPORT SessionItem
0068 {
0069 public:
0070     SessionItem();
0071 
0072     bool isLeaf() const;
0073     int childrenCount() const;
0074     SessionData* data() const;
0075     int indexOf(SessionItem*) const;
0076     void insertChildAt(int row, SessionItem*);
0077     SessionItem* childAt(int i) const;
0078     void setParentNode(SessionItem* parent);
0079     SessionItem* parentNode() const;
0080     void removeChild(SessionItem* item);
0081     bool contains(const QString& id) const;
0082     QString uuid() const;
0083     void clear();
0084 
0085     SessionItem* find(const QString& id) const;
0086 
0087     void setName(const QString& name);
0088 
0089 private:
0090     std::vector<std::unique_ptr<SessionItem>> m_children;
0091     std::unique_ptr<SessionData> m_data;
0092     SessionItem* m_parent= nullptr;
0093 };
0094 
0095 /**
0096  * @brief Show opened resources and allow to sort and order them
0097  */
0098 class CORE_EXPORT SessionItemModel : public QAbstractItemModel
0099 {
0100     Q_OBJECT
0101 public:
0102     enum COLUMN_TYPE
0103     {
0104         Name,
0105         Path
0106     };
0107 
0108     SessionItemModel();
0109     virtual ~SessionItemModel();
0110     virtual QModelIndex index(int row, int column, const QModelIndex& parent= QModelIndex()) const;
0111     virtual QModelIndex parent(const QModelIndex& index) const;
0112     virtual int rowCount(const QModelIndex&) const;
0113     virtual int columnCount(const QModelIndex&) const;
0114     virtual QVariant data(const QModelIndex& index, int role= Qt::DisplayRole) const;
0115     QVariant headerData(int section, Qt::Orientation orientation, int role= Qt::DisplayRole) const;
0116 
0117     void remove(QModelIndex& index);
0118     void addResource(SessionItem* uri, const QModelIndex& index);
0119     virtual bool setData(const QModelIndex& index, const QVariant& value, int role= Qt::EditRole);
0120     virtual Qt::ItemFlags flags(const QModelIndex& index) const;
0121     void clearData();
0122 
0123     Qt::DropActions supportedDropActions() const;
0124     bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent);
0125     QStringList mimeTypes() const;
0126     QMimeData* mimeData(const QModelIndexList& indexes) const;
0127 
0128     void addMedia(const QString& id, const QString& path, Core::ContentType type, const QString& name);
0129     void removeMedia(const QString& id);
0130 public slots:
0131     void removeNode(SessionItem*);
0132 
0133 signals:
0134     void openResource(SessionItem*, bool);
0135 
0136 protected:
0137     bool moveMediaItem(QList<SessionItem*> items, const QModelIndex& parentToBe, int row,
0138                        QList<QModelIndex>& formerPosition);
0139 
0140 private:
0141     std::unique_ptr<SessionItem> m_rootItem;
0142     QStringList m_header;
0143 };
0144 } // namespace session
0145 #endif // SESSIONITEMMODEL_H