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/textcontrollerupdater.h"
0021 
0022 #include "controller/item_controllers/textcontroller.h"
0023 #include "network/networkmessagereader.h"
0024 #include "worker/messagehelper.h"
0025 
0026 TextControllerUpdater::TextControllerUpdater(QObject* parent) : VMapItemControllerUpdater(parent) {}
0027 
0028 void TextControllerUpdater::addItemController(vmap::VisualItemController* ctrl, bool sendOff)
0029 {
0030     if(nullptr == ctrl)
0031         return;
0032 
0033     auto textCtrl= dynamic_cast<vmap::TextController*>(ctrl);
0034 
0035     if(nullptr == textCtrl)
0036         return;
0037 
0038     VMapItemControllerUpdater::addItemController(textCtrl);
0039 
0040     connect(textCtrl, &vmap::TextController::textChanged, this,
0041             [this, textCtrl]() { sendOffVMapChanges<QString>(textCtrl, QStringLiteral("text")); });
0042     connect(textCtrl, &vmap::TextController::borderRectEditFinished, this,
0043             [this, textCtrl]() { sendOffVMapChanges<QRectF>(textCtrl, QStringLiteral("borderRect")); });
0044     connect(textCtrl, &vmap::TextController::fontChanged, this,
0045             [this, textCtrl]() { sendOffVMapChanges<QFont>(textCtrl, QStringLiteral("font")); });
0046     connect(textCtrl, &vmap::TextController::textPosChanged, this,
0047             [this, textCtrl]() { sendOffVMapChanges<QPointF>(textCtrl, QStringLiteral("textPos")); });
0048     if(!ctrl->remote() && sendOff)
0049         MessageHelper::sendOffText(textCtrl, ctrl->mapUuid());
0050 }
0051 
0052 bool TextControllerUpdater::updateItemProperty(NetworkMessageReader* msg, vmap::VisualItemController* ctrl)
0053 {
0054     if(nullptr == msg || nullptr == ctrl)
0055         return false;
0056 
0057     // TODO implement save/restore
0058     auto datapos= msg->pos();
0059 
0060     if(VMapItemControllerUpdater::updateItemProperty(msg, ctrl))
0061         return true;
0062 
0063     msg->resetToPos(datapos);
0064 
0065     updatingCtrl= ctrl;
0066 
0067     auto property= msg->string16();
0068 
0069     QVariant var;
0070 
0071     if(property == QStringLiteral("text"))
0072     {
0073         var= QVariant::fromValue(msg->string32());
0074     }
0075     else if(property == QStringLiteral("borderRect"))
0076     {
0077         auto x= msg->real();
0078         auto y= msg->real();
0079         auto w= msg->real();
0080         auto h= msg->real();
0081         var= QVariant::fromValue(QRectF(x, y, w, h));
0082     }
0083     else if(property == QStringLiteral("font"))
0084     {
0085         QFont font;
0086         font.fromString(msg->string16());
0087         var= QVariant::fromValue(font);
0088     }
0089     else if(property == QStringLiteral("textPos"))
0090     {
0091         auto x= msg->real();
0092         auto y= msg->real();
0093         var= QVariant::fromValue(QPointF(x, y));
0094     }
0095     m_updatingFromNetwork= true;
0096     auto feedback= ctrl->setProperty(property.toLocal8Bit().data(), var);
0097     m_updatingFromNetwork= false;
0098     updatingCtrl= nullptr;
0099 
0100     return feedback;
0101 }