File indexing completed on 2024-05-05 04:47:30

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Alexandre Pereira de Oliveira <aleprj@gmail.com>                  *
0003  * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0004  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) any later           *
0009  * version.                                                                             *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #define DEBUG_PREFIX "SingleCollectionTreeItemModel"
0020 
0021 #include "SingleCollectionTreeItemModel.h"
0022 
0023 #include "amarokconfig.h"
0024 #include "browsers/CollectionTreeItem.h"
0025 #include "core/collections/Collection.h"
0026 #include "core/meta/Meta.h"
0027 #include "core/support/Amarok.h"
0028 #include "core/support/Debug.h"
0029 
0030 #include <KLocalizedString>
0031 
0032 SingleCollectionTreeItemModel::SingleCollectionTreeItemModel( Collections::Collection *collection,
0033                                                               const QList<CategoryId::CatMenuId> &levelType )
0034     : m_collection( collection )
0035 {
0036     m_rootItem = new CollectionTreeItem( m_collection, nullptr, this );
0037     connect( collection, &Collections::Collection::updated, this, &SingleCollectionTreeItemModel::slotFilterWithoutAutoExpand ) ;
0038     m_collections.insert( m_collection->collectionId(), CollectionRoot( m_collection, m_rootItem ) );
0039     //we only have one collection that, by its very nature, is always expanded
0040     m_expandedCollections.insert( m_collection );
0041 
0042     setLevels( levelType );
0043 }
0044 
0045 QVariant
0046 SingleCollectionTreeItemModel::data(const QModelIndex &index, int role) const
0047 {
0048     if( !index.isValid() )
0049         return QVariant();
0050 
0051     CollectionTreeItem *item = static_cast<CollectionTreeItem*>( index.internalPointer() );
0052     return dataForItem( item, role );
0053 }
0054 
0055 Qt::ItemFlags
0056 SingleCollectionTreeItemModel::flags(const QModelIndex &index) const
0057 {
0058     Qt::ItemFlags f = CollectionTreeItemModelBase::flags( index );
0059     return ( f &= ~Qt::ItemIsEditable );
0060 }
0061 
0062 bool
0063 SingleCollectionTreeItemModel::canFetchMore( const QModelIndex &parent ) const
0064 {
0065     if ( !parent.isValid() )
0066        return m_rootItem->requiresUpdate();
0067 
0068     CollectionTreeItem *item = static_cast<CollectionTreeItem*>( parent.internalPointer() );
0069     return item->level() < m_levelType.count() && item->requiresUpdate();
0070 }
0071 
0072 void
0073 SingleCollectionTreeItemModel::fetchMore( const QModelIndex &parent )
0074 {
0075     CollectionTreeItem *item;
0076     if ( parent.isValid() )
0077         item = static_cast<CollectionTreeItem*>( parent.internalPointer() );
0078     else
0079         item = m_rootItem;
0080 
0081     ensureChildrenLoaded( item );
0082 }
0083 
0084 void
0085 SingleCollectionTreeItemModel::filterChildren()
0086 {
0087     markSubTreeAsDirty( m_rootItem );
0088     ensureChildrenLoaded( m_rootItem );
0089 }
0090