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/linecontrollerupdater.h"
0021 
0022 #include <QDebug>
0023 #include <QMetaObject>
0024 #include <QMetaProperty>
0025 #include <QSet>
0026 
0027 #include "controller/item_controllers/linecontroller.h"
0028 #include "network/networkmessagereader.h"
0029 #include "worker/convertionhelper.h"
0030 #include "worker/messagehelper.h"
0031 
0032 LineControllerUpdater::LineControllerUpdater() {}
0033 
0034 void LineControllerUpdater::addItemController(vmap::VisualItemController* ctrl, bool sendOff)
0035 {
0036     if(nullptr == ctrl)
0037         return;
0038 
0039     auto lineCrtl= dynamic_cast<vmap::LineController*>(ctrl);
0040 
0041     if(nullptr == lineCrtl)
0042         return;
0043 
0044     VMapItemControllerUpdater::addItemController(lineCrtl);
0045 
0046     connect(lineCrtl, &vmap::LineController::endPointEditFinished, this,
0047             [this, lineCrtl]() { sendOffVMapChanges<QPointF>(lineCrtl, QStringLiteral("endPoint")); });
0048     connect(lineCrtl, &vmap::LineController::startPointEditFinished, this,
0049             [this, lineCrtl]() { sendOffVMapChanges<QPointF>(lineCrtl, QStringLiteral("startPoint")); });
0050     connect(lineCrtl, &vmap::LineController::penWidthChanged, this,
0051             [this, lineCrtl]() { sendOffVMapChanges<quint16>(lineCrtl, QStringLiteral("penWidth")); });
0052 
0053     if(!ctrl->remote() && sendOff)
0054         connect(lineCrtl, &vmap::LineController::initializedChanged, this,
0055                 [lineCrtl]() { MessageHelper::sendOffLine(lineCrtl, lineCrtl->mapUuid()); });
0056 }
0057 
0058 bool LineControllerUpdater::updateItemProperty(NetworkMessageReader* msg, vmap::VisualItemController* ctrl)
0059 {
0060     if(nullptr == msg || nullptr == ctrl)
0061         return false;
0062 
0063     // TODO implement save/restore
0064     auto datapos= msg->pos();
0065 
0066     if(VMapItemControllerUpdater::updateItemProperty(msg, ctrl))
0067         return true;
0068 
0069     msg->resetToPos(datapos);
0070 
0071     updatingCtrl= ctrl;
0072 
0073     auto property= msg->string16();
0074 
0075     QVariant var;
0076 
0077     if(property == QStringLiteral("endPoint"))
0078     {
0079         auto x= msg->real();
0080         auto y= msg->real();
0081         var= QVariant::fromValue(QPointF(x, y));
0082     }
0083     else if(property == QStringLiteral("startPoint"))
0084     {
0085         auto x= msg->real();
0086         auto y= msg->real();
0087         var= QVariant::fromValue(QPointF(x, y));
0088     }
0089     else if(property == QStringLiteral("penWidth"))
0090     {
0091         var= QVariant::fromValue(msg->int64());
0092     }
0093     else
0094     {
0095         return false;
0096     }
0097 
0098     m_updatingFromNetwork= true;
0099     auto feedback= ctrl->setProperty(property.toLocal8Bit().data(), var);
0100     m_updatingFromNetwork= false;
0101     updatingCtrl= nullptr;
0102 
0103     return feedback;
0104 }