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