2 * Copyright (C) 2010 Charles Clement <caratorn _at_ gmail.com>
4 * This file is part of qquiz.
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.
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.
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
25 question::question(QString h, QString r) : hint(h), answer(r), answered(0) {
28 question::~question() {
32 quiz::quiz() : current(NULL), subset(0), correct(0) {
36 window = new QWidget();
37 window->setWindowTitle(QApplication::translate("Qquiz", "Qquiz"));
39 menu = new QMenuBar(window);
40 choose = new QAction("Open", window);
41 QObject::connect(choose, SIGNAL(triggered()), this, SLOT(choose_quiz()));
43 about = new QAction("About", window);
44 QObject::connect(about, SIGNAL(triggered()), this, SLOT(about()));
46 menu->addAction(choose);
47 menu->addAction(about);
48 window->resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
56 void quiz::retrieve_quizzes() {
62 QString datadir(PKGDATADIR);
65 list = dir.entryInfoList(QDir::Files);
67 cerr << "Can't find directory " << datadir.toStdString() << endl;
69 path.append(dir.homePath());
71 path.append(APP_NAME);
73 list.append(dir.entryInfoList(QDir::Files));
75 cerr << "Can't find directory " << path.toStdString() << endl;
77 for (int i = 0; i < list.size(); ++i) {
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);
86 buf = stream.readLine();
87 } while (buf[0] == '#');
94 void quiz::choose_quiz() {
96 vector<quiz_file *>::iterator itr;
99 for (itr = files.begin(); itr != files.end() ; itr++) {
100 items << (*itr)->title;
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);
108 if (ok && !item.isEmpty()) {
110 vector<question *>::iterator itrq;
113 for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) {
114 (*itrq)->label->hide();
115 grid->removeWidget((*itrq)->label);
125 current = files.at(items.indexOf(item));
126 if(read_quiz(current->path.absolutePath().toStdString().c_str())) {
131 menu->addAction(give_up);
134 current_time = total_time;
138 timer = new QTimer(this);
139 connect(timer, SIGNAL(timeout()), this, SLOT(update_timer()));
143 QString message = "Can't read file :\n";
145 message.append(current->path.absolutePath());
146 QMessageBox::warning (window, tr("Input file error"), message);
152 int quiz::read_quiz(const char *filename) {
153 string parse_line, hint, answer;
155 string::size_type loc, mloc;
158 ifstream ifs (filename);
159 max_label_length = 0;
165 cerr << "Failed to open file " << filename << endl;
169 getline(ifs, buffer);
170 } while (buffer[0] == '#');
171 title = QString::fromStdString(buffer);
173 getline(ifs, buffer);
174 } while (buffer[0] == '#');
175 description = QString::fromStdString(buffer);
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);
185 getline(ifs, buffer);
186 } while (buffer[0] == '#');
188 total_time = QString::fromStdString(buffer).toInt(&ok);
192 /* convert minutes to seconds */
194 while (getline(ifs, parse_line)){
195 if (parse_line[0] == '#')
198 loc = parse_line.find(CHAR_DELIM);
199 if (loc == string::npos) {
200 cerr << "Wrong format in file " << filename << endl;
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);
210 if (mloc == string::npos) {
211 answer = parse_line.substr(1);
213 answer = parse_line.substr(1, mloc - 1);
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);
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);
228 answer = parse_line.substr(0);
230 qanswer = QString::fromStdString(answer);
231 qanswer = qanswer.trimmed();
232 q->alternate_answers.append(qanswer);
233 mloc = parse_line.find(ANSWER_DELIM);
243 void quiz::trim_questions() {
244 vector<question *> new_questions;
248 qsrand(QDateTime::currentDateTime().toTime_t());
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);
258 questions = new_questions;
262 void quiz::build_index() {
263 vector<question *>::iterator itr;
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;
271 for (list_itr = (*itr)->alternate_answers.begin();
272 list_itr != (*itr)->alternate_answers.end(); list_itr++)
273 index[(*list_itr).toLower()] = position;
278 void quiz::init_gui() {
279 QHBoxLayout *menu_layout;
281 line = new QLineEdit(window);
282 score = new QLabel("", window);
283 timer_label = new QLabel("0:00", window);
285 /* Disable auto-completion
287 //line->setCompleter((QCompleter *)0);
288 //line->setCompleter(0);
290 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
291 // supposed to work in Qt 4.6
292 line->setInputMethodHints(Qt::ImhNoPredictiveText);
295 //line->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
296 line->setMinimumWidth(window->width() / 2);
297 score->setAlignment(Qt::AlignHCenter);
298 timer_label->setAlignment(Qt::AlignHCenter);
300 layout = new QVBoxLayout();
301 menu_layout = new QHBoxLayout();
303 score->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
304 line->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
305 timer_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
307 menu_layout->addWidget(score);
308 menu_layout->addWidget(line);
309 menu_layout->addWidget(timer_label);
311 menu_layout->setSizeConstraint(QLayout::SetFixedSize);
312 layout->addLayout(menu_layout);
314 scrollArea = new QScrollArea();
315 sub_window = new QWidget(scrollArea);
316 grid = new QGridLayout();
318 sub_window->setLayout(grid);
319 scrollArea->setWidget(sub_window);
321 scrollArea->setWidgetResizable(true);
322 scrollArea->setProperty("FingerScrollable", true);
323 scrollArea->setFocusPolicy(Qt::NoFocus);
325 layout->addWidget(scrollArea);
326 window->setLayout(layout);
328 QObject::connect(line, SIGNAL(textChanged(const QString&)), this, SLOT(buzz(const QString&)));
330 give_up = new QAction("Give up", window);
331 QObject::connect(give_up, SIGNAL(triggered()), this, SLOT(end()));
334 void quiz::start_dialog() {
336 QMessageBox::information(window, tr("Description"), description);
340 void quiz::buzz(const QString& buffer) {
341 map <QString, int>::iterator itr;
344 itr = index.find(buffer.toLower());
345 if (itr != index.end()) {
346 q = questions.at(itr->second);
351 q->label->setStyleSheet("QLabel { color: green }");
352 q->label->setText(q->answer);
357 if (correct == total) {
369 void quiz::display_score() {
370 QString score_text, ext;
372 score_text.setNum(correct);
373 score_text.append('/');
375 score_text.append(ext);
377 score->setText(score_text);
380 void quiz::display_grid() {
381 vector<question *>::iterator itrq;
382 int i,j, nr_col_padding;
389 QFontMetrics qfm = QFontMetrics(font);
390 example.append(max_label_length + 2, 'C');
391 pixelsWide = qfm.width(QString::fromStdString(example));
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;
400 nr_columns = DEFAULT_NR_COL;
403 itrq = questions.begin();
404 for ( i = 0; itrq != questions.end() ; i++) {
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);
411 grid->addWidget((*itrq)->label, i, j);
418 void quiz::update_timer() {
423 void quiz::display_timer() {
425 QTime t(0, current_time / 60, current_time % 60);
426 timer_label->setText(t.toString("m:ss"));
427 if (current_time == 0) {
433 vector<question *>::iterator itrq;
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);
450 QString message = "";
453 message.append(APP_NAME);
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);