* @brief Apply a bitmask of DatasetOption flags in one call. */
| 2281 | * @brief Apply a bitmask of DatasetOption flags in one call. |
| 2282 | */ |
| 2283 | API::CommandResponse API::Handlers::ProjectHandler::datasetSetOptions(const QString& id, |
| 2284 | const QJsonObject& params) |
| 2285 | { |
| 2286 | if (!params.contains(QStringLiteral("groupId"))) |
| 2287 | return CommandResponse::makeError( |
| 2288 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: groupId")); |
| 2289 | |
| 2290 | if (!params.contains(Keys::DatasetId)) |
| 2291 | return CommandResponse::makeError( |
| 2292 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: datasetId")); |
| 2293 | |
| 2294 | if (!params.contains(QStringLiteral("options"))) |
| 2295 | return CommandResponse::makeError( |
| 2296 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: options")); |
| 2297 | |
| 2298 | const int groupId = params.value(QStringLiteral("groupId")).toInt(); |
| 2299 | const int datasetId = params.value(Keys::DatasetId).toInt(); |
| 2300 | |
| 2301 | int options = 0; |
| 2302 | const QJsonValue optionsJson = params.value(QStringLiteral("options")); |
| 2303 | if (optionsJson.isArray()) { |
| 2304 | QStringList slugs; |
| 2305 | for (const auto& v : optionsJson.toArray()) |
| 2306 | slugs.append(v.toString()); |
| 2307 | |
| 2308 | options = API::EnumLabels::datasetOptionsSlugsToBits(slugs); |
| 2309 | } else { |
| 2310 | options = optionsJson.toInt(); |
| 2311 | } |
| 2312 | |
| 2313 | auto& project = DataModel::ProjectModel::instance(); |
| 2314 | const auto& groups = project.groups(); |
| 2315 | if (groupId < 0 || static_cast<size_t>(groupId) >= groups.size()) |
| 2316 | return CommandResponse::makeError( |
| 2317 | id, ErrorCode::InvalidParam, QStringLiteral("Group id not found: %1").arg(groupId)); |
| 2318 | |
| 2319 | if (datasetId < 0 || static_cast<size_t>(datasetId) >= groups[groupId].datasets.size()) |
| 2320 | return CommandResponse::makeError(id, |
| 2321 | ErrorCode::InvalidParam, |
| 2322 | QStringLiteral("Dataset id not found: %1 in group %2") |
| 2323 | .arg(QString::number(datasetId), QString::number(groupId))); |
| 2324 | |
| 2325 | DataModel::Dataset d = groups[groupId].datasets[datasetId]; |
| 2326 | d.plt = (options & SerialStudio::DatasetPlot) != 0; |
| 2327 | d.fft = (options & SerialStudio::DatasetFFT) != 0; |
| 2328 | d.led = (options & SerialStudio::DatasetLED) != 0; |
| 2329 | d.waterfall = (options & SerialStudio::DatasetWaterfall) != 0; |
| 2330 | |
| 2331 | const QString chosen = widgetForDatasetOptions(options); |
| 2332 | const bool wasOneOf = d.widget == QStringLiteral("bar") || d.widget == QStringLiteral("gauge") |
| 2333 | || d.widget == QStringLiteral("compass"); |
| 2334 | if (!chosen.isEmpty()) |
| 2335 | d.widget = chosen; |
| 2336 | else if (wasOneOf) |
| 2337 | d.widget = QString(); |
| 2338 | |
| 2339 | project.updateDataset(groupId, datasetId, d, true); |
| 2340 |