Clear entry line when charging a new quiz
[qquiz] / src / quiz.cpp
index 76d5ac8..311ca13 100644 (file)
@@ -29,17 +29,23 @@ question::~question() {
        delete(label);
 }
 
-quiz::quiz() : current(NULL), correct(0) {
+quiz::quiz() : current(NULL), subset(0), correct(0)  {
        QAction *choose;
+       QAction *about;
 
        window = new QWidget();
-       window->setWindowTitle(QApplication::translate("Qtquiz", "Qtquiz"));
+       window->setWindowTitle(QApplication::translate("Qquiz", "Qquiz"));
 
        menu = new QMenuBar(window);
        choose = new QAction("Open", window);
        QObject::connect(choose, SIGNAL(triggered()), this, SLOT(choose_quiz()));
 
+       about = new QAction("About", window);
+       QObject::connect(about, SIGNAL(triggered()), this, SLOT(about()));
+
        menu->addAction(choose);
+       menu->addAction(about);
+       window->resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        window->show();
 
        retrieve_quizzes();
@@ -110,21 +116,34 @@ void quiz::choose_quiz() {
                        }
                        delete(timer);
                        questions.clear();
+                       subset = 0;
                        correct = 0;
+                       line->clear();
                } else {
                        init_gui();
                }
                current = files.at(items.indexOf(item));
                if(read_quiz(current->path.absolutePath().toStdString().c_str())) {
+                       if (subset) {
+                               trim_questions();
+                       }
+                       build_index();
                        menu->addAction(give_up);
                        display_score();
                        display_grid();
+                       current_time = total_time;
+                       display_timer();
+                       start_dialog();
                        if (total_time) {
-                               current_time = total_time;
                                timer = new QTimer(this);
                                connect(timer, SIGNAL(timeout()), this, SLOT(update_timer()));
                                timer->start(1000);
                        }
+               } else {
+                       QString message = "Can't read file :\n";
+
+                       message.append(current->path.absolutePath());
+                       QMessageBox::warning (window, tr("Input file error"), message);
                }
        }
 }
@@ -139,6 +158,7 @@ int quiz::read_quiz(const char *filename) {
        ifstream ifs (filename);
        max_label_length = 0;
        int i = 0;
+    bool ok;
 
        total_time = 0;
        if (!ifs) {
@@ -152,7 +172,23 @@ int quiz::read_quiz(const char *filename) {
                do {
                        getline(ifs, buffer);
                } while (buffer[0] == '#');
-               total_time = atoi(buffer.c_str());
+               description = QString::fromStdString(buffer);
+               do {
+                       getline(ifs, buffer);
+               } while (buffer[0] == '#');
+               if (! buffer.compare(0, strlen(SUBSET_PATTERN), SUBSET_PATTERN)) {
+                       subset = QString::fromStdString(buffer.substr(strlen(SUBSET_PATTERN))).toInt(&ok);
+                       if (!ok) {
+                               return 0;
+                       }
+                       do {
+                               getline(ifs, buffer);
+                       } while (buffer[0] == '#');
+               }
+               total_time = QString::fromStdString(buffer).toInt(&ok);
+               if (!ok) {
+                       return 0;
+               }
                /* convert minutes to seconds */
                total_time *= 60;
                while (getline(ifs, parse_line)){
@@ -162,7 +198,7 @@ int quiz::read_quiz(const char *filename) {
                        loc = parse_line.find(CHAR_DELIM);
                        if (loc == string::npos) {
                                cerr << "Wrong format in file " << filename << endl;
-                               return 0 ;
+                               return 0;
                        }
 
                        hint = parse_line.substr(0, loc);
@@ -182,7 +218,6 @@ int quiz::read_quiz(const char *filename) {
                        qanswer = qanswer.trimmed();
                        q = new question(QString::fromStdString(hint), qanswer );
                        questions.push_back(q);
-                       index[qanswer.toLower()] = i;
 
                        while (mloc != string::npos) {
                                parse_line = parse_line.substr(mloc + 1);
@@ -194,7 +229,7 @@ int quiz::read_quiz(const char *filename) {
                                }
                                qanswer = QString::fromStdString(answer);
                                qanswer = qanswer.trimmed();
-                               index[qanswer.toLower()] = i;
+                               q->alternate_answers.append(qanswer);
                                mloc = parse_line.find(ANSWER_DELIM);
                        }
                        i++;
@@ -205,6 +240,41 @@ int quiz::read_quiz(const char *filename) {
        return 1;
 }
 
+void quiz::trim_questions() {
+       vector<question *> new_questions;
+       int number;
+       int nr_questions;
+
+       qsrand(QDateTime::currentDateTime().toTime_t());
+
+       nr_questions = subset;
+       while (nr_questions) {
+               number = qrand() % questions.size();
+               new_questions.push_back(questions.at(number));
+               questions.erase(questions.begin() + number);
+               nr_questions--;
+       }
+       questions.clear();
+       questions = new_questions;
+       total = subset;
+}
+
+void quiz::build_index() {
+       vector<question *>::iterator itr;
+       int position = 0;
+
+       for (itr = questions.begin(); itr != questions.end() ; itr++, position++) {
+               index[(*itr)->answer.toLower()] = position;
+               if (!(*itr)->alternate_answers.isEmpty()) {
+                       QList<QString>::iterator list_itr;
+
+                       for (list_itr = (*itr)->alternate_answers.begin();
+                                       list_itr != (*itr)->alternate_answers.end(); list_itr++)
+                               index[(*list_itr).toLower()] = position;
+               }
+       }
+}
+
 void quiz::init_gui() {
        QHBoxLayout *menu_layout;
 
@@ -261,6 +331,12 @@ void quiz::init_gui() {
        QObject::connect(give_up, SIGNAL(triggered()), this, SLOT(end()));
 }
 
+void quiz::start_dialog() {
+
+     QMessageBox::information(window, tr("Description"), description);
+
+}
+
 void quiz::buzz(const QString& buffer) {
        map <QString, int>::iterator itr;
        question *q;
@@ -303,7 +379,8 @@ void quiz::display_score() {
 
 void quiz::display_grid() {
        vector<question *>::iterator itrq;
-       int i,j, nr_columns, nr_col_padding;
+       int i,j, nr_col_padding;
+       int nr_columns = 0;
        int padding = 3;
        int pixelsWide ;
        QFont font;
@@ -339,8 +416,12 @@ void quiz::display_grid() {
 }
 
 void quiz::update_timer() {
-
        current_time--;
+       display_timer();
+}
+
+void quiz::display_timer() {
+
        QTime t(0, current_time / 60, current_time % 60);
        timer_label->setText(t.toString("m:ss"));
        if (current_time == 0) {
@@ -364,3 +445,17 @@ void quiz::end() {
                }
        }
 }
+
+void quiz::about() {
+       QString message = "";
+       QString version;
+
+       message.append(APP_NAME);
+       message.append("-");
+       version = version.setNum(APP_VERSION);
+       message.append(version);
+       message.append("\n by ");
+       message.append(AUTHOR);
+       QMessageBox::about ( window, tr("About"), message);
+}
+