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

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/playerproxymodel.h"
0021 
0022 #include "data/person.h"
0023 #include "data/player.h"
0024 #include "model/playermodel.h"
0025 
0026 PlayerProxyModel::PlayerProxyModel(QObject* parent) : QAbstractProxyModel(parent) {}
0027 
0028 int PlayerProxyModel::columnCount(const QModelIndex& parent) const
0029 {
0030     if(parent.isValid())
0031         return 0;
0032     return 1;
0033 }
0034 
0035 int PlayerProxyModel::rowCount(const QModelIndex& parent) const
0036 {
0037     if(parent.isValid() || !sourceModel())
0038         return 0;
0039 
0040     return sourceModel()->rowCount(); // only item at one depth level
0041 }
0042 
0043 QModelIndex PlayerProxyModel::mapFromSource(const QModelIndex& sourceIndex) const
0044 {
0045     if(!sourceIndex.isValid())
0046         return QModelIndex();
0047 
0048     auto parent= sourceIndex.parent();
0049 
0050     if(parent.isValid())
0051         return QModelIndex();
0052 
0053     return createIndex(sourceIndex.row(), 0, sourceIndex.internalPointer());
0054 }
0055 
0056 QModelIndex PlayerProxyModel::mapToSource(const QModelIndex& proxyIndex) const
0057 {
0058     return sourceModel()->index(proxyIndex.row(), 0, QModelIndex());
0059 }
0060 
0061 QModelIndex PlayerProxyModel::parent(const QModelIndex&) const
0062 {
0063     return QModelIndex();
0064 }
0065 
0066 QModelIndex PlayerProxyModel::index(int r, int c, const QModelIndex&) const
0067 {
0068     return createIndex(r, c);
0069 }
0070 
0071 Player* PlayerProxyModel::player(const QString& id)
0072 {
0073     Player* find= nullptr;
0074 
0075     for(int i= 0; i < rowCount() && find == nullptr; ++i)
0076     {
0077         auto idx= index(i, 0, QModelIndex());
0078         auto uuid= idx.data(PlayerModel::IdentifierRole).toString();
0079         auto person= idx.data(PlayerModel::PersonPtrRole).value<Person*>();
0080         if(uuid == id)
0081             find= dynamic_cast<Player*>(person);
0082     }
0083 
0084     return find;
0085 }