| 131 | } |
| 132 | |
| 133 | void TutorialBuilder::readTutorialRequirements() |
| 134 | { |
| 135 | // open and read the file |
| 136 | if(m_jsonFile.fileName().isEmpty()) { |
| 137 | throw std::runtime_error("No file name was provided."); |
| 138 | } |
| 139 | |
| 140 | m_jsonFile.open(QIODevice::ReadOnly | QIODevice::Text); |
| 141 | if(!m_jsonFile.isOpen()) { |
| 142 | throw std::runtime_error("File could not be opened (read): " + m_jsonFile.fileName().toStdString()); |
| 143 | } else { |
| 144 | qDebug(CAT_TUTORIALBUILDER) << "File opened (read): " << m_jsonFile.fileName(); |
| 145 | } |
| 146 | QString contents = m_jsonFile.readAll(); |
| 147 | m_jsonFile.close(); |
| 148 | |
| 149 | // try to parse the contents as a json |
| 150 | QJsonParseError parse_error{}; |
| 151 | QJsonDocument document = QJsonDocument::fromJson(contents.toUtf8(), &parse_error); |
| 152 | if(document.isNull()) { |
| 153 | throw std::runtime_error("Invalid json: " + parse_error.errorString().toStdString()); |
| 154 | } |
| 155 | |
| 156 | // extract the values related to the m_jsonEntry mode |
| 157 | QJsonObject document_object = document.object(); |
| 158 | QJsonValue value = document_object.value(m_jsonEntry); |
| 159 | if(value == QJsonValue::Undefined) { |
| 160 | throw std::runtime_error("Invalid json: Could not extract value " + m_jsonEntry.toStdString()); |
| 161 | } |
| 162 | |
| 163 | // turn the json values into an array of ChapterInstructions and save the data |
| 164 | // each element from the json array must have the following structure: |
| 165 | // { |
| 166 | // "index": int |
| 167 | // "name": [string] |
| 168 | // "description": string |
| 169 | // "main_widget": string |
| 170 | // "x_offset": int, optional |
| 171 | // "y_offset": int, optional |
| 172 | // "anchor": string, HoverPosition, optional |
| 173 | // "content": string, HoverPosition, optional |
| 174 | // } |
| 175 | QJsonArray chapters_array = value.toArray(); |
| 176 | for(QJsonValueRef item : chapters_array) { |
| 177 | QJsonObject item_object = item.toObject(); |
| 178 | int index = item_object.value("index").toInt(-1); |
| 179 | QStringList names; |
| 180 | QJsonArray jsonNameArray = item_object.value("names").toArray(); |
| 181 | for(auto name : jsonNameArray) { |
| 182 | names.push_back(name.toString()); |
| 183 | } |
| 184 | QString description = item_object.value("description").toString(); |
| 185 | QString mainWidget = item_object.value("main_widget").toString(); |
| 186 | int x_offset = item_object.value("x_offset").toInt(0); |
| 187 | int y_offset = item_object.value("y_offset").toInt(0); |
| 188 | QString anchorString = item_object.value("anchor").toString(); |
| 189 | QString contentString = item_object.value("content").toString(); |
| 190 | HoverPosition anchor = convertStringToHoverPosition(anchorString); |