From a3c66786eb8127338c694e1c9ae8d28a66e2670a Mon Sep 17 00:00:00 2001 From: Rafael Date: Mon, 21 Mar 2016 21:33:22 +0100 Subject: [PATCH] json parser now part of SnapmaticPicture --- PictureDialog.cpp | 27 ++++----------------- PictureDialog.h | 4 ++-- SnapmaticPicture.cpp | 57 ++++++++++++++++++++++++++++++++++++++++---- SnapmaticPicture.h | 16 ++++++++++++- main.cpp | 7 +++--- 5 files changed, 77 insertions(+), 34 deletions(-) diff --git a/PictureDialog.cpp b/PictureDialog.cpp index da576b0..241d29f 100755 --- a/PictureDialog.cpp +++ b/PictureDialog.cpp @@ -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) diff --git a/PictureDialog.h b/PictureDialog.h index 42ff0bf..96e6f00 100755 --- a/PictureDialog.h +++ b/PictureDialog.h @@ -19,6 +19,7 @@ #ifndef PICTUREDIALOG_H #define PICTUREDIALOG_H +#include "SnapmaticPicture.h" #include 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: diff --git a/SnapmaticPicture.cpp b/SnapmaticPicture.cpp index 6c427dc..9cae37a 100755 --- a/SnapmaticPicture.cpp +++ b/SnapmaticPicture.cpp @@ -17,6 +17,10 @@ *****************************************************************************/ #include "SnapmaticPicture.h" +#include +#include +#include +#include #include #include #include @@ -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; +} diff --git a/SnapmaticPicture.h b/SnapmaticPicture.h index f38ccce..0e85268 100755 --- a/SnapmaticPicture.h +++ b/SnapmaticPicture.h @@ -19,6 +19,7 @@ #ifndef SNAPMATICPICTURE_H #define SNAPMATICPICTURE_H +#include #include #include #include @@ -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: diff --git a/main.cpp b/main.cpp index e8b6495..0c0ba0d 100755 --- a/main.cpp +++ b/main.cpp @@ -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();