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 "ChooseProvidersPage.h"
0018 
0019 #include "App.h"
0020 #include "core/meta/support/MetaConstants.h"
0021 #include "statsyncing/models/ProvidersModel.h"
0022 
0023 #include <QCheckBox>
0024 #include <QPushButton>
0025 
0026 using namespace StatSyncing;
0027 
0028 ChooseProvidersPage::ChooseProvidersPage( QWidget *parent, Qt::WindowFlags f )
0029     : QWidget( parent, f )
0030     , m_providersModel( nullptr )
0031 {
0032     setupUi( this );
0033     QPushButton *configure = buttonBox->addButton( i18n( "Configure Synchronization..." ), QDialogButtonBox::ActionRole );
0034     connect( configure, &QPushButton::clicked, this, &ChooseProvidersPage::openConfiguration );
0035     QPushButton *next = buttonBox->addButton( i18n( "Next" ), QDialogButtonBox::ActionRole );
0036     next->setIcon( QIcon( "go-next" ) );
0037     connect( next, &QPushButton::clicked, buttonBox, &QDialogButtonBox::accepted );
0038     connect( buttonBox, &QDialogButtonBox::accepted, this, &ChooseProvidersPage::accepted );
0039     connect( buttonBox, &QDialogButtonBox::rejected, this, &ChooseProvidersPage::rejected );
0040     progressBar->hide();
0041 }
0042 
0043 ChooseProvidersPage::~ChooseProvidersPage()
0044 {
0045 }
0046 
0047 void
0048 ChooseProvidersPage::setFields( const QList<qint64> &fields, qint64 checkedFields )
0049 {
0050     QLayout *fieldsLayout = fieldsBox->layout();
0051     foreach( qint64 field, fields )
0052     {
0053         QString name = Meta::i18nForField( field );
0054         QCheckBox *checkBox = new QCheckBox( name );
0055         fieldsLayout->addWidget( checkBox );
0056         checkBox->setCheckState( ( field & checkedFields ) ? Qt::Checked : Qt::Unchecked );
0057         checkBox->setProperty( "field", field );
0058         connect( checkBox, &QCheckBox::stateChanged, this, &ChooseProvidersPage::checkedFieldsChanged );
0059     }
0060     fieldsLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding ) );
0061 
0062     connect( this, &ChooseProvidersPage::checkedFieldsChanged, this, &ChooseProvidersPage::updateEnabledFields );
0063     updateEnabledFields();
0064 }
0065 
0066 qint64
0067 ChooseProvidersPage::checkedFields() const
0068 {
0069     qint64 ret = 0;
0070     QLayout *fieldsLayout = fieldsBox->layout();
0071     for( int i = 0; i < fieldsLayout->count(); i++ )
0072     {
0073         QCheckBox *checkBox = qobject_cast<QCheckBox *>( fieldsLayout->itemAt( i )->widget() );
0074         if( !checkBox )
0075             continue;
0076         if( checkBox->isChecked() && checkBox->property( "field" ).canConvert<qint64>() )
0077             ret |= checkBox->property( "field" ).value<qint64>();
0078     }
0079     return ret;
0080 }
0081 
0082 void
0083 ChooseProvidersPage::setProvidersModel( ProvidersModel *model, QItemSelectionModel *selectionModel )
0084 {
0085     m_providersModel = model;
0086     providersView->setModel( model );
0087     providersView->setSelectionModel( selectionModel );
0088 
0089     connect( model, &StatSyncing::ProvidersModel::selectedProvidersChanged,
0090              this, &ChooseProvidersPage::updateMatchedLabel );
0091     connect( model, &StatSyncing::ProvidersModel::selectedProvidersChanged,
0092              this, &ChooseProvidersPage::updateEnabledFields );
0093     updateMatchedLabel();
0094     updateEnabledFields();
0095 }
0096 
0097 void
0098 ChooseProvidersPage::disableControls()
0099 {
0100     // disable checkboxes
0101     QLayout *fieldsLayout = fieldsBox->layout();
0102     for( int i = 0; i < fieldsLayout->count(); i++ )
0103     {
0104         QWidget *widget = fieldsLayout->itemAt( i )->widget();
0105         if( widget )
0106             widget->setEnabled( false );
0107     }
0108 
0109     // disable view
0110     providersView->setEnabled( false );
0111 
0112     // disable all but Cancel button
0113     foreach( QAbstractButton *button, buttonBox->buttons() )
0114     {
0115         if( buttonBox->buttonRole( button ) != QDialogButtonBox::RejectRole )
0116             button->setEnabled( false );
0117     }
0118 }
0119 
0120 void
0121 ChooseProvidersPage::setProgressBarText( const QString &text )
0122 {
0123     progressBar->setFormat( text );
0124     progressBar->show();
0125 }
0126 
0127 void
0128 ChooseProvidersPage::setProgressBarMaximum( int maximum )
0129 {
0130     progressBar->setMaximum( maximum );
0131     progressBar->show();
0132 }
0133 
0134 void
0135 ChooseProvidersPage::progressBarIncrementProgress()
0136 {
0137     progressBar->setValue( progressBar->value() + 1 );
0138     progressBar->show();
0139 }
0140 
0141 void
0142 ChooseProvidersPage::updateMatchedLabel()
0143 {
0144     qint64 fields = m_providersModel->reliableTrackMetadataIntersection();
0145     QString fieldNames = m_providersModel->fieldsToString( fields );
0146     matchLabel->setText( i18n( "Tracks matched by: %1", fieldNames ) );
0147 }
0148 
0149 void
0150 ChooseProvidersPage::updateEnabledFields()
0151 {
0152     if( !m_providersModel )
0153         return;
0154 
0155     qint64 writableFields = m_providersModel->writableTrackStatsDataUnion();
0156     QLayout *fieldsLayout = fieldsBox->layout();
0157     for( int i = 0; i < fieldsLayout->count(); i++ )
0158     {
0159         QWidget *checkBox = fieldsLayout->itemAt( i )->widget();
0160         if( !checkBox || !checkBox->property( "field" ).canConvert<qint64>() )
0161             continue;
0162         qint64 field = checkBox->property( "field" ).value<qint64>();
0163         bool enabled = writableFields & field;
0164         checkBox->setEnabled( enabled );
0165         QString text = i18nc( "%1 is field name such as Rating", "No selected collection "
0166                 "supports writing %1 - it doesn't make sense to synchronize it.",
0167                 Meta::i18nForField( field ) );
0168         checkBox->setToolTip( enabled ? QString() : text );
0169     }
0170 
0171     QAbstractButton *nextButton = nullptr;
0172     foreach( QAbstractButton *button, buttonBox->buttons() )
0173     {
0174         if( buttonBox->buttonRole( button ) == QDialogButtonBox::AcceptRole )
0175             nextButton = button;
0176     }
0177     if( nextButton )
0178         nextButton->setEnabled( writableFields != 0 );
0179 }
0180 
0181 void ChooseProvidersPage::openConfiguration()
0182 {
0183     App *app = pApp;
0184     if( app )
0185         app->slotConfigAmarok( QStringLiteral("MetadataConfig") );
0186 }