Skip to content

Commit f7d45bf

Browse files
committed
Merge remote-tracking branch 'upstream/feature/plugin/atrialfibres' into fetch_upstream_afib
2 parents e6911f6 + 2340c45 commit f7d45bf

2 files changed

Lines changed: 141 additions & 2 deletions

File tree

CemrgApp/Modules/CemrgAppModule/include/CemrgCommonUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ PURPOSE. See the above copyright notices for more information.
3535
#include <mitkDataNode.h>
3636
#include <mitkDataStorage.h>
3737
#include <QString>
38+
#include <QJsonObject>
3839

3940
class MITKCEMRGAPPMODULE_EXPORT CemrgCommonUtils {
4041

@@ -89,6 +90,10 @@ class MITKCEMRGAPPMODULE_EXPORT CemrgCommonUtils {
8990

9091
//Generic
9192
static mitk::DataNode::Pointer AddToStorage(mitk::BaseData* data, std::string nodeName, mitk::DataStorage::Pointer ds, bool init = true);
93+
static QJsonObject CreateJSONObject(QStringList keys_list, QStringList values_list, QStringList types_list);
94+
static QJsonObject ReadJSONFile(QString dir, QString fname);
95+
static bool WriteJSONFile(QJsonObject json, QString dir, QString fname);
96+
static bool ModifyJSONFile(QString dir, QString fname, QString key, QString value = "", QString type = "");
9297

9398
//Carp Utils
9499
static void OriginalCoordinates(QString imagePath, QString pointPath, QString outputPath, double scaling = 1000);

CemrgApp/Modules/CemrgAppModule/src/CemrgCommonUtils.cpp

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ PURPOSE. See the above copyright notices for more information.
9393
#include <QFileInfo>
9494
#include <QDir>
9595
#include <QTextStream>
96-
96+
#include <QJsonParseError>
97+
#include <QJsonDocument>
98+
#include <QJsonArray>
9799

98100
#include "CemrgCommonUtils.h"
99101

@@ -1026,13 +1028,145 @@ mitk::DataNode::Pointer CemrgCommonUtils::AddToStorage(
10261028
return node;
10271029
}
10281030

1031+
QJsonObject CemrgCommonUtils::ReadJSONFile(QString dir, QString fname) {
1032+
fname += (!fname.endsWith(".json")) ? ".json" : "";
1033+
QFile file(dir + "/" + fname);
1034+
1035+
if (!file.open(QIODevice::ReadOnly)) {
1036+
qWarning("Failed to open file");
1037+
return QJsonObject();
1038+
}
1039+
1040+
QByteArray jsonData = file.readAll();
1041+
1042+
QJsonParseError error;
1043+
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error);
1044+
1045+
MITK_WARN((jsonDoc.isNull())) << ("Failed to parse JSON: " + error.errorString()).toStdString();
1046+
1047+
QJsonObject jsonObj = jsonDoc.object();
1048+
1049+
file.close();
1050+
1051+
return jsonObj;
1052+
}
1053+
1054+
bool CemrgCommonUtils::WriteJSONFile(QJsonObject json, QString dir, QString fname) {
1055+
// Create the JSON document
1056+
QJsonDocument jsonDoc(json);
1057+
bool success = true;
1058+
1059+
// Open the file for writing
1060+
fname += (!fname.endsWith(".json")) ? ".json" : "";
1061+
QFile file(dir + "/" + fname);
1062+
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
1063+
qWarning("Failed to open file");
1064+
success = false;
1065+
}
1066+
1067+
file.write(jsonDoc.toJson());
1068+
file.close();
1069+
1070+
return success;
1071+
}
1072+
1073+
bool CemrgCommonUtils::ModifyJSONFile(QString dir, QString fname, QString key, QString value, QString type) {
1074+
bool success = true;
1075+
QJsonObject json = ReadJSONFile(dir, fname);
1076+
1077+
if (json.empty()) {
1078+
return false;
1079+
}
1080+
1081+
if (value.isEmpty()) {
1082+
json.remove(key);
1083+
} else {
1084+
// Determine the data type of the value
1085+
QJsonValue jsonValue;
1086+
QJsonArray json_array;
1087+
if (type == "string") {
1088+
jsonValue = QJsonValue::fromVariant(value);
1089+
}
1090+
else if (type == "int") {
1091+
jsonValue = QJsonValue::fromVariant(value.toInt());
1092+
}
1093+
else if (type == "float" || type == "double") {
1094+
jsonValue = QJsonValue::fromVariant(value.toDouble());
1095+
}
1096+
else if (type == "bool") {
1097+
jsonValue = QJsonValue::fromVariant(value.toLower() == "true");
1098+
}
1099+
else if (type == "array") {
1100+
// split value
1101+
QStringList array_values = value.split(",");
1102+
for (int ix = 0; ix < array_values.size(); ix++) {
1103+
// QJsonValue json_value = array_values.at(ix).toDouble();
1104+
json_array.push_back(array_values.at(ix).toDouble());
1105+
}
1106+
}
1107+
else {
1108+
// If the data type is not recognized, use a null value
1109+
jsonValue = QJsonValue();
1110+
}
1111+
1112+
// Add the key-value pair to the object
1113+
json[key] = (type == "array") ? json_array : jsonValue;
1114+
}
1115+
1116+
success = WriteJSONFile(json, dir, fname);
1117+
return success;
1118+
}
1119+
1120+
QJsonObject CemrgCommonUtils::CreateJSONObject(QStringList keys_list, QStringList values_list, QStringList types_list) {
1121+
QJsonObject jsonObj;
1122+
1123+
for (int i = 0; i < keys_list.size(); i++) {
1124+
QString key = keys_list.at(i);
1125+
QString value = values_list.at(i);
1126+
QString type = types_list.at(i);
1127+
1128+
// Determine the data type of the value
1129+
QJsonValue jsonValue;
1130+
QJsonArray json_array;
1131+
if (type == "string") {
1132+
jsonValue = QJsonValue::fromVariant(value);
1133+
}
1134+
else if (type == "int") {
1135+
jsonValue = QJsonValue::fromVariant(value.toInt());
1136+
}
1137+
else if (type == "float" || type == "double") {
1138+
jsonValue = QJsonValue::fromVariant(value.toDouble());
1139+
}
1140+
else if (type == "bool") {
1141+
jsonValue = QJsonValue::fromVariant(value.toLower() == "true");
1142+
}
1143+
else if (type == "array") {
1144+
// split value
1145+
QStringList array_values = value.split(",");
1146+
for (int ix = 0; ix < array_values.size(); ix++) {
1147+
// QJsonValue json_value = array_values.at(ix).toDouble();
1148+
json_array.push_back(array_values.at(ix).toDouble());
1149+
}
1150+
}
1151+
else {
1152+
// If the data type is not recognized, use a null value
1153+
jsonValue = QJsonValue();
1154+
}
1155+
1156+
// Add the key-value pair to the object
1157+
jsonObj[key] = (type == "array") ? json_array : jsonValue;
1158+
}
1159+
1160+
return jsonObj;
1161+
}
1162+
10291163
mitk::Image::Pointer CemrgCommonUtils::ImageFromSurfaceMesh(mitk::Surface::Pointer surf, double origin[3], double spacing[3], int pad_num){
10301164
vtkSmartPointer<vtkPolyData> pd = surf->GetVtkPolyData();
10311165
double bounds[6];
10321166
pd->GetBounds(bounds);
10331167

10341168
// prepare for padding (pad_num=0 by default, so it does not affect)
1035-
for (int ix; ix<3; ix++) {
1169+
for (int ix = 0; ix < 3; ix++) {
10361170
double pad_offset = pad_num*spacing[ix];
10371171
bounds[2*ix] -= pad_offset;
10381172
bounds[2*ix + 1] += pad_offset;

0 commit comments

Comments
 (0)