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

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/charactermodel.h"
0021 
0022 #include "data/character.h"
0023 #include "data/person.h"
0024 #include "data/player.h"
0025 #include "model/playermodel.h"
0026 
0027 CharacterModel::CharacterModel(QObject* parent) : QAbstractProxyModel(parent)
0028 {
0029     // setDynamicSortFilter(true);
0030 }
0031 
0032 int CharacterModel::columnCount(const QModelIndex& parent) const
0033 {
0034     if(parent.isValid())
0035         return 0;
0036     return 1;
0037 }
0038 
0039 int CharacterModel::rowCount(const QModelIndex& parent) const
0040 {
0041     if(parent.isValid() || !sourceModel())
0042         return 0;
0043 
0044     int row= 0;
0045     auto size= sourceModel()->rowCount();
0046     for(int i= 0; i < size; ++i)
0047     {
0048         auto index= sourceModel()->index(i, 0);
0049         row+= sourceModel()->rowCount(index);
0050     }
0051     return row;
0052 }
0053 
0054 QModelIndex CharacterModel::mapFromSource(const QModelIndex& sourceIndex) const
0055 {
0056     if(!sourceIndex.isValid())
0057         return QModelIndex();
0058 
0059     auto parent= sourceIndex.parent();
0060 
0061     if(!parent.isValid())
0062         return QModelIndex();
0063 
0064     int row= sourceIndex.row();
0065     for(int i= 0; i < parent.row(); ++i)
0066     {
0067         auto index= sourceModel()->index(i, 0);
0068         row+= sourceModel()->rowCount(index);
0069     }
0070 
0071     if(parent.isValid())
0072         return createIndex(row, 0, sourceIndex.internalPointer());
0073 
0074     return QModelIndex();
0075 }
0076 
0077 QModelIndex CharacterModel::mapToSource(const QModelIndex& proxyIndex) const
0078 {
0079     if(!sourceModel())
0080         return {};
0081 
0082     auto row= proxyIndex.row();
0083     QModelIndex parent;
0084     bool found= false;
0085     for(int i= 0; i < sourceModel()->rowCount() && !found; ++i)
0086     {
0087         auto index= sourceModel()->index(i, 0);
0088         auto count= sourceModel()->rowCount(index);
0089         if((row - count) >= 0)
0090         {
0091             row-= count;
0092             parent= index;
0093         }
0094         else if(row < count)
0095         {
0096             parent= index;
0097             found= true;
0098         }
0099     }
0100     return sourceModel()->index(row, 0, parent);
0101 }
0102 
0103 QModelIndex CharacterModel::parent(const QModelIndex&) const
0104 {
0105     return QModelIndex();
0106 }
0107 
0108 QModelIndex CharacterModel::index(int r, int c, const QModelIndex&) const
0109 {
0110     return createIndex(r, c);
0111 }
0112 
0113 Character* CharacterModel::character(const QString& id)
0114 {
0115     Character* find= nullptr;
0116 
0117     for(int i= 0; i < rowCount() && find == nullptr; ++i)
0118     {
0119         auto idx= index(i, 0, QModelIndex());
0120         auto uuid= idx.data(PlayerModel::IdentifierRole).toString();
0121         auto person= idx.data(PlayerModel::PersonPtrRole).value<Person*>();
0122         if(uuid == id)
0123             find= dynamic_cast<Character*>(person);
0124     }
0125 
0126     return find;
0127 }