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

0001 /***************************************************************************
0002  *  Copyright (C) 2019 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/profilemodel.h"
0021 
0022 #include <QFileInfo>
0023 #include <QSettings>
0024 
0025 #include "data/character.h"
0026 #include "data/player.h"
0027 #include "network/connectionprofile.h"
0028 
0029 #include <QDebug>
0030 
0031 ProfileModel::ProfileModel() {}
0032 
0033 ProfileModel::~ProfileModel()
0034 {
0035     m_connectionProfileList.clear();
0036 }
0037 
0038 QVariant ProfileModel::headerData(int, Qt::Orientation, int) const
0039 {
0040     return QVariant();
0041 }
0042 
0043 int ProfileModel::rowCount(const QModelIndex& parent) const
0044 {
0045     if(!parent.isValid())
0046         return static_cast<int>(m_connectionProfileList.size());
0047     return 0;
0048 }
0049 QVariant ProfileModel::data(const QModelIndex& index, int role) const
0050 {
0051     if(!index.isValid())
0052         return QVariant();
0053 
0054     QVariant var;
0055     switch(role)
0056     {
0057     case Qt::DisplayRole:
0058     case NameRole:
0059         var= m_connectionProfileList.at(static_cast<std::size_t>(index.row()))->profileTitle();
0060         break;
0061     case IndexRole:
0062         var= index.row();
0063         break;
0064     default:
0065         break;
0066     }
0067     return var;
0068 }
0069 
0070 void ProfileModel::appendProfile()
0071 {
0072     ConnectionProfile* profile= new ConnectionProfile();
0073     profile->setProfileTitle(QStringLiteral("Profile #%1").arg(m_connectionProfileList.size() + 1));
0074     appendProfile(profile);
0075 }
0076 void ProfileModel::appendProfile(ConnectionProfile* profile)
0077 {
0078     if(nullptr == profile)
0079         return;
0080 
0081     if(profile->playerId().isEmpty())
0082         profile->setPlayerId(QUuid::createUuid().toString(QUuid::WithoutBraces));
0083 
0084     auto idx= static_cast<int>(m_connectionProfileList.size());
0085 
0086     connect(profile, &ConnectionProfile::titleChanged, profile, [this, profile](){
0087         auto idx = indexOf(profile);
0088         emit dataChanged(index(idx,0),index(idx,0),{NameRole});
0089     });
0090 
0091     beginInsertRows(QModelIndex(), idx, idx);
0092     m_connectionProfileList.push_back(std::unique_ptr<ConnectionProfile>(std::move(profile)));
0093     endInsertRows();
0094 
0095     emit profileAdded(profile);
0096 }
0097 
0098 ConnectionProfile* ProfileModel::getProfile(const QModelIndex& index)
0099 {
0100     return getProfile(index.row());
0101 }
0102 
0103 int ProfileModel::cloneProfile(int index)
0104 {
0105     auto profileSrc= getProfile(index);
0106 
0107     if(nullptr == profileSrc)
0108         return -1;
0109 
0110     ConnectionProfile* clonedProfile= new ConnectionProfile();
0111     clonedProfile->cloneProfile(profileSrc);
0112     auto name= clonedProfile->profileTitle();
0113     clonedProfile->setProfileTitle(name.append(tr(" (clone)")));
0114     appendProfile(clonedProfile);
0115 
0116     return indexOf(clonedProfile);
0117 }
0118 
0119 int ProfileModel::indexOf(ConnectionProfile* tmp)
0120 {
0121     auto const& pos
0122         = std::find_if(m_connectionProfileList.begin(), m_connectionProfileList.end(),
0123                        [tmp](const std::unique_ptr<ConnectionProfile>& profile) { return tmp == profile.get(); });
0124 
0125     if(pos == m_connectionProfileList.end())
0126         return -1;
0127 
0128     auto idx= std::distance(m_connectionProfileList.begin(), pos);
0129 
0130     return static_cast<int>(idx);
0131 }
0132 ConnectionProfile* ProfileModel::getProfile(int index)
0133 {
0134     ConnectionProfile* profile= nullptr;
0135     auto idx= static_cast<std::size_t>(index);
0136     if((!m_connectionProfileList.empty()) && (m_connectionProfileList.size() > idx))
0137     {
0138         profile= m_connectionProfileList.at(idx).get();
0139     }
0140     return profile;
0141 }
0142 
0143 QHash<int, QByteArray> ProfileModel::roleNames() const
0144 {
0145     return {{NameRole, "name"}, {IndexRole, "uuid"}};
0146 }
0147 void ProfileModel::removeProfile(int index)
0148 {
0149     auto idx= static_cast<std::size_t>(index);
0150     if(m_connectionProfileList.size() <= idx)
0151         return;
0152 
0153     auto pos= m_connectionProfileList.begin() + index;
0154 
0155     beginRemoveRows(QModelIndex(), index, index);
0156     m_connectionProfileList.erase(pos);
0157     endRemoveRows();
0158 }
0159 
0160 Qt::ItemFlags ProfileModel::flags(const QModelIndex& index) const
0161 {
0162     if(!index.isValid())
0163         return Qt::NoItemFlags;
0164 
0165     return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
0166 }