diff --git a/Modules/Forms/files.cmake b/Modules/Forms/files.cmake index 6f8dc23d24..ffa7c50a1e 100644 --- a/Modules/Forms/files.cmake +++ b/Modules/Forms/files.cmake @@ -1,29 +1,31 @@ set(H_FILES include/mitkCheckboxesQuestion.h include/mitkDropdownQuestion.h include/mitkForm.h include/mitkIQuestionFactory.h include/mitkLinearScaleQuestion.h include/mitkMultipleChoiceQuestion.h include/mitkParagraphQuestion.h include/mitkQuestion.h include/mitkQuestionWithOptions.h + include/mitkQuestionWithOtherOption.h include/mitkShortAnswerQuestion.h include/mitkTextQuestion.h ) set(CPP_FILES mitkCheckboxesQuestion.cpp mitkDropdownQuestion.cpp mitkForm.cpp mitkIQuestionFactory.cpp mitkLinearScaleQuestion.cpp mitkModuleActivator.cpp mitkMultipleChoiceQuestion.cpp mitkParagraphQuestion.cpp mitkQuestion.cpp mitkQuestionFactory.cpp mitkQuestionWithOptions.cpp + mitkQuestionWithOtherOption.cpp mitkShortAnswerQuestion.cpp mitkTextQuestion.cpp ) diff --git a/Modules/Forms/include/mitkCheckboxesQuestion.h b/Modules/Forms/include/mitkCheckboxesQuestion.h index a5df48f5a9..713263f095 100644 --- a/Modules/Forms/include/mitkCheckboxesQuestion.h +++ b/Modules/Forms/include/mitkCheckboxesQuestion.h @@ -1,43 +1,42 @@ /*============================================================================ 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 mitkCheckboxesQuestion_h #define mitkCheckboxesQuestion_h -#include +#include namespace mitk::Forms { - class MITKFORMS_EXPORT CheckboxesQuestion : public QuestionWithOptions + class MITKFORMS_EXPORT CheckboxesQuestion : public QuestionWithOtherOption { public: ~CheckboxesQuestion() override; std::string GetType() const override; Question* CreateAnother() const override; void FromJSON(const nlohmann::ordered_json& j) override; void ToJSON(nlohmann::ordered_json& j) const override; using QuestionWithOptions::AddResponse; using QuestionWithOptions::RemoveResponse; - using QuestionWithOptions::EnableOtherOption; - using QuestionWithOptions::HasOtherOption; - using QuestionWithOptions::AddOtherResponse; - using QuestionWithOptions::RemoveOtherResponse; + + using QuestionWithOtherOption::AddOtherResponse; + using QuestionWithOtherOption::RemoveOtherResponse; }; MITKFORMS_EXPORT void from_json(const nlohmann::ordered_json& j, CheckboxesQuestion& q); MITKFORMS_EXPORT void to_json(nlohmann::ordered_json& j, const CheckboxesQuestion& q); } #endif diff --git a/Modules/Forms/include/mitkMultipleChoiceQuestion.h b/Modules/Forms/include/mitkMultipleChoiceQuestion.h index 22af1cc293..90100ef835 100644 --- a/Modules/Forms/include/mitkMultipleChoiceQuestion.h +++ b/Modules/Forms/include/mitkMultipleChoiceQuestion.h @@ -1,41 +1,39 @@ /*============================================================================ 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 mitkMultipleChoiceQuestion_h #define mitkMultipleChoiceQuestion_h -#include +#include namespace mitk::Forms { - class MITKFORMS_EXPORT MultipleChoiceQuestion : public QuestionWithOptions + class MITKFORMS_EXPORT MultipleChoiceQuestion : public QuestionWithOtherOption { public: ~MultipleChoiceQuestion() override; std::string GetType() const override; Question* CreateAnother() const override; void FromJSON(const nlohmann::ordered_json& j) override; void ToJSON(nlohmann::ordered_json& j) const override; using QuestionWithOptions::SetResponse; - using QuestionWithOptions::EnableOtherOption; - using QuestionWithOptions::HasOtherOption; - using QuestionWithOptions::SetOtherResponse; + using QuestionWithOtherOption::SetOtherResponse; }; MITKFORMS_EXPORT void from_json(const nlohmann::ordered_json& j, MultipleChoiceQuestion& q); MITKFORMS_EXPORT void to_json(nlohmann::ordered_json& j, const MultipleChoiceQuestion& q); } #endif diff --git a/Modules/Forms/include/mitkQuestionWithOptions.h b/Modules/Forms/include/mitkQuestionWithOptions.h index c0afb963c4..024d351363 100644 --- a/Modules/Forms/include/mitkQuestionWithOptions.h +++ b/Modules/Forms/include/mitkQuestionWithOptions.h @@ -1,60 +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); std::vector GetOptions() const; - bool HasOtherOption() const; - protected: void AddResponse(size_t i); void RemoveResponse(size_t i); - void SetResponse(size_t i); - - void EnableOtherOption(); - - void AddOtherResponse(const std::string& response); - void RemoveOtherResponse(); - - void SetOtherResponse(const std::string& response); + virtual void SetResponse(size_t i); private: std::vector m_Options; std::vector m_Responses; - bool m_EnableOtherOption = false; - std::optional m_OtherResponse; }; 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/include/mitkQuestionWithOptions.h b/Modules/Forms/include/mitkQuestionWithOtherOption.h similarity index 64% copy from Modules/Forms/include/mitkQuestionWithOptions.h copy to Modules/Forms/include/mitkQuestionWithOtherOption.h index c0afb963c4..9c560a84e0 100644 --- a/Modules/Forms/include/mitkQuestionWithOptions.h +++ b/Modules/Forms/include/mitkQuestionWithOtherOption.h @@ -1,60 +1,52 @@ /*============================================================================ 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 +#ifndef mitkQuestionWithOtherOption_h +#define mitkQuestionWithOtherOption_h -#include +#include #include namespace mitk::Forms { - class MITKFORMS_EXPORT QuestionWithOptions : public Question + class MITKFORMS_EXPORT QuestionWithOtherOption : public QuestionWithOptions { public: - ~QuestionWithOptions() override; + QuestionWithOtherOption(); + ~QuestionWithOtherOption() override; std::vector GetResponsesAsStrings() const override; void ClearResponses() override; bool IsComplete() const override; - void AddOption(const std::string& option); - std::vector GetOptions() const; - bool HasOtherOption() const; + void EnableOtherOption(); protected: - void AddResponse(size_t i); - void RemoveResponse(size_t i); - - void SetResponse(size_t i); - - void EnableOtherOption(); + void SetResponse(size_t i) override; void AddOtherResponse(const std::string& response); void RemoveOtherResponse(); void SetOtherResponse(const std::string& response); private: - std::vector m_Options; - std::vector m_Responses; - bool m_EnableOtherOption = false; + bool m_HasOtherOption; std::optional m_OtherResponse; }; - MITKFORMS_EXPORT void from_json(const nlohmann::ordered_json& j, QuestionWithOptions& q); - MITKFORMS_EXPORT void to_json(nlohmann::ordered_json& j, const QuestionWithOptions& q); + MITKFORMS_EXPORT void from_json(const nlohmann::ordered_json& j, QuestionWithOtherOption& q); + MITKFORMS_EXPORT void to_json(nlohmann::ordered_json& j, const QuestionWithOtherOption& q); } #endif diff --git a/Modules/Forms/src/mitkCheckboxesQuestion.cpp b/Modules/Forms/src/mitkCheckboxesQuestion.cpp index dde85eb049..4945d756c9 100644 --- a/Modules/Forms/src/mitkCheckboxesQuestion.cpp +++ b/Modules/Forms/src/mitkCheckboxesQuestion.cpp @@ -1,55 +1,48 @@ /*============================================================================ 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; CheckboxesQuestion::~CheckboxesQuestion() = default; std::string CheckboxesQuestion::GetType() const { return "Checkboxes"; } Question* CheckboxesQuestion::CreateAnother() const { return new CheckboxesQuestion; } void CheckboxesQuestion::FromJSON(const nlohmann::ordered_json& j) { from_json(j, *this); } void CheckboxesQuestion::ToJSON(nlohmann::ordered_json& j) const { to_json(j, *this); } void mitk::Forms::from_json(const ordered_json& j, CheckboxesQuestion& q) { - from_json(j, static_cast(q)); - - if (j.contains("Other") && j["Other"] == true) - q.EnableOtherOption(); + from_json(j, static_cast(q)); } void mitk::Forms::to_json(ordered_json& j, const CheckboxesQuestion& q) { - to_json(j, static_cast(q)); - - if (q.HasOtherOption()) - j["Other"] = true; + to_json(j, static_cast(q)); } diff --git a/Modules/Forms/src/mitkMultipleChoiceQuestion.cpp b/Modules/Forms/src/mitkMultipleChoiceQuestion.cpp index 7655ccac71..78ef357c4f 100644 --- a/Modules/Forms/src/mitkMultipleChoiceQuestion.cpp +++ b/Modules/Forms/src/mitkMultipleChoiceQuestion.cpp @@ -1,55 +1,48 @@ /*============================================================================ 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; MultipleChoiceQuestion::~MultipleChoiceQuestion() = default; std::string MultipleChoiceQuestion::GetType() const { return "Multiple choice"; } Question* MultipleChoiceQuestion::CreateAnother() const { return new MultipleChoiceQuestion; } void MultipleChoiceQuestion::FromJSON(const nlohmann::ordered_json& j) { from_json(j, *this); } void MultipleChoiceQuestion::ToJSON(nlohmann::ordered_json& j) const { to_json(j, *this); } void mitk::Forms::from_json(const ordered_json& j, MultipleChoiceQuestion& q) { - from_json(j, static_cast(q)); - - if (j.contains("Other") && j["Other"] == true) - q.EnableOtherOption(); + from_json(j, static_cast(q)); } void mitk::Forms::to_json(ordered_json& j, const MultipleChoiceQuestion& q) { - to_json(j, static_cast(q)); - - if (q.HasOtherOption()) - j["Other"] = true; + to_json(j, static_cast(q)); } diff --git a/Modules/Forms/src/mitkQuestionWithOptions.cpp b/Modules/Forms/src/mitkQuestionWithOptions.cpp index f4f499eefb..00a5d25bca 100644 --- a/Modules/Forms/src/mitkQuestionWithOptions.cpp +++ b/Modules/Forms/src/mitkQuestionWithOptions.cpp @@ -1,122 +1,83 @@ /*============================================================================ 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 - #include using namespace mitk::Forms; using namespace nlohmann; QuestionWithOptions::~QuestionWithOptions() = default; void QuestionWithOptions::AddOption(const std::string& option) { m_Options.push_back(option); } std::vector QuestionWithOptions::GetOptions() const { return m_Options; } -void QuestionWithOptions::EnableOtherOption() -{ - m_EnableOtherOption = true; -} - -bool QuestionWithOptions::HasOtherOption() const -{ - return m_EnableOtherOption; -} - 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_OtherResponse.reset(); m_Responses = { i }; } -void QuestionWithOptions::AddOtherResponse(const std::string& response) -{ - if (!m_EnableOtherOption) - mitkThrow() << "Adding \"Other\" response is disallowed!"; - - m_OtherResponse = response; -} - -void QuestionWithOptions::RemoveOtherResponse() -{ - m_OtherResponse.reset(); -} - -void QuestionWithOptions::SetOtherResponse(const std::string& response) -{ - if (!m_EnableOtherOption) - mitkThrow() << "Setting \"Other\" response is disallowed!"; - - m_Responses.clear(); - m_OtherResponse = response; -} - std::vector QuestionWithOptions::GetResponsesAsStrings() const { std::vector responses; for (const auto i : m_Responses) responses.push_back(m_Options[i]); - if (m_OtherResponse.has_value()) - responses.push_back(m_OtherResponse.value()); - return responses; } void QuestionWithOptions::ClearResponses() { m_Responses.clear(); - m_OtherResponse.reset(); } bool QuestionWithOptions::IsComplete() const { - return !m_Responses.empty() || m_OtherResponse.has_value(); + 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(); } diff --git a/Modules/Forms/src/mitkQuestionWithOtherOption.cpp b/Modules/Forms/src/mitkQuestionWithOtherOption.cpp new file mode 100644 index 0000000000..eaa9f5ad97 --- /dev/null +++ b/Modules/Forms/src/mitkQuestionWithOtherOption.cpp @@ -0,0 +1,101 @@ +/*============================================================================ + +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 + +#include + +using namespace mitk::Forms; +using namespace nlohmann; + +QuestionWithOtherOption::QuestionWithOtherOption() + : m_HasOtherOption(false) +{ +} + +QuestionWithOtherOption::~QuestionWithOtherOption() = default; + +void QuestionWithOtherOption::EnableOtherOption() +{ + m_HasOtherOption = true; +} + +bool QuestionWithOtherOption::HasOtherOption() const +{ + return m_HasOtherOption; +} + +void QuestionWithOtherOption::SetResponse(size_t i) +{ + QuestionWithOptions::SetResponse(i); + m_OtherResponse.reset(); +} + +void QuestionWithOtherOption::AddOtherResponse(const std::string& response) +{ + if (!m_HasOtherOption) + mitkThrow() << "Adding \"Other\" response is disallowed!"; + + m_OtherResponse = response; +} + +void QuestionWithOtherOption::RemoveOtherResponse() +{ + m_OtherResponse.reset(); +} + +void QuestionWithOtherOption::SetOtherResponse(const std::string& response) +{ + if (!m_HasOtherOption) + mitkThrow() << "Setting \"Other\" response is disallowed!"; + + QuestionWithOptions::ClearResponses(); + m_OtherResponse = response; +} + +std::vector QuestionWithOtherOption::GetResponsesAsStrings() const +{ + auto responses = QuestionWithOptions::GetResponsesAsStrings(); + + if (m_OtherResponse.has_value()) + responses.push_back(m_OtherResponse.value()); + + return responses; +} + +void QuestionWithOtherOption::ClearResponses() +{ + QuestionWithOptions::ClearResponses(); + m_OtherResponse.reset(); +} + +bool QuestionWithOtherOption::IsComplete() const +{ + return QuestionWithOptions::IsComplete() || m_OtherResponse.has_value(); +} + +void mitk::Forms::from_json(const ordered_json& j, QuestionWithOtherOption& q) +{ + from_json(j, static_cast(q)); + + if (j.contains("Other") && j["Other"] == true) + q.EnableOtherOption(); +} + +void mitk::Forms::to_json(ordered_json& j, const QuestionWithOtherOption& q) +{ + to_json(j, static_cast(q)); + + if (q.HasOtherOption()) + j["Other"] = true; +}