File indexing completed on 2024-05-05 05:40:27
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/colormodel.h" 0021 #include "model/nonplayablecharactermodel.h" 0022 #include <QSet> 0023 0024 ColorModel::ColorModel(QObject* parent) : QAbstractListModel(parent) {} 0025 0026 int ColorModel::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 ColorModel::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 color"); 0045 else 0046 return {}; 0047 0048 auto color= m_data[i - 1]; 0049 0050 if(role == Qt::DecorationRole) 0051 return color; 0052 else // if(role == Qt::DisplayRole) 0053 return color.name(); 0054 } 0055 0056 void ColorModel::setSourceModel(const campaign::NonPlayableCharacterModel* source) 0057 { 0058 0059 connect(source, &campaign::NonPlayableCharacterModel::dataChanged, this, 0060 [this, &source](const QModelIndex& start, const QModelIndex& end, QList<int> roles) 0061 { 0062 if(roles.contains(campaign::NonPlayableCharacterModel::RoleColor)) 0063 { 0064 auto index= start; 0065 for(int i= start.row(); i < end.row(); ++i) 0066 { 0067 auto color= source->data(index, campaign::NonPlayableCharacterModel::RoleColor).value<QColor>(); 0068 0069 if(m_data.contains(color)) 0070 return; 0071 0072 beginInsertRows(QModelIndex(), rowCount(), rowCount()); 0073 m_data.append(color); 0074 endInsertRows(); 0075 } 0076 } 0077 }); 0078 connect(source, &campaign::NonPlayableCharacterModel::rowsInserted, this, 0079 [this, source](const QModelIndex& parent, int start, int end) 0080 { 0081 for(int i= start; i < end; ++i) 0082 { 0083 if(i < source->rowCount()) 0084 { 0085 auto color 0086 = source->data(source->index(i, 0, parent), campaign::NonPlayableCharacterModel::RoleColor) 0087 .value<QColor>(); 0088 0089 if(m_data.contains(color)) 0090 return; 0091 0092 beginInsertRows(QModelIndex(), rowCount(), rowCount()); 0093 m_data.append(color); 0094 endInsertRows(); 0095 } 0096 } 0097 }); 0098 0099 QSet<QColor> colors; 0100 auto const& npcs= source->npcList(); 0101 std::for_each(std::begin(npcs), std::end(npcs), 0102 [&colors](const std::unique_ptr<campaign::NonPlayableCharacter>& npc) 0103 { 0104 colors.insert(npc->getColor()); 0105 }); 0106 0107 beginResetModel(); 0108 m_data= colors.values(); 0109 endResetModel(); 0110 }