File indexing completed on 2024-05-19 04:48:39
0001 /**************************************************************************************** 0002 * Copyright (c) 2008 Daniel Caleb Jones <danielcjones@gmail.com> * 0003 * Copyright (c) 2009 Mark Kretschmann <kretschmann@kde.org> * 0004 * Copyright (c) 2010,2011 Ralf Engels <ralf-engels@gmx.de> * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify it under * 0007 * the terms of the GNU General Public License as published by the Free Software * 0008 * Foundation; either version 2 of the License, or (at your option) version 3 or * 0009 * any later version accepted by the membership of KDE e.V. (or its successor approved * 0010 * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of * 0011 * version 3 of the license. * 0012 * * 0013 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0014 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0015 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU General Public License along with * 0018 * this program. If not, see <http://www.gnu.org/licenses/>. * 0019 ****************************************************************************************/ 0020 0021 #include "DynamicBiasDialog.h" 0022 0023 #include "core/support/Debug.h" 0024 #include "dynamic/Bias.h" 0025 #include "dynamic/BiasFactory.h" 0026 0027 #include <QComboBox> 0028 #include <KLocalizedString> 0029 0030 #include <QDialogButtonBox> 0031 #include <QGridLayout> 0032 #include <QHBoxLayout> 0033 #include <QLabel> 0034 0035 PlaylistBrowserNS::BiasDialog::BiasDialog( const Dynamic::BiasPtr &bias, QWidget* parent ) 0036 : QDialog( parent ) 0037 , m_mainLayout( nullptr ) 0038 , m_biasLayout( nullptr ) 0039 , m_descriptionLabel( nullptr ) 0040 , m_biasWidget( nullptr ) 0041 , m_origBias( bias ) 0042 , m_bias( bias->clone() ) // m_bias is a clone 0043 { 0044 setWindowTitle( i18nc( "Bias dialog window title", "Edit bias" ) ); 0045 m_mainLayout = new QVBoxLayout( this ); 0046 0047 0048 // -- the bias selection combo 0049 QLabel* selectionLabel = new QLabel( i18nc("Bias selection label in bias view.", "Match Type:" ) ); 0050 m_biasSelection = new QComboBox(); 0051 QHBoxLayout *selectionLayout = new QHBoxLayout(); 0052 selectionLabel->setBuddy( m_biasSelection ); 0053 selectionLayout->addWidget( selectionLabel ); 0054 selectionLayout->addWidget( m_biasSelection ); 0055 selectionLayout->addStretch( 1 ); 0056 m_mainLayout->addLayout( selectionLayout ); 0057 0058 // -- bias itself 0059 m_descriptionLabel = new QLabel( QLatin1String("") ); 0060 m_descriptionLabel->setWordWrap( true ); 0061 m_mainLayout->addWidget( m_descriptionLabel ); 0062 0063 m_biasLayout = new QVBoxLayout(); 0064 m_mainLayout->addLayout( m_biasLayout ); 0065 0066 // -- button box 0067 QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this ); 0068 m_mainLayout->addWidget( buttonBox ); 0069 0070 factoriesChanged(); 0071 biasReplaced( Dynamic::BiasPtr(), m_bias ); 0072 0073 connect( Dynamic::BiasFactory::instance(), &Dynamic::BiasFactory::changed, 0074 this, &PlaylistBrowserNS::BiasDialog::factoriesChanged ); 0075 connect( m_biasSelection, QOverload<int>::of(&QComboBox::activated), 0076 this, &PlaylistBrowserNS::BiasDialog::selectionChanged ); 0077 connect(buttonBox, &QDialogButtonBox::accepted, 0078 this, &PlaylistBrowserNS::BiasDialog::accept); 0079 connect(buttonBox, &QDialogButtonBox::rejected, 0080 this, &PlaylistBrowserNS::BiasDialog::reject); 0081 } 0082 0083 void PlaylistBrowserNS::BiasDialog::accept() 0084 { 0085 // use the newly edited bias 0086 m_origBias->replace( m_bias ); // tell the old bias it has just been replaced 0087 QDialog::accept(); 0088 } 0089 0090 void PlaylistBrowserNS::BiasDialog::reject() 0091 { 0092 // do nothing. 0093 QDialog::reject(); 0094 } 0095 0096 PlaylistBrowserNS::BiasDialog::~BiasDialog() 0097 { } 0098 0099 void 0100 PlaylistBrowserNS::BiasDialog::factoriesChanged() 0101 { 0102 m_biasSelection->clear(); 0103 0104 disconnect( Dynamic::BiasFactory::instance(), &Dynamic::BiasFactory::changed, 0105 this, &PlaylistBrowserNS::BiasDialog::factoriesChanged ); 0106 0107 // -- add all the bias types to the list 0108 bool factoryFound = false; 0109 QList<Dynamic::AbstractBiasFactory*> factories = Dynamic::BiasFactory::factories(); 0110 for( int i = 0; i < factories.count(); i++ ) 0111 { 0112 Dynamic::AbstractBiasFactory* factory = factories.at( i ); 0113 m_biasSelection->addItem( factory->i18nName(), QVariant( factory->name() ) ); 0114 0115 // -- set the current index if we have found our own factory 0116 if( m_bias && factory->name() == m_bias->name() ) 0117 { 0118 factoryFound = true; 0119 m_biasSelection->setCurrentIndex( i ); 0120 m_descriptionLabel->setText( factory->i18nDescription() ); 0121 } 0122 } 0123 0124 // -- In cases of replacement bias 0125 if( !factoryFound ) 0126 { 0127 m_biasSelection->addItem( m_bias->name() ); 0128 m_biasSelection->setCurrentIndex( m_biasSelection->count() - 1 ); 0129 m_descriptionLabel->setText( i18n( "This bias is a replacement for another bias\n" 0130 "which is currently not loaded or deactivated.\n" 0131 "The original bias name was %1.", m_bias->name() ) ); 0132 } 0133 0134 connect( Dynamic::BiasFactory::instance(), &Dynamic::BiasFactory::changed, 0135 this, &PlaylistBrowserNS::BiasDialog::factoriesChanged ); 0136 } 0137 0138 void 0139 PlaylistBrowserNS::BiasDialog::selectionChanged( int index ) 0140 { 0141 DEBUG_BLOCK; 0142 Q_ASSERT( m_biasSelection ); 0143 0144 QString biasName = m_biasSelection->itemData( index ).toString(); 0145 0146 Dynamic::BiasPtr oldBias( m_bias ); 0147 Dynamic::BiasPtr newBias( Dynamic::BiasFactory::fromName( biasName ) ); 0148 if( !newBias ) 0149 { 0150 warning() << "Could not create bias with name:"<<biasName; 0151 return; 0152 } 0153 0154 debug() << "replace bias" << oldBias->toString() << "with" << newBias->toString(); 0155 m_bias->replace( newBias ); // tell the old bias it has just been replaced 0156 debug() << "replaced"; 0157 0158 // -- if the new bias is AndBias, try to add the old biase(s) into it 0159 Dynamic::AndBias *oldABias = qobject_cast<Dynamic::AndBias*>(oldBias.data()); 0160 Dynamic::AndBias *newABias = qobject_cast<Dynamic::AndBias*>(newBias.data()); 0161 if( newABias ) { 0162 if( oldABias ) { 0163 for( int i = 0; i < oldABias->biases().count(); i++ ) 0164 { 0165 newABias->appendBias( oldABias->biases()[i] ); 0166 } 0167 } 0168 else 0169 { 0170 newABias->appendBias( oldBias ); 0171 } 0172 } 0173 } 0174 0175 void 0176 PlaylistBrowserNS::BiasDialog::biasReplaced( const Dynamic::BiasPtr &oldBias, Dynamic::BiasPtr newBias ) 0177 { 0178 Q_UNUSED( oldBias ); 0179 0180 if( m_biasWidget ) 0181 { 0182 m_biasLayout->removeWidget( m_biasWidget ); 0183 m_biasWidget->deleteLater(); 0184 m_biasWidget = nullptr; 0185 } 0186 0187 m_bias = newBias; 0188 if( !newBias ) 0189 return; 0190 0191 connect( newBias.data(), &Dynamic::AbstractBias::replaced, 0192 this, &PlaylistBrowserNS::BiasDialog::biasReplaced ); 0193 0194 m_biasWidget = newBias->widget( nullptr ); 0195 if( !m_biasWidget ) 0196 m_biasWidget = new QLabel( i18n("This bias has no settings.") ); 0197 m_biasLayout->addWidget( m_biasWidget ); 0198 0199 factoriesChanged(); // update the bias description and select the new combo entry 0200 } 0201 0202 0203