Debian Codegraph @

src/playlist/layouts/LayoutConfigAction.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2008 Nikolaj Hald Nielsen <[email protected]> *
3 * Copyright (c) 2009 Téo Mrnjavac <[email protected]> *
4 * *
5 * This program is free software; you can redistribute it and/or modify it under *
6 * the terms of the GNU General Public License as published by the Free Software *
7 * Foundation; either version 2 of the License, or (at your option) any later *
8 * version. *
9 * *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
12 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License along with *
15 * this program. If not, see <http://www.gnu.org/licenses/>. *
16 ****************************************************************************************/
17
18 #include "LayoutConfigAction.h"
19
20 #include "core/support/Debug.h"
21 #include "LayoutManager.h"
22 #include "PlaylistLayoutEditDialog.h"
23 #include "MainWindow.h"
24
25 #include <QActionGroup>
26 #include <QPixmap>
27 #include <QStandardPaths>
28
29 namespace Playlist
30 {
31
32 LayoutConfigAction::LayoutConfigAction( QWidget * parent )
33 : QAction( parent )
34 , m_layoutDialog( nullptr )
35 {
36 QIcon actionIcon = QIcon::fromTheme( QStringLiteral("view-media-playlist-amarok") );
37 setIcon( actionIcon );
38 m_layoutMenu = new QMenu( parent );
39 setMenu( m_layoutMenu );
40 setText( i18n( "Playlist Layouts" ) );
41 m_configAction = new QAction( m_layoutMenu );
42
43 m_layoutMenu->addAction( m_configAction );
44 m_layoutMenu->addSeparator();
45 m_layoutActions = new QActionGroup( m_layoutMenu );
46 m_layoutActions->setExclusive( true );
47
48 QStringList layoutsList( LayoutManager::instance()->layouts() );
49 for( const QString &iterator : layoutsList )
50 {
51 m_layoutActions->addAction( iterator )->setCheckable( true );
52 }
53 m_layoutMenu->addActions( m_layoutActions->actions() );
54 int index = LayoutManager::instance()->layouts().indexOf( LayoutManager::instance()->activeLayoutName() );
55 if( index > -1 ) //needed to avoid crash when created a layout which is moved by the LayoutManager when sorting alphabetically.
56 //this should be fixed by itself when layouts ordering will be supported in the LayoutManager
57 m_layoutActions->actions()[ index ]->setChecked( true );
58
59 connect( m_layoutActions,&QActionGroup::triggered, this, &LayoutConfigAction::setActiveLayout );
60
61 connect( LayoutManager::instance(), &LayoutManager::layoutListChanged, this, &LayoutConfigAction::layoutListChanged );
62 connect( LayoutManager::instance(), &LayoutManager::activeLayoutChanged, this, &LayoutConfigAction::onActiveLayoutChanged );
63
64 const QIcon configIcon( QStringLiteral("configure") );
65 m_configAction->setIcon( configIcon );
66 m_configAction->setText( i18n( "Configure Playlist Layouts..." ) );
67
68 connect( m_configAction, &QAction::triggered, this, &LayoutConfigAction::configureLayouts );
69 }
70
71
72 LayoutConfigAction::~LayoutConfigAction()
73 {}
74
75 void LayoutConfigAction::setActiveLayout( QAction *layoutAction )
76 {
77 QString layoutName( layoutAction->text() );
78 layoutName = layoutName.remove( QLatin1Char( '&' ) ); //need to remove the & from the string, used for the shortcut key underscore
79 LayoutManager::instance()->setActiveLayout( layoutName );
80 }
81
82 void LayoutConfigAction::configureLayouts()
83 {
84 if( m_layoutDialog == nullptr )
85 m_layoutDialog = new PlaylistLayoutEditDialog( The::mainWindow() );
86 else
87 m_layoutDialog->resetFirstActiveLayout();
88
89 m_layoutDialog->setModal( false );
90 connect( m_layoutDialog, &Playlist::PlaylistLayoutEditDialog::accepted, this, &LayoutConfigAction::layoutListChanged );
91
92 m_layoutDialog->show();
93 }
94
95 void Playlist::LayoutConfigAction::layoutListChanged()
96 {
97 m_layoutMenu->removeAction( m_configAction );
98 m_layoutMenu->clear();
99 m_layoutMenu->addAction( m_configAction );
100 m_layoutMenu->addSeparator();
101
102 for( QAction * action : m_layoutActions->actions() )
103 delete action;
104
105 QStringList layoutsList( LayoutManager::instance()->layouts() );
106 for( const QString &iterator : layoutsList )
107 m_layoutActions->addAction( iterator )->setCheckable( true );
108
109 m_layoutMenu->addActions( m_layoutActions->actions() );
110
111 int index = LayoutManager::instance()->layouts().indexOf( LayoutManager::instance()->activeLayoutName() );
112 if( index > -1 ) //needed to avoid crash when created a layout which is moved by the LayoutManager when sorting alphabetically.
113 //this should be fixed by itself when layouts ordering will be supported in the LayoutManager
114 m_layoutActions->actions()[ index ]->setChecked( true );
115 }
116
117 void LayoutConfigAction::onActiveLayoutChanged()
118 {
119 QString layoutName( LayoutManager::instance()->activeLayoutName() );
120 layoutName = layoutName.remove( QLatin1Char( '&' ) ); //need to remove the & from the string, used for the shortcut key underscore
121 if( layoutName != QStringLiteral( "%%PREVIEW%%" ) ) //if it's not just a preview
122 {
123 int index = LayoutManager::instance()->layouts().indexOf( layoutName );
124 if( index != -1 && m_layoutActions->actions()[ index ] != m_layoutActions->checkedAction() )
125 m_layoutActions->actions()[ index ]->setChecked( true );
126 }
127 }
128
129 }
130