File indexing completed on 2024-05-05 04:48:42
0001 /**************************************************************************************** 0002 * Copyright (c) 2009 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 "PlaylistBreadcrumbItem.h" 0019 0020 #include "PlaylistSortWidget.h" 0021 0022 #include <QIcon> 0023 #include <KLocalizedString> 0024 0025 namespace Playlist 0026 { 0027 0028 BreadcrumbItemMenu::BreadcrumbItemMenu( Column currentColumn, QWidget *parent ) 0029 : QMenu( parent ) 0030 { 0031 for( Column col = Column( 0 ); col != NUM_COLUMNS; col = Column( col + 1 ) ) 0032 { 0033 if( !isSortableColumn( col ) || currentColumn == col ) 0034 continue; 0035 0036 QAction *action = addAction( QIcon::fromTheme( iconName( col ) ), 0037 QString( columnName( col ) ) ); 0038 action->setData( internalColumnName( col ) ); 0039 } 0040 0041 addSeparator(); 0042 QAction *shuffleAction = addAction( QIcon::fromTheme( QStringLiteral("media-playlist-shuffle") ), 0043 i18n( "Shuffle" ) ); 0044 shuffleAction->setData( QStringLiteral( "Shuffle" ) ); 0045 0046 connect( this, &BreadcrumbItemMenu::triggered, this, &BreadcrumbItemMenu::actionTriggered ); 0047 } 0048 0049 BreadcrumbItemMenu::~BreadcrumbItemMenu() 0050 {} 0051 0052 void 0053 BreadcrumbItemMenu::actionTriggered( QAction *action ) 0054 { 0055 const QString actionName( action->data().toString() ); 0056 if( actionName == QLatin1String("Shuffle") ) 0057 Q_EMIT shuffleActionClicked(); 0058 else 0059 Q_EMIT actionClicked( actionName ); 0060 } 0061 0062 /////// BreadcrumbItem methods begin here 0063 0064 BreadcrumbItem::BreadcrumbItem( BreadcrumbLevel *level, QWidget *parent ) 0065 : BoxWidget( false, parent ) 0066 , m_name( level->name() ) 0067 , m_prettyName( level->prettyName() ) 0068 { 0069 // Let's set up the "siblings" button first... 0070 m_menuButton = new BreadcrumbItemMenuButton( this ); 0071 m_menu = new BreadcrumbItemMenu( columnForName( m_name ), this ); 0072 0073 // Disable used levels 0074 QStringList usedBreadcrumbLevels = qobject_cast< SortWidget * >( parent )->levels(); 0075 foreach( QAction *action, m_menu->actions() ) 0076 if( usedBreadcrumbLevels.contains( action->data().toString() ) ) 0077 action->setEnabled( false ); 0078 0079 m_menuButton->setMenu( m_menu ); 0080 m_menu->setContentsMargins( /*offset*/ 6, 1, 1, 2 ); 0081 0082 // And then the main breadcrumb button... 0083 m_mainButton = new BreadcrumbItemSortButton( level->icon(), level->prettyName(), this ); 0084 connect( m_mainButton, &BreadcrumbItemSortButton::clicked, this, &BreadcrumbItem::clicked ); 0085 connect( m_mainButton, &BreadcrumbItemSortButton::arrowToggled, this, &BreadcrumbItem::orderInverted ); 0086 connect( m_mainButton, &BreadcrumbItemSortButton::sizePolicyChanged, this, &BreadcrumbItem::updateSizePolicy ); 0087 m_menu->hide(); 0088 0089 updateSizePolicy(); 0090 } 0091 0092 BreadcrumbItem::~BreadcrumbItem() 0093 {} 0094 0095 QString 0096 BreadcrumbItem::name() const 0097 { 0098 return m_name; 0099 } 0100 0101 Qt::SortOrder 0102 BreadcrumbItem::sortOrder() const 0103 { 0104 return m_mainButton->orderState(); 0105 } 0106 0107 void 0108 BreadcrumbItem::invertOrder() 0109 { 0110 m_mainButton->invertOrder(); 0111 } 0112 0113 void 0114 BreadcrumbItem::updateSizePolicy() 0115 { 0116 setSizePolicy( m_mainButton->sizePolicy() ); 0117 } 0118 0119 const BreadcrumbItemMenu* 0120 BreadcrumbItem::menu() 0121 { 0122 return m_menu; 0123 } 0124 0125 /////// BreadcrumbAddMenuButton methods begin here 0126 0127 BreadcrumbAddMenuButton::BreadcrumbAddMenuButton( QWidget *parent ) 0128 : BreadcrumbItemMenuButton( parent ) 0129 { 0130 setToolTip( i18n( "Add a sorting level to the playlist." ) ); 0131 0132 //FIXME: the menu should have the same margins as other Playlist::Breadcrumb and 0133 // BrowserBreadcrumb menus. 0134 m_menu = new BreadcrumbItemMenu( PlaceHolder, this ); 0135 setMenu( m_menu ); 0136 } 0137 0138 BreadcrumbAddMenuButton::~BreadcrumbAddMenuButton() 0139 {} 0140 0141 const BreadcrumbItemMenu* 0142 BreadcrumbAddMenuButton::menu() 0143 { 0144 return m_menu; 0145 } 0146 0147 void 0148 BreadcrumbAddMenuButton::updateMenu( const QStringList &usedBreadcrumbLevels ) 0149 { 0150 // Enable unused, disable used levels 0151 foreach( QAction *action, m_menu->actions() ) 0152 action->setEnabled( !usedBreadcrumbLevels.contains( action->data().toString() ) ); 0153 } 0154 0155 } //namespace Playlist