1 /****************************************************************************************
2  * Copyright (c) 2009 Téo Mrnjavac <teo@kde.org>                                        *
3  * Copyright (c) 2010 Nanno Langstraat <langstr@gmail.com>                              *
4  *                                                                                      *
5  * This program is free software; you can redistribute it and/or modify it under        *
6  * the terms of the GNU General Public License as published by the Free Software        *
7  * Foundation; either version 2 of the License, or (at your option) any later           *
8  * version.                                                                             *
9  *                                                                                      *
10  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
12  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
13  *                                                                                      *
14  * You should have received a copy of the GNU General Public License along with         *
15  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
16  ****************************************************************************************/
17 
18 #ifndef AMAROK_PROXYBASE_H
19 #define AMAROK_PROXYBASE_H
20 
21 #include "AbstractModel.h"
22 #include "playlist/PlaylistItem.h"
23 
24 #include <QSortFilterProxyModel>
25 
26 namespace Playlist
27 {
28 
29 /**
30  * A ProxyModel that implements all the common forwarders for the interface of any
31  * playlist proxy.
32  * @author Téo Mrnjavac <teo@kde.org>
33  */
34 class ProxyBase : public QSortFilterProxyModel, public Playlist::AbstractModel
35 {
36     Q_OBJECT
37 public:
38     /**
39      * Constructor.
40      */
41     explicit ProxyBase( AbstractModel *belowModel, QObject *parent = nullptr );
42 
43     /**
44      * Destructor.
45      */
46     ~ProxyBase() override;
47 
48     //! Inherited from Playlist::AbstractModel
qaim()49     QAbstractItemModel* qaim() const override { return const_cast<ProxyBase*>( this ); }
50 
51     quint64 activeId() const override;
52     int activeRow() const override;
53     Meta::TrackPtr activeTrack() const override;
54     QSet<int> allRowsForTrack( const Meta::TrackPtr& track ) const override;
55     void clearSearchTerm() override;
56     bool containsTrack( const Meta::TrackPtr& track ) const override;
57     int currentSearchFields() override;
58     QString currentSearchTerm() override;
59     bool exportPlaylist( const QString &path, bool relative = false ) override;
60     void filterUpdated() override;
61     int find( const QString &searchTerm, int searchFields ) override;
62     int findNext( const QString &searchTerm, int selectedRow, int searchFields ) override;
63     int findPrevious( const QString &searchTerm, int selectedRow, int searchFields ) override;
64     int firstRowForTrack( const Meta::TrackPtr& track ) const override;
65     quint64 idAt( const int row ) const override;
66     bool rowExists( int row ) const override;
67     int rowForId( const quint64 id ) const override;
68     int rowFromBottomModel( const int rowInBase ) override;
69     int rowToBottomModel( const int rowInProxy ) override;
70     void setActiveId( const quint64 id ) override;
71     void setActiveRow( int row ) override;
72     void setAllUnplayed() override;
73     void emitQueueChanged() override;
74     int queuePositionOfRow( int row ) override;
75     void showOnlyMatches( bool onlyMatches ) override;
76     Item::State stateOfId( quint64 id ) const override;
77     Item::State stateOfRow( int row ) const override;
78     qint64 totalLength() const override;
79     quint64 totalSize() const override;
80     Meta::TrackPtr trackAt( int row ) const override;
81     Meta::TrackPtr trackForId( const quint64 id ) const override;
82     Meta::TrackList tracks() override;
83 
84 Q_SIGNALS:
85     //! Proxied from Playlist::Model.
86     void activeTrackChanged( const quint64 );
87     void queueChanged();
88 
89 protected:
90     /**
91      * Check if a certain row in the source model matches a search term when looking at
92      * the fields specified by the searchFields bitmask.
93      * @param sourceModelRow The row number in the source model to match against.
94      * @param searchTerm The search term.
95      * @param searchFields A bitmask containing the fields that should be matched against.
96      * @return True if a match is found in any field, false otherwise.
97      */
98     bool rowMatch( int sourceModelRow, const QString &searchTerm, int searchFields ) const;
99 
100     /**
101      * Converts a row number in the underlying model to a row number in this proxy.
102      * @param sourceModelRow the row number that's valid in 'sourceModel()'.
103      * @return the row number that's valid in this proxy.
104      */
105     virtual int rowFromSource( int sourceModelRow ) const;
106 
107     /**
108      * Converts a row number in this proxy to a row number in the underlying model.
109      *
110      * As a special case, 'proxyModelRow == rowCount()' returns the bottom model's
111      * 'rowCount()'. See comment at 'rowToBottomModel()'.
112      *
113      * @param proxyModelRow the row number that's valid in this proxy.
114      * @return the row number that's valid in 'sourceModel()'.
115      */
116     virtual int rowToSource( int proxyModelRow ) const;
117 
118 
119     AbstractModel *m_belowModel;
120 };
121 
122 }   //namespace Playlist
123 
124 #endif  //AMAROK_PROXYBASE_H
125