Add quiz description
[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                         current_time = total_time;
128                         display_timer();
129                         start_dialog();
130                         if (total_time) {
131                                 timer = new QTimer(this);
132                                 connect(timer, SIGNAL(timeout()), this, SLOT(update_timer()));
133                                 timer->start(1000);
134                         }
135                 } else {
136                         QString message = "Can't read file :\n";
137
138                         message.append(current->path.absolutePath());
139                         QMessageBox::warning (window, tr("Input file error"), message);
140                 }
141         }
142 }
143
144
145 int quiz::read_quiz(const char *filename) {
146         string parse_line, hint, answer;
147         string buffer;
148         string::size_type loc, mloc;
149         QString qanswer;
150         question *q;
151         ifstream ifs (filename);
152         max_label_length = 0;
153         int i = 0;
154     bool ok;
155
156         total_time = 0;
157         if (!ifs) {
158                 cerr << "Failed to open file " << filename << endl;
159                 return 0;
160         } else {
161                 do {
162                         getline(ifs, buffer);
163                 } while (buffer[0] == '#');
164                 title = QString::fromStdString(buffer);
165                 do {
166                         getline(ifs, buffer);
167                 } while (buffer[0] == '#');
168                 description = QString::fromStdString(buffer);
169                 do {
170                         getline(ifs, buffer);
171                 } while (buffer[0] == '#');
172                 if (! buffer.compare(0, strlen(SUBSET_PATTERN), SUBSET_PATTERN)) {
173                         subset = QString::fromStdString(buffer.substr(strlen(SUBSET_PATTERN))).toInt(&ok);
174                         if (!ok) {
175                                 return 0;
176                         }
177                         do {
178                                 getline(ifs, buffer);
179                         } while (buffer[0] == '#');
180                 }
181                 total_time = QString::fromStdString(buffer).toInt(&ok);
182                 if (!ok) {
183                         return 0;
184                 }
185                 /* convert minutes to seconds */
186                 total_time *= 60;
187                 while (getline(ifs, parse_line)){
188                         if (parse_line[0] == '#')
189                                 continue;
190
191                         loc = parse_line.find(CHAR_DELIM);
192                         if (loc == string::npos) {
193                                 cerr << "Wrong format in file " << filename << endl;
194                                 return 0;
195                         }
196
197                         hint = parse_line.substr(0, loc);
198                         if (hint.length() > max_label_length)
199                                 max_label_length = hint.length();
200                         parse_line = parse_line.substr(loc);
201                         mloc = parse_line.find(ANSWER_DELIM);
202
203                         if (mloc == string::npos) {
204                                 answer = parse_line.substr(1);
205                         } else {
206                                 answer = parse_line.substr(1, mloc - 1);
207                         }
208                         if (answer.length() > max_label_length )
209                                 max_label_length = answer.length();
210                         qanswer = QString::fromStdString(answer);
211                         qanswer = qanswer.trimmed();
212                         q = new question(QString::fromStdString(hint), qanswer );
213                         questions.push_back(q);
214
215                         while (mloc != string::npos) {
216                                 parse_line = parse_line.substr(mloc + 1);
217                                 mloc = parse_line.find(ANSWER_DELIM);
218                                 if (mloc != string::npos) {
219                                         answer = parse_line.substr(0, mloc);
220                                 } else {
221                                         answer = parse_line.substr(0);
222                                 }
223                                 qanswer = QString::fromStdString(answer);
224                                 qanswer = qanswer.trimmed();
225                                 q->alternate_answers.append(qanswer);
226                                 mloc = parse_line.find(ANSWER_DELIM);
227                         }
228                         i++;
229                 }
230                 ifs.close();
231                 total = i;
232         }
233         return 1;
234 }
235
236 void quiz::trim_questions() {
237         vector<question *> new_questions;
238         int number;
239         int nr_questions;
240
241         qsrand(QDateTime::currentDateTime().toTime_t());
242
243         nr_questions = subset;
244         while (nr_questions) {
245                 number = qrand() % questions.size();
246                 new_questions.push_back(questions.at(number));
247                 questions.erase(questions.begin() + number);
248                 nr_questions--;
249         }
250         questions.clear();
251         questions = new_questions;
252         total = subset;
253 }
254
255 void quiz::build_index() {
256         vector<question *>::iterator itr;
257         int position = 0;
258
259         for (itr = questions.begin(); itr != questions.end() ; itr++, position++) {
260                 index[(*itr)->answer.toLower()] = position;
261                 if (!(*itr)->alternate_answers.isEmpty()) {
262                         QList<QString>::iterator list_itr;
263
264                         for (list_itr = (*itr)->alternate_answers.begin();
265                                         list_itr != (*itr)->alternate_answers.end(); list_itr++)
266                                 index[(*list_itr).toLower()] = position;
267                 }
268         }
269 }
270
271 void quiz::init_gui() {
272         QHBoxLayout *menu_layout;
273
274         line = new QLineEdit(window);
275         score = new QLabel("", window);
276         timer_label = new QLabel("0:00", window);
277
278         /* Disable auto-completion
279          */
280         //line->setCompleter((QCompleter *)0);
281         //line->setCompleter(0);
282
283 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
284         // supposed to work in Qt 4.6
285         line->setInputMethodHints(Qt::ImhNoPredictiveText);
286 #endif
287
288         //line->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
289         line->setMinimumWidth(window->width() / 2);
290         score->setAlignment(Qt::AlignHCenter);
291         timer_label->setAlignment(Qt::AlignHCenter);
292
293         layout = new QVBoxLayout();
294         menu_layout = new QHBoxLayout();
295
296         score->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
297         line->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
298         timer_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
299
300         menu_layout->addWidget(score);
301         menu_layout->addWidget(line);
302         menu_layout->addWidget(timer_label);
303
304         menu_layout->setSizeConstraint(QLayout::SetFixedSize);
305         layout->addLayout(menu_layout);
306
307         scrollArea = new QScrollArea();
308         sub_window = new QWidget(scrollArea);
309         grid = new QGridLayout();
310
311         sub_window->setLayout(grid);
312         scrollArea->setWidget(sub_window);
313
314         scrollArea->setWidgetResizable(true);
315         scrollArea->setProperty("FingerScrollable", true);
316         scrollArea->setFocusPolicy(Qt::NoFocus);
317
318         layout->addWidget(scrollArea);
319         window->setLayout(layout);
320
321         QObject::connect(line, SIGNAL(textChanged(const QString&)), this, SLOT(buzz(const QString&)));
322
323         give_up = new QAction("Give up", window);
324         QObject::connect(give_up, SIGNAL(triggered()), this, SLOT(end()));
325 }
326
327 void quiz::start_dialog() {
328
329      QMessageBox::information(window, tr("Description"), description);
330
331 }
332
333 void quiz::buzz(const QString& buffer) {
334         map <QString, int>::iterator itr;
335         question *q;
336
337         itr = index.find(buffer.toLower());
338         if (itr != index.end()) {
339                 q = questions.at(itr->second);
340
341                 if (q->answered) {
342                         return ;
343                 } else {
344                         q->label->setStyleSheet("QLabel { color: green }");
345                         q->label->setText(q->answer);
346                         correct++;
347                         line->clear();
348                         display_score();
349                         q->answered = 1;
350                         if (correct == total) {
351                                 end();
352                         }
353                 }
354         }
355 }
356
357 quiz::~quiz() {
358         index.clear();
359         questions.clear();
360 }
361
362 void quiz::display_score() {
363         QString score_text, ext;
364
365         score_text.setNum(correct);
366         score_text.append('/');
367         ext.setNum(total);
368         score_text.append(ext);
369
370         score->setText(score_text);
371 }
372
373 void quiz::display_grid() {
374         vector<question *>::iterator itrq;
375         int i,j, nr_col_padding;
376         int nr_columns = 0;
377         int padding = 3;
378         int pixelsWide ;
379         QFont font;
380         string example;
381
382         QFontMetrics qfm = QFontMetrics(font);
383         example.append(max_label_length + 2, 'C');
384         pixelsWide = qfm.width(QString::fromStdString(example));
385
386         if (pixelsWide) {
387                 nr_columns = window->width() / pixelsWide;
388                 nr_col_padding = (window->width() -
389                                 (2 * padding + nr_columns * 2 * padding) ) / pixelsWide;
390                 nr_columns = nr_col_padding;
391         }
392         if (!nr_columns) {
393                 nr_columns = DEFAULT_NR_COL;
394         }
395
396         itrq = questions.begin();
397         for ( i = 0; itrq != questions.end() ; i++) {
398
399                 for ( j = 0 ; j < nr_columns && itrq != questions.end() ; j++) {
400                         (*itrq)->label = new QLabel((*itrq)->hint);
401                         // Doesn't do anything on maemo
402                         //(*itrq)->label->setFrameStyle(QFrame::Panel | QFrame::Raised);
403
404                         grid->addWidget((*itrq)->label, i, j);
405                         itrq++;
406                 }
407         }
408
409 }
410
411 void quiz::update_timer() {
412         current_time--;
413         display_timer();
414 }
415
416 void quiz::display_timer() {
417
418         QTime t(0, current_time / 60, current_time % 60);
419         timer_label->setText(t.toString("m:ss"));
420         if (current_time == 0) {
421                 end();
422         }
423 }
424
425 void quiz::end() {
426         vector<question *>::iterator itrq;
427
428         if(!current)
429                 return;
430
431         timer->stop();
432
433         menu->removeAction(give_up);
434         for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) {
435                 if (!(*itrq)->answered) {
436                         (*itrq)->label->setStyleSheet("QLabel { color: red }");
437                         (*itrq)->label->setText((*itrq)->answer);
438                 }
439         }
440 }