File indexing completed on 2024-05-05 05:40:37

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 "worker/convertionhelper.h"
0021 
0022 #include <QIODevice>
0023 
0024 namespace Helper
0025 {
0026 template <>
0027 void variantToType<bool>(const bool& val, NetworkMessageWriter& msg)
0028 {
0029     msg.uint8(val);
0030 }
0031 
0032 template <>
0033 void variantToType<qreal>(const qreal& val, NetworkMessageWriter& msg)
0034 {
0035     msg.real(val);
0036 }
0037 
0038 template <>
0039 void variantToType<int>(const int& val, NetworkMessageWriter& msg)
0040 {
0041     msg.int64(val);
0042 }
0043 
0044 template <>
0045 void variantToType<Core::ScaleUnit>(const Core::ScaleUnit& val, NetworkMessageWriter& msg)
0046 {
0047     msg.uint8(val);
0048 }
0049 template <>
0050 void variantToType<Core::PermissionMode>(const Core::PermissionMode& val, NetworkMessageWriter& msg)
0051 {
0052     msg.uint8(val);
0053 }
0054 template <>
0055 void variantToType<Core::GridPattern>(const Core::GridPattern& val, NetworkMessageWriter& msg)
0056 {
0057     msg.uint8(static_cast<typename std::underlying_type<Core::GridPattern>::type>(val));
0058 }
0059 template <>
0060 void variantToType<Core::Layer>(const Core::Layer& val, NetworkMessageWriter& msg)
0061 {
0062     msg.uint8(static_cast<typename std::underlying_type<Core::Layer>::type>(val));
0063 }
0064 template <>
0065 void variantToType<Core::VisibilityMode>(const Core::VisibilityMode& val, NetworkMessageWriter& msg)
0066 {
0067     msg.uint8(val);
0068 }
0069 
0070 template <>
0071 void variantToType<QColor>(const QColor& val, NetworkMessageWriter& msg)
0072 {
0073     msg.rgb(val.rgb());
0074 }
0075 
0076 template <>
0077 void variantToType<QImage>(const QImage& val, NetworkMessageWriter& msg)
0078 {
0079     QByteArray data;
0080     QDataStream in(&data, QIODevice::WriteOnly);
0081     in.setVersion(QDataStream::Qt_5_7);
0082     in << val;
0083     msg.byteArray32(data);
0084 }
0085 
0086 template <>
0087 void variantToType<QPointF>(const QPointF& val, NetworkMessageWriter& msg)
0088 {
0089     msg.real(val.x());
0090     msg.real(val.y());
0091 }
0092 
0093 template <>
0094 void variantToType<QRectF>(const QRectF& val, NetworkMessageWriter& msg)
0095 {
0096     msg.real(val.x());
0097     msg.real(val.y());
0098     msg.real(val.width());
0099     msg.real(val.height());
0100 }
0101 template <>
0102 void variantToType<quint16>(const quint16& val, NetworkMessageWriter& msg)
0103 {
0104     msg.uint16(val);
0105 }
0106 
0107 template <>
0108 void variantToType<QByteArray>(const QByteArray& val, NetworkMessageWriter& msg)
0109 {
0110     msg.byteArray32(val);
0111 }
0112 
0113 template <>
0114 void variantToType<QFont>(const QFont& val, NetworkMessageWriter& msg)
0115 {
0116     msg.string16(val.toString());
0117 }
0118 template <>
0119 void variantToType<std::vector<QPointF>>(const std::vector<QPointF>& val, NetworkMessageWriter& msg)
0120 {
0121     msg.int64(static_cast<qint64>(val.size()));
0122     for(auto p : val)
0123     {
0124         msg.real(p.x());
0125         msg.real(p.y());
0126     }
0127 }
0128 template <>
0129 void variantToType<CharacterVision::SHAPE>(const CharacterVision::SHAPE& shape, NetworkMessageWriter& msg)
0130 {
0131     msg.uint8(static_cast<quint8>(shape));
0132 }
0133 template <>
0134 void variantToType<QSize>(const QSize& size, NetworkMessageWriter& msg)
0135 {
0136     msg.int32(size.width());
0137     msg.int32(size.height());
0138 }
0139 
0140 template <>
0141 void variantToType<ParticipantModel::Permission>(const ParticipantModel::Permission& perm, NetworkMessageWriter& msg)
0142 {
0143     msg.uint8(static_cast<quint8>(perm));
0144 }
0145 
0146 template <>
0147 void variantToType<mindmap::ArrowDirection>(const mindmap::ArrowDirection& perm, NetworkMessageWriter& msg)
0148 {
0149     msg.uint8(static_cast<quint8>(perm));
0150 }
0151 
0152 template <>
0153 void variantToType(const QPainterPath &val, NetworkMessageWriter &msg)
0154 {
0155     QByteArray data;
0156     {
0157         QDataStream writer(&data, QIODevice::WriteOnly);
0158         writer << val;
0159     }
0160     msg.byteArray32(data);
0161 }
0162 
0163 } // namespace Helper