static QJsonValue readLocalValue(const QJsonObject &json, const QString &key) { // start with language_country const QString localeName = QLocale().name(); QString localKey = key + QLatin1Char('[') + localeName + QLatin1Char(']'); QJsonObject::ConstIterator it = json.constFind(localKey); if (it != json.constEnd()) { return it.value(); } // drop _country const int separatorIndex = localeName.indexOf(QLatin1Char('_')); if (separatorIndex != -1) { const int localKeySeparatorIndex = key.length() + 1 + separatorIndex; localKey[localKeySeparatorIndex] = QLatin1Char(']'); localKey.truncate(localKeySeparatorIndex + 1); it = json.constFind(localKey); if (it != json.constEnd()) { return it.value(); } } // default to unlocalized value return json.value(key); }
void MainWindow::on_treeWidget_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) { QJsonValue jsonObj = current->data(0, Qt::UserRole).toJsonValue(); QJsonValue::Type jsonType = jsonObj.type(); switch(jsonType) { case QJsonValue::Bool: ui->boolButton->setChecked(jsonObj.toBool()); break; case QJsonValue::Double: ui->doubleValue->setValue(jsonObj.toDouble()); break; case QJsonValue::String: ui->stringEdit->document()->setPlainText(jsonObj.toString()); break; case QJsonValue::Array: { QJsonArray arr = jsonObj.toArray(); int count = arr.count(); ui->arrayList->clear(); for(int i = 0; i < count; ++i) { QString label = renderJsonValue(arr.at(i)); ui->arrayList->addItem(label); } } break; case QJsonValue::Object: { QJsonObject obj = jsonObj.toObject(); ui->objectTable->setRowCount(obj.count()); int row = 0; for(QJsonObject::ConstIterator i = obj.begin(); i != obj.end(); ++i, ++row) { QTableWidgetItem *keyItem = new QTableWidgetItem(i.key()); QJsonValue val = i.value(); QTableWidgetItem *valItem = new QTableWidgetItem(renderJsonValue(val)); ui->objectTable->setItem(row, 0, keyItem); ui->objectTable->setItem(row, 1, valItem); } } break; default: break; } ui->typeSelector->setCurrentIndex(jsonObj.type() - 1); }
void MainWindow::buildJsonTree(QTreeWidgetItem *parent, QJsonValue &obj, QString key = "") { QTreeWidgetItem *toAdd = NULL; switch(obj.type()) { case QJsonValue::Bool: case QJsonValue::Double: case QJsonValue::String: case QJsonValue::Undefined: case QJsonValue::Null: toAdd = createJsonTreeLeaf(parent, obj, key); break; case QJsonValue::Array: { toAdd = new QTreeWidgetItem(parent); QJsonArray array = obj.toArray(); int count = array.count(); toAdd->setText(0, key+"["+QString::number(count)+"]"); for(int i = 0; i < count; ++i) { QJsonValue val = array.at(i); buildJsonTree(toAdd, val, QString::number(i)+" : "); } break; } case QJsonValue::Object: { toAdd = new QTreeWidgetItem(parent); QJsonObject object = obj.toObject(); int count = object.count(); toAdd->setText(0, key+"{"+QString::number(count)+"}"); for(QJsonObject::ConstIterator i = object.begin(); i != object.end(); ++i) { QJsonValue val = i.value(); buildJsonTree(toAdd, val, i.key()+" : "); } break; } default: break; } toAdd->setData(0, Qt::UserRole, QVariant(obj)); parent->addChild(toAdd); }
void QEditorSettings::fromJson(const QJsonObject &root){ for( QJsonObject::ConstIterator it = root.begin(); it != root.end(); ++it ){ if ( it.key() == "font" ){ QJsonObject fontObj = it.value().toObject(); if ( fontObj.contains("size") ){ m_fontSize = root["size"].toInt(); emit fontSizeChanged(m_fontSize); } } else { QEditorSettingsCategory* category = settingsFor(it.key()); if ( category ) category->fromJson(it.value()); else qCritical("Failed to find settings for: %s", qPrintable(it.key())); } } }
bool JsonParser::read( QIODevice* device ) { // Assert previous document got released. delete m_document; m_document = new GeoDataDocument; Q_ASSERT( m_document ); // Read file data QJsonParseError error; const QJsonDocument jsonDoc = QJsonDocument::fromJson(device->readAll(), &error); if (jsonDoc.isNull()) { qDebug() << "Error parsing GeoJSON : " << error.errorString(); return false; } // Start parsing const QJsonValue featuresValue = jsonDoc.object().value(QStringLiteral("features")); // In GeoJSON format, geometries are stored in features, so we iterate on features if (featuresValue.isArray()) { const QJsonArray featureArray = featuresValue.toArray(); // Parse each feature for (int featureIndex = 0; featureIndex < featureArray.size(); ++featureIndex) { const QJsonObject featureObject = featureArray[featureIndex].toObject(); // Check if the feature contains a geometry const QJsonValue geometryValue = featureObject.value(QStringLiteral("geometry")); if (geometryValue.isObject()) { const QJsonObject geometryObject = geometryValue.toObject(); // Variables for creating the geometry QList<GeoDataGeometry*> geometryList; QList<GeoDataPlacemark*> placemarkList; // Create the different geometry types const QString geometryType = geometryObject.value(QStringLiteral("type")).toString().toUpper(); if (geometryType == QLatin1String("POLYGON")) { // Check first that there are coordinates const QJsonValue coordinatesValue = geometryObject.value(QStringLiteral("coordinates")); if (coordinatesValue.isArray()) { const QJsonArray coordinateArray = coordinatesValue.toArray(); GeoDataPolygon * geom = new GeoDataPolygon( RespectLatitudeCircle | Tessellate ); // Coordinates first array will be the outer boundary, if there are more // positions those will be inner holes for (int ringIndex = 0 ; ringIndex < coordinateArray.size(); ++ringIndex) { const QJsonArray ringArray = coordinateArray[ringIndex].toArray(); GeoDataLinearRing linearRing; for (int coordinatePairIndex = 0; coordinatePairIndex < ringArray.size(); ++coordinatePairIndex) { const QJsonArray coordinatePairArray = ringArray[coordinatePairIndex].toArray(); const qreal longitude = coordinatePairArray.at(0).toDouble(); const qreal latitude = coordinatePairArray.at(1).toDouble(); linearRing.append( GeoDataCoordinates( longitude , latitude , 0 , GeoDataCoordinates::Degree ) ); } // Outer ring if (ringIndex == 0) { geom->setOuterBoundary( linearRing ); } // Inner holes else { geom->appendInnerBoundary( linearRing ); } } geometryList.append( geom ); } } else if (geometryType == QLatin1String("MULTIPOLYGON")) { // Check first that there are coordinates const QJsonValue coordinatesValue = geometryObject.value(QStringLiteral("coordinates")); if (coordinatesValue.isArray()) { const QJsonArray coordinateArray = coordinatesValue.toArray(); for (int polygonIndex = 0; polygonIndex < coordinateArray.size(); ++polygonIndex) { const QJsonArray polygonArray = coordinateArray[polygonIndex].toArray(); GeoDataPolygon * geom = new GeoDataPolygon( RespectLatitudeCircle | Tessellate ); // Coordinates first array will be the outer boundary, if there are more // positions those will be inner holes for (int ringIndex = 0 ; ringIndex < polygonArray.size(); ++ringIndex) { const QJsonArray ringArray = polygonArray[ringIndex].toArray(); GeoDataLinearRing linearRing; for (int coordinatePairIndex = 0; coordinatePairIndex < ringArray.size(); ++coordinatePairIndex) { const QJsonArray coordinatePairArray = ringArray[coordinatePairIndex].toArray(); const qreal longitude = coordinatePairArray.at(0).toDouble(); const qreal latitude = coordinatePairArray.at(1).toDouble(); linearRing.append( GeoDataCoordinates( longitude , latitude , 0 , GeoDataCoordinates::Degree ) ); } // Outer ring if (ringIndex == 0) { geom->setOuterBoundary( linearRing ); } // Inner holes else { geom->appendInnerBoundary( linearRing ); } } geometryList.append( geom ); } } } else if (geometryType == QLatin1String("LINESTRING")) { // Check first that there are coordinates const QJsonValue coordinatesValue = geometryObject.value(QStringLiteral("coordinates")); if (coordinatesValue.isArray()) { const QJsonArray coordinateArray = coordinatesValue.toArray(); GeoDataLineString * geom = new GeoDataLineString( RespectLatitudeCircle | Tessellate ); for (int coordinatePairIndex = 0; coordinatePairIndex < coordinateArray.size(); ++coordinatePairIndex) { const QJsonArray coordinatePairArray = coordinateArray[coordinatePairIndex].toArray(); const qreal longitude = coordinatePairArray.at(0).toDouble(); const qreal latitude = coordinatePairArray.at(1).toDouble(); geom->append( GeoDataCoordinates( longitude , latitude , 0 , GeoDataCoordinates::Degree ) ); } geometryList.append( geom ); } } else if (geometryType == QLatin1String("MULTILINESTRING")) { // Check first that there are coordinates const QJsonValue coordinatesValue = geometryObject.value(QStringLiteral("coordinates")); if (coordinatesValue.isArray()) { const QJsonArray coordinateArray = coordinatesValue.toArray(); for (int lineStringIndex = 0; lineStringIndex < coordinateArray.size(); ++lineStringIndex) { const QJsonArray lineStringArray = coordinateArray[lineStringIndex].toArray(); GeoDataLineString * geom = new GeoDataLineString( RespectLatitudeCircle | Tessellate ); for (int coordinatePairIndex = 0; coordinatePairIndex < lineStringArray.size(); ++coordinatePairIndex) { const QJsonArray coordinatePairArray = lineStringArray[coordinatePairIndex].toArray(); const qreal longitude = coordinatePairArray.at(0).toDouble(); const qreal latitude = coordinatePairArray.at(1).toDouble(); geom->append( GeoDataCoordinates( longitude , latitude , 0 , GeoDataCoordinates::Degree ) ); } geometryList.append( geom ); } } } else if (geometryType == QLatin1String("POINT")) { // Check first that there are coordinates const QJsonValue coordinatesValue = geometryObject.value(QStringLiteral("coordinates")); if (coordinatesValue.isArray()) { const QJsonArray coordinatePairArray = coordinatesValue.toArray(); GeoDataPoint * geom = new GeoDataPoint(); const qreal longitude = coordinatePairArray.at(0).toDouble(); const qreal latitude = coordinatePairArray.at(1).toDouble(); geom->setCoordinates( GeoDataCoordinates( longitude , latitude , 0 , GeoDataCoordinates::Degree ) ); geometryList.append( geom ); } } else if (geometryType == QLatin1String("MULTIPOINT")) { // Check first that there are coordinates const QJsonValue coordinatesValue = geometryObject.value(QStringLiteral("coordinates")); if (coordinatesValue.isArray()) { const QJsonArray coordinateArray = coordinatesValue.toArray(); for (int pointIndex = 0; pointIndex < coordinateArray.size(); ++pointIndex) { const QJsonArray coordinatePairArray = coordinateArray[pointIndex].toArray(); GeoDataPoint * geom = new GeoDataPoint(); const qreal longitude = coordinatePairArray.at(0).toDouble(); const qreal latitude = coordinatePairArray.at(1).toDouble(); geom->setCoordinates( GeoDataCoordinates( longitude , latitude , 0 , GeoDataCoordinates::Degree ) ); geometryList.append( geom ); } } } // Parse the features properties const QJsonValue propertiesValue = featureObject.value(QStringLiteral("properties")); if (!geometryList.isEmpty() && propertiesValue.isObject()) { const QJsonObject propertiesObject = propertiesValue.toObject(); // First create a placemark for each geometry, there could be multi geometries // that are translated into more than one geometry/placemark for ( int numberGeometries = 0 ; numberGeometries < geometryList.length() ; numberGeometries++ ) { GeoDataPlacemark * placemark = new GeoDataPlacemark(); placemarkList.append( placemark ); } OsmPlacemarkData osmData; QJsonObject::ConstIterator it = propertiesObject.begin(); const QJsonObject::ConstIterator end = propertiesObject.end(); for ( ; it != end; ++it) { if (it.value().isObject() || it.value().isArray()) { qDebug() << "Skipping property, values of type arrays and objects not supported:" << it.key(); continue; } // pass value through QVariant to also get bool & numbers osmData.addTag(it.key(), it.value().toVariant().toString()); } // If the property read, is the features name const auto tagIter = osmData.findTag(QStringLiteral("name")); if (tagIter != osmData.tagsEnd()) { const QString& name = tagIter.value(); for (int pl = 0 ; pl < placemarkList.length(); ++pl) { placemarkList.at(pl)->setName(name); } } const GeoDataPlacemark::GeoDataVisualCategory category = StyleBuilder::determineVisualCategory(osmData); if (category != GeoDataPlacemark::None) { // Add the visual category to all the placemarks for (int pl = 0 ; pl < placemarkList.length(); ++pl) { placemarkList.at(pl)->setVisualCategory(category); placemarkList.at(pl)->setOsmData(osmData); } } } // Add the geometry to the document if ( geometryList.length() == placemarkList.length() ) { while( placemarkList.length() > 0 ) { GeoDataPlacemark * placemark = placemarkList.last(); placemarkList.pop_back(); GeoDataGeometry * geom = geometryList.last(); geometryList.pop_back(); placemark->setGeometry( geom ); placemark->setVisible( true ); m_document->append( placemark ); } } // If geometries or placemarks missing inside the lists, delete them qDeleteAll( geometryList.begin(), geometryList.end() ); geometryList.clear(); qDeleteAll( placemarkList.begin(), placemarkList.end() ); placemarkList.clear(); } } } return true; }
void MainWindow::LoadJSON(QString fileName) { QFile jsonFile(fileName); if(!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) { ui->statusBar->showMessage("Could not open file: " + fileName); return; } QByteArray rawJson = jsonFile.readAll(); ui->textEdit->setText(QString(rawJson)); QJsonParseError error; doc = new QJsonDocument(QJsonDocument::fromJson(rawJson, &error)); if(error.error != QJsonParseError::NoError) { ui->statusBar->showMessage("Failed to parse file: " + error.errorString()); delete doc; doc = NULL; return; } /* QJsonObject root = doc->object(); for(QJsonObject::ConstIterator i = root.begin(); i != root.end(); ++i) { QTreeWidgetItem *toAdd = new QTreeWidgetItem(); toAdd->setText(0, i.key()); ui->treeWidget->addTopLevelItem(toAdd); } */ QTreeWidgetItem *root = new QTreeWidgetItem(); QString rootId = "root"; QJsonValue rootVal; if(doc->isArray()) { QJsonArray array = doc->array(); rootVal = QJsonValue(array); int count = array.count(); rootId += "["+QString::number(count)+"]"; for(int i = 0; i < count; ++i) { QJsonValue val = array.at(i); buildJsonTree(root, val, QString::number(i)+" : "); } } else if(doc->isObject()) { QJsonObject object = doc->object(); rootVal = QJsonValue(object); rootId += "{"+QString::number(object.count())+"}"; for(QJsonObject::ConstIterator i = object.begin(); i != object.end(); ++i) { QJsonValue val = i.value(); buildJsonTree(root, val, i.key()+" : "); } } else { rootId = "Null or Undefined root object"; return; } root->setText(0, rootId); root->setData(0, Qt::UserRole, QVariant(rootVal)); ui->treeWidget->addTopLevelItem(root); ui->treeWidget->setCurrentItem(root); }