1 /****************************************************************************************
2  * Copyright (c) 2008 Daniel Caleb Jones <danielcjones@gmail.com>                       *
3  * Copyright (c) 2009 Mark Kretschmann <kretschmann@kde.org>                            *
4  * Copyright (c) 2010,2011 Ralf Engels <ralf-engels@gmx.de>                             *
5  *                                                                                      *
6  * This program is free software; you can redistribute it and/or modify it under        *
7  * the terms of the GNU General Public License as published by the Free Software        *
8  * Foundation; either version 2 of the License, or (at your option) version 3 or        *
9  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
10  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
11  * version 3 of the license.                                                            *
12  *                                                                                      *
13  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
15  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
16  *                                                                                      *
17  * You should have received a copy of the GNU General Public License along with         *
18  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
19  ****************************************************************************************/
20 
21 #include "DynamicBiasDialog.h"
22 
23 #include "core/support/Debug.h"
24 #include "dynamic/Bias.h"
25 #include "dynamic/BiasFactory.h"
26 
27 #include <QComboBox>
28 #include <KLocalizedString>
29 
30 #include <QDialogButtonBox>
31 #include <QGridLayout>
32 #include <QHBoxLayout>
33 #include <QLabel>
34 
BiasDialog(const Dynamic::BiasPtr & bias,QWidget * parent)35 PlaylistBrowserNS::BiasDialog::BiasDialog( const Dynamic::BiasPtr &bias, QWidget* parent )
36     : QDialog( parent )
37     , m_mainLayout( 0 )
38     , m_biasLayout( 0 )
39     , m_descriptionLabel( 0 )
40     , m_biasWidget( 0 )
41     , m_origBias( bias )
42     , m_bias( bias->clone() ) // m_bias is a clone
43 {
44     setWindowTitle( i18nc( "Bias dialog window title", "Edit bias" ) );
45     m_mainLayout = new QVBoxLayout( this );
46 
47 
48     // -- the bias selection combo
49     QLabel* selectionLabel = new QLabel( i18nc("Bias selection label in bias view.", "Match Type:" ) );
50     m_biasSelection = new QComboBox();
51     QHBoxLayout *selectionLayout = new QHBoxLayout();
52     selectionLabel->setBuddy( m_biasSelection );
53     selectionLayout->addWidget( selectionLabel );
54     selectionLayout->addWidget( m_biasSelection );
55     selectionLayout->addStretch( 1 );
56     m_mainLayout->addLayout( selectionLayout );
57 
58     // -- bias itself
59     m_descriptionLabel = new QLabel( QLatin1String("") );
60     m_descriptionLabel->setWordWrap( true );
61     m_mainLayout->addWidget( m_descriptionLabel );
62 
63     m_biasLayout = new QVBoxLayout();
64     m_mainLayout->addLayout( m_biasLayout );
65 
66     // -- button box
67     QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this );
68     m_mainLayout->addWidget( buttonBox );
69 
70     factoriesChanged();
71     biasReplaced( Dynamic::BiasPtr(), m_bias );
72 
73     connect( Dynamic::BiasFactory::instance(), &Dynamic::BiasFactory::changed,
74              this, &PlaylistBrowserNS::BiasDialog::factoriesChanged );
75     connect( m_biasSelection, QOverload<int>::of(&QComboBox::activated),
76              this, &PlaylistBrowserNS::BiasDialog::selectionChanged );
77     connect(buttonBox, &QDialogButtonBox::accepted,
78             this, &PlaylistBrowserNS::BiasDialog::accept);
79     connect(buttonBox, &QDialogButtonBox::rejected,
80             this, &PlaylistBrowserNS::BiasDialog::reject);
81 }
82 
accept()83 void PlaylistBrowserNS::BiasDialog::accept()
84 {
85     // use the newly edited bias
86     m_origBias->replace( m_bias ); // tell the old bias it has just been replaced
87     QDialog::accept();
88 }
89 
reject()90 void PlaylistBrowserNS::BiasDialog::reject()
91 {
92     // do nothing.
93     QDialog::reject();
94 }
95 
~BiasDialog()96 PlaylistBrowserNS::BiasDialog::~BiasDialog()
97 { }
98 
99 void
factoriesChanged()100 PlaylistBrowserNS::BiasDialog::factoriesChanged()
101 {
102     m_biasSelection->clear();
103 
104     disconnect( Dynamic::BiasFactory::instance(), &Dynamic::BiasFactory::changed,
105                 this, &PlaylistBrowserNS::BiasDialog::factoriesChanged );
106 
107     // -- add all the bias types to the list
108     bool factoryFound = false;
109     QList<Dynamic::AbstractBiasFactory*> factories = Dynamic::BiasFactory::factories();
110     for( int i = 0; i <  factories.count(); i++ )
111     {
112         Dynamic::AbstractBiasFactory* factory = factories.at( i );
113         m_biasSelection->addItem( factory->i18nName(), QVariant( factory->name() ) );
114 
115         // -- set the current index if we have found our own factory
116         if( m_bias && factory->name() == m_bias->name() )
117         {
118             factoryFound = true;
119             m_biasSelection->setCurrentIndex( i );
120             m_descriptionLabel->setText( factory->i18nDescription() );
121         }
122     }
123 
124     // -- In cases of replacement bias
125     if( !factoryFound )
126     {
127         m_biasSelection->addItem( m_bias->name() );
128         m_biasSelection->setCurrentIndex( m_biasSelection->count() - 1 );
129         m_descriptionLabel->setText( i18n( "This bias is a replacement for another bias\n"
130                                          "which is currently not loaded or deactivated.\n"
131                                          "The original bias name was %1.", m_bias->name() ) );
132     }
133 
134     connect( Dynamic::BiasFactory::instance(), &Dynamic::BiasFactory::changed,
135              this, &PlaylistBrowserNS::BiasDialog::factoriesChanged );
136 }
137 
138 void
selectionChanged(int index)139 PlaylistBrowserNS::BiasDialog::selectionChanged( int index )
140 {
141     DEBUG_BLOCK;
142     Q_ASSERT( m_biasSelection );
143 
144     QString biasName = m_biasSelection->itemData( index ).toString();
145 
146     Dynamic::BiasPtr oldBias( m_bias );
147     Dynamic::BiasPtr newBias( Dynamic::BiasFactory::fromName( biasName ) );
148     if( !newBias )
149     {
150         warning() << "Could not create bias with name:"<<biasName;
151         return;
152     }
153 
154     debug() << "replace bias" << oldBias->toString() << "with" << newBias->toString();
155     m_bias->replace( newBias ); // tell the old bias it has just been replaced
156     debug() << "replaced";
157 
158     // -- if the new bias is AndBias, try to add the old biase(s) into it
159     Dynamic::AndBias *oldABias = qobject_cast<Dynamic::AndBias*>(oldBias.data());
160     Dynamic::AndBias *newABias = qobject_cast<Dynamic::AndBias*>(newBias.data());
161     if( newABias ) {
162         if( oldABias ) {
163             for( int i = 0; i < oldABias->biases().count(); i++ )
164             {
165                 newABias->appendBias( oldABias->biases()[i] );
166             }
167         }
168         else
169         {
170             newABias->appendBias( oldBias );
171         }
172     }
173 }
174 
175 void
biasReplaced(const Dynamic::BiasPtr & oldBias,Dynamic::BiasPtr newBias)176 PlaylistBrowserNS::BiasDialog::biasReplaced( const Dynamic::BiasPtr &oldBias, Dynamic::BiasPtr newBias )
177 {
178     Q_UNUSED( oldBias );
179 
180     if( m_biasWidget )
181     {
182         m_biasLayout->removeWidget( m_biasWidget );
183         m_biasWidget->deleteLater();
184         m_biasWidget = nullptr;
185     }
186 
187     m_bias = newBias;
188     if( !newBias )
189         return;
190 
191     connect( newBias.data(), &Dynamic::AbstractBias::replaced,
192              this, &PlaylistBrowserNS::BiasDialog::biasReplaced );
193 
194     m_biasWidget = newBias->widget( nullptr );
195     if( !m_biasWidget )
196         m_biasWidget = new QLabel( i18n("This bias has no settings.") );
197     m_biasLayout->addWidget( m_biasWidget );
198 
199     factoriesChanged(); // update the bias description and select the new combo entry
200 }
201 
202 
203 
204