Add quiz subset question handling
[qquiz] / src / quiz.cpp
1 /*
2  *  Copyright (C) 2010 Charles Clement <caratorn _at_ gmail.com>
3  *
4  *  This file is part of qquiz.
5  *
6  *  qquiz is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  *  02110-1301  USA
20  *
21  */
22
23 #include "quiz.h"
24
25 question::question(QString h, QString r) : hint(h), answer(r), answered(0) {
26 }
27
28 question::~question() {
29         delete(label);
30 }
31
32 quiz::quiz() : current(NULL), subset(0), correct(0)  {
33         QAction *choose;
34
35         window = new QWidget();
36         window->setWindowTitle(QApplication::translate("Qtquiz", "Qtquiz"));
37
38         menu = new QMenuBar(window);
39         choose = new QAction("Open", window);
40         QObject::connect(choose, SIGNAL(triggered()), this, SLOT(choose_quiz()));
41
42         menu->addAction(choose);
43         window->show();
44
45         retrieve_quizzes();
46
47         choose_quiz();
48 };
49
50 void quiz::retrieve_quizzes() {
51         QDir dir;
52         QFileInfoList list;
53         quiz_file *q;
54         QString path("");
55         QString buf;
56         QString datadir(PKGDATADIR);
57
58         if(dir.cd(datadir)) {
59                 list = dir.entryInfoList(QDir::Files);
60         } else {
61                 cerr << "Can't find directory " << datadir.toStdString() << endl;
62         }
63         path.append(dir.homePath());
64         path.append("/.");
65         path.append(APP_NAME);
66         if (dir.cd(path)) {
67                 list.append(dir.entryInfoList(QDir::Files));
68         } else {
69                 cerr << "Can't find directory " << path.toStdString() << endl;
70         }
71         for (int i = 0; i < list.size(); ++i) {
72                 q = new quiz_file();
73
74                 QFileInfo fileInfo = list.at(i);
75                 q->path = fileInfo.absoluteFilePath();
76                 QFile file(fileInfo.absoluteFilePath());
77                 file.open(QFile::ReadOnly);
78                 QTextStream stream(&file);
79                 do {
80                         buf = stream.readLine();
81                 } while (buf[0] == '#');
82                 q->title = buf;
83                 q->id = i;
84                 files.push_back(q);
85         }
86 }
87
88 void quiz::choose_quiz() {
89     QStringList items;
90         vector<quiz_file *>::iterator itr;
91     bool ok;
92
93         for (itr = files.begin(); itr != files.end() ; itr++) {
94                 items << (*itr)->title;
95         }
96
97     //QString item = QInputDialog::getItem(window, tr("Choose quiz"),
98         //                                        tr("Available quizzes:"), items, 0, false, &ok, QInputDialog::UseListViewForComboBoxItems);
99     QString item = QInputDialog::getItem(window, tr("Choose quiz"),
100                                                   tr("Available quizzes:"), items, 0, false, &ok);
101
102     if (ok && !item.isEmpty()) {
103                 if (current) {
104                         vector<question *>::iterator itrq;
105
106                         index.clear();
107                         for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) {
108                                 (*itrq)->label->hide();
109                                 grid->removeWidget((*itrq)->label);
110                         }
111                         delete(timer);
112                         questions.clear();
113                         subset = 0;
114                         correct = 0;
115                 } else {
116                         init_gui();
117                 }
118                 current = files.at(items.indexOf(item));
119                 if(read_quiz(current->path.absolutePath().toStdString().c_str())) {
120                         if (subset) {
121                                 trim_questions();
122                         }
123                         build_index();
124                         menu->addAction(give_up);
125                         display_score();
126                         display_grid();
127                         if (total_time) {
128                                 current_time = total_time;
129                                 timer = new QTimer(this);
130                                 connect(timer, SIGNAL(timeout()), this, SLOT(update_timer()));
131                                 timer->start(1000);
132                         }
133                 }
134         }
135 }
136
137
138 int quiz::read_quiz(const char *filename) {
139         string parse_line, hint, answer;
140         string buffer;
141         string::size_type loc, mloc;
142         QString qanswer;
143         question *q;
144         ifstream ifs (filename);
145         max_label_length = 0;
146         int i = 0;
147
148         total_time = 0;
149         if (!ifs) {
150                 cerr << "Failed to open file " << filename << endl;
151                 return 0;
152         } else {
153                 do {
154                         getline(ifs, buffer);
155                 } while (buffer[0] == '#');
156                 title = QString::fromStdString(buffer);
157                 do {
158                         getline(ifs, buffer);
159                 } while (buffer[0] == '#');
160                 if (! buffer.compare(0, strlen(SUBSET_PATTERN), SUBSET_PATTERN)) {
161                         subset = atoi(buffer.substr(strlen(SUBSET_PATTERN)).c_str());
162                                 do {
163                                         getline(ifs, buffer);
164                                 } while (buffer[0] == '#');
165                 }
166                 total_time = atoi(buffer.c_str());
167                 /* convert minutes to seconds */
168                 total_time *= 60;
169                 while (getline(ifs, parse_line)){
170                         if (parse_line[0] == '#')
171                                 continue;
172
173                         loc = parse_line.find(CHAR_DELIM);
174                         if (loc == string::npos) {
175                                 cerr << "Wrong format in file " << filename << endl;
176                                 return 0;
177                         }
178
179                         hint = parse_line.substr(0, loc);
180                         if (hint.length() > max_label_length)
181                                 max_label_length = hint.length();
182                         parse_line = parse_line.substr(loc);
183                         mloc = parse_line.find(ANSWER_DELIM);
184
185                         if (mloc == string::npos) {
186                                 answer = parse_line.substr(1);
187                         } else {
188                                 answer = parse_line.substr(1, mloc - 1);
189                         }
190                         if (answer.length() > max_label_length )
191                                 max_label_length = answer.length();
192                         qanswer = QString::fromStdString(answer);
193                         qanswer = qanswer.trimmed();
194                         q = new question(QString::fromStdString(hint), qanswer );
195                         questions.push_back(q);
196
197                         while (mloc != string::npos) {
198                                 parse_line = parse_line.substr(mloc + 1);
199                                 mloc = parse_line.find(ANSWER_DELIM);
200                                 if (mloc != string::npos) {
201                                         answer = parse_line.substr(0, mloc);
202                                 } else {
203                                         answer = parse_line.substr(0);
204                                 }
205                                 qanswer = QString::fromStdString(answer);
206                                 qanswer = qanswer.trimmed();
207                                 q->alternate_answers.append(qanswer);
208                                 mloc = parse_line.find(ANSWER_DELIM);
209                         }
210                         i++;
211                 }
212                 ifs.close();
213                 total = i;
214         }
215         return 1;
216 }
217
218 void quiz::trim_questions() {
219         vector<question *> new_questions;
220         int number;
221         int nr_questions;
222
223         qsrand(QDateTime::currentDateTime().toTime_t());
224
225         nr_questions = subset;
226         while (nr_questions) {
227                 number = qrand() % questions.size();
228                 new_questions.push_back(questions.at(number));
229                 questions.erase(questions.begin() + number);
230                 nr_questions--;
231         }
232         questions.clear();
233         questions = new_questions;
234         total = subset;
235 }
236
237 void quiz::build_index() {
238         vector<question *>::iterator itr;
239         int position = 0;
240
241         for (itr = questions.begin(); itr != questions.end() ; itr++, position++) {
242                 index[(*itr)->answer.toLower()] = position;
243                 if (!(*itr)->alternate_answers.isEmpty()) {
244                         QList<QString>::iterator list_itr;
245
246                         for (list_itr = (*itr)->alternate_answers.begin();
247                                         list_itr != (*itr)->alternate_answers.end(); list_itr++)
248                                 index[(*list_itr).toLower()] = position;
249                 }
250         }
251 }
252
253 void quiz::init_gui() {
254         QHBoxLayout *menu_layout;
255
256         line = new QLineEdit(window);
257         score = new QLabel("", window);
258         timer_label = new QLabel("0:00", window);
259
260         /* Disable auto-completion
261          */
262         //line->setCompleter((QCompleter *)0);
263         //line->setCompleter(0);
264
265 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
266         // supposed to work in Qt 4.6
267         line->setInputMethodHints(Qt::ImhNoPredictiveText);
268 #endif
269
270         //line->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
271         line->setMinimumWidth(window->width() / 2);
272         score->setAlignment(Qt::AlignHCenter);
273         timer_label->setAlignment(Qt::AlignHCenter);
274
275         layout = new QVBoxLayout();
276         menu_layout = new QHBoxLayout();
277
278         score->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
279         line->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
280         timer_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
281
282         menu_layout->addWidget(score);
283         menu_layout->addWidget(line);
284         menu_layout->addWidget(timer_label);
285
286         menu_layout->setSizeConstraint(QLayout::SetFixedSize);
287         layout->addLayout(menu_layout);
288
289         scrollArea = new QScrollArea();
290         sub_window = new QWidget(scrollArea);
291         grid = new QGridLayout();
292
293         sub_window->setLayout(grid);
294         scrollArea->setWidget(sub_window);
295
296         scrollArea->setWidgetResizable(true);
297         scrollArea->setProperty("FingerScrollable", true);
298         scrollArea->setFocusPolicy(Qt::NoFocus);
299
300         layout->addWidget(scrollArea);
301         window->setLayout(layout);
302
303         QObject::connect(line, SIGNAL(textChanged(const QString&)), this, SLOT(buzz(const QString&)));
304
305         give_up = new QAction("Give up", window);
306         QObject::connect(give_up, SIGNAL(triggered()), this, SLOT(end()));
307 }
308
309 void quiz::buzz(const QString& buffer) {
310         map <QString, int>::iterator itr;
311         question *q;
312
313         itr = index.find(buffer.toLower());
314         if (itr != index.end()) {
315                 q = questions.at(itr->second);
316
317                 if (q->answered) {
318                         return ;
319                 } else {
320                         q->label->setStyleSheet("QLabel { color: green }");
321                         q->label->setText(q->answer);
322                         correct++;
323                         line->clear();
324                         display_score();
325                         q->answered = 1;
326                         if (correct == total) {
327                                 end();
328                         }
329                 }
330         }
331 }
332
333 quiz::~quiz() {
334         index.clear();
335         questions.clear();
336 }
337
338 void quiz::display_score() {
339         QString score_text, ext;
340
341         score_text.setNum(correct);
342         score_text.append('/');
343         ext.setNum(total);
344         score_text.append(ext);
345
346         score->setText(score_text);
347 }
348
349 void quiz::display_grid() {
350         vector<question *>::iterator itrq;
351         int i,j, nr_col_padding;
352         int nr_columns = 0;
353         int padding = 3;
354         int pixelsWide ;
355         QFont font;
356         string example;
357
358         QFontMetrics qfm = QFontMetrics(font);
359         example.append(max_label_length + 2, 'C');
360         pixelsWide = qfm.width(QString::fromStdString(example));
361
362         if (pixelsWide) {
363                 nr_columns = window->width() / pixelsWide;
364                 nr_col_padding = (window->width() -
365                                 (2 * padding + nr_columns * 2 * padding) ) / pixelsWide;
366                 nr_columns = nr_col_padding;
367         }
368         if (!nr_columns) {
369                 nr_columns = DEFAULT_NR_COL;
370         }
371
372         itrq = questions.begin();
373         for ( i = 0; itrq != questions.end() ; i++) {
374
375                 for ( j = 0 ; j < nr_columns && itrq != questions.end() ; j++) {
376                         (*itrq)->label = new QLabel((*itrq)->hint);
377                         // Doesn't do anything on maemo
378                         //(*itrq)->label->setFrameStyle(QFrame::Panel | QFrame::Raised);
379
380                         grid->addWidget((*itrq)->label, i, j);
381                         itrq++;
382                 }
383         }
384
385 }
386
387 void quiz::update_timer() {
388
389         current_time--;
390         QTime t(0, current_time / 60, current_time % 60);
391         timer_label->setText(t.toString("m:ss"));
392         if (current_time == 0) {
393                 end();
394         }
395 }
396
397 void quiz::end() {
398         vector<question *>::iterator itrq;
399
400         if(!current)
401                 return;
402
403         timer->stop();
404
405         menu->removeAction(give_up);
406         for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) {
407                 if (!(*itrq)->answered) {
408                         (*itrq)->label->setStyleSheet("QLabel { color: red }");
409                         (*itrq)->label->setText((*itrq)->answer);
410                 }
411         }
412 }