2010-10-25 01:46:05 +02:00
/* This file is part of Clementine.
2010-11-20 14:27:10 +01:00
Copyright 2010 , David Sansome < me @ davidsansome . com >
2010-10-25 01:46:05 +02:00
Clementine is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
Clementine is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with Clementine . If not , see < http : //www.gnu.org/licenses/>.
*/
2010-11-18 22:13:43 +01:00
# include "querywizardplugin.h"
2010-11-18 21:19:33 +01:00
# include "wizard.h"
2010-11-18 22:13:43 +01:00
# include "wizardplugin.h"
2010-11-19 00:08:37 +01:00
# include "ui_wizardfinishpage.h"
2010-10-25 01:46:05 +02:00
2010-11-18 22:13:43 +01:00
# include <QLabel>
# include <QRadioButton>
# include <QSignalMapper>
# include <QVBoxLayout>
2010-10-29 20:41:49 +02:00
2010-11-18 22:13:43 +01:00
namespace smart_playlists {
2010-10-29 20:41:49 +02:00
2010-11-19 00:08:37 +01:00
class Wizard : : TypePage : public QWizardPage {
public :
TypePage ( QWidget * parent )
: QWizardPage ( parent ) , next_id_ ( - 1 ) { }
int nextId ( ) const { return next_id_ ; }
int next_id_ ;
} ;
class Wizard : : FinishPage : public QWizardPage {
public :
FinishPage ( QWidget * parent )
: QWizardPage ( parent ) ,
ui_ ( new Ui_SmartPlaylistWizardFinishPage ) {
ui_ - > setupUi ( this ) ;
connect ( ui_ - > name , SIGNAL ( textChanged ( QString ) ) , SIGNAL ( completeChanged ( ) ) ) ;
}
~ FinishPage ( ) {
delete ui_ ;
}
int nextId ( ) const { return - 1 ; }
bool isComplete ( ) const { return ! ui_ - > name - > text ( ) . isEmpty ( ) ; }
Ui_SmartPlaylistWizardFinishPage * ui_ ;
} ;
2010-11-18 21:19:33 +01:00
Wizard : : Wizard ( LibraryBackend * library , QWidget * parent )
2010-10-25 01:46:05 +02:00
: QWizard ( parent ) ,
2010-11-18 22:13:43 +01:00
library_ ( library ) ,
type_page_ ( new TypePage ( this ) ) ,
2010-11-19 00:08:37 +01:00
finish_page_ ( new FinishPage ( this ) ) ,
type_index_ ( - 1 ) ,
2010-11-18 22:13:43 +01:00
type_mapper_ ( new QSignalMapper ( this ) )
2010-10-25 01:46:05 +02:00
{
2010-11-18 22:13:43 +01:00
setWindowIcon ( QIcon ( " :/icon.png " ) ) ;
setWindowTitle ( tr ( " Smart playlist " ) ) ;
resize ( 687 , 628 ) ;
2010-11-01 21:24:44 +01:00
2010-11-19 00:08:37 +01:00
// Type page
2010-11-18 22:13:43 +01:00
type_page_ - > setTitle ( tr ( " Playlist type " ) ) ;
type_page_ - > setSubTitle ( tr ( " A smart playlist is a dynamic list of songs that come from your library. There are different types of smart playlist that offer different ways of selecting songs. " ) ) ;
2010-11-19 00:08:37 +01:00
type_page_ - > setStyleSheet (
" QRadioButton { font-weight: bold; } "
" QLabel { margin-bottom: 1em; margin-left: 24px; } " ) ;
2010-11-18 22:13:43 +01:00
addPage ( type_page_ ) ;
2010-10-29 20:41:49 +02:00
2010-11-19 00:08:37 +01:00
// Finish page
finish_page_ - > setTitle ( tr ( " Finish " ) ) ;
finish_page_ - > setSubTitle ( tr ( " Choose a name for your smart playlist " ) ) ;
finish_id_ = addPage ( finish_page_ ) ;
2010-11-18 22:13:43 +01:00
connect ( type_mapper_ , SIGNAL ( mapped ( int ) ) , SLOT ( TypeChanged ( int ) ) ) ;
2010-11-01 21:24:44 +01:00
2010-11-18 22:13:43 +01:00
new QVBoxLayout ( type_page_ ) ;
AddPlugin ( new QueryWizardPlugin ( library_ , this ) ) ;
2010-10-29 20:41:49 +02:00
}
2010-11-18 22:13:43 +01:00
Wizard : : ~ Wizard ( ) {
qDeleteAll ( plugins_ ) ;
2010-11-01 21:24:44 +01:00
}
2010-11-19 00:08:37 +01:00
void Wizard : : SetGenerator ( GeneratorPtr gen ) {
// Find the right type and jump to the start page
for ( int i = 0 ; i < plugins_ . count ( ) ; + + i ) {
if ( plugins_ [ i ] - > type ( ) = = gen - > type ( ) ) {
TypeChanged ( i ) ;
next ( ) ;
break ;
}
}
// Set the name
finish_page_ - > ui_ - > name - > setText ( gen - > name ( ) ) ;
2010-11-20 19:49:54 +01:00
finish_page_ - > ui_ - > dynamic - > setChecked ( gen - > is_dynamic ( ) ) ;
2010-11-19 00:08:37 +01:00
// Tell the plugin to load
plugins_ [ type_index_ ] - > SetGenerator ( gen ) ;
}
2010-11-18 22:13:43 +01:00
void Wizard : : AddPlugin ( WizardPlugin * plugin ) {
const int index = plugins_ . count ( ) ;
plugins_ < < plugin ;
2010-11-19 00:08:37 +01:00
plugin - > Init ( this , finish_id_ ) ;
2010-10-29 20:41:49 +02:00
2010-11-18 22:13:43 +01:00
// Create the radio button
2010-11-19 00:08:37 +01:00
QRadioButton * radio_button = new QRadioButton ( plugin - > name ( ) , type_page_ ) ;
2010-11-18 22:13:43 +01:00
QLabel * description = new QLabel ( plugin - > description ( ) , type_page_ ) ;
2010-11-19 00:08:37 +01:00
type_page_ - > layout ( ) - > addWidget ( radio_button ) ;
2010-11-18 22:13:43 +01:00
type_page_ - > layout ( ) - > addWidget ( description ) ;
2010-11-03 21:58:33 +01:00
2010-11-19 00:08:37 +01:00
type_mapper_ - > setMapping ( radio_button , index ) ;
connect ( radio_button , SIGNAL ( clicked ( ) ) , type_mapper_ , SLOT ( map ( ) ) ) ;
2010-10-29 20:41:49 +02:00
2010-11-18 22:13:43 +01:00
if ( index = = 0 ) {
2010-11-19 00:08:37 +01:00
radio_button - > setChecked ( true ) ;
2010-11-18 22:13:43 +01:00
TypeChanged ( 0 ) ;
2010-11-01 21:24:44 +01:00
}
2010-10-29 20:41:49 +02:00
}
2010-11-03 21:58:33 +01:00
2010-11-18 22:13:43 +01:00
void Wizard : : TypeChanged ( int index ) {
2010-11-19 00:08:37 +01:00
type_index_ = index ;
type_page_ - > next_id_ = plugins_ [ type_index_ ] - > start_page ( ) ;
}
GeneratorPtr Wizard : : CreateGenerator ( ) const {
GeneratorPtr ret ;
if ( type_index_ = = - 1 )
return ret ;
ret = plugins_ [ type_index_ ] - > CreateGenerator ( ) ;
if ( ! ret )
return ret ;
ret - > set_name ( finish_page_ - > ui_ - > name - > text ( ) ) ;
2010-11-20 19:49:54 +01:00
ret - > set_dynamic ( finish_page_ - > ui_ - > dynamic - > isChecked ( ) ) ;
2010-11-19 00:08:37 +01:00
return ret ;
2010-11-03 21:58:33 +01:00
}
2010-11-18 21:19:33 +01:00
2010-11-20 19:49:54 +01:00
void Wizard : : initializePage ( int id ) {
if ( id = = finish_id_ ) {
finish_page_ - > ui_ - > dynamic_container - > setEnabled (
plugins_ [ type_index_ ] - > is_dynamic ( ) ) ;
}
QWizard : : initializePage ( id ) ;
}
2010-11-18 21:19:33 +01:00
} // namespace