File indexing completed on 2024-05-05 05:40:32
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 "network/characterdatamodel.h" 0021 0022 #include <QDebug> 0023 #include <QPixmap> 0024 #include <QUuid> 0025 0026 #include "network/connectionprofile.h" 0027 #include "utils/iohelper.h" 0028 #include "utils/logcategories.h" 0029 0030 CharacterDataModel::CharacterDataModel(QObject* parent) : QAbstractListModel(parent) {} 0031 0032 int CharacterDataModel::rowCount(const QModelIndex& parent) const 0033 { 0034 if(parent.isValid() || !m_profile) 0035 return 0; 0036 0037 return m_profile->characterCount(); 0038 } 0039 0040 QVariant CharacterDataModel::data(const QModelIndex& index, int role) const 0041 { 0042 if(!index.isValid() || !m_profile) 0043 return QVariant(); 0044 0045 auto character= m_profile->character(index.row()); 0046 QVariant var; 0047 0048 QSet<int> roles{AvatarData, Color, Name, Avatar, None}; 0049 0050 int col= role; 0051 if(!roles.contains(role)) 0052 { 0053 static std::vector<std::map<int, Role>> matrix( 0054 {{{Qt::BackgroundRole, Color}, {Qt::DecorationRole, Avatar}, {Qt::EditRole, AvatarData}}, 0055 {{Qt::BackgroundRole, Color}, {Qt::EditRole, Color}, {Qt::DisplayRole, Color}}, 0056 {{Qt::BackgroundRole, Color}, {Qt::DisplayRole, Name}, {Qt::EditRole, Name}}}); 0057 0058 if(role == Qt::TextAlignmentRole) 0059 { 0060 var= Qt::AlignCenter; 0061 } 0062 else 0063 { 0064 auto map= matrix[static_cast<std::size_t>(index.column())]; 0065 auto it= map.find(role); 0066 0067 if(it != map.end()) 0068 col= map[role]; 0069 } 0070 } 0071 0072 switch(col) 0073 { 0074 case Avatar: 0075 { 0076 auto pix= utils::IOHelper::dataToImage(character.m_avatarData); 0077 if(pix.isNull()) 0078 var= tr("No Avatar"); 0079 else 0080 var= QVariant::fromValue(pix.scaled(50, 50, Qt::KeepAspectRatio)); 0081 } 0082 break; 0083 case AvatarData: 0084 var= character.m_avatarData; 0085 break; 0086 case Name: 0087 var= character.m_name; 0088 break; 0089 case Color: 0090 var= QVariant::fromValue(character.m_color); 0091 break; 0092 case None: 0093 break; 0094 } 0095 return var; 0096 } 0097 0098 connection::CharacterData CharacterDataModel::character(int i) const 0099 { 0100 if(m_profile) 0101 return m_profile->character(i); 0102 else 0103 return {}; 0104 } 0105 /*bool CharacterDataModel::setData(const QModelIndex& index, const QVariant& value, int role) 0106 { 0107 if(m_profile.isNull()) 0108 return false; 0109 if(data(index, role) != value) 0110 { 0111 auto& character= m_profile->character(index.row()); 0112 switch(index.column()) 0113 { 0114 case 2: 0115 character.m_name= value.toString(); 0116 break; 0117 case 1: 0118 character.m_color= value.value<QColor>(); 0119 break; 0120 case 0: 0121 character.m_avatarData= value.toByteArray(); 0122 break; 0123 default: 0124 return false; 0125 } 0126 // m_profile->setCharacter(index.row(), character); 0127 emit dataChanged(index, index, QVector<int>() << Qt::DisplayRole << Qt::BackgroundRole << Qt::DecorationRole); 0128 return true; 0129 } 0130 return false; 0131 }*/ 0132 0133 Qt::ItemFlags CharacterDataModel::flags(const QModelIndex& index) const 0134 { 0135 if(!index.isValid()) 0136 return Qt::NoItemFlags; 0137 0138 return Qt::ItemIsSelectable | Qt::ItemIsEnabled; 0139 } 0140 0141 bool CharacterDataModel::insertCharacter() 0142 { 0143 if(m_profile.isNull()) 0144 return false; 0145 beginInsertRows(QModelIndex(), m_profile->characterCount(), m_profile->characterCount()); 0146 connection::CharacterData data( 0147 {QUuid::createUuid().toString(), tr("New Character"), Qt::lightGray, "", QHash<QString, QVariant>()}); 0148 m_profile->addCharacter(data); 0149 endInsertRows(); 0150 return true; 0151 } 0152 0153 bool CharacterDataModel::addCharacter(const connection::CharacterData& data) 0154 { 0155 if(m_profile.isNull()) 0156 return false; 0157 beginInsertRows(QModelIndex(), m_profile->characterCount(), m_profile->characterCount()); 0158 m_profile->addCharacter(data); 0159 endInsertRows(); 0160 return true; 0161 } 0162 0163 bool CharacterDataModel::removeCharacter(int index) 0164 { 0165 if(m_profile.isNull()) 0166 return false; 0167 if(m_profile->characterCount() < 1) 0168 return false; 0169 0170 if(index >= rowCount()) 0171 return false; 0172 0173 beginRemoveRows(QModelIndex(), index, index); 0174 m_profile->removeCharacter(index); 0175 endRemoveRows(); 0176 return true; 0177 } 0178 0179 void CharacterDataModel::setAvatar(int i, const QByteArray& img) 0180 { 0181 if(!m_profile || i < 0 || i >= m_profile->characterCount()) 0182 { 0183 qCDebug(logCategory::network) << "no profile in set avatar character data"; 0184 return; 0185 } 0186 0187 auto& character= m_profile->character(i); 0188 character.m_avatarData= img; 0189 emit dataChanged(index(i, 0), index(i, 0), {Avatar, AvatarData}); 0190 m_profile->characterHasChanged(); 0191 } 0192 0193 void CharacterDataModel::setName(int i, const QString& string) 0194 { 0195 if(!m_profile || i < 0 || i >= m_profile->characterCount()) 0196 { 0197 qCDebug(logCategory::network) << "no profile in set name character data"; 0198 return; 0199 } 0200 auto& character= m_profile->character(i); 0201 character.m_name= string; 0202 emit dataChanged(index(i, 0), index(i, 0), {Name}); 0203 m_profile->characterHasChanged(); 0204 } 0205 void CharacterDataModel::setColor(int i, const QColor& color) 0206 { 0207 if(!m_profile || i < 0 || i >= m_profile->characterCount()) 0208 { 0209 qCDebug(logCategory::network) << "no profile in set color character data"; 0210 return; 0211 } 0212 0213 auto& character= m_profile->character(i); 0214 character.m_color= color; 0215 emit dataChanged(index(i, 0), index(i, 0), {Color}); 0216 m_profile->characterHasChanged(); 0217 } 0218 0219 void CharacterDataModel::setProfile(ConnectionProfile* profile) 0220 { 0221 beginResetModel(); 0222 m_profile= profile; 0223 endResetModel(); 0224 } 0225 0226 QHash<int, QByteArray> CharacterDataModel::roleNames() const 0227 { 0228 return {{None, "none"}, {Avatar, "avatar"}, {Color, "color"}, {AvatarData, "avatarData"}, {Name, "name"}}; 0229 }