File indexing completed on 2024-05-19 04:49:49

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  * Copyright (c) 2009 Téo Mrnjavac <teo@kde.org>                                        *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017  
0018 #include "LayoutConfigAction.h"
0019 
0020 #include "core/support/Debug.h"
0021 #include "LayoutManager.h"
0022 #include "PlaylistLayoutEditDialog.h"
0023 #include "widgets/EditDeleteDelegate.h"
0024 #include "widgets/EditDeleteComboBoxView.h"
0025 #include "MainWindow.h"
0026 
0027 #include <QStandardPaths>
0028 
0029 #include <QLabel>
0030 #include <QComboBox>
0031 #include <QPixmap>
0032 
0033 namespace Playlist
0034 {
0035 
0036 LayoutConfigAction::LayoutConfigAction( QWidget * parent )
0037     : QAction( parent )
0038     , m_layoutDialog( nullptr )
0039 {
0040     QIcon actionIcon( QPixmap( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/playlist-layouts-22.png") ) );    //TEMPORARY ICON
0041     setIcon( actionIcon );
0042     m_layoutMenu = new QMenu( parent );
0043     setMenu( m_layoutMenu );
0044     setText( i18n( "Playlist Layouts" ) );
0045     m_configAction = new QAction( m_layoutMenu );
0046     
0047     m_layoutMenu->addAction( m_configAction );
0048     m_layoutMenu->addSeparator();
0049     m_layoutActions = new QActionGroup( m_layoutMenu );
0050     m_layoutActions->setExclusive( true );
0051 
0052     QStringList layoutsList( LayoutManager::instance()->layouts() );
0053     foreach( const QString &iterator, layoutsList )
0054     {
0055         m_layoutActions->addAction( iterator )->setCheckable( true );
0056     }
0057     m_layoutMenu->addActions( m_layoutActions->actions() );
0058     int index = LayoutManager::instance()->layouts().indexOf( LayoutManager::instance()->activeLayoutName() );
0059     if( index > -1 )    //needed to avoid crash when created a layout which is moved by the LayoutManager when sorting alphabetically.
0060                         //this should be fixed by itself when layouts ordering will be supported in the LayoutManager
0061     m_layoutActions->actions()[ index ]->setChecked( true );
0062 
0063     connect( m_layoutActions,&QActionGroup::triggered, this, &LayoutConfigAction::setActiveLayout );
0064 
0065     connect( LayoutManager::instance(), &LayoutManager::layoutListChanged, this, &LayoutConfigAction::layoutListChanged );
0066     connect( LayoutManager::instance(), &LayoutManager::activeLayoutChanged, this, &LayoutConfigAction::onActiveLayoutChanged );
0067 
0068     const QIcon configIcon( "configure" );
0069     m_configAction->setIcon( configIcon );
0070     m_configAction->setText( i18n( "Configure Playlist Layouts..." ) );
0071 
0072     connect( m_configAction, &QAction::triggered, this, &LayoutConfigAction::configureLayouts );
0073 }
0074 
0075 
0076 LayoutConfigAction::~LayoutConfigAction()
0077 {}
0078 
0079 void LayoutConfigAction::setActiveLayout( QAction *layoutAction )
0080 {
0081     QString layoutName( layoutAction->text() );
0082     layoutName = layoutName.remove( QLatin1Char( '&' ) );        //need to remove the & from the string, used for the shortcut key underscore
0083     LayoutManager::instance()->setActiveLayout( layoutName );
0084 }
0085 
0086 void LayoutConfigAction::configureLayouts()
0087 {
0088     if( m_layoutDialog == nullptr )
0089         m_layoutDialog = new PlaylistLayoutEditDialog( The::mainWindow() );
0090 
0091     m_layoutDialog->setModal( false );
0092     connect( m_layoutDialog, &Playlist::PlaylistLayoutEditDialog::accepted, this, &LayoutConfigAction::layoutListChanged );
0093 
0094     m_layoutDialog->show();
0095 }
0096 
0097 void Playlist::LayoutConfigAction::layoutListChanged()
0098 {
0099     m_layoutMenu->removeAction( m_configAction );
0100     m_layoutMenu->clear();
0101     m_layoutMenu->addAction( m_configAction );
0102     m_layoutMenu->addSeparator();
0103     
0104     foreach( QAction * action, m_layoutActions->actions() )
0105         delete action;
0106     
0107     QStringList layoutsList( LayoutManager::instance()->layouts() );
0108     foreach( const QString &iterator, layoutsList )
0109         m_layoutActions->addAction( iterator )->setCheckable( true );
0110     
0111     m_layoutMenu->addActions( m_layoutActions->actions() );
0112     
0113     int index = LayoutManager::instance()->layouts().indexOf( LayoutManager::instance()->activeLayoutName() );
0114     if( index > -1 )    //needed to avoid crash when created a layout which is moved by the LayoutManager when sorting alphabetically.
0115                         //this should be fixed by itself when layouts ordering will be supported in the LayoutManager
0116         m_layoutActions->actions()[ index ]->setChecked( true );
0117 }
0118 
0119 void LayoutConfigAction::onActiveLayoutChanged()
0120 {
0121     QString layoutName( LayoutManager::instance()->activeLayoutName() );
0122     layoutName = layoutName.remove( QLatin1Char( '&' ) );        //need to remove the & from the string, used for the shortcut key underscore
0123     if( layoutName != QStringLiteral( "%%PREVIEW%%" ) )           //if it's not just a preview
0124     {
0125         int index = LayoutManager::instance()->layouts().indexOf( layoutName );
0126         if( index != -1 && m_layoutActions->actions()[ index ] != m_layoutActions->checkedAction() )
0127             m_layoutActions->actions()[ index ]->setChecked( true );
0128     }
0129 }
0130 
0131 }
0132