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 "BrowserCategory.h"
0018 
0019 #include "App.h"
0020 #include "amarokconfig.h"
0021 #include "BrowserBreadcrumbItem.h"
0022 #include "BrowserCategoryList.h"
0023 #include "PaletteHandler.h"
0024 #include "core/support/Debug.h"
0025 
0026 
0027 BrowserCategory::BrowserCategory( const QString &name, QWidget *parent )
0028     : BoxWidget( true, parent )
0029     , m_name( name )
0030     , m_parentList( nullptr )
0031 {
0032     setObjectName( name );
0033     setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
0034 
0035     connect( pApp, &App::settingsChanged, this, &BrowserCategory::slotSettingsChanged );
0036     connect( The::paletteHandler(), &PaletteHandler::newPalette, this, &BrowserCategory::slotSettingsChanged );
0037 }
0038 
0039 BrowserCategory::~BrowserCategory()
0040 {
0041 }
0042 
0043 QString
0044 BrowserCategory::name() const
0045 {
0046     return m_name;
0047 }
0048 
0049 void
0050 BrowserCategory::setPrettyName( const QString &prettyName )
0051 {
0052     m_prettyName = prettyName;
0053 }
0054 
0055 QString
0056 BrowserCategory::prettyName() const
0057 {
0058     return m_prettyName;
0059 }
0060 
0061 void
0062 BrowserCategory::setShortDescription( const QString &shortDescription )
0063 {
0064     m_shortDescription = shortDescription;
0065 }
0066 
0067 QString
0068 BrowserCategory::shortDescription() const
0069 {
0070     return m_shortDescription;
0071 }
0072 
0073 void
0074 BrowserCategory::setLongDescription( const QString &longDescription )
0075 {
0076     m_longDescription = longDescription;
0077 }
0078 
0079 QString
0080 BrowserCategory::longDescription() const
0081 {
0082     return m_longDescription;
0083 }
0084 
0085 void
0086 BrowserCategory::setIcon( const QIcon & icon )
0087 {
0088     m_icon = icon;
0089 }
0090 
0091 QIcon
0092 BrowserCategory::icon() const
0093 {
0094     return m_icon;
0095 }
0096 
0097 void
0098 BrowserCategory::setBackgroundImage(const QString& path)
0099 {
0100     if ( path.isEmpty() || !QUrl(path).isLocalFile() ) {
0101         setStyleSheet( QString() );
0102         return;
0103     }
0104 
0105     // Hack alert: Use the class name of the most derived object (using polymorphism) for CSS
0106     // This is required to limit the style to this specific class only (avoiding cascading)
0107     // \sa http://doc.qt.nokia.com/latest/stylesheet-syntax.html#widgets-inside-c-namespaces
0108     const QString escapedClassName = QString( metaObject()->className() ).replace( QLatin1String("::"), QLatin1String("--") );
0109     setStyleSheet( QStringLiteral("%1 { background-image: url(\"%2\"); \
0110             background-repeat: no-repeat; \
0111             background-attachment: fixed; \
0112             background-position: center; }").arg( escapedClassName, path )
0113     );
0114 }
0115 
0116 void BrowserCategory::slotSettingsChanged()
0117 {
0118     setBackgroundImage( AmarokConfig::showBrowserBackgroundImage() ? m_imagePath : QString() );
0119 }
0120 
0121 void BrowserCategory::setParentList( BrowserCategoryList * parent )
0122 {
0123     m_parentList = parent;
0124 }
0125 
0126 BrowserCategoryList * BrowserCategory::parentList() const
0127 {
0128     return m_parentList;
0129 }
0130 
0131 void BrowserCategory::activate()
0132 {
0133     DEBUG_BLOCK
0134     if ( parentList() )
0135         parentList()->setActiveCategory( this );
0136 }
0137 
0138 BrowserBreadcrumbItem *BrowserCategory::breadcrumb()
0139 {
0140     return new BrowserBreadcrumbItem( this );
0141 }
0142 
0143 void BrowserCategory::setImagePath( const QString & path )
0144 {
0145     m_imagePath = path;
0146 }
0147 
0148 QString BrowserCategory::imagePath() const
0149 {
0150     return m_imagePath;
0151 }
0152 
0153 void
0154 BrowserCategory::addAdditionalItem( BrowserBreadcrumbItem * item )
0155 {
0156     m_additionalItems.append( item );
0157 }
0158 
0159 void
0160 BrowserCategory::clearAdditionalItems()
0161 {
0162     foreach( BrowserBreadcrumbItem *item, m_additionalItems )
0163     {
0164         m_additionalItems.removeAll( item );
0165         /* deleting immediately isn't safe, this method may be called from an inner
0166          * QEventLoop inside QMenu::exec() of another breadcrumb item, which could
0167          * then leas to crash bug 265626 */
0168         item->deleteLater();
0169     }
0170 }
0171 
0172 QList<BrowserBreadcrumbItem *>
0173 BrowserCategory::additionalItems()
0174 {
0175     return m_additionalItems;
0176 }
0177