diff --git a/Modules/Forms/include/mitkQuestionWithOptions.h b/Modules/Forms/include/mitkQuestionWithOptions.h index 024d351363..b461ba25a3 100644 --- a/Modules/Forms/include/mitkQuestionWithOptions.h +++ b/Modules/Forms/include/mitkQuestionWithOptions.h @@ -1,49 +1,49 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef mitkQuestionWithOptions_h #define mitkQuestionWithOptions_h #include #include namespace mitk::Forms { class MITKFORMS_EXPORT QuestionWithOptions : public Question { public: ~QuestionWithOptions() override; std::vector GetResponsesAsStrings() const override; void ClearResponses() override; bool IsComplete() const override; - void AddOption(const std::string& option); + size_t AddOption(const std::string& option); std::vector GetOptions() const; protected: void AddResponse(size_t i); void RemoveResponse(size_t i); virtual void SetResponse(size_t i); private: std::vector m_Options; std::vector m_Responses; }; MITKFORMS_EXPORT void from_json(const nlohmann::ordered_json& j, QuestionWithOptions& q); MITKFORMS_EXPORT void to_json(nlohmann::ordered_json& j, const QuestionWithOptions& q); } #endif diff --git a/Modules/Forms/src/mitkQuestionWithOptions.cpp b/Modules/Forms/src/mitkQuestionWithOptions.cpp index 00a5d25bca..f375ca5601 100644 --- a/Modules/Forms/src/mitkQuestionWithOptions.cpp +++ b/Modules/Forms/src/mitkQuestionWithOptions.cpp @@ -1,83 +1,85 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #include #include using namespace mitk::Forms; using namespace nlohmann; QuestionWithOptions::~QuestionWithOptions() = default; -void QuestionWithOptions::AddOption(const std::string& option) +size_t QuestionWithOptions::AddOption(const std::string& option) { + auto i = m_Options.size(); m_Options.push_back(option); + return i; } std::vector QuestionWithOptions::GetOptions() const { return m_Options; } void QuestionWithOptions::AddResponse(size_t i) { if (std::find(m_Responses.begin(), m_Responses.end(), i) == m_Responses.end()) m_Responses.insert(std::lower_bound(m_Responses.begin(), m_Responses.end(), i), i); } void QuestionWithOptions::RemoveResponse(size_t i) { m_Responses.erase(std::remove(m_Responses.begin(), m_Responses.end(), i)); } void QuestionWithOptions::SetResponse(size_t i) { m_Responses = { i }; } std::vector QuestionWithOptions::GetResponsesAsStrings() const { std::vector responses; for (const auto i : m_Responses) responses.push_back(m_Options[i]); return responses; } void QuestionWithOptions::ClearResponses() { m_Responses.clear(); } bool QuestionWithOptions::IsComplete() const { return !m_Responses.empty(); } void mitk::Forms::from_json(const ordered_json& j, QuestionWithOptions& q) { from_json(j, static_cast(q)); if (j.contains("Options")) { for (const auto& option : j["Options"]) q.AddOption(option); } } void mitk::Forms::to_json(ordered_json& j, const QuestionWithOptions& q) { to_json(j, static_cast(q)); j["Options"] = q.GetOptions(); }