File indexing completed on 2024-05-19 05:40:35
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 "controller/item_controllers/visualitemcontroller.h" 0021 0022 #include <QDebug> 0023 #include <QUuid> 0024 #include <QVariant> 0025 0026 #include "controller/view_controller/vectorialmapcontroller.h" 0027 #include "worker/utilshelper.h" 0028 0029 namespace vmap 0030 { 0031 0032 VisualItemController::VisualItemController(ItemType itemType, const std::map<QString, QVariant>& params, 0033 VectorialMapController* ctrl, QObject* parent) 0034 : QObject(nullptr), m_ctrl(ctrl), m_itemType(itemType), m_uuid(QUuid::createUuid().toString(QUuid::WithoutBraces)) 0035 { 0036 Q_UNUSED(parent) 0037 if(m_ctrl) 0038 { 0039 connect(m_ctrl, &VectorialMapController::layerChanged, this, &VisualItemController::selectableChanged); 0040 connect(m_ctrl, &VectorialMapController::visibilityChanged, this, &VisualItemController::visibilityChanged); 0041 connect(m_ctrl, &VectorialMapController::localGMChanged, this, &VisualItemController::localIsGMChanged); 0042 0043 connect(m_ctrl, &VectorialMapController::permissionChanged, this, &VisualItemController::computeEditable); 0044 connect(m_ctrl, &VectorialMapController::localGMChanged, this, &VisualItemController::computeEditable); 0045 connect(m_ctrl, &VectorialMapController::layerChanged, this, &VisualItemController::computeEditable); 0046 0047 setLayer(m_ctrl->layer()); 0048 } 0049 connect(this, &VisualItemController::lockedChanged, this, &VisualItemController::computeEditable); 0050 connect(this, &VisualItemController::removedChanged, this, &VisualItemController::visibleChanged); 0051 0052 initializedVisualItem(params); 0053 0054 computeEditable(); 0055 0056 connect(this, &VisualItemController::colorChanged, this, [this] { setModified(); }); 0057 connect(this, &VisualItemController::opacityChanged, this, [this] { setModified(); }); 0058 connect(this, &VisualItemController::rotationChanged, this, [this] { setModified(); }); 0059 connect(this, &VisualItemController::posChanged, this, [this] { setModified(); }); 0060 connect(this, &VisualItemController::layerChanged, this, 0061 [this] 0062 { 0063 setOpacity(layer() == Core::Layer::GAMEMASTER_LAYER ? 0.5 : 1.0); 0064 computeEditable(); 0065 setModified(); 0066 }); 0067 connect(this, &VisualItemController::lockedChanged, this, [this] { setModified(); }); 0068 } 0069 0070 VisualItemController::~VisualItemController() {} 0071 0072 void VisualItemController::initializedVisualItem(const std::map<QString, QVariant>& params) 0073 { 0074 if(params.empty()) 0075 return; 0076 namespace hu= helper::utils; 0077 using std::placeholders::_1; 0078 0079 // clang-format off 0080 hu::setParamIfAny<QString>(Core::vmapkeys::KEY_UUID, params, std::bind(&VisualItemController::setUuid, this, _1)); 0081 hu::setParamIfAny<bool>(Core::vmapkeys::KEY_VISIBLE, params, std::bind(&VisualItemController::setVisible, this, _1)); 0082 hu::setParamIfAny<bool>(Core::vmapkeys::KEY_INITIALIZED, params, std::bind(&VisualItemController::setInitialized, this, _1)); 0083 hu::setParamIfAny<qreal>(Core::vmapkeys::KEY_OPACITY, params, std::bind(&VisualItemController::setOpacity, this, _1)); 0084 hu::setParamIfAny<qreal>(Core::vmapkeys::KEY_ROTATION, params, std::bind(&VisualItemController::setRotation, this, _1)); 0085 hu::setParamIfAny<Core::Layer>(Core::vmapkeys::KEY_LAYER, params, std::bind(&VisualItemController::setLayer, this, _1)); 0086 hu::setParamIfAny<QPointF>(Core::vmapkeys::KEY_POS, params, std::bind(&VisualItemController::setPos, this, _1)); 0087 hu::setParamIfAny<QColor>(Core::vmapkeys::KEY_COLOR, params, std::bind(&VisualItemController::setColor, this, _1)); 0088 hu::setParamIfAny<bool>(Core::vmapkeys::KEY_LOCKED, params, std::bind(&VisualItemController::setLocked, this, _1)); 0089 // clang-format on 0090 } 0091 0092 QPointer<VectorialMapController> VisualItemController::mapController() const 0093 { 0094 return m_ctrl; 0095 } 0096 0097 bool VisualItemController::selected() const 0098 { 0099 return m_selected; 0100 } 0101 0102 bool VisualItemController::localIsGM() const 0103 { 0104 return m_ctrl->localGM(); 0105 } 0106 0107 bool VisualItemController::initialized() const 0108 { 0109 return m_initialized; 0110 } 0111 0112 Core::SelectableTool VisualItemController::tool() const 0113 { 0114 return m_tool; 0115 } 0116 0117 QColor VisualItemController::color() const 0118 { 0119 return m_color; 0120 } 0121 0122 VisualItemController::ItemType VisualItemController::itemType() const 0123 { 0124 return m_itemType; 0125 } 0126 0127 bool VisualItemController::remote() const 0128 { 0129 return m_remote; 0130 } 0131 0132 QPointF VisualItemController::rotationOriginPoint() const 0133 { 0134 return rect().center(); 0135 } 0136 0137 bool VisualItemController::editable() const 0138 { 0139 return (selectable() & m_editable); 0140 } 0141 0142 bool VisualItemController::selectable() const 0143 { 0144 return (m_ctrl->layer() == m_layer); 0145 } 0146 0147 bool VisualItemController::visible() const 0148 { 0149 return (m_visible && !m_removed); 0150 } 0151 0152 qreal VisualItemController::opacity() const 0153 { 0154 return m_opacity; 0155 } 0156 Core::VisibilityMode VisualItemController::visibility() const 0157 { 0158 return m_ctrl->visibility(); 0159 } 0160 Core::Layer VisualItemController::layer() const 0161 { 0162 return m_layer; 0163 } 0164 0165 QPointF VisualItemController::pos() const 0166 { 0167 return m_pos; 0168 } 0169 0170 QString VisualItemController::uuid() const 0171 { 0172 return m_uuid; 0173 } 0174 0175 qreal VisualItemController::rotation() const 0176 { 0177 return m_rotation; 0178 } 0179 0180 int VisualItemController::gridSize() const 0181 { 0182 return m_ctrl->gridSize(); 0183 } 0184 0185 bool VisualItemController::locked() const 0186 { 0187 return m_locked; 0188 } 0189 0190 QString VisualItemController::getLayerText(Core::Layer layer) const 0191 { 0192 return m_ctrl->layerToText(layer); 0193 } 0194 0195 void VisualItemController::endGeometryChange() 0196 { 0197 if(m_posEditing) 0198 { 0199 emit posEditFinished(); 0200 m_posEditing= false; 0201 } 0202 0203 if(m_rotationEditing) 0204 { 0205 emit rotationEditFinished(); 0206 m_rotationEditing= false; 0207 } 0208 setInitialized(true); 0209 } 0210 0211 const QString VisualItemController::mapUuid() const 0212 { 0213 return m_ctrl->uuid(); 0214 } 0215 0216 void VisualItemController::setColor(const QColor& color) 0217 { 0218 if(m_color == color) 0219 return; 0220 0221 m_color= color; 0222 emit colorChanged(m_color); 0223 } 0224 0225 void VisualItemController::setSelected(bool b) 0226 { 0227 if(b == m_selected) 0228 return; 0229 m_selected= b; 0230 emit selectedChanged(m_selected); 0231 } 0232 0233 void VisualItemController::setUuid(QString uuid) 0234 { 0235 if(uuid == m_uuid) 0236 return; 0237 m_uuid= uuid; 0238 emit uuidChanged(); 0239 } 0240 0241 void VisualItemController::setRotation(qreal rota) 0242 { 0243 if(qFuzzyCompare(rota, m_rotation)) 0244 return; 0245 m_rotation= rota; 0246 emit rotationChanged(m_rotation); 0247 m_rotationEditing= true; 0248 } 0249 0250 void VisualItemController::setEditable(bool b) 0251 { 0252 if(b == m_editable) 0253 return; 0254 m_editable= b; 0255 emit editableChanged(); 0256 } 0257 0258 void VisualItemController::setVisible(bool b) 0259 { 0260 if(b == m_visible) 0261 return; 0262 m_visible= b; 0263 emit visibleChanged(); 0264 } 0265 0266 void VisualItemController::setOpacity(qreal b) 0267 { 0268 if(qFuzzyCompare(b, m_opacity)) 0269 return; 0270 m_opacity= b; 0271 emit opacityChanged(); 0272 } 0273 0274 void VisualItemController::setLayer(Core::Layer layer) 0275 { 0276 if(layer == m_layer) 0277 return; 0278 m_layer= layer; 0279 emit layerChanged(); 0280 emit selectableChanged(); 0281 } 0282 0283 void VisualItemController::setPos(const QPointF& pos) 0284 { 0285 if(m_pos == pos) 0286 return; 0287 m_pos= pos; 0288 m_posEditing= true; 0289 emit posChanged(m_pos); 0290 } 0291 void VisualItemController::setLocked(bool locked) 0292 { 0293 if(m_locked == locked) 0294 return; 0295 0296 m_locked= locked; 0297 emit lockedChanged(m_locked); 0298 } 0299 void VisualItemController::setInitialized(bool b) 0300 { 0301 if(b == m_initialized) 0302 return; 0303 m_initialized= b; 0304 emit initializedChanged(m_initialized); 0305 } 0306 0307 void VisualItemController::setRemote(bool b) 0308 { 0309 if(b == m_remote) 0310 return; 0311 m_remote= b; 0312 emit remoteChanged(m_remote); 0313 } 0314 0315 void VisualItemController::setModified() 0316 { 0317 emit modifiedChanged(); 0318 } 0319 0320 void VisualItemController::computeEditable() 0321 { 0322 if(!m_ctrl) 0323 return; 0324 auto editableByPermission= (localIsGM() || m_ctrl->permission() == Core::PermissionMode::PC_ALL); 0325 auto sameLayer= m_ctrl->layer() == layer(); 0326 setEditable(!m_locked && sameLayer && editableByPermission); 0327 } 0328 0329 qreal VisualItemController::zOrder() const 0330 { 0331 return m_zOrder; 0332 } 0333 0334 void VisualItemController::setZOrder(qreal newZOrder) 0335 { 0336 if(qFuzzyCompare(m_zOrder, newZOrder)) 0337 return; 0338 m_zOrder= newZOrder; 0339 emit zOrderChanged(m_zOrder); 0340 } 0341 0342 bool VisualItemController::removed() const 0343 { 0344 return m_removed; 0345 } 0346 0347 void VisualItemController::setRemoved(bool newRemoved) 0348 { 0349 if(m_removed == newRemoved) 0350 return; 0351 m_removed= newRemoved; 0352 emit removedChanged(); 0353 } 0354 0355 QString VisualItemController::parentUuid() const 0356 { 0357 return m_parentUuid; 0358 } 0359 0360 void VisualItemController::setParentUuid(const QString &newParentUuid) 0361 { 0362 if (m_parentUuid == newParentUuid) 0363 return; 0364 m_parentUuid = newParentUuid; 0365 emit parentUuidChanged(); 0366 } 0367 0368 } // namespace vmap