File indexing completed on 2024-05-19 04:48:39

0001 /****************************************************************************************
0002  * Copyright (c) 2008-2012 Soren Harward <stharward@gmail.com>                          *
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 "APGCategory.h"
0018 
0019 #include "amarokconfig.h"
0020 #include "playlistgenerator/ConstraintSolver.h"
0021 #include "playlistgenerator/PresetModel.h"
0022 #include "widgets/PrettyTreeView.h"
0023 
0024 #include <QIcon>
0025 #include <KLocalizedString>
0026 #include <QStandardPaths>
0027 
0028 #include <QAction>
0029 #include <QHBoxLayout>
0030 #include <QLabel>
0031 #include <QModelIndex>
0032 #include <QToolBar>
0033 
0034 PlaylistBrowserNS::APGCategory::APGCategory( QWidget* )
0035     : BrowserCategory ( QStringLiteral("APG"), nullptr )
0036 {
0037     m_qualityFactor = AmarokConfig::qualityFactorAPG();
0038 
0039     setPrettyName( i18n( "Automated Playlist Generator" ) );
0040     setShortDescription( i18n("Create playlists by specifying criteria") );
0041     setIcon( QIcon::fromTheme( QStringLiteral("playlist-generator") ) );
0042 
0043     // set background
0044     if( AmarokConfig::showBrowserBackgroundImage() )
0045         setBackgroundImage( imagePath() );
0046 
0047     setLongDescription( i18n("Create playlists by specifying criteria") );
0048 
0049     setContentsMargins( 0, 0, 0, 0 );
0050 
0051     APG::PresetModel* presetmodel = APG::PresetModel::instance();
0052     connect( presetmodel, &APG::PresetModel::lock, this, &APGCategory::setDisabled );
0053 
0054     /* Create the toolbar -- Qt's Designer doesn't let us put a toolbar
0055      * anywhere except in a MainWindow, so we've got to create it by hand here. */
0056     QToolBar* toolBar_Actions = new QToolBar( this );
0057     toolBar_Actions->setMovable( false );
0058     toolBar_Actions->setFloatable( false );
0059     toolBar_Actions->setIconSize( QSize( 22, 22 ) );
0060     toolBar_Actions->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
0061 
0062     QAction* a;
0063     a = toolBar_Actions->addAction( QIcon::fromTheme( QStringLiteral("list-add-amarok") ), i18n("Add new preset") );
0064     connect( a, &QAction::triggered, presetmodel, &APG::PresetModel::addNew );
0065 
0066     a = toolBar_Actions->addAction( QIcon::fromTheme( QStringLiteral("document-properties-amarok") ), i18n("Edit selected preset") );
0067     a->setEnabled( false );
0068     connect( a, &QAction::triggered, presetmodel, &APG::PresetModel::edit );
0069     connect( this, &APGCategory::validIndexSelected, a, &QAction::setEnabled );
0070 
0071     a = toolBar_Actions->addAction( QIcon::fromTheme( QStringLiteral("list-remove-amarok") ), i18n("Delete selected preset") );
0072     a->setEnabled( false );
0073     connect( a, &QAction::triggered, presetmodel, &APG::PresetModel::removeActive );
0074     connect( this, &APGCategory::validIndexSelected, a, &QAction::setEnabled );
0075 
0076     a = toolBar_Actions->addAction( QIcon::fromTheme( QStringLiteral("document-import-amarok") ), i18n("Import a new preset") );
0077     a->setEnabled( true );
0078     connect( a, &QAction::triggered, presetmodel, &APG::PresetModel::import );
0079 
0080     a = toolBar_Actions->addAction( QIcon::fromTheme( QStringLiteral("document-export-amarok") ), i18n("Export the selected preset") );
0081     a->setEnabled( false );
0082     connect( a, &QAction::triggered, presetmodel, &APG::PresetModel::exportActive );
0083     connect( this, &APGCategory::validIndexSelected, a, &QAction::setEnabled );
0084 
0085     toolBar_Actions->addSeparator();
0086 
0087     a = toolBar_Actions->addAction( QIcon::fromTheme( QStringLiteral("go-next-amarok") ), i18n("Run APG with selected preset") );
0088     a->setEnabled( false );
0089     connect( a, &QAction::triggered, this, &APGCategory::runGenerator );
0090     connect( this, &APGCategory::validIndexSelected, a, &QAction::setEnabled );
0091 
0092     /* Create the preset list view */
0093     QLabel* label_Title = new QLabel( i18n("APG Presets"), this );
0094     label_Title->setAlignment( Qt::AlignCenter );
0095 
0096     Amarok::PrettyTreeView* listView = new Amarok::PrettyTreeView( this );
0097     listView->setHeaderHidden( true );
0098     listView->setRootIsDecorated( false );
0099     listView->setModel( presetmodel );
0100     listView->setSelectionMode( QAbstractItemView::SingleSelection );
0101     listView->setFrameShape( QFrame::NoFrame );
0102     listView->setAutoFillBackground( false );
0103     connect( listView->selectionModel(), &QItemSelectionModel::currentChanged, this, &APGCategory::activeChanged );
0104     connect( listView, &Amarok::PrettyTreeView::doubleClicked, presetmodel, &APG::PresetModel::editPreset );
0105 
0106     // Speed/Quality tradeoff slider
0107     QLabel* label_Tradeoff = new QLabel( i18n("Generator Optimization"), this );
0108     label_Tradeoff->setAlignment( Qt::AlignCenter );
0109 
0110     QFrame* qual_Frame = new QFrame( this );
0111     QLabel* label_Speed = new QLabel( i18n("Speed"), qual_Frame );
0112     QSlider* qual_Slider = new QSlider( Qt::Horizontal, qual_Frame );
0113     qual_Slider->setRange( 0, APG::ConstraintSolver::QUALITY_RANGE );
0114     qual_Slider->setValue( m_qualityFactor );
0115     connect( qual_Slider, &QSlider::sliderMoved, this, &APGCategory::setQualityFactor );
0116     QLabel* label_Quality = new QLabel( i18n("Accuracy"), qual_Frame );
0117 
0118     QLayout* qf_Layout = new QHBoxLayout( qual_Frame );
0119     qf_Layout->addWidget( label_Speed );
0120     qf_Layout->addWidget( qual_Slider );
0121     qf_Layout->addWidget( label_Quality );
0122     qual_Frame->setLayout( qf_Layout );
0123 
0124     QMetaObject::connectSlotsByName( this );
0125 }
0126 
0127 PlaylistBrowserNS::APGCategory::~APGCategory()
0128 {
0129     APG::PresetModel::destroy();
0130     AmarokConfig::setQualityFactorAPG( m_qualityFactor );
0131     AmarokConfig::self()->save();
0132 }
0133 
0134 void
0135 PlaylistBrowserNS::APGCategory::activeChanged( const QModelIndex& index )
0136 {
0137     APG::PresetModel::instance()->setActivePreset( index );
0138     Q_EMIT validIndexSelected( index.isValid() );
0139 }
0140 
0141 void
0142 PlaylistBrowserNS::APGCategory::setQualityFactor( int f )
0143 {
0144     m_qualityFactor = f;
0145 }
0146 
0147 void
0148 PlaylistBrowserNS::APGCategory::runGenerator()
0149 {
0150     APG::PresetModel::instance()->savePresetsToXmlDefault();
0151     APG::PresetModel::instance()->runGenerator( m_qualityFactor );
0152 }