//QMap<qint32, QString> qint32 RtCmdClient::requestConnectors(QMap<qint32, QString> &p_qMapConnectors) { //Send m_commandManager["conlist"].send(); //Receive m_qMutex.lock(); QByteArray t_sJsonConnectors = m_sAvailableData.toUtf8(); m_qMutex.unlock(); //Parse QJsonParseError error; QJsonDocument t_jsonDocumentOrigin = QJsonDocument::fromJson( t_sJsonConnectors, &error); QJsonObject t_jsonObjectConnectors; //Switch to command object if(t_jsonDocumentOrigin.isObject() && t_jsonDocumentOrigin.object().value(QString("connectors")) != QJsonValue::Undefined) t_jsonObjectConnectors = t_jsonDocumentOrigin.object().value(QString("connectors")).toObject(); //inits qint32 p_iActiveId = -1; p_qMapConnectors.clear(); //insert connectors QJsonObject::Iterator it; for(it = t_jsonObjectConnectors.begin(); it != t_jsonObjectConnectors.end(); ++it) { QString t_qConnectorName = it.key(); qint32 id = it.value().toObject().value(QString("id")).toDouble(); if(!p_qMapConnectors.contains(id)) p_qMapConnectors.insert(id, t_qConnectorName); else qWarning("Warning: CommandMap contains command %s already. Insertion skipped.\n", it.key().toLatin1().constData()); //if connector is active indicate it if(it.value().toObject().value(QString("active")).toBool()) p_iActiveId = id; } return p_iActiveId; }
bool MainWindow::Open() { QString tempFileName = QFileDialog::getOpenFileName(this, tr("Open file")); if (!tempFileName.isEmpty()) { QFile json(tempFileName); if(json.open(QIODevice::ReadOnly)) { QJsonParseError parseError; QJsonDocument jsonDoc = QJsonDocument::fromJson(json.readAll(), &parseError); if(parseError.error == QJsonParseError::NoError) { if(jsonDoc.isObject()) { m_model->Clear(); QJsonObject jsonObject = jsonDoc.object(); for (QJsonObject::Iterator iter = jsonObject.begin(); iter != jsonObject.end(); ++iter) { Population newPopulation(iter.key(), iter.value().toInt()); m_model->append(newPopulation); } m_fileName = tempFileName; this->setWindowTitle(m_fileName); ui->menuInsert->setEnabled(true); m_isSomethingChanged = false; } }else { json.close(); return false; } json.close(); m_stack->clear(); return true; }else { return false; } } return true; }
//************************************************************************************************************* //ToDo connect all commands inserted in this class by default. void CommandManager::insert(const QJsonDocument &p_jsonDocument) { QJsonObject t_jsonObjectCommand; //Switch to command object if(p_jsonDocument.isObject() && p_jsonDocument.object().value(QString("commands")) != QJsonValue::Undefined) t_jsonObjectCommand = p_jsonDocument.object().value(QString("commands")).toObject(); else return; QJsonObject::Iterator it; for(it = t_jsonObjectCommand.begin(); it != t_jsonObjectCommand.end(); ++it) { if(!m_qMapCommands.contains(it.key())) m_qMapCommands.insert(it.key(), Command(it.key(), it.value().toObject(), true, this)); else qWarning("Warning: CommandMap contains command %s already. Insertion skipped.\n", it.key().toLatin1().constData()); } emit commandMapChanged(); }
void table::Open() { if (m_model->smthChanged) { if (SaveRequest()) { Save(); } } QString fileName = QFileDialog::getOpenFileName(this,tr("Open"),QString(),tr("JSON Files (*.json)")); if (fileName == "") { return; } QFile f(fileName); QString data = ""; if(f.open(QIODevice::ReadOnly)) { data = f.readAll(); f.close(); } QJsonParseError parseError; QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8(), &parseError); if(parseError.error) { QMessageBox::warning(this, tr("Error"), tr("Parse error")); return; } auto items = doc.object(); m_model->clear(); for (QJsonObject::Iterator iter = items.begin(); iter != items.end(); ++iter) { Team newTeam(iter.key(), iter.value().toInt()); m_model->append(newTeam); } m_fileName = fileName; m_model->smthChanged = false; ui->actionSave->setEnabled(false); }
bool CommandParser::parse(const QString &p_sInput, QStringList &p_qListCommandsParsed) { if(p_sInput.size() <= 0) return false; p_qListCommandsParsed.clear(); //Check if JSON format; bool isJson = false; if(QString::compare(p_sInput.at(0), QString("{")) == 0) isJson = true; if(isJson) { qDebug() << "JSON command recognized"; QJsonObject t_jsonObjectCommand; QJsonObject t_jsonObjectParameters; QJsonDocument t_jsonDocument(QJsonDocument::fromJson(p_sInput.toLatin1())); //Switch to command object if(t_jsonDocument.isObject() && t_jsonDocument.object().value(QString("commands")) != QJsonValue::Undefined) t_jsonObjectCommand = t_jsonDocument.object().value(QString("commands")).toObject(); else return false; //iterate over commands QJsonObject::Iterator it; QJsonObject::Iterator itParam; for(it = t_jsonObjectCommand.begin(); it != t_jsonObjectCommand.end(); ++it) { //Print Command printf("%s\r\n", it.key().toLatin1().constData()); if(exists(it.key())) { RawCommand t_rawCommand(it.key(), true); m_rawCommand = t_rawCommand; t_jsonObjectParameters = it.value().toObject(); // push command to processed commands p_qListCommandsParsed.push_back(it.key()); //append the parameters for(itParam= t_jsonObjectParameters.begin(); itParam != t_jsonObjectParameters.end(); ++itParam) { printf(" %s", itParam.value().toString().toLatin1().constData()); //ToDo do a cross check with the param naming and key m_rawCommand.pValues().append(itParam.value().toString()); // qDebug() << itParam.key() << " + " << itParam.value().toString(); } //Notify attached command manager notify(); } printf("\r\n"); } } else { QStringList t_qCommandList = p_sInput.split(" "); //Print command printf("%s\r\n", t_qCommandList[0].toLatin1().constData()); if(!exists(t_qCommandList[0])) { printf("\r\n"); return false; } RawCommand t_rawCommand(t_qCommandList[0], false); m_rawCommand = t_rawCommand; // push command to processed commands p_qListCommandsParsed.push_back(t_qCommandList[0]); if(t_qCommandList.size() > 1) //Parameter parsing { //Parse Parameters for(qint32 i = 1; i < t_qCommandList.size(); ++i) { printf(" %s", t_qCommandList[i].toLatin1().constData()); m_rawCommand.pValues().append(t_qCommandList[i]); } } printf("\r\n"); notify(); } return true; }
void JsonParser::parseAndSet(const QByteArray &data, const QString label) { //qDebug() << "parseAndSet data:" << data; QJsonDocument jsonDocument = QJsonDocument::fromJson(data); QJsonObject jsonObject = jsonDocument.object(); // get project versions (7.0, 8.0, ...) for (QJsonObject::Iterator itProjectVersions = jsonObject.begin(); itProjectVersions != jsonObject.end(); itProjectVersions++) { QString project = itProjectVersions.key(); QString projectUrl = itProjectVersions.value().toObject()["url"].toString(); // get projects (imx6, Wetek, ...) QJsonObject val = itProjectVersions.value().toObject()["project"].toObject(); for (QJsonObject::Iterator itProjects = val.begin(); itProjects != val.end(); itProjects++) { QString projectId = itProjects.key(); QString projectName = itProjects.value().toObject()["displayName"].toString(); // skip Virtual if (projectId == "Virtual.x86_64") continue; if (label != "") projectName = projectName + " - " + label; QVariantMap projectData; projectData.insert("name", projectName); projectData.insert("id", projectId); projectData.insert("url", projectUrl); // get releases QJsonObject val1 = itProjects.value().toObject(); for (QJsonObject::Iterator itReleasesNode = val1.begin(); itReleasesNode != val1.end(); itReleasesNode++) { QList<QVariantMap> images; JsonData projectCheck; projectCheck.name = projectName; int projectIx = dataList.indexOf(projectCheck); QJsonObject val2 = itReleasesNode.value().toObject(); for (QJsonObject::Iterator itReleases = val2.begin(); itReleases != val2.end(); itReleases++) { QJsonObject projectReleasesList = itReleases.value().toObject(); for (QJsonObject::Iterator itImageFile = projectReleasesList.begin(); itImageFile != projectReleasesList.end(); itImageFile++) { QString imageName = itImageFile.value().toObject().toVariantMap()["name"].toString(); if (! imageName.endsWith(".img.gz")) continue; // we want to see only image files if (projectIx < 0) { // new project images.append(itImageFile.value().toObject().toVariantMap()); } else { // old project dataList[projectIx].images.append(itImageFile.value().toObject().toVariantMap()); } } } if (projectIx < 0) { // new project JsonData projectData(projectName, projectId, projectUrl, images); dataList.append(projectData); } } } } QCollator collator; collator.setNumericMode(true); collator.setCaseSensitivity(Qt::CaseSensitive); std::sort(dataList.begin(), dataList.end(), [&collator](const JsonData &proj1, const JsonData &proj2) {return collator.compare(proj1.name, proj2.name) > 0;}); for (int ix = 0; ix < dataList.size(); ix++) std::sort(dataList[ix].images.begin(), dataList[ix].images.end(), compareVersion); }