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 #include "SingleTracksModel.h"
0018 
0019 #include "AmarokMimeData.h"
0020 #include "MetaValues.h"
0021 #include "core/meta/support/MetaConstants.h"
0022 
0023 using namespace StatSyncing;
0024 
0025 SingleTracksModel::SingleTracksModel( const TrackList &tracks,
0026                                       const QList<qint64> &columns, const Options &options,
0027                                       QObject *parent )
0028     : QAbstractTableModel( parent )
0029     , CommonModel( columns, options )
0030     , m_tracks( tracks )
0031 {
0032 }
0033 
0034 int
0035 SingleTracksModel::rowCount( const QModelIndex &parent ) const
0036 {
0037     return parent.isValid() ? 0 : m_tracks.count();
0038 }
0039 
0040 int
0041 SingleTracksModel::columnCount( const QModelIndex &parent ) const
0042 {
0043     return parent.isValid() ? 0 : m_columns.count();
0044 }
0045 
0046 QVariant
0047 SingleTracksModel::headerData( int section, Qt::Orientation orientation, int role ) const
0048 {
0049     return CommonModel::headerData( section, orientation, role );
0050 }
0051 
0052 QVariant
0053 SingleTracksModel::data( const QModelIndex &index, int role ) const
0054 {
0055     if( !index.isValid() ||
0056         index.row() < 0 || index.row() >= m_tracks.count() ||
0057         index.column() < 0 || index.column() >= m_columns.count() )
0058     {
0059         return QVariant();
0060     }
0061 
0062     qint64 field = m_columns.at( index.column() );
0063     const TrackPtr &track = m_tracks.at( index.row() );
0064     return trackData( track, field, role );
0065 }
0066 
0067 Qt::ItemFlags
0068 SingleTracksModel::flags( const QModelIndex &index ) const
0069 {
0070     return QAbstractItemModel::flags( index ) | Qt::ItemIsDragEnabled;
0071 }
0072 
0073 QStringList
0074 SingleTracksModel::mimeTypes() const
0075 {
0076     return QStringList() << AmarokMimeData::TRACK_MIME << QStringLiteral("text/uri-list") << QStringLiteral("text/plain");
0077 }
0078 
0079 QMimeData *
0080 SingleTracksModel::mimeData( const QModelIndexList &indexes ) const
0081 {
0082     Meta::TrackList tracks;
0083     foreach( const QModelIndex &idx, indexes )
0084     {
0085         if( idx.isValid() && idx.row() >= 0 && idx.row() < m_tracks.count() &&
0086             idx.column() == 0 )
0087         {
0088             Meta::TrackPtr metaTrack = m_tracks.at( idx.row() )->metaTrack();
0089             if( metaTrack )
0090                 tracks << metaTrack;
0091         }
0092     }
0093     if( tracks.isEmpty() )
0094         return nullptr;
0095 
0096     AmarokMimeData *mime = new AmarokMimeData();
0097     mime->setTracks( tracks );
0098     return mime;
0099 }