File indexing completed on 2024-05-19 04:50:29

0001 /****************************************************************************************
0002  * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz>                                      *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef STATSYNCING_MATCHEDTRACKSMODEL_H
0018 #define STATSYNCING_MATCHEDTRACKSMODEL_H
0019 
0020 #include "statsyncing/Provider.h"
0021 #include "statsyncing/models/CommonModel.h"
0022 
0023 #include <QAbstractItemModel>
0024 
0025 namespace StatSyncing
0026 {
0027     class TrackTuple;
0028 
0029     /**
0030      * Model that provides data about matched tracks that should participate in statistics
0031      * synchronization.
0032      */
0033     class MatchedTracksModel : public QAbstractItemModel, protected CommonModel
0034     {
0035         Q_OBJECT
0036 
0037         public:
0038             enum {
0039                 TupleFlagsRole = CommonModel::UserRole,
0040             };
0041 
0042             /**
0043              * Flags for track tuple status
0044              */
0045             enum TupleFlag {
0046                 HasConflict = 1 << 0, /// there is at least one potential rating conflict
0047                 HasUpdate = 1 << 1, /// there is at least one field going to be updated
0048             };
0049 
0050             /**
0051              * Construct model of matched tracks.
0052              *
0053              * @param matchedTuples list of matched track tuples
0054              * @param columns list of Meta::val* fields that will form columns of the model
0055              *                must include Meta::valTitle, may include: valRating,
0056              *                valFirstPlayed, valLastPlayed, valPlaycount, valLabel.
0057              * @param options options for synchronizing individual tracks
0058              * @param parent parent QObject
0059              */
0060             MatchedTracksModel( const QList<TrackTuple> &matchedTuples,
0061                                 const QList<qint64> &columns, const Options &options,
0062                                 QObject *parent = nullptr );
0063 
0064             QModelIndex index( int row, int column,
0065                                const QModelIndex &parent = QModelIndex() ) const override;
0066             QModelIndex parent( const QModelIndex &child ) const override;
0067 
0068             bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
0069             int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
0070             int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
0071 
0072             QVariant headerData( int section, Qt::Orientation orientation,
0073                                  int role = Qt::DisplayRole ) const override;
0074             QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
0075             bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
0076             Qt::ItemFlags flags( const QModelIndex &index ) const override;
0077 
0078             // MatchedTracksModel-specific methods:
0079             /**
0080              * Return a list of matched tuples, the same passed to model constructor, but
0081              * some may be changed, e.g. conflict-resolved etc.
0082              */
0083             const QList<TrackTuple> &matchedTuples();
0084 
0085             /**
0086              * Return true if at least one of the tuple is going to be updated. Warning:
0087              * can depend on whether a conflict of one tuple is resolved or not.
0088              */
0089             bool hasUpdate() const;
0090 
0091             /**
0092              * Return true if given or at least one tuple has potential conflict.
0093              * @param i if >= 0, queries tuple at position i; otherwise match any tuple
0094              */
0095             bool hasConflict( int i = -1 ) const;
0096 
0097             /**
0098              * Go through all tuples with (both resolved and unresolved) rating conflict
0099              * and (re)set their preferred rating provider to @p provider. Null
0100              * @param provider resets all tuples to "undecided". If @p provider is
0101              * not null and given tuple has no track from provider, its state remains
0102              * unchanged.
0103              */
0104             void takeRatingsFrom( const ProviderPtr &provider );
0105 
0106             /**
0107              * Go through all tuples with (both resolved and unresolved) labels conflict
0108              * and add @param provider to list of their label sources. Tracks that don't
0109              * have @param provider in their providers remain unchanged.
0110              */
0111             void includeLabelsFrom( const ProviderPtr &provider );
0112 
0113             /**
0114              * Go through all tuples with (both resolved and unresolved) labels conflict
0115              * and remove @param provider from their list of label sources. Tracks that
0116              * don't have @param provider in their label sources remain unchanged.
0117              *
0118              * If @param provider is null, this methods resets all tuples to "undecided"
0119              * wrt labels (clears their list of label sources).
0120              */
0121             void excludeLabelsFrom( const ProviderPtr &provider );
0122 
0123         private:
0124             QVariant tupleData( const TrackTuple &tuple, qint64 field, int role ) const;
0125             QVariant trackData( ProviderPtr provider, const TrackTuple &tuple,
0126                                 qint64 field, int role ) const;
0127             using CommonModel::trackData;
0128 
0129             QList<TrackTuple> m_matchedTuples;
0130             int m_titleColumn;
0131     };
0132 
0133 } // namespace StatSyncing
0134 
0135 #endif // STATSYNCING_MATCHEDTRACKSMODEL_H