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

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 "model/vmapitemmodel.h"
0021 
0022 #include <QDebug>
0023 #include <set>
0024 
0025 namespace vmap
0026 {
0027 VmapItemModel::VmapItemModel(QObject* parent) : QAbstractListModel(parent) {}
0028 
0029 VmapItemModel::~VmapItemModel()= default;
0030 
0031 int VmapItemModel::rowCount(const QModelIndex& parent) const
0032 {
0033     if(!parent.isValid())
0034         return 0;
0035 
0036     return static_cast<int>(m_items.size());
0037 }
0038 
0039 QVariant VmapItemModel::data(const QModelIndex& index, int role) const
0040 {
0041     if(!index.isValid())
0042         return QVariant();
0043 
0044     std::set<int> allowedRoles({Qt::DisplayRole, IdRole, SelectedRole, EditableRole, SelectableRole, VisibleRole,
0045                                 OpacityRole, RotationRole, LayerRole, PositionRole, LocalGmRole, ColorRole, LockedRole,
0046                                 ToolRole, TypeRole, InitializedRole, ControllerRole});
0047 
0048     if(allowedRoles.find(role) == allowedRoles.end())
0049         return QVariant();
0050 
0051     auto item= m_items.at(static_cast<std::size_t>(index.row())).get();
0052 
0053     if(nullptr == item)
0054         return {};
0055 
0056     QVariant var;
0057     switch(role)
0058     {
0059     case Qt::DisplayRole:
0060     case IdRole:
0061         var= item->uuid();
0062         break;
0063     case SelectedRole:
0064         var= item->selected();
0065         break;
0066     case EditableRole:
0067         var= item->editable();
0068         break;
0069     case SelectableRole:
0070         var= item->selectable();
0071         break;
0072     case VisibleRole:
0073         var= item->visible();
0074         break;
0075     case OpacityRole:
0076         var= item->opacity();
0077         break;
0078     case RotationRole:
0079         var= item->rotation();
0080         break;
0081     case LayerRole:
0082         var= QVariant::fromValue(item->layer());
0083         break;
0084     case PositionRole:
0085         var= item->pos();
0086         break;
0087     case LocalGmRole:
0088         var= item->localIsGM();
0089         break;
0090     case ColorRole:
0091         var= item->color();
0092         break;
0093     case LockedRole:
0094         var= item->locked();
0095         break;
0096     case ToolRole:
0097         var= item->tool();
0098         break;
0099     case TypeRole:
0100         var= item->itemType();
0101         break;
0102     case InitializedRole:
0103         var= item->initialized();
0104         break;
0105     case ControllerRole:
0106         var= QVariant::fromValue(item);
0107         break;
0108     }
0109 
0110     return var;
0111 }
0112 
0113 bool vmap::VmapItemModel::appendItemController(vmap::VisualItemController* item)
0114 {
0115     if(!item)
0116         return false;
0117 
0118     if(std::end(m_items)
0119        != std::find_if(std::begin(m_items), std::end(m_items),
0120                        [item](const std::unique_ptr<vmap::VisualItemController>& ctrl)
0121                        { return item->uuid() == ctrl->uuid(); }))
0122         return false;
0123 
0124     auto size= static_cast<int>(m_items.size());
0125     std::unique_ptr<vmap::VisualItemController> ctrl(item);
0126     beginInsertRows(QModelIndex(), size, size);
0127     m_items.push_back(std::move(ctrl));
0128     endInsertRows();
0129 
0130     connect(item, &vmap::VisualItemController::modifiedChanged, this, &vmap::VmapItemModel::modifiedChanged);
0131     if(item->tool() == Core::SelectableTool::NonPlayableCharacter)
0132         emit npcAdded();
0133     emit itemControllerAdded(item);
0134     return true;
0135 }
0136 
0137 void vmap::VmapItemModel::setModifiedToAllItem()
0138 {
0139     std::for_each(std::begin(m_items), std::end(m_items),
0140                   [](const std::unique_ptr<vmap::VisualItemController>& itemCtrl) { itemCtrl->setModified(); });
0141 }
0142 
0143 bool vmap::VmapItemModel::removeItemController(const QSet<QString>& ids, bool fromNetwork)
0144 {
0145     auto s= m_items.size();
0146 
0147     beginResetModel();
0148     m_items.erase(std::remove_if(std::begin(m_items), std::end(m_items),
0149                                  [ids](const std::unique_ptr<vmap::VisualItemController>& itemCtrl)
0150                                  {
0151                                      auto res= ids.contains(itemCtrl->uuid());
0152                                      if(res)
0153                                          itemCtrl->aboutToBeRemoved();
0154                                      return res;
0155                                  }),
0156                   std::end(m_items));
0157     endResetModel();
0158 
0159     if(!fromNetwork)
0160         emit itemControllersRemoved(ids.values());
0161     return s > m_items.size();
0162 }
0163 
0164 void vmap::VmapItemModel::clearData()
0165 {
0166     beginResetModel();
0167     m_items.clear();
0168     endResetModel();
0169 }
0170 
0171 std::vector<VisualItemController*> VmapItemModel::items() const
0172 {
0173     std::vector<VisualItemController*> vec;
0174     vec.reserve(m_items.size());
0175 
0176     std::transform(std::begin(m_items), std::end(m_items), std::back_inserter(vec),
0177                    [](const std::unique_ptr<vmap::VisualItemController>& item) { return item.get(); });
0178 
0179     return vec;
0180 }
0181 
0182 VisualItemController* VmapItemModel::item(const QString& id) const
0183 {
0184     auto it= std::find_if(std::begin(m_items), std::end(m_items),
0185                           [id](const std::unique_ptr<vmap::VisualItemController>& itemCtrl)
0186                           { return id == itemCtrl->uuid(); });
0187 
0188     if(it == std::end(m_items))
0189         return nullptr;
0190 
0191     return it->get();
0192 }
0193 
0194 } // namespace vmap