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

0001 /*************************************************************************
0002  *   Copyright (c) 2015 by Renaud Guezennec                              *
0003  *   Copyright (C) 2011 by Joseph Boudou                                 *
0004  *                                                                       *
0005  *   https://rolisteam.org/                                           *
0006  *                                                                       *
0007  *   Rolisteam is free software; you can redistribute it and/or modify   *
0008  *   it under the terms of the GNU General Public License as published   *
0009  *   by the Free Software Foundation; either version 2 of the License,   *
0010  *   or (at your option) any later version.                              *
0011  *                                                                       *
0012  *   This program is distributed in the hope that it will be useful,     *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
0015  *   GNU General Public License for more details.                        *
0016  *                                                                       *
0017  *   You should have received a copy of the GNU General Public License   *
0018  *   along with this program; if not, write to the                       *
0019  *   Free Software Foundation, Inc.,                                     *
0020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
0021  *************************************************************************/
0022 
0023 #include "data/localpersonmodel.h"
0024 
0025 #include "data/character.h"
0026 #include "data/person.h"
0027 #include "data/player.h"
0028 
0029 #include <QDebug>
0030 
0031 LocalPersonModel::LocalPersonModel() : QAbstractProxyModel() {}
0032 
0033 QModelIndex LocalPersonModel::mapFromSource(const QModelIndex& sourceIndex) const
0034 {
0035     if(!sourceIndex.isValid())
0036         return QModelIndex();
0037 
0038     auto parent= sourceIndex.parent();
0039 
0040     if(!parent.isValid() && sourceIndex.row() > 0)
0041         return QModelIndex();
0042 
0043     if(!parent.isValid() && sourceIndex.row() == 0)
0044         return createIndex(0, 0, sourceIndex.internalPointer());
0045 
0046     if(parent.isValid() && parent.row() == 0)
0047         return createIndex(1 + sourceIndex.row(), 0, sourceIndex.internalPointer());
0048 
0049     return QModelIndex();
0050 }
0051 
0052 QModelIndex LocalPersonModel::mapToSource(const QModelIndex& proxyIndex) const
0053 {
0054     if(!sourceModel())
0055         return {};
0056 
0057     QModelIndex idx;
0058     if(proxyIndex.row() == 0)
0059         idx= sourceModel()->index(0, 0, QModelIndex());
0060     else
0061         idx= sourceModel()->index(proxyIndex.row() - 1, 0, sourceModel()->index(0, 0, QModelIndex()));
0062 
0063     return idx;
0064 }
0065 
0066 QHash<int, QByteArray> LocalPersonModel::roleNames() const
0067 {
0068     return sourceModel()->roleNames();
0069 }
0070 
0071 void LocalPersonModel::setSourceModel(QAbstractItemModel* sourceModel)
0072 {
0073     beginResetModel();
0074     QAbstractProxyModel::setSourceModel(sourceModel);
0075     endResetModel();
0076 
0077     auto func= [this]()
0078     {
0079         beginResetModel();
0080 
0081         endResetModel();
0082     };
0083     connect(sourceModel, &QAbstractItemModel::dataChanged, this, func);
0084     connect(sourceModel, &QAbstractItemModel::rowsInserted, this, func);
0085     connect(sourceModel, &QAbstractItemModel::modelReset, this, func);
0086 }
0087 
0088 QModelIndex LocalPersonModel::index(int r, int c, const QModelIndex&) const
0089 {
0090     return createIndex(r, c);
0091 }
0092 
0093 QModelIndex LocalPersonModel::parent(const QModelIndex&) const
0094 {
0095     return QModelIndex();
0096 }
0097 
0098 int LocalPersonModel::rowCount(const QModelIndex& parent) const
0099 {
0100     if(parent.isValid())
0101         return 0;
0102 
0103     auto firstItem= sourceModel()->index(0, 0);
0104     if(!firstItem.isValid())
0105         return 0;
0106 
0107     auto child= sourceModel()->rowCount(firstItem);
0108 
0109     return child + 1;
0110 }
0111 
0112 int LocalPersonModel::columnCount(const QModelIndex& parent) const
0113 {
0114     if(parent.isValid())
0115         return 0;
0116     return 1;
0117 }