File indexing completed on 2024-05-05 04:48:30

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Daniel Jones <danielcjones@gmail.com>                             *
0003  * Copyright (c) 2009-2010 Leo Franchi <lfranchi@kde.org>                               *
0004  * Copyright (c) 2011 Ralf Engels <ralf-engels@gmx.de>                                  *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) any later           *
0009  * version.                                                                             *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #ifndef DYNAMICMODEL_H
0020 #define DYNAMICMODEL_H
0021 
0022 #include "Bias.h"
0023 #include "DynamicPlaylist.h"
0024 
0025 #include "amarok_export.h" // we are exporting it for the tests
0026 
0027 #include <QAbstractItemModel>
0028 #include <QList>
0029 #include <QString>
0030 
0031 class TestDynamicModel;
0032 
0033 namespace Dynamic {
0034 
0035 class BiasedPlaylist;
0036 
0037 class AMAROK_EXPORT DynamicModel : public QAbstractItemModel
0038 {
0039     Q_OBJECT
0040     public:
0041         // role used for the model
0042         enum Roles
0043         {
0044             // WidgetRole = 0xf00d,
0045             PlaylistRole = 0xf00e,
0046             BiasRole = 0xf00f,
0047 
0048             BiasPercentage = 0xf010, // for sub-biases underneath a part bias
0049             BiasOperation = 0xf011   // for sub-biases underneath a and-bias
0050         };
0051 
0052         static DynamicModel* instance();
0053 
0054         ~DynamicModel() override;
0055 
0056         // void changePlaylist( int i );
0057 
0058         /** Returns the currently active playlist.
0059             Don't free this pointer
0060         */
0061         Dynamic::DynamicPlaylist* activePlaylist() const;
0062         int activePlaylistIndex() const;
0063 
0064         /** Find the playlist with name, make it active and return it */
0065         Dynamic::DynamicPlaylist* setActivePlaylist( int );
0066 
0067         int playlistIndex( Dynamic::DynamicPlaylist* playlist ) const;
0068 
0069         /** Inserts a playlist at the given index.
0070             If the playlist is already in the model it will be moved
0071             to the position
0072         */
0073         QModelIndex insertPlaylist( int index, Dynamic::DynamicPlaylist* playlist );
0074 
0075         /** Inserts a bias at the given index.
0076             The bias must not be part of a model. When in doubt call bias->replace(BiasPtr())
0077             to remove the bias from it's current position.
0078         */
0079         QModelIndex insertBias( int row, const QModelIndex &parentIndex, const Dynamic::BiasPtr &bias );
0080 
0081         Qt::DropActions supportedDropActions() const override;
0082 
0083         // --- QAbstractItemModel functions ---
0084         QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override;
0085         bool setData( const QModelIndex& index, const QVariant& value, int role = Qt::EditRole ) override;
0086         Qt::ItemFlags flags( const QModelIndex& index ) const override;
0087         QModelIndex index( int row, int column, const QModelIndex& parent = QModelIndex() ) const override;
0088         QModelIndex parent(const QModelIndex& index) const override;
0089         int rowCount( const QModelIndex& parent = QModelIndex() ) const override;
0090         int columnCount( const QModelIndex& parent = QModelIndex() ) const override;
0091 
0092         QStringList mimeTypes() const override;
0093         QMimeData* mimeData(const QModelIndexList &indexes) const override;
0094         bool dropMimeData(const QMimeData *data,
0095                           Qt::DropAction action,
0096                           int row, int column, const QModelIndex &parent) override;
0097 
0098         // ---
0099 
0100         /** Returns the index for the bias
0101             @return Returns an invalid index if the bias is not in the model.
0102         */
0103         QModelIndex index( const Dynamic::BiasPtr &bias ) const;
0104 
0105         /** Returns the index for the playlist
0106             @return Returns an invalid index if the playlist is not in the model.
0107         */
0108         QModelIndex index( Dynamic::DynamicPlaylist* playlist ) const;
0109 
0110         /** Returns a representation of the whole model for debugging */
0111         QString toString();
0112 
0113     Q_SIGNALS:
0114         void activeChanged( int index ); // active row changed
0115 
0116     public Q_SLOTS:
0117         /** Saves all playlists to disk */
0118         void savePlaylists();
0119 
0120         /** Loads the last saved playlists form disk */
0121         void loadPlaylists();
0122 
0123         /** Removes the playlist or bias at the given index. */
0124         void removeAt( const QModelIndex& index );
0125 
0126         /** Clone the playlist or bias at the given index. */
0127         QModelIndex cloneAt( const QModelIndex& index );
0128 
0129         /** Creates a new playlist and returns the index to it. */
0130         QModelIndex newPlaylist();
0131 
0132     private:
0133         // two functions to search for parents
0134         QModelIndex parent( int row, Dynamic::BiasedPlaylist* list, const Dynamic::BiasPtr &bias ) const;
0135         QModelIndex parent( int row, const Dynamic::BiasPtr &parent, const Dynamic::BiasPtr &bias ) const;
0136 
0137         /** Writes the index to the data stream */
0138         void serializeIndex( QDataStream *stream, const QModelIndex& index ) const;
0139 
0140         /** Gets an index from the data stream */
0141         QModelIndex unserializeIndex( QDataStream *stream ) const;
0142 
0143         Dynamic::BiasedPlaylist* cloneList( Dynamic::BiasedPlaylist* list );
0144         Dynamic::BiasPtr cloneBias( Dynamic::BiasPtr bias );
0145 
0146         // -- model change signals ---
0147         // The following functions are called by the biases to
0148         // notify the model about changes.
0149 
0150         void playlistChanged( Dynamic::DynamicPlaylist* playlist );
0151         void biasChanged( const Dynamic::BiasPtr &bias );
0152 
0153         void beginRemoveBias( Dynamic::BiasedPlaylist* parent );
0154         void beginRemoveBias( const Dynamic::BiasPtr &parent, int index );
0155         void endRemoveBias();
0156 
0157         void beginInsertBias( Dynamic::BiasedPlaylist* parent );
0158         void beginInsertBias( const Dynamic::BiasPtr &parent, int index );
0159         void endInsertBias();
0160 
0161         void beginMoveBias( const Dynamic::BiasPtr &parent, int from, int to );
0162         void endMoveBias();
0163 
0164         // ----
0165 
0166         bool savePlaylists( const QString &filename );
0167         bool loadPlaylists( const QString &filename );
0168         void initPlaylists();
0169 
0170         DynamicModel(QObject* parent = nullptr);
0171         static DynamicModel* s_instance;
0172 
0173         int m_activePlaylistIndex;
0174 
0175         /** Contains all the dynamic playlists.  */
0176         QList<Dynamic::DynamicPlaylist*> m_playlists;
0177 
0178         friend class Dynamic::DynamicPlaylist;
0179         friend class Dynamic::BiasedPlaylist;
0180         friend class Dynamic::AndBias;
0181 
0182         friend class ::TestDynamicModel;
0183 };
0184 
0185 }
0186 
0187 #endif
0188