File indexing completed on 2024-05-05 04:47:27
0001 /**************************************************************************************** 0002 * Copyright (c) 2009 Nikolaj Hald Nielsen <nhn@kde.org> * 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 "BrowserBreadcrumbItem.h" 0018 0019 #include "browsers/BrowserCategoryList.h" 0020 #include "browsers/filebrowser/FileBrowser.h" 0021 #include "core/support/Debug.h" 0022 #include "widgets/BreadcrumbItemButton.h" 0023 0024 #include <QDir> 0025 #include <QHBoxLayout> 0026 #include <QIcon> 0027 #include <QMenu> 0028 0029 #include <KLocalizedString> 0030 0031 0032 BrowserBreadcrumbItem::BrowserBreadcrumbItem( BrowserCategory *category, QWidget *parent ) 0033 : BoxWidget( false, parent ) 0034 , m_menuButton( nullptr ) 0035 { 0036 //figure out if we want to add a menu to this item. A menu allows you to select 0037 //any of the _sibling_ items. (yes, I know, this is different from how Dolphin 0038 //does it, but I find the Dolphin way amazingly unintuitive and I always get it 0039 //wrong when using it...) 0040 BrowserCategoryList * parentList = category->parentList(); 0041 if( parentList ) 0042 { 0043 m_menuButton = new BreadcrumbItemMenuButton( this ); 0044 QMenu *menu = new QMenu( this ); //see QMenu docs: it's still a top-level widget. 0045 //parent is only for memory management. 0046 QMap<QString,BrowserCategory *> siblingMap = parentList->categories(); 0047 0048 const QStringList siblingNames = siblingMap.keys(); 0049 0050 foreach( const QString &siblingName, siblingNames ) 0051 { 0052 //no point in adding ourselves to this menu 0053 if ( siblingName == category->name() ) 0054 continue; 0055 0056 BrowserCategory *siblingCategory = siblingMap.value( siblingName ); 0057 0058 QAction *action = menu->addAction( siblingCategory->icon(), siblingCategory->prettyName() ); 0059 connect( action, &QAction::triggered, siblingMap.value( siblingName ), &BrowserCategory::activate ); 0060 } 0061 0062 m_menuButton->setMenu( menu ); 0063 } 0064 0065 m_mainButton = new BreadcrumbItemButton( category->icon(), category->prettyName(), this ); 0066 0067 if( category->prettyName().isEmpty() ) 0068 { 0069 // root item 0070 m_mainButton->setToolTip( i18n( "Media Sources Home" ) ); 0071 m_mainButton->setIcon( QIcon::fromTheme( QStringLiteral("user-home") ) ); 0072 } 0073 0074 connect( m_mainButton, &BreadcrumbItemButton::sizePolicyChanged, this, &BrowserBreadcrumbItem::updateSizePolicy ); 0075 0076 //if this is a list, make clicking on this item cause us 0077 //to navigate to its home. 0078 BrowserCategoryList *list = qobject_cast<BrowserCategoryList*>( category ); 0079 if ( list ) 0080 { 0081 connect( m_mainButton, &QAbstractButton::clicked, list, &BrowserCategoryList::home ); 0082 } 0083 else 0084 { 0085 connect( m_mainButton, &QAbstractButton::clicked, category, &BrowserCategory::reActivate ); 0086 } 0087 0088 adjustSize(); 0089 m_nominalWidth = width(); 0090 0091 hide(); 0092 0093 updateSizePolicy(); 0094 } 0095 0096 BrowserBreadcrumbItem::BrowserBreadcrumbItem( const QString &name, const QString &callback, 0097 const BreadcrumbSiblingList &childItems, FileBrowser *handler, QWidget *parent ) 0098 : BoxWidget( false, parent ) 0099 , m_menuButton( nullptr ) 0100 , m_callback( callback ) 0101 { 0102 if ( !childItems.isEmpty() ) 0103 { 0104 m_menuButton = new BreadcrumbItemMenuButton( this ); 0105 QMenu *menu = new QMenu( this ); 0106 0107 int i = 0; 0108 foreach( const BreadcrumbSibling &sibling, childItems ) 0109 { 0110 QString visibleName = sibling.name; 0111 visibleName.replace( '&', QLatin1String("&&") ); // prevent bug 244817 0112 QAction *action = menu->addAction( sibling.icon, visibleName ); 0113 action->setProperty( "callback", sibling.callback ); 0114 0115 // the current action should be bolded 0116 if( sibling.name == name ) 0117 { 0118 QFont font = action->font(); 0119 font.setBold( true ); 0120 action->setFont( font ); 0121 } 0122 connect( action, &QAction::triggered, this, &BrowserBreadcrumbItem::activateSibling ); 0123 i++; 0124 } 0125 m_menuButton->setMenu( menu ); 0126 } 0127 0128 m_mainButton = new BreadcrumbItemButton( name, this ); 0129 connect( m_mainButton, &BreadcrumbItemButton::sizePolicyChanged, this, &BrowserBreadcrumbItem::updateSizePolicy ); 0130 connect( m_mainButton, &QAbstractButton::clicked, this, &BrowserBreadcrumbItem::activate ); 0131 connect( this, &BrowserBreadcrumbItem::activated, handler, &FileBrowser::addItemActivated ); 0132 0133 adjustSize(); 0134 m_nominalWidth = width(); 0135 0136 hide(); 0137 updateSizePolicy(); 0138 } 0139 0140 BrowserBreadcrumbItem::~BrowserBreadcrumbItem() 0141 { 0142 } 0143 0144 void 0145 BrowserBreadcrumbItem::setActive( bool active ) 0146 { 0147 m_mainButton->setActive( active ); 0148 } 0149 0150 QSizePolicy BrowserBreadcrumbItem::sizePolicy() const 0151 { 0152 return m_mainButton->sizePolicy(); 0153 } 0154 0155 void BrowserBreadcrumbItem::updateSizePolicy() 0156 { 0157 setSizePolicy( m_mainButton->sizePolicy() ); 0158 } 0159 0160 void BrowserBreadcrumbItem::activate() 0161 { 0162 Q_EMIT activated( m_callback ); 0163 } 0164 0165 void BrowserBreadcrumbItem::activateSibling() 0166 { 0167 QAction *action = qobject_cast<QAction *>( sender() ); 0168 if( action ) 0169 Q_EMIT activated( action->property( "callback" ).toString() ); 0170 } 0171 0172 int BrowserBreadcrumbItem::nominalWidth() const 0173 { 0174 return m_nominalWidth; 0175 }