json parser now part of SnapmaticPicture

This commit is contained in:
Rafael 2016-03-21 21:33:22 +01:00
parent 4050358462
commit a3c66786eb
5 changed files with 77 additions and 34 deletions

View file

@ -43,29 +43,12 @@ void PictureDialog::setSnapmaticPicture(QPixmap pixmap)
ui->labPicture->setPixmap(pixmap);
}
void PictureDialog::setJsonString(QString jsonStr)
void PictureDialog::setJsonString(SnapmaticPicture *picture)
{
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonStr.toLatin1());
QJsonObject jsonObject = jsonDocument.object();
QVariantMap jsonMap = jsonObject.toVariantMap();
QString locX;
QString locY;
QString locZ;
QStringList plyrsList;
if (jsonMap.contains("loc"))
{
QJsonObject locObject = jsonObject["loc"].toObject();
QVariantMap locMap = locObject.toVariantMap();
if (locMap.contains("x")) { locX = locMap["x"].toString(); }
if (locMap.contains("y")) { locY = locMap["y"].toString(); }
if (locMap.contains("z")) { locZ = locMap["z"].toString(); }
}
if (jsonMap.contains("plyrs"))
{
plyrsList = jsonMap["plyrs"].toStringList();
}
QString locX = QString::number(picture->getLocationX());
QString locY = QString::number(picture->getLocationY());
QString locZ = QString::number(picture->getLocationZ());
QStringList plyrsList = picture->getPlayers();
QString plyrsStr;
foreach (const QString &player, plyrsList)

View file

@ -19,6 +19,7 @@
#ifndef PICTUREDIALOG_H
#define PICTUREDIALOG_H
#include "SnapmaticPicture.h"
#include <QDialog>
namespace Ui {
@ -28,11 +29,10 @@ class PictureDialog;
class PictureDialog : public QDialog
{
Q_OBJECT
public:
explicit PictureDialog(QWidget *parent = 0);
void setSnapmaticPicture(QPixmap pixmap);
void setJsonString(QString jsonStr);
void setJsonString(SnapmaticPicture *picture);
~PictureDialog();
private:

View file

@ -17,6 +17,10 @@
*****************************************************************************/
#include "SnapmaticPicture.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QVariantMap>
#include <QJsonArray>
#include <QPixmap>
#include <QString>
#include <QDebug>
@ -99,6 +103,7 @@ bool SnapmaticPicture::readingPicture()
}
QByteArray jsonRawContent = picFile->read(jsonStreamLength);
jsonStr = getSnapmaticJSONString(jsonRawContent);
parseJsonContent(); // JSON parsing is own function
return cachePicture.loadFromData(jpegRawContent);
}
@ -147,11 +152,6 @@ QString SnapmaticPicture::getLastStep()
return lastStep;
}
QString SnapmaticPicture::getJsonStr()
{
return jsonStr;
}
QPixmap SnapmaticPicture::getPixmap()
{
return cachePicture;
@ -166,3 +166,50 @@ QString SnapmaticPicture::convertLogStringForDraw(QString inputStr)
{
return inputStr.replace("&c;",",").replace("&u;","&");
}
// JSON part
void SnapmaticPicture::parseJsonContent()
{
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonStr.toLatin1());
QJsonObject jsonObject = jsonDocument.object();
QVariantMap jsonMap = jsonObject.toVariantMap();
if (jsonMap.contains("loc"))
{
QJsonObject locObject = jsonObject["loc"].toObject();
QVariantMap locMap = locObject.toVariantMap();
if (locMap.contains("x")) { jsonLocX = locMap["x"].toDouble(); }
if (locMap.contains("y")) { jsonLocY = locMap["y"].toDouble(); }
if (locMap.contains("z")) { jsonLocZ = locMap["z"].toDouble(); }
}
if (jsonMap.contains("plyrs"))
{
jsonPlyrsList = jsonMap["plyrs"].toStringList();
}
}
QString SnapmaticPicture::getJsonStr()
{
return jsonStr;
}
double SnapmaticPicture::getLocationX()
{
return jsonLocX;
}
double SnapmaticPicture::getLocationY()
{
return jsonLocY;
}
double SnapmaticPicture::getLocationZ()
{
return jsonLocZ;
}
QStringList SnapmaticPicture::getPlayers()
{
return jsonPlyrsList;
}

View file

@ -19,6 +19,7 @@
#ifndef SNAPMATICPICTURE_H
#define SNAPMATICPICTURE_H
#include <QStringList>
#include <QObject>
#include <QPixmap>
#include <QString>
@ -34,10 +35,16 @@ public:
void setPixmap(QPixmap pixmap);
void resetValues();
QPixmap getPixmap();
QString getJsonStr();
QString getLastStep();
QString getPictureStr();
// JSON
QString getJsonStr();
double getLocationX();
double getLocationY();
double getLocationZ();
QStringList getPlayers();
private:
QString getSnapmaticPictureString(QByteArray snapmaticHeader);
QString getSnapmaticJSONString(QByteArray jsonBytes);
@ -47,7 +54,14 @@ private:
QString picFileName;
QString pictureStr;
QString lastStep;
// JSON
void parseJsonContent();
QString jsonStr;
double jsonLocX;
double jsonLocY;
double jsonLocZ;
QStringList jsonPlyrsList;
signals:

View file

@ -44,14 +44,13 @@ int main(int argc, char *argv[])
if (selectedAction == "showpic")
{
SnapmaticPicture picture;
qDebug() << picture.readingPictureFromFile(arg1);
qDebug() << picture.getLastStep();
PictureDialog picDialog;
SnapmaticPicture picture;
picture.readingPictureFromFile(arg1);
picDialog.setWindowFlags(picDialog.windowFlags()^Qt::WindowContextHelpButtonHint);
picDialog.setWindowTitle(picture.getPictureStr());
picDialog.setSnapmaticPicture(picture.getPixmap());
picDialog.setJsonString(picture.getJsonStr());
picDialog.setJsonString(&picture);
picDialog.show();
return a.exec();