File indexing completed on 2024-05-19 04:56:07

0001 /**
0002  * \file playlistmodel.h
0003  * Model containing files in playlist.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 05 Aug 2018
0008  *
0009  * Copyright (C) 2018-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QAbstractProxyModel>
0030 #include "playlistconfig.h"
0031 #include "kid3api.h"
0032 
0033 class FileProxyModel;
0034 
0035 /**
0036  * Playlist model.
0037  */
0038 class KID3_CORE_EXPORT PlaylistModel : public QAbstractProxyModel {
0039   Q_OBJECT
0040 public:
0041   /**
0042    * Constructor.
0043    * @param fsModel file proxy model
0044    * @param parent parent object
0045    */
0046   explicit PlaylistModel(FileProxyModel* fsModel, QObject* parent = nullptr);
0047 
0048   /**
0049    * Destructor.
0050    */
0051   ~PlaylistModel() override = default;
0052 
0053   /**
0054    * Get item flags for index.
0055    * @param index model index
0056    * @return item flags
0057    */
0058   Qt::ItemFlags flags(const QModelIndex& index) const override;
0059 
0060   /**
0061    * Set data for a given role.
0062    * @param index model index
0063    * @param value data value
0064    * @param role item data role
0065    * @return true if successful
0066    */
0067   bool setData(const QModelIndex& index, const QVariant& value,
0068                int role = Qt::EditRole) override;
0069 
0070   /**
0071    * Get number of rows.
0072    * @param parent parent model index, invalid for table models
0073    * @return number of rows,
0074    * if parent is valid number of children (0 for table models)
0075    */
0076   int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0077 
0078   /**
0079    * Get number of columns.
0080    * @param parent parent model index, invalid for table models
0081    * @return number of columns,
0082    * if parent is valid number of children (0 for table models)
0083    */
0084   int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0085 
0086   /**
0087    * Insert rows.
0088    * @param row rows are inserted before this row, if 0 at the begin,
0089    * if rowCount() at the end
0090    * @param count number of rows to insert
0091    * @param parent parent model index, invalid for table models
0092    * @return true if successful
0093    */
0094   bool insertRows(int row, int count,
0095                   const QModelIndex& parent = QModelIndex()) override;
0096 
0097   /**
0098    * Remove rows.
0099    * @param row rows are removed starting with this row
0100    * @param count number of rows to remove
0101    * @param parent parent model index, invalid for table models
0102    * @return true if successful
0103    */
0104   bool removeRows(int row, int count,
0105                   const QModelIndex& parent = QModelIndex()) override;
0106 
0107   /**
0108    * Get model index of item.
0109    * @param row row of item
0110    * @param column column of item
0111    * @param parent index of parent item
0112    * @return model index of item
0113    */
0114   QModelIndex index(int row, int column,
0115                     const QModelIndex& parent = QModelIndex()) const override;
0116 
0117   /**
0118    * Get parent of item.
0119    * @param child model index of item
0120    * @return model index of parent item
0121    */
0122   QModelIndex parent(const QModelIndex& child) const override;
0123 
0124   /**
0125    * Map from index in proxy model to model index.
0126    * @param proxyIndex index in proxy model
0127    * @return index in source model.
0128    */
0129   QModelIndex mapToSource(const QModelIndex& proxyIndex) const override;
0130 
0131   /**
0132    * Map from model index to index in proxy model.
0133    * @param sourceIndex index in source model
0134    * @return index in proxy model.
0135    */
0136   QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override;
0137 
0138   /**
0139    * Get supported drop actions.
0140    * @return supported drop actions.
0141    */
0142   Qt::DropActions supportedDropActions() const override;
0143 
0144   /**
0145    * Get name of playlist file.
0146    * @return playlist file name.
0147    */
0148   QString playlistFileName() const { return m_playlistFileName; }
0149 
0150   /**
0151    * Set playlist to edit.
0152    * If the same @a path is already set, nothing is done.
0153    * An empty @a path can be used to clear the model, so that the playlist
0154    * will be read from the file when called the next time with a path.
0155    * Check filesNotFound() to see if some files could not be located.
0156    * @param path path to playlist file, empty to clear
0157    */
0158   void setPlaylistFile(const QString& path);
0159 
0160   /**
0161    * Get list of files which were not found when setPlaylistFile() was called.
0162    * @return list of file entries which could not be located.
0163    */
0164   QStringList filesNotFound() const { return m_filesNotFound; }
0165 
0166   /**
0167    * Modification state of playlist.
0168    * @return true if modified.
0169    */
0170   bool isModified() const { return m_modified; }
0171 
0172   /**
0173    * Set modification state of playlist.
0174    * If the state is changed, modifiedChanged() is emitted.
0175    * @param modified true if modified
0176    */
0177   void setModified(bool modified);
0178 
0179   /**
0180    * Get paths to files in playlist.
0181    * @return list of absolute paths.
0182    */
0183   QStringList pathsInPlaylist() const;
0184 
0185   /**
0186    * Set paths to files in playlist.
0187    * @param paths list of absolute paths
0188    * @return true if ok, false if not all @a paths were found and added.
0189    */
0190   bool setPathsInPlaylist(const QStringList& paths);
0191 
0192 public slots:
0193   /**
0194    * Save changes to playlist file.
0195    * @return true if ok.
0196    */
0197   bool save();
0198 
0199 signals:
0200   /**
0201    * Emitted when isModified() is changed.
0202    * @param modified true if modified
0203    */
0204   void modifiedChanged(bool modified);
0205 
0206 private slots:
0207   void onSourceModelAboutToBeReset();
0208   void onSourceModelReloaded();
0209 
0210 private:
0211   PlaylistConfig m_playlistConfig;
0212   QString m_playlistFilePath;
0213   QString m_playlistFileName;
0214   QList<QPersistentModelIndex> m_items;
0215   QStringList m_pathsSavedDuringReset;
0216   QStringList m_filesNotFound;
0217   FileProxyModel* m_fsModel;
0218   bool m_modified;
0219 };