File indexing completed on 2024-05-05 05:40:31

0001 /***************************************************************************
0002  *  Copyright (C) 2020 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "model/singlecontenttypemodel.h"
0021 
0022 #include "controller/view_controller/mediacontrollerbase.h"
0023 #include "model/contentmodel.h"
0024 
0025 SingleContentTypeModel::SingleContentTypeModel(Core::ContentType type, QObject* parent)
0026     : QSortFilterProxyModel(parent), m_type(type)
0027 {
0028     setFilterRole(ContentModel::ContentTypeRole);
0029 }
0030 
0031 bool SingleContentTypeModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
0032 {
0033     QModelIndex index0= sourceModel()->index(sourceRow, 0, sourceParent);
0034     return sourceModel()->data(index0, filterRole()).value<Core::ContentType>() == m_type;
0035 }
0036 
0037 Core::ContentType SingleContentTypeModel::type() const
0038 {
0039     return m_type;
0040 }
0041 
0042 bool SingleContentTypeModel::contains(const QString& uuid) const
0043 {
0044     bool found= false;
0045     for(int i= 0; i < rowCount() && !found; ++i)
0046     {
0047         auto idx= index(i, 0);
0048         auto id= idx.data(ContentModel::UuidRole);
0049         found= id == uuid;
0050     }
0051     return found;
0052 }
0053 
0054 MediaControllerBase* SingleContentTypeModel::controller(const QString& uuid) const
0055 {
0056     QModelIndex idx;
0057     for(int i= 0; i < rowCount() && !idx.isValid(); ++i)
0058     {
0059         auto modelIndex= index(i, 0);
0060         auto id= modelIndex.data(ContentModel::UuidRole);
0061         if(id == uuid)
0062             idx= modelIndex;
0063     }
0064     return idx.data(ContentModel::ControllerRole).value<MediaControllerBase*>();
0065 }