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 "ProvidersModel.h" 0018 0019 #include "core/meta/support/MetaConstants.h" 0020 #include "statsyncing/Provider.h" 0021 0022 #include <KLocalizedString> 0023 0024 #include <QItemSelectionModel> 0025 0026 using namespace StatSyncing; 0027 0028 ProvidersModel::ProvidersModel( const ProviderPtrList &providers, 0029 const ProviderPtrSet &preSelectedProviders, QObject *parent ) 0030 : QAbstractListModel( parent ) 0031 , m_providers( providers ) 0032 , m_selectionModel( new QItemSelectionModel( this, this ) ) 0033 { 0034 // TODO: sort providers 0035 0036 // selection defaults to model's tick state 0037 for( int i = 0; i < m_providers.count(); i++ ) 0038 { 0039 if( preSelectedProviders.contains( m_providers.at( i ) ) ) 0040 { 0041 QModelIndex idx = index( i ); 0042 m_selectionModel->select( idx, QItemSelectionModel::Select ); 0043 } 0044 } 0045 connect( m_selectionModel, &QItemSelectionModel::selectionChanged, 0046 this, &ProvidersModel::selectedProvidersChanged ); 0047 } 0048 0049 ProvidersModel::~ProvidersModel() 0050 { 0051 } 0052 0053 QVariant 0054 ProvidersModel::data( const QModelIndex &index, int role ) const 0055 { 0056 if( !index.isValid() || index.column() != 0 || 0057 index.row() < 0 || index.row() >= m_providers.count() ) 0058 { 0059 return QVariant(); 0060 } 0061 ProviderPtr provider = m_providers.at( index.row() ); 0062 switch( role ) 0063 { 0064 case Qt::DisplayRole: 0065 if( provider->description().isEmpty() ) 0066 return provider->prettyName(); 0067 return i18nc( "%1: name, %2: description", "%1 (%2)", provider->prettyName(), 0068 provider->description() ); 0069 case Qt::DecorationRole: 0070 return provider->icon(); 0071 case Qt::ToolTipRole: 0072 return i18n( "Can match tracks by: %1\nCan synchronize: %2", 0073 fieldsToString( provider->reliableTrackMetaData() ), 0074 fieldsToString( provider->writableTrackStatsData() ) ); 0075 } 0076 return QVariant(); 0077 } 0078 0079 int 0080 ProvidersModel::rowCount( const QModelIndex &parent ) const 0081 { 0082 return parent.isValid() ? 0 : m_providers.count(); 0083 } 0084 0085 ProviderPtrList 0086 ProvidersModel::selectedProviders() const 0087 { 0088 ProviderPtrList ret; 0089 // preserve order, so do it the hard way 0090 for( int i = 0; i < rowCount(); i++ ) 0091 { 0092 QModelIndex idx = index( i, 0 ); 0093 if( m_selectionModel->isSelected( idx ) ) 0094 ret << m_providers.at( i ); 0095 } 0096 return ret; 0097 } 0098 0099 qint64 0100 ProvidersModel::reliableTrackMetadataIntersection() const 0101 { 0102 if( selectedProviders().isEmpty() ) 0103 return 0; 0104 QListIterator<ProviderPtr> it( selectedProviders() ); 0105 qint64 fields = it.next()->reliableTrackMetaData(); 0106 while( it.hasNext() ) 0107 fields &= it.next()->reliableTrackMetaData(); 0108 return fields; 0109 } 0110 0111 qint64 0112 ProvidersModel::writableTrackStatsDataUnion() const 0113 { 0114 qint64 fields = 0; 0115 foreach( const ProviderPtr &provider, selectedProviders() ) 0116 { 0117 fields |= provider->writableTrackStatsData(); 0118 } 0119 return fields; 0120 } 0121 0122 QItemSelectionModel * 0123 ProvidersModel::selectionModel() const 0124 { 0125 return m_selectionModel; 0126 } 0127 0128 QString 0129 ProvidersModel::fieldsToString( qint64 fields ) const 0130 { 0131 QStringList fieldNames; 0132 for( qint64 i = 0; i < 64; i++ ) 0133 { 0134 qint64 field = 1LL << i; 0135 if( !( field & fields ) ) 0136 continue; 0137 QString name = Meta::i18nForField( field ); 0138 if( !name.isEmpty() ) 0139 fieldNames << name; 0140 } 0141 return fieldNames.join( i18nc( "comma between list words", ", " ) ); 0142 }