File indexing completed on 2024-09-15 04:57:48
0001 /*************************************************************************** 0002 * Copyright (C) 2022 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/statemodel.h" 0021 #include "model/nonplayablecharactermodel.h" 0022 #include <QSet> 0023 0024 StateModel::StateModel(QObject* parent) : QAbstractListModel(parent) {} 0025 0026 int StateModel::rowCount(const QModelIndex& parent) const 0027 { 0028 // For list models only the root node (an invalid parent) should return the list's size. For all 0029 // other (valid) parents, rowCount() should return 0 so that it does not become a tree model. 0030 if(parent.isValid()) 0031 return 0; 0032 0033 return m_data.size() + 1; 0034 } 0035 0036 QVariant StateModel::data(const QModelIndex& index, int role) const 0037 { 0038 if(!index.isValid()) 0039 return QVariant(); 0040 0041 auto i= index.row(); 0042 0043 if(i == 0 && role == Qt::DisplayRole) 0044 return tr("Any State"); 0045 else 0046 return {}; 0047 0048 return m_data[i - 1]; 0049 } 0050 0051 void StateModel::setSourceModel(const campaign::NonPlayableCharacterModel* source) 0052 { 0053 0054 connect(source, &campaign::NonPlayableCharacterModel::dataChanged, this, 0055 [this, &source](const QModelIndex& start, const QModelIndex& end, QList<int> roles) 0056 { 0057 if(roles.contains(campaign::NonPlayableCharacterModel::RoleColor)) 0058 { 0059 auto index= start; 0060 for(int i= start.row(); i < end.row(); ++i) 0061 { 0062 auto stateId= source->data(index, campaign::NonPlayableCharacterModel::RoleState).toString(); 0063 0064 if(m_data.contains(stateId)) 0065 return; 0066 0067 beginInsertRows(QModelIndex(), rowCount(), rowCount()); 0068 m_data.append(stateId); 0069 endInsertRows(); 0070 } 0071 } 0072 }); 0073 connect(source, &campaign::NonPlayableCharacterModel::rowsInserted, this, 0074 [this, source](const QModelIndex& parent, int start, int end) 0075 { 0076 for(int i= start; i < end; ++i) 0077 { 0078 if(i < source->rowCount()) 0079 { 0080 auto stateId 0081 = source->data(source->index(i, 0, parent), campaign::NonPlayableCharacterModel::RoleState) 0082 .toString(); 0083 0084 if(m_data.contains(stateId)) 0085 return; 0086 0087 beginInsertRows(QModelIndex(), rowCount(), rowCount()); 0088 m_data.append(stateId); 0089 endInsertRows(); 0090 } 0091 } 0092 }); 0093 0094 QSet<QString> ids; 0095 auto const& npcs= source->npcList(); 0096 0097 std::for_each(std::begin(npcs), std::end(npcs), 0098 [&ids](const std::unique_ptr<campaign::NonPlayableCharacter>& npc) 0099 { 0100 ids.insert(npc->stateId()); 0101 }); 0102 0103 beginResetModel(); 0104 m_data= ids.values(); 0105 endResetModel(); 0106 }