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