#include "TriviaApp.h" #include #include #include #include "Question.h" TriviaApp::TriviaApp(int argc, char* argv[]) : QApplication(argc, argv) { } TriviaApp::~TriviaApp() { qDeleteAll(questions); } int TriviaApp::exec() { if ( ! loadQuestions() ) return 1; printQuestions(); return 0; } inline bool showError(const QString& message) { QMessageBox::critical(0, "Error", message); return false; } bool TriviaApp::loadQuestions() { QFile file(triviaFilename); if ( ! file.open(QIODevice::ReadOnly | QIODevice::Text) ) return showError( QString("Could not open %1").arg(triviaFilename) ); QDomDocument document("trivia"); QString errorMsg; int errorLine = 0; int errorColumn = 0; if ( ! document.setContent(&file, &errorMsg, &errorLine, &errorColumn) ) { file.close(); return showError(QString("%1:%2.%3: %4") .arg(triviaFilename).arg(errorLine).arg(errorColumn).arg(errorMsg)); } file.close(); QDomElement triviaElement = document.documentElement(); for( QDomNode node = triviaElement.firstChild(); ! node.isNull(); node = node.nextSibling() ) { QDomElement element = node.toElement(); // try to convert the node to an element. if( element.isNull() || element.tagName() != "question" ) continue; Question* question = createQuestion(element.attribute("type")); if ( ! question->loadFromNode(element) ) { // Hubo una pregunta mala. Que hacer con ella? delete question; continue; } questions.append(question); } return true; } void TriviaApp::printQuestions() const { for ( int i = 0; i < questions.count(); ++i ) questions[i]->print(); } Question* TriviaApp::createQuestion(const QString& /*type*/) { //if ( type == "single_choice" ) return new SingleChoiceQuestion(); //if ( type == "numeric" ) return new NumericQuestion(); //... return new Question(); }