File indexing completed on 2024-05-19 05:40:50

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 "updater/vmapitem/pathcontrollerupdater.h"
0021 
0022 #include <QVariant>
0023 
0024 #include "controller/item_controllers/pathcontroller.h"
0025 #include "network/networkmessagereader.h"
0026 #include "network/networkmessagewriter.h"
0027 #include "worker/messagehelper.h"
0028 
0029 PathControllerUpdater::PathControllerUpdater() {}
0030 
0031 void PathControllerUpdater::addItemController(vmap::VisualItemController* ctrl, bool sendOff)
0032 {
0033     if(nullptr == ctrl)
0034         return;
0035 
0036     auto pathCtrl= dynamic_cast<vmap::PathController*>(ctrl);
0037 
0038     if(nullptr == pathCtrl)
0039         return;
0040 
0041     VMapItemControllerUpdater::addItemController(ctrl);
0042 
0043     connect(pathCtrl, &vmap::PathController::pointCountChanged, this,
0044             [this, pathCtrl]() { sendOffVMapChanges<std::vector<QPointF>>(pathCtrl, QStringLiteral("points")); });
0045     connect(pathCtrl, &vmap::PathController::penWidthChanged, this,
0046             [this, pathCtrl]() { sendOffVMapChanges<quint16>(pathCtrl, QStringLiteral("penWidth")); });
0047     connect(pathCtrl, &vmap::PathController::closedChanged, this,
0048             [this, pathCtrl]() { sendOffVMapChanges<bool>(pathCtrl, QStringLiteral("closed")); });
0049     connect(pathCtrl, &vmap::PathController::filledChanged, this,
0050             [this, pathCtrl]() { sendOffVMapChanges<bool>(pathCtrl, QStringLiteral("filled")); });
0051 
0052     connect(pathCtrl, &vmap::PathController::pointPositionEditFinished, this,
0053             [pathCtrl](int idx, const QPointF& pos)
0054             {
0055                 if(idx < 0)
0056                     return;
0057                 NetworkMessageWriter msg(NetMsg::VMapCategory, NetMsg::MovePoint);
0058                 msg.string8(pathCtrl->mapUuid());
0059                 msg.uint8(pathCtrl->itemType());
0060                 msg.string8(pathCtrl->uuid());
0061                 msg.int64(idx);
0062                 msg.real(pos.x());
0063                 msg.real(pos.y());
0064                 msg.sendToServer();
0065             });
0066 
0067     if(!ctrl->remote() && sendOff)
0068     {
0069         if(pathCtrl->penLine())
0070             connect(pathCtrl, &vmap::PathController::initializedChanged, this,
0071                     [pathCtrl]() { MessageHelper::sendOffPath(pathCtrl, pathCtrl->mapUuid()); });
0072         else
0073             MessageHelper::sendOffPath(pathCtrl, pathCtrl->mapUuid());
0074     }
0075 }
0076 
0077 bool PathControllerUpdater::movePoint(NetworkMessageReader* msg, vmap::PathController* ctrl)
0078 {
0079     if(nullptr == msg || nullptr == ctrl)
0080         return false;
0081 
0082     auto idx= msg->int64();
0083     auto x= msg->real();
0084     auto y= msg->real();
0085 
0086     ctrl->setPoint(QPointF(x, y), static_cast<int>(idx));
0087     return true;
0088 }
0089 
0090 bool PathControllerUpdater::updateItemProperty(NetworkMessageReader* msg, vmap::VisualItemController* ctrl)
0091 {
0092     if(nullptr == msg || nullptr == ctrl)
0093         return false;
0094 
0095     // TODO implement save/restore
0096     auto datapos= msg->pos();
0097 
0098     if(VMapItemControllerUpdater::updateItemProperty(msg, ctrl))
0099         return true;
0100 
0101     msg->resetToPos(datapos);
0102 
0103     updatingCtrl= ctrl;
0104 
0105     auto property= msg->string16();
0106 
0107     QVariant var;
0108     if(property == QStringLiteral("penWidth"))
0109     {
0110         var= QVariant::fromValue(msg->uint16());
0111     }
0112     else if(property == QStringLiteral("closed"))
0113     {
0114         var= QVariant::fromValue(static_cast<bool>(msg->uint8()));
0115     }
0116     else if(property == QStringLiteral("filled"))
0117     {
0118         var= QVariant::fromValue(static_cast<bool>(msg->uint8()));
0119     }
0120     else if(property == QStringLiteral("points"))
0121     {
0122         auto count= msg->int64();
0123         std::vector<QPointF> points;
0124         points.reserve(static_cast<std::size_t>(count));
0125         for(int i= 0; i < count; ++i)
0126         {
0127             auto x= msg->real();
0128             auto y= msg->real();
0129             points.push_back(QPointF(x, y));
0130         }
0131         var= QVariant::fromValue(points);
0132     }
0133     m_updatingFromNetwork= true;
0134     auto feedback= ctrl->setProperty(property.toLocal8Bit().data(), var);
0135     m_updatingFromNetwork= false;
0136     updatingCtrl= nullptr;
0137 
0138     return feedback;
0139 }