* @brief Attaches a widget ref to a workspace. */
| 1067 | * @brief Attaches a widget ref to a workspace. |
| 1068 | */ |
| 1069 | API::CommandResponse API::Handlers::WorkspacesHandler::widgetAdd(const QString& id, |
| 1070 | const QJsonObject& params) |
| 1071 | { |
| 1072 | if (!inProjectFileMode()) |
| 1073 | return CommandResponse::makeError( |
| 1074 | id, ErrorCode::InvalidParam, QStringLiteral("Workspace mutations require ProjectFile mode")); |
| 1075 | |
| 1076 | const QStringList required{ |
| 1077 | QStringLiteral("workspaceId"), |
| 1078 | QStringLiteral("widgetType"), |
| 1079 | QStringLiteral("groupId"), |
| 1080 | }; |
| 1081 | |
| 1082 | for (const auto& key : required) |
| 1083 | if (!params.contains(key)) |
| 1084 | return CommandResponse::makeError( |
| 1085 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: %1").arg(key)); |
| 1086 | |
| 1087 | auto& pm = DataModel::ProjectModel::instance(); |
| 1088 | if (!pm.customizeWorkspaces()) |
| 1089 | return CommandResponse::makeError( |
| 1090 | id, |
| 1091 | ErrorCode::InvalidParam, |
| 1092 | QStringLiteral("customizeWorkspaces is off; call project.workspaces.customize.set first")); |
| 1093 | |
| 1094 | const int wid = params.value(QStringLiteral("workspaceId")).toInt(); |
| 1095 | const int gid = params.value(QStringLiteral("groupId")).toInt(); |
| 1096 | |
| 1097 | const QJsonValue wtypeJson = params.value(QStringLiteral("widgetType")); |
| 1098 | const int wtype = resolveWidgetType(wtypeJson); |
| 1099 | if (const auto err = validateResolvedWidgetType(wtype, wtypeJson)) |
| 1100 | return CommandResponse::makeError(id, ErrorCode::InvalidParam, *err); |
| 1101 | |
| 1102 | const auto& wsList = pm.editorWorkspaces(); |
| 1103 | const auto exists = std::any_of( |
| 1104 | wsList.begin(), wsList.end(), [wid](const auto& ws) { return ws.workspaceId == wid; }); |
| 1105 | if (!exists) |
| 1106 | return CommandResponse::makeError( |
| 1107 | id, ErrorCode::InvalidParam, QStringLiteral("Workspace not found: %1").arg(wid)); |
| 1108 | |
| 1109 | const auto& groups = DataModel::ProjectModel::instance().groups(); |
| 1110 | const auto group_it = std::find_if( |
| 1111 | groups.begin(), groups.end(), [gid](const DataModel::Group& g) { return g.uniqueId == gid; }); |
| 1112 | if (group_it == groups.end()) |
| 1113 | return CommandResponse::makeError(id, |
| 1114 | ErrorCode::InvalidParam, |
| 1115 | QStringLiteral("Group not found: %1. Use uniqueId from " |
| 1116 | "project.group.list, not the array index.") |
| 1117 | .arg(gid)); |
| 1118 | |
| 1119 | if (const auto err = validateGroupCompatibility(*group_it, wtype, gid)) |
| 1120 | return CommandResponse::makeError(id, ErrorCode::InvalidParam, *err); |
| 1121 | |
| 1122 | const bool hasRelIndex = params.contains(QStringLiteral("relativeIndex")); |
| 1123 | const bool hasDatasetId = params.contains(Keys::DatasetId); |
| 1124 | const int targetDatasetId = hasDatasetId ? params.value(Keys::DatasetId).toInt() : -1; |
| 1125 | |
| 1126 | if (hasDatasetId) |
nothing calls this directly
no test coverage detected