Clear entry line when charging a new quiz
[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         QAction *about;
35
36         window = new QWidget();
37         window->setWindowTitle(QApplication::translate("Qquiz", "Qquiz"));
38
39         menu = new QMenuBar(window);
40         choose = new QAction("Open", window);
41         QObject::connect(choose, SIGNAL(triggered()), this, SLOT(choose_quiz()));
42
43         about = new QAction("About", window);
44         QObject::connect(about, SIGNAL(triggered()), this, SLOT(about()));
45
46         menu->addAction(choose);
47         menu->addAction(about);
48         window->resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
49         window->show();
50
51         retrieve_quizzes();
52
53         choose_quiz();
54 };
55
56 void quiz::retrieve_quizzes() {
57         QDir dir;
58         QFileInfoList list;
59         quiz_file *q;
60         QString path("");
61         QString buf;
62         QString datadir(PKGDATADIR);
63
64         if(dir.cd(datadir)) {
65                 list = dir.entryInfoList(QDir::Files);
66         } else {
67                 cerr << "Can't find directory " << datadir.toStdString() << endl;
68         }
69         path.append(dir.homePath());
70         path.append("/.");
71         path.append(APP_NAME);
72         if (dir.cd(path)) {
73                 list.append(dir.entryInfoList(QDir::Files));
74         } else {
75                 cerr << "Can't find directory " << path.toStdString() << endl;
76         }
77         for (int i = 0; i < list.size(); ++i) {
78                 q = new quiz_file();
79
80                 QFileInfo fileInfo = list.at(i);
81                 q->path = fileInfo.absoluteFilePath();
82                 QFile file(fileInfo.absoluteFilePath());
83                 file.open(QFile::ReadOnly);
84                 QTextStream stream(&file);
85                 do {
86                         buf = stream.readLine();
87                 } while (buf[0] == '#');
88                 q->title = buf;
89                 q->id = i;
90                 files.push_back(q);
91         }
92 }
93
94 void quiz::choose_quiz() {
95     QStringList items;
96         vector<quiz_file *>::iterator itr;
97     bool ok;
98
99         for (itr = files.begin(); itr != files.end() ; itr++) {
100                 items << (*itr)->title;
101         }
102
103     //QString item = QInputDialog::getItem(window, tr("Choose quiz"),
104         //                                        tr("Available quizzes:"), items, 0, false, &ok, QInputDialog::UseListViewForComboBoxItems);
105     QString item = QInputDialog::getItem(window, tr("Choose quiz"),
106                                                   tr("Available quizzes:"), items, 0, false, &ok);
107
108     if (ok && !item.isEmpty()) {
109                 if (current) {
110                         vector<question *>::iterator itrq;
111
112                         index.clear();
113                         for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) {
114                                 (*itrq)->label->hide();
115                                 grid->removeWidget((*itrq)->label);
116                         }
117                         delete(timer);
118                         questions.clear();
119                         subset = 0;
120                         correct = 0;
121                         line->clear();
122                 } else {
123                         init_gui();
124                 }
125                 current = files.at(items.indexOf(item));
126                 if(read_quiz(current->path.absolutePath().toStdString().c_str())) {
127                         if (subset) {
128                                 trim_questions();
129                         }
130                         build_index();
131                         menu->addAction(give_up);
132                         display_score();
133                         display_grid();
134                         current_time = total_time;
135                         display_timer();
136                         start_dialog();
137                         if (total_time) {
138                                 timer = new QTimer(this);
139                                 connect(timer, SIGNAL(timeout()), this, SLOT(update_timer()));
140                                 timer->start(1000);
141                         }
142                 } else {
143                         QString message = "Can't read file :\n";
144
145                         message.append(current->path.absolutePath());
146                         QMessageBox::warning (window, tr("Input file error"), message);
147                 }
148         }
149 }
150
151
152 int quiz::read_quiz(const char *filename) {
153         string parse_line, hint, answer;
154         string buffer;
155         string::size_type loc, mloc;
156         QString qanswer;
157         question *q;
158         ifstream ifs (filename);
159         max_label_length = 0;
160         int i = 0;
161     bool ok;
162
163         total_time = 0;
164         if (!ifs) {
165                 cerr << "Failed to open file " << filename << endl;
166                 return 0;
167         } else {
168                 do {
169                         getline(ifs, buffer);
170                 } while (buffer[0] == '#');
171                 title = QString::fromStdString(buffer);
172                 do {
173                         getline(ifs, buffer);
174                 } while (buffer[0] == '#');
175                 description = QString::fromStdString(buffer);
176                 do {
177                         getline(ifs, buffer);
178                 } while (buffer[0] == '#');
179                 if (! buffer.compare(0, strlen(SUBSET_PATTERN), SUBSET_PATTERN)) {
180                         subset = QString::fromStdString(buffer.substr(strlen(SUBSET_PATTERN))).toInt(&ok);
181                         if (!ok) {
182                                 return 0;
183                         }
184                         do {
185                                 getline(ifs, buffer);
186                         } while (buffer[0] == '#');
187                 }
188                 total_time = QString::fromStdString(buffer).toInt(&ok);
189                 if (!ok) {
190                         return 0;
191                 }
192                 /* convert minutes to seconds */
193                 total_time *= 60;
194                 while (getline(ifs, parse_line)){
195                         if (parse_line[0] == '#')
196                                 continue;
197
198                         loc = parse_line.find(CHAR_DELIM);
199                         if (loc == string::npos) {
200                                 cerr << "Wrong format in file " << filename << endl;
201                                 return 0;
202                         }
203
204                         hint = parse_line.substr(0, loc);
205                         if (hint.length() > max_label_length)
206                                 max_label_length = hint.length();
207                         parse_line = parse_line.substr(loc);
208                         mloc = parse_line.find(ANSWER_DELIM);
209
210                         if (mloc == string::npos) {
211                                 answer = parse_line.substr(1);
212                         } else {
213                                 answer = parse_line.substr(1, mloc - 1);
214                         }
215                         if (answer.length() > max_label_length )
216                                 max_label_length = answer.length();
217                         qanswer = QString::fromStdString(answer);
218                         qanswer = qanswer.trimmed();
219                         q = new question(QString::fromStdString(hint), qanswer );
220                         questions.push_back(q);
221
222                         while (mloc != string::npos) {
223                                 parse_line = parse_line.substr(mloc + 1);
224                                 mloc = parse_line.find(ANSWER_DELIM);
225                                 if (mloc != string::npos) {
226                                         answer = parse_line.substr(0, mloc);
227                                 } else {
228                                         answer = parse_line.substr(0);
229                                 }
230                                 qanswer = QString::fromStdString(answer);
231                                 qanswer = qanswer.trimmed();
232                                 q->alternate_answers.append(qanswer);
233                                 mloc = parse_line.find(ANSWER_DELIM);
234                         }
235                         i++;
236                 }
237                 ifs.close();
238                 total = i;
239         }
240         return 1;
241 }
242
243 void quiz::trim_questions() {
244         vector<question *> new_questions;
245         int number;
246         int nr_questions;
247
248         qsrand(QDateTime::currentDateTime().toTime_t());
249
250         nr_questions = subset;
251         while (nr_questions) {
252                 number = qrand() % questions.size();
253                 new_questions.push_back(questions.at(number));
254                 questions.erase(questions.begin() + number);
255                 nr_questions--;
256         }
257         questions.clear();
258         questions = new_questions;
259         total = subset;
260 }
261
262 void quiz::build_index() {
263         vector<question *>::iterator itr;
264         int position = 0;
265
266         for (itr = questions.begin(); itr != questions.end() ; itr++, position++) {
267                 index[(*itr)->answer.toLower()] = position;
268                 if (!(*itr)->alternate_answers.isEmpty()) {
269                         QList<QString>::iterator list_itr;
270
271                         for (list_itr = (*itr)->alternate_answers.begin();
272                                         list_itr != (*itr)->alternate_answers.end(); list_itr++)
273                                 index[(*list_itr).toLower()] = position;
274                 }
275         }
276 }
277
278 void quiz::init_gui() {
279         QHBoxLayout *menu_layout;
280
281         line = new QLineEdit(window);
282         score = new QLabel("", window);
283         timer_label = new QLabel("0:00", window);
284
285         /* Disable auto-completion
286          */
287         //line->setCompleter((QCompleter *)0);
288         //line->setCompleter(0);
289
290 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
291         // supposed to work in Qt 4.6
292         line->setInputMethodHints(Qt::ImhNoPredictiveText);
293 #endif
294
295         //line->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
296         line->setMinimumWidth(window->width() / 2);
297         score->setAlignment(Qt::AlignHCenter);
298         timer_label->setAlignment(Qt::AlignHCenter);
299
300         layout = new QVBoxLayout();
301         menu_layout = new QHBoxLayout();
302
303         score->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
304         line->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
305         timer_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
306
307         menu_layout->addWidget(score);
308         menu_layout->addWidget(line);
309         menu_layout->addWidget(timer_label);
310
311         menu_layout->setSizeConstraint(QLayout::SetFixedSize);
312         layout->addLayout(menu_layout);
313
314         scrollArea = new QScrollArea();
315         sub_window = new QWidget(scrollArea);
316         grid = new QGridLayout();
317
318         sub_window->setLayout(grid);
319         scrollArea->setWidget(sub_window);
320
321         scrollArea->setWidgetResizable(true);
322         scrollArea->setProperty("FingerScrollable", true);
323         scrollArea->setFocusPolicy(Qt::NoFocus);
324
325         layout->addWidget(scrollArea);
326         window->setLayout(layout);
327
328         QObject::connect(line, SIGNAL(textChanged(const QString&)), this, SLOT(buzz(const QString&)));
329
330         give_up = new QAction("Give up", window);
331         QObject::connect(give_up, SIGNAL(triggered()), this, SLOT(end()));
332 }
333
334 void quiz::start_dialog() {
335
336      QMessageBox::information(window, tr("Description"), description);
337
338 }
339
340 void quiz::buzz(const QString& buffer) {
341         map <QString, int>::iterator itr;
342         question *q;
343
344         itr = index.find(buffer.toLower());
345         if (itr != index.end()) {
346                 q = questions.at(itr->second);
347
348                 if (q->answered) {
349                         return ;
350                 } else {
351                         q->label->setStyleSheet("QLabel { color: green }");
352                         q->label->setText(q->answer);
353                         correct++;
354                         line->clear();
355                         display_score();
356                         q->answered = 1;
357                         if (correct == total) {
358                                 end();
359                         }
360                 }
361         }
362 }
363
364 quiz::~quiz() {
365         index.clear();
366         questions.clear();
367 }
368
369 void quiz::display_score() {
370         QString score_text, ext;
371
372         score_text.setNum(correct);
373         score_text.append('/');
374         ext.setNum(total);
375         score_text.append(ext);
376
377         score->setText(score_text);
378 }
379
380 void quiz::display_grid() {
381         vector<question *>::iterator itrq;
382         int i,j, nr_col_padding;
383         int nr_columns = 0;
384         int padding = 3;
385         int pixelsWide ;
386         QFont font;
387         string example;
388
389         QFontMetrics qfm = QFontMetrics(font);
390         example.append(max_label_length + 2, 'C');
391         pixelsWide = qfm.width(QString::fromStdString(example));
392
393         if (pixelsWide) {
394                 nr_columns = window->width() / pixelsWide;
395                 nr_col_padding = (window->width() -
396                                 (2 * padding + nr_columns * 2 * padding) ) / pixelsWide;
397                 nr_columns = nr_col_padding;
398         }
399         if (!nr_columns) {
400                 nr_columns = DEFAULT_NR_COL;
401         }
402
403         itrq = questions.begin();
404         for ( i = 0; itrq != questions.end() ; i++) {
405
406                 for ( j = 0 ; j < nr_columns && itrq != questions.end() ; j++) {
407                         (*itrq)->label = new QLabel((*itrq)->hint);
408                         // Doesn't do anything on maemo
409                         //(*itrq)->label->setFrameStyle(QFrame::Panel | QFrame::Raised);
410
411                         grid->addWidget((*itrq)->label, i, j);
412                         itrq++;
413                 }
414         }
415
416 }
417
418 void quiz::update_timer() {
419         current_time--;
420         display_timer();
421 }
422
423 void quiz::display_timer() {
424
425         QTime t(0, current_time / 60, current_time % 60);
426         timer_label->setText(t.toString("m:ss"));
427         if (current_time == 0) {
428                 end();
429         }
430 }
431
432 void quiz::end() {
433         vector<question *>::iterator itrq;
434
435         if(!current)
436                 return;
437
438         timer->stop();
439
440         menu->removeAction(give_up);
441         for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) {
442                 if (!(*itrq)->answered) {
443                         (*itrq)->label->setStyleSheet("QLabel { color: red }");
444                         (*itrq)->label->setText((*itrq)->answer);
445                 }
446         }
447 }
448
449 void quiz::about() {
450         QString message = "";
451         QString version;
452
453         message.append(APP_NAME);
454         message.append("-");
455         version = version.setNum(APP_VERSION);
456         message.append(version);
457         message.append("\n by ");
458         message.append(AUTHOR);
459         QMessageBox::about ( window, tr("About"), message);
460 }
461