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

0001 /***************************************************************************
0002  *  Copyright (C) 2009 by Renaud Guezennec                             *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   Rolisteam 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/dicealiasmodel.h"
0021 
0022 #include <QColor>
0023 #include <QJsonArray>
0024 #include <QJsonObject>
0025 #include <QObject>
0026 
0027 #include "diceparser/dicealias.h"
0028 
0029 DiceAliasModel::DiceAliasModel(QObject* parent) : QAbstractListModel(parent)
0030 {
0031     m_header << tr("Pattern") << tr("Value") << tr("Regular Expression") << tr("Disable") << tr("Comments");
0032 }
0033 
0034 DiceAliasModel::~DiceAliasModel()
0035 {
0036     /* if(m_diceAliasList!=nullptr)
0037      {
0038          qDeleteAll(*m_diceAliasList);
0039          delete m_diceAliasList;
0040          m_diceAliasList = nullptr;
0041      }*/
0042 }
0043 
0044 QVariant DiceAliasModel::data(const QModelIndex& index, int role) const
0045 {
0046 
0047     if(!index.isValid())
0048         return {};
0049 
0050     auto const& diceAlias= m_diceAliasList.at(index.row());
0051 
0052     if(!diceAlias)
0053         return {};
0054 
0055     if((Qt::DisplayRole == role) || (Qt::EditRole == role))
0056     {
0057         if(index.column() == PATTERN)
0058         {
0059             return diceAlias->pattern();
0060         }
0061         else if(index.column() == COMMAND)
0062         {
0063             return diceAlias->command();
0064         }
0065         else if(index.column() == METHOD)
0066         {
0067             return !diceAlias->isReplace();
0068         }
0069         else if(index.column() == DISABLE)
0070         {
0071             return !diceAlias->isEnable();
0072         }
0073         else if(index.column() == COMMENT)
0074         {
0075             return diceAlias->comment();
0076         }
0077     }
0078     else if(Qt::BackgroundRole == role)
0079     {
0080         if(!diceAlias->isEnable())
0081         {
0082             return QColor(Qt::red).lighter();
0083         }
0084     }
0085     else if(Qt::TextAlignmentRole == role)
0086     {
0087         if(index.column() == METHOD)
0088         {
0089             return Qt::AlignCenter;
0090         }
0091     }
0092 
0093     return QVariant();
0094 }
0095 int DiceAliasModel::rowCount(const QModelIndex& parent) const
0096 {
0097     if(!parent.isValid())
0098         return static_cast<int>(m_diceAliasList.size());
0099     return 0;
0100 }
0101 int DiceAliasModel::columnCount(const QModelIndex&) const
0102 {
0103     return m_header.size();
0104 }
0105 QVariant DiceAliasModel::headerData(int section, Qt::Orientation orientation, int role) const
0106 {
0107     if(Qt::DisplayRole == role)
0108     {
0109         if(orientation == Qt::Horizontal)
0110         {
0111             return m_header.at(section);
0112         }
0113     }
0114     return QVariant();
0115 }
0116 
0117 /*void DiceAliasModel::setAliases(QList<DiceAlias*>* lst)
0118 {
0119     m_diceAliasList= lst;
0120 }
0121 void DiceAliasModel::appendAlias()
0122 {
0123     addAlias(new DiceAlias(tr("New Alias%1").arg(m_diceAliasList->size()), ""));
0124 }*/
0125 
0126 /*void DiceAliasModel::preferencesHasChanged(const QString& pref)
0127 {
0128     if(pref == "isPlayer")
0129     {
0130         m_isGM= !PreferencesManager::getInstance()->value(pref, true).toBool();
0131     }
0132 }*/
0133 
0134 QString DiceAliasModel::convert(const QString& str)
0135 {
0136     auto res= str;
0137     for(auto const& alias : m_diceAliasList)
0138     {
0139         alias->resolved(res);
0140     }
0141     return res;
0142 }
0143 
0144 void DiceAliasModel::appendAlias(DiceAlias&& alias)
0145 {
0146     beginInsertRows(QModelIndex(), m_diceAliasList.size(), m_diceAliasList.size());
0147     m_diceAliasList.push_back(std::make_unique<DiceAlias>(alias));
0148     endInsertRows();
0149 
0150     auto const& it= m_diceAliasList.end() - 1;
0151     emit aliasAdded((*it).get(), std::distance(m_diceAliasList.begin(), it));
0152 }
0153 Qt::ItemFlags DiceAliasModel::flags(const QModelIndex& index) const
0154 {
0155     if(!index.isValid())
0156         return Qt::NoItemFlags;
0157 
0158     return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
0159 }
0160 bool DiceAliasModel::setData(const QModelIndex& index, const QVariant& value, int role)
0161 {
0162     bool result= false;
0163     if(!index.isValid())
0164         return result;
0165 
0166     auto const& diceAlias= m_diceAliasList[index.row()];
0167     if(role == Qt::EditRole)
0168     {
0169         switch(index.column())
0170         {
0171         case PATTERN:
0172             diceAlias->setPattern(value.toString());
0173             result= true;
0174             break;
0175         case COMMAND:
0176             diceAlias->setCommand(value.toString());
0177             result= true;
0178             break;
0179         case METHOD:
0180             if(value.toBool())
0181             {
0182                 diceAlias->setType(DiceAlias::REGEXP);
0183             }
0184             else
0185             {
0186                 diceAlias->setType(DiceAlias::REPLACE);
0187             }
0188             result= true;
0189             break;
0190         case DISABLE:
0191             diceAlias->setEnable(!value.toBool());
0192             result= true;
0193             break;
0194         case COMMENT:
0195             diceAlias->setComment(value.toString());
0196             result= true;
0197             break;
0198         }
0199     }
0200 
0201     if(result)
0202     {
0203         emit dataChanged(index, index, QVector<int>() << role);
0204         emit aliasChanged(diceAlias.get(), index.row());
0205     }
0206 
0207     return result;
0208 }
0209 
0210 const std::vector<std::unique_ptr<DiceAlias>>& DiceAliasModel::aliases() const
0211 {
0212     return m_diceAliasList;
0213 }
0214 
0215 void DiceAliasModel::deleteAlias(const QModelIndex& index)
0216 {
0217     if(!index.isValid())
0218         return;
0219 
0220     beginRemoveRows(QModelIndex(), index.row(), index.row());
0221     auto const& it= m_diceAliasList.begin() + index.row();
0222     m_diceAliasList.erase(it);
0223     endRemoveRows();
0224 
0225     emit aliasRemoved(index.row());
0226 
0227     /*NetworkMessageWriter msg(NetMsg::SharePreferencesCategory, NetMsg::removeDiceAlias);
0228     msg.int64(index.row());
0229     msg.sendToServer();*/
0230 }
0231 void DiceAliasModel::upAlias(const QModelIndex& index)
0232 {
0233     if(!index.isValid())
0234         return;
0235     if(index.row() == 0)
0236         return;
0237     if(beginMoveRows(QModelIndex(), index.row(), index.row(), QModelIndex(), index.row() - 1))
0238     {
0239         std::iter_swap(m_diceAliasList.begin() + index.row(), m_diceAliasList.begin() + index.row() - 1);
0240         // m_diceAliasList->swapItemsAt(index.row(), index.row() - 1);
0241         endMoveRows();
0242         emit aliasMoved(index.row(), index.row() - 1);
0243     }
0244 }
0245 
0246 void DiceAliasModel::downAlias(const QModelIndex& index)
0247 {
0248     if(!index.isValid())
0249         return;
0250 
0251     if(index.row() == static_cast<int>(m_diceAliasList.size()) - 1)
0252         return;
0253 
0254     if(beginMoveRows(QModelIndex(), index.row(), index.row(), QModelIndex(), index.row() + 2))
0255     {
0256         std::iter_swap(m_diceAliasList.begin() + index.row(), m_diceAliasList.begin() + index.row() + 1);
0257         endMoveRows();
0258         emit aliasMoved(index.row(), index.row() + 1);
0259     }
0260 }
0261 
0262 void DiceAliasModel::topAlias(const QModelIndex& index)
0263 {
0264     if(!index.isValid())
0265         return;
0266 
0267     if(index.row() == 0)
0268         return;
0269     if(beginMoveRows(QModelIndex(), index.row(), index.row(), QModelIndex(), 0))
0270     {
0271         std::iter_swap(m_diceAliasList.begin(), m_diceAliasList.begin() + index.row());
0272         endMoveRows();
0273         emit aliasMoved(index.row(), 0);
0274     }
0275 }
0276 
0277 void DiceAliasModel::bottomAlias(const QModelIndex& index)
0278 {
0279     if(!index.isValid())
0280         return;
0281     auto size= static_cast<int>(m_diceAliasList.size());
0282     if(index.row() == size - 1)
0283         return;
0284     if(beginMoveRows(QModelIndex(), index.row(), index.row(), QModelIndex(), size))
0285     {
0286         std::iter_swap(m_diceAliasList.end() - 1, m_diceAliasList.begin() + index.row());
0287         endMoveRows();
0288         emit aliasMoved(index.row(), size);
0289     }
0290 }
0291 
0292 /*void DiceAliasModel::setGM(bool b)
0293 {
0294     m_isGM= b;
0295 }*/
0296 
0297 void DiceAliasModel::clear()
0298 {
0299     beginResetModel();
0300     m_diceAliasList.clear();
0301     endResetModel();
0302 }
0303 
0304 /*void DiceAliasModel::load(const QJsonObject& obj)
0305 {
0306     QJsonArray aliases= obj["aliases"].toArray();
0307 
0308     for(auto aliasRef : aliases)
0309     {
0310         auto alias= aliasRef.toObject();
0311         auto da= new DiceAlias(alias["command"].toString(), alias["value"].toString(), alias["replace"].toBool(),
0312                                alias["enable"].toBool());
0313         da->setComment(alias["comment"].toString());
0314         addAlias(da);
0315     }
0316 }
0317 
0318 void DiceAliasModel::save(QJsonObject& obj)
0319 {
0320     QJsonArray dices;
0321     for(auto& dice : *m_diceAliasList)
0322     {
0323         QJsonObject diceObj;
0324         diceObj["command"]= dice->getCommand();
0325         diceObj["value"]= dice->getValue();
0326         diceObj["replace"]= dice->isReplace();
0327         diceObj["enable"]= dice->isEnable();
0328         diceObj["comment"]= dice->getComment();
0329         dices.append(diceObj);
0330     }
0331     obj["aliases"]= dices;
0332 }
0333 void DiceAliasModel::moveAlias(int from, int to)
0334 {
0335     if(!m_isGM)
0336         return;
0337 
0338     NetworkMessageWriter msg(NetMsg::SharePreferencesCategory, NetMsg::moveDiceAlias);
0339     msg.int64(from);
0340     msg.int64(to);
0341     msg.sendToServer();
0342 }*/