Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) MultipleChoiceQuestionDialog

 

STLQt CreatorLubuntu

 

MultipleChoiceQuestionDialog is a dialog for MultipleChoiceQuestion.

Technical facts

 

 

 

 

 

 

./CppMultipleChoiceQuestionDialog/CppMultipleChoiceQuestionDialog.pri

 

INCLUDEPATH += \
    ../../Classes/CppMultipleChoiceQuestionDialog

SOURCES += \
    ../../Classes/CppMultipleChoiceQuestionDialog/multiplechoicequestiondialog.cpp

HEADERS  += \
    ../../Classes/CppMultipleChoiceQuestionDialog/multiplechoicequestiondialog.h

OTHER_FILES += \
    ../../Classes/CppMultipleChoiceQuestionDialog/Licence.txt

 

 

 

 

 

./CppMultipleChoiceQuestionDialog/multiplechoicequestiondialog.h

 

//---------------------------------------------------------------------------
/*
MultipleChoiceQuestionDialog, dialog for MultipleChoiceQuestion
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppMultipleChoiceQuestionDialog.htm
//---------------------------------------------------------------------------
#ifndef MULTIPLECHOICEQUESTIONDIALOG_H
#define MULTIPLECHOICEQUESTIONDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include "questiondialog.h"
#pragma GCC diagnostic pop

namespace ribi {

struct MultipleChoiceQuestion;
struct Question;

struct MultipleChoiceQuestionDialog final : public QuestionDialog
{
  explicit MultipleChoiceQuestionDialog(const std::string& question);

  ///Will work if question is not nullptr
  explicit MultipleChoiceQuestionDialog(const boost::shared_ptr<const MultipleChoiceQuestion> question);

  ~MultipleChoiceQuestionDialog() = default;

  ///The answer the user has typed in so far
  int GetAnswerInProgress() const noexcept { return m_answer_in_progress; }

  boost::shared_ptr<const MultipleChoiceQuestion> GetMultipleChoiceQuestion() const noexcept { return m_question; }


  boost::shared_ptr<const Question> GetQuestion() const noexcept;

  ///Obtain the version
  static std::string GetVersion() noexcept;

  ///Obtain the version history
  static std::vector<std::string> GetVersionHistory() noexcept;

  ///The answer the user has selected
  ///Used for synching between multiple QtMultipleChoiceQuestionDialogs displaying
  ///the same MultipleChoiceQuestionDialog
  void SetAnswerInProgress(const int index) noexcept;

  ///Submit an answer
  ///For a multiple choice question, s will be the index of the answer
  void Submit(const std::string& s);

  std::string ToStr() const noexcept;

  mutable boost::signals2::signal<void (MultipleChoiceQuestionDialog*)> m_signal_mc_question_changed;

  private:
  int m_answer_in_progress;

  const boost::shared_ptr<const MultipleChoiceQuestion> m_question;

  ///Create a default Question
  //static MultipleChoiceQuestion * CreateDefaultQuestion();
  static boost::shared_ptr<MultipleChoiceQuestion> CreateDefaultQuestion() noexcept;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // MULTIPLECHOICEQUESTIONDIALOG_H

 

 

 

 

 

./CppMultipleChoiceQuestionDialog/multiplechoicequestiondialog.cpp

 

//---------------------------------------------------------------------------
/*
MultipleChoiceQuestionDialog, dialog for MultipleChoiceQuestion
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppMultipleChoiceQuestionDialog.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include "multiplechoicequestiondialog.h"

#include <cassert>
#include <boost/lexical_cast.hpp>
#include <boost/scoped_ptr.hpp>

#include "multiplechoicequestion.h"
#include "question.h"
#include "testtimer.h"
#include "trace.h"
#pragma GCC diagnostic pop

ribi::MultipleChoiceQuestionDialog::MultipleChoiceQuestionDialog(
  const boost::shared_ptr<const MultipleChoiceQuestion> question)
  : m_signal_mc_question_changed{},
    m_answer_in_progress{-1},
    m_question(question)
{
  #ifndef NDEBUG
  Test();
  #endif
  assert(GetQuestion());
  assert(!HasSubmitted());
}

ribi::MultipleChoiceQuestionDialog::MultipleChoiceQuestionDialog(const std::string& question)
  : m_signal_mc_question_changed{},
    m_answer_in_progress{-1},
    m_question(new MultipleChoiceQuestion(question))
{
  #ifndef NDEBUG
  Test();
  #endif
  assert(!HasSubmitted());
  assert(GetQuestion());
}

boost::shared_ptr<ribi::MultipleChoiceQuestion>
  ribi::MultipleChoiceQuestionDialog::CreateDefaultQuestion() noexcept
{
  return boost::shared_ptr<MultipleChoiceQuestion>(
    new MultipleChoiceQuestion(
      "*",
      "1+2=",
      "3",
      {"1","2","4","5"}
    )
  );
}

boost::shared_ptr<const ribi::Question> ribi::MultipleChoiceQuestionDialog::GetQuestion() const noexcept
{
  return m_question;
}

std::string ribi::MultipleChoiceQuestionDialog::GetVersion() noexcept
{
  return "1.1";
}

std::vector<std::string> ribi::MultipleChoiceQuestionDialog::GetVersionHistory() noexcept
{
  return {
    "2011-06-29: version 1.0: initial version",
    "2013-10-24: version 1.1: added testing"
  };
}

void ribi::MultipleChoiceQuestionDialog::Submit(const std::string& s)
{
  if (HasSubmitted())
  {
    throw std::logic_error("Cannot submit a second answer");
  }
  assert(!HasSubmitted());
  try
  {
    const int index = boost::lexical_cast<int>(s);
    const int sz = this->GetMultipleChoiceQuestion()->GetOptions().size();
    if (index < 0)
    {
      throw std::logic_error("Must submit a positive index to a multiple choice question dialog");
    }
    if (index >= sz)
    {
      throw std::logic_error("Must submit an existing index to a multiple choice question dialog");
    }
    //The real (that is non-index) answer
    const std::string t = this->GetMultipleChoiceQuestion()->GetOptions()[index];
    this->SetIsCorrect(GetQuestion()->IsCorrect(t));
  }
  catch (boost::bad_lexical_cast&)
  {
    throw std::logic_error("Must submit an index to a multiple choice question dialog");
  }
}

#ifndef NDEBUG
void ribi::MultipleChoiceQuestionDialog::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
  //Test setting the multiple choice questions
  for(const std::string& s: MultipleChoiceQuestion::GetValidMultipleChoiceQuestions())
  {
    const boost::shared_ptr<MultipleChoiceQuestion> q {
      new MultipleChoiceQuestion(s)
    };
    assert(q);
    const auto d = std::make_unique<MultipleChoiceQuestionDialog>(q);
    //const boost::scoped_ptr<MultipleChoiceQuestionDialog> d {
    //  new MultipleChoiceQuestionDialog(q)
    //};
    assert(d);
    assert(!d->HasSubmitted() );
  }
  //Test submitting correct and incorrect answers to this dialog
  {
    const std::vector<std::string> valid {
      MultipleChoiceQuestion::GetValidMultipleChoiceQuestions()
    };
    for (const std::string& s: valid)
    {
      //Create a question
      const boost::shared_ptr<const MultipleChoiceQuestion> question {
        new MultipleChoiceQuestion(s)
      };

      //Obtain the shuffled possibilities
      const std::vector<std::string> options = question->GetOptions();
      assert(options == question->GetOptions()
        && "The possibilities must be shuffled exactly once");

      //Submit correct answer to this dialog
      {
        boost::scoped_ptr<MultipleChoiceQuestionDialog> dialog {
          new MultipleChoiceQuestionDialog(
            question
          )
        };
        assert(!dialog->HasSubmitted());

        const std::string answer = question->GetAnswer();
        assert(!question->GetWrongAnswers().empty());

        const int index = std::distance(options.begin(),std::find(options.begin(),options.end(),answer));
        assert(index >= 0);
        assert(index < static_cast<int>(options.size()));

        dialog->Submit(boost::lexical_cast<std::string>(index));

        assert(dialog->HasSubmitted());
        assert(dialog->IsAnswerCorrect());
      }
      //Submit incorrect answer to this dialog
      {
        boost::scoped_ptr<MultipleChoiceQuestionDialog> dialog {
          new MultipleChoiceQuestionDialog(
            question
          )
        };
        assert(!dialog->HasSubmitted());

        assert(!question->GetWrongAnswers().empty());
        const std::string wrong_answer = question->GetWrongAnswers().at(0);

        const int index = std::distance(options.begin(),std::find(options.begin(),options.end(),wrong_answer));
        assert(index >= 0);
        assert(index < static_cast<int>(options.size()));

        dialog->Submit(boost::lexical_cast<std::string>(index));

        assert(dialog->HasSubmitted());
        assert(!dialog->IsAnswerCorrect());
      }
    }
  }
}
#endif

std::string ribi::MultipleChoiceQuestionDialog::ToStr() const noexcept
{
  std::stringstream s;
  s << m_question->ToStr();
  return s.str();
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml