Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QuestionDialog

 

QuestionDialog is a dialog for Question.

 

 

 

 

 

questiondialog.h

 

//---------------------------------------------------------------------------
/*
QuestionDialog, dialog for Question
Copyright (C) 2011 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/CppQtQuestionDialog.htm
//---------------------------------------------------------------------------
#ifndef QUESTIONDIALOG_H
#define QUESTIONDIALOG_H
//---------------------------------------------------------------------------
#include <boost/checked_delete.hpp>
#include <boost/shared_ptr.hpp>
//---------------------------------------------------------------------------
struct Question;
//---------------------------------------------------------------------------
///Dialog for an Question
struct QuestionDialog
{
  QuestionDialog(const boost::shared_ptr<Question>& question);

  ///Check if an answer can be submitted
  bool CanSubmit() const { return !m_has_submitted; }

  ///Get all possible correct answers
  const std::vector<std::string> GetCorrectAnswers() const;

  ///Obtain the question
  boost::shared_ptr<Question> GetQuestion() const { return m_question; }

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

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

  ///Check if an answer has been submitted
  bool HasSubmitted() const { return m_has_submitted; }

  ///See if the submitted answer is correct
  bool IsAnswerCorrect() const;

  ///(Re)set the Question
  //void SetQuestion(const Question * const question);
  void SetQuestion(const boost::shared_ptr<Question>& question);

  ///Submit an answer
  void Submit(const std::string& s);

  protected:
  virtual ~QuestionDialog() {}
  friend void boost::checked_delete<>(QuestionDialog*);

  private:
  ///The question
  boost::shared_ptr<Question> m_question;

  ///Has the user already submitted an answer?
  bool m_has_submitted;

  ///Was the submitted answer correct?
  bool m_is_correct;
};
//---------------------------------------------------------------------------
#endif // OPENQUESTIONDIALOG_H

 

 

 

 

 

questiondialog.cpp

 

//---------------------------------------------------------------------------
/*
QuestionDialog, dialog for Question
Copyright (C) 2011 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/CppQtQuestionDialog.htm
//---------------------------------------------------------------------------
#include "openquestion.h"
#include "openquestiondialog.h"
//---------------------------------------------------------------------------
QuestionDialog::QuestionDialog(const boost::shared_ptr<Question>& question)
  : m_has_submitted(false),
    m_is_correct(false)
{
  SetQuestion(question);

  if (question)
  {
    assert(CanSubmit());
    assert(!HasSubmitted());
    assert(GetQuestion());
  }
}
//---------------------------------------------------------------------------
const std::string QuestionDialog::GetVersion()
{
  return "1.0";
}
//---------------------------------------------------------------------------
const std::vector<std::string> QuestionDialog::GetVersionHistory()
{
  std::vector<std::string> v;
  v.push_back("2011-06-29: version 1.0: initial version");
  return v;
}
//---------------------------------------------------------------------------
///See if the submitted answer is correct
bool QuestionDialog::IsAnswerCorrect() const
{
  assert(HasSubmitted());
  return m_is_correct;
}
//---------------------------------------------------------------------------
///Set the Question
void QuestionDialog::SetQuestion(const boost::shared_ptr<Question>& question)
{
  assert(question);
  if (question && question.get() != m_question.get())
  {
    m_question = question;
    m_has_submitted = false;
  }
}
//---------------------------------------------------------------------------
///Submit an answer
void QuestionDialog::Submit(const std::string& s)
{
  assert(CanSubmit());

  m_is_correct = m_question->IsCorrect(s);

  m_has_submitted = true;
  assert(!CanSubmit());
}
//---------------------------------------------------------------------------

 

 

 

 

 

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