* @brief Add a dataset to a specific group by id */
| 1879 | * @brief Add a dataset to a specific group by id |
| 1880 | */ |
| 1881 | API::CommandResponse API::Handlers::ProjectHandler::datasetAdd(const QString& id, |
| 1882 | const QJsonObject& params) |
| 1883 | { |
| 1884 | if (!params.contains(QStringLiteral("groupId"))) |
| 1885 | return CommandResponse::makeError( |
| 1886 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: groupId")); |
| 1887 | |
| 1888 | if (!params.contains(QStringLiteral("options"))) |
| 1889 | return CommandResponse::makeError( |
| 1890 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: options")); |
| 1891 | |
| 1892 | const int groupId = params.value(QStringLiteral("groupId")).toInt(); |
| 1893 | const int options = params.value(QStringLiteral("options")).toInt(); |
| 1894 | if (options < 0 || options > 0b11111111) |
| 1895 | return CommandResponse::makeError( |
| 1896 | id, ErrorCode::InvalidParam, QStringLiteral("Invalid options: must be 0-255 (bit flags)")); |
| 1897 | |
| 1898 | auto& project = DataModel::ProjectModel::instance(); |
| 1899 | const auto& groups = project.groups(); |
| 1900 | if (groupId < 0 || static_cast<size_t>(groupId) >= groups.size()) |
| 1901 | return CommandResponse::makeError( |
| 1902 | id, ErrorCode::InvalidParam, QStringLiteral("Group id not found: %1").arg(groupId)); |
| 1903 | |
| 1904 | SerialStudio::DatasetOption headline = SerialStudio::DatasetGeneric; |
| 1905 | for (const auto cand : {SerialStudio::DatasetPlot, |
| 1906 | SerialStudio::DatasetFFT, |
| 1907 | SerialStudio::DatasetBar, |
| 1908 | SerialStudio::DatasetGauge, |
| 1909 | SerialStudio::DatasetCompass, |
| 1910 | SerialStudio::DatasetLED, |
| 1911 | SerialStudio::DatasetWaterfall, |
| 1912 | SerialStudio::DatasetMeter}) { |
| 1913 | if (options & cand) { |
| 1914 | headline = cand; |
| 1915 | break; |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | project.setSelectedGroup(groups[groupId]); |
| 1920 | project.addDataset(headline); |
| 1921 | |
| 1922 | const int remaining = options & ~static_cast<int>(headline); |
| 1923 | const auto& post = project.groups(); |
| 1924 | const int newIndex = static_cast<int>(post[groupId].datasets.size()) - 1; |
| 1925 | if (remaining != 0 && newIndex >= 0) { |
| 1926 | DataModel::Dataset d = post[groupId].datasets[newIndex]; |
| 1927 | applyDatasetVisualizationFlags(d, remaining); |
| 1928 | project.updateDataset(groupId, newIndex, d, true); |
| 1929 | } |
| 1930 | |
| 1931 | QJsonObject result; |
| 1932 | result[QStringLiteral("groupId")] = groupId; |
| 1933 | result[QStringLiteral("options")] = options; |
| 1934 | return CommandResponse::makeSuccess(id, result); |
| 1935 | } |
| 1936 | |
| 1937 | /** |
| 1938 | * @brief Add several datasets to a group in one call (bulk creation). |
nothing calls this directly
no test coverage detected