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); ui->labPicture->setPixmap(pixmap);
} }
void PictureDialog::setJsonString(QString jsonStr) void PictureDialog::setJsonString(SnapmaticPicture *picture)
{ {
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonStr.toLatin1()); QString locX = QString::number(picture->getLocationX());
QJsonObject jsonObject = jsonDocument.object(); QString locY = QString::number(picture->getLocationY());
QVariantMap jsonMap = jsonObject.toVariantMap(); QString locZ = QString::number(picture->getLocationZ());
QStringList plyrsList = picture->getPlayers();
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 plyrsStr; QString plyrsStr;
foreach (const QString &player, plyrsList) foreach (const QString &player, plyrsList)

View file

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

View file

@ -17,6 +17,10 @@
*****************************************************************************/ *****************************************************************************/
#include "SnapmaticPicture.h" #include "SnapmaticPicture.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QVariantMap>
#include <QJsonArray>
#include <QPixmap> #include <QPixmap>
#include <QString> #include <QString>
#include <QDebug> #include <QDebug>
@ -99,6 +103,7 @@ bool SnapmaticPicture::readingPicture()
} }
QByteArray jsonRawContent = picFile->read(jsonStreamLength); QByteArray jsonRawContent = picFile->read(jsonStreamLength);
jsonStr = getSnapmaticJSONString(jsonRawContent); jsonStr = getSnapmaticJSONString(jsonRawContent);
parseJsonContent(); // JSON parsing is own function
return cachePicture.loadFromData(jpegRawContent); return cachePicture.loadFromData(jpegRawContent);
} }
@ -147,11 +152,6 @@ QString SnapmaticPicture::getLastStep()
return lastStep; return lastStep;
} }
QString SnapmaticPicture::getJsonStr()
{
return jsonStr;
}
QPixmap SnapmaticPicture::getPixmap() QPixmap SnapmaticPicture::getPixmap()
{ {
return cachePicture; return cachePicture;
@ -166,3 +166,50 @@ QString SnapmaticPicture::convertLogStringForDraw(QString inputStr)
{ {
return inputStr.replace("&c;",",").replace("&u;","&"); 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 #ifndef SNAPMATICPICTURE_H
#define SNAPMATICPICTURE_H #define SNAPMATICPICTURE_H
#include <QStringList>
#include <QObject> #include <QObject>
#include <QPixmap> #include <QPixmap>
#include <QString> #include <QString>
@ -34,10 +35,16 @@ public:
void setPixmap(QPixmap pixmap); void setPixmap(QPixmap pixmap);
void resetValues(); void resetValues();
QPixmap getPixmap(); QPixmap getPixmap();
QString getJsonStr();
QString getLastStep(); QString getLastStep();
QString getPictureStr(); QString getPictureStr();
// JSON
QString getJsonStr();
double getLocationX();
double getLocationY();
double getLocationZ();
QStringList getPlayers();
private: private:
QString getSnapmaticPictureString(QByteArray snapmaticHeader); QString getSnapmaticPictureString(QByteArray snapmaticHeader);
QString getSnapmaticJSONString(QByteArray jsonBytes); QString getSnapmaticJSONString(QByteArray jsonBytes);
@ -47,7 +54,14 @@ private:
QString picFileName; QString picFileName;
QString pictureStr; QString pictureStr;
QString lastStep; QString lastStep;
// JSON
void parseJsonContent();
QString jsonStr; QString jsonStr;
double jsonLocX;
double jsonLocY;
double jsonLocZ;
QStringList jsonPlyrsList;
signals: signals:

View file

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