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 "BrowserCategoryListModel.h"
0018 #include "BrowserCategory.h"
0019 #include "core/support/Debug.h"
0020 
0021 #include "widgets/PrettyTreeRoles.h"
0022 
0023 BrowserCategoryListModel::BrowserCategoryListModel( QObject* parent )
0024  : QAbstractListModel( parent )
0025 {
0026 }
0027 
0028 BrowserCategoryListModel::~BrowserCategoryListModel()
0029 {
0030     qDeleteAll( m_categories );
0031 }
0032 
0033 int
0034 BrowserCategoryListModel::rowCount( const QModelIndex & parent ) const
0035 {
0036     Q_UNUSED( parent );
0037     return m_categories.count();
0038 }
0039 
0040 QVariant
0041 BrowserCategoryListModel::data( const QModelIndex &index, int role ) const
0042 {
0043     //DEBUG_BLOCK
0044     if( !index.isValid() || m_categories.count() <= index.row() )
0045         return QVariant();
0046 
0047     switch( role )
0048     {
0049         case Qt::DisplayRole:
0050             return QVariant( m_categories[index.row()]->prettyName() );
0051 
0052         case Qt::DecorationRole:
0053             return QVariant( m_categories[index.row()]->icon() );
0054 
0055         case PrettyTreeRoles::SortRole:
0056         case PrettyTreeRoles::ByLineRole:
0057             return QVariant( m_categories[index.row()]->shortDescription() );
0058 
0059         case Qt::ToolTipRole:
0060             return QVariant( m_categories[index.row()]->longDescription() );
0061 
0062         case CustomCategoryRoles::CategoryRole:
0063             return QVariant::fromValue( m_categories[index.row()] );
0064 
0065         default:
0066             return QVariant();
0067      }
0068 }
0069 
0070 void
0071 BrowserCategoryListModel::addCategory( BrowserCategory *category )
0072 {
0073     if( !category )
0074     {
0075         debug() << "Trying to add a nonexistent service to the BrowserCategoryListModel!";
0076         return;
0077     }
0078     beginInsertRows ( QModelIndex(), m_categories.count(), m_categories.count() );
0079     m_categories.push_back( category );
0080     endInsertRows();
0081 }
0082 
0083 void
0084 BrowserCategoryListModel::removeCategory( BrowserCategory *category )
0085 {
0086     if( !category )
0087     {
0088         debug() << "Trying to remove a nonexistent service from the BrowserCategoryListModel!";
0089         return;
0090     }
0091     int index = m_categories.indexOf( category );
0092     beginRemoveRows ( QModelIndex(), index, index );
0093     m_categories.removeAt( index );
0094     endRemoveRows();
0095 }
0096