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