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);
124 current = files.at(items.indexOf(item));
125 if(read_quiz(current->path.absolutePath().toStdString().c_str())) {
130 menu->addAction(give_up);
133 current_time = total_time;
137 timer = new QTimer(this);
138 connect(timer, SIGNAL(timeout()), this, SLOT(update_timer()));
142 QString message = "Can't read file :\n";
144 message.append(current->path.absolutePath());
145 QMessageBox::warning (window, tr("Input file error"), message);
151 int quiz::read_quiz(const char *filename) {
152 string parse_line, hint, answer;
154 string::size_type loc, mloc;
157 ifstream ifs (filename);
158 max_label_length = 0;
164 cerr << "Failed to open file " << filename << endl;
168 getline(ifs, buffer);
169 } while (buffer[0] == '#');
170 title = QString::fromStdString(buffer);
172 getline(ifs, buffer);
173 } while (buffer[0] == '#');
174 description = QString::fromStdString(buffer);
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);
184 getline(ifs, buffer);
185 } while (buffer[0] == '#');
187 total_time = QString::fromStdString(buffer).toInt(&ok);
191 /* convert minutes to seconds */
193 while (getline(ifs, parse_line)){
194 if (parse_line[0] == '#')
197 loc = parse_line.find(CHAR_DELIM);
198 if (loc == string::npos) {
199 cerr << "Wrong format in file " << filename << endl;
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);
209 if (mloc == string::npos) {
210 answer = parse_line.substr(1);
212 answer = parse_line.substr(1, mloc - 1);
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);
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);
227 answer = parse_line.substr(0);
229 qanswer = QString::fromStdString(answer);
230 qanswer = qanswer.trimmed();
231 q->alternate_answers.append(qanswer);
232 mloc = parse_line.find(ANSWER_DELIM);
242 void quiz::trim_questions() {
243 vector<question *> new_questions;
247 qsrand(QDateTime::currentDateTime().toTime_t());
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);
257 questions = new_questions;
261 void quiz::build_index() {
262 vector<question *>::iterator itr;
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;
270 for (list_itr = (*itr)->alternate_answers.begin();
271 list_itr != (*itr)->alternate_answers.end(); list_itr++)
272 index[(*list_itr).toLower()] = position;
277 void quiz::init_gui() {
278 QHBoxLayout *menu_layout;
280 line = new QLineEdit(window);
281 score = new QLabel("", window);
282 timer_label = new QLabel("0:00", window);
284 /* Disable auto-completion
286 //line->setCompleter((QCompleter *)0);
287 //line->setCompleter(0);
289 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
290 // supposed to work in Qt 4.6
291 line->setInputMethodHints(Qt::ImhNoPredictiveText);
294 //line->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
295 line->setMinimumWidth(window->width() / 2);
296 score->setAlignment(Qt::AlignHCenter);
297 timer_label->setAlignment(Qt::AlignHCenter);
299 layout = new QVBoxLayout();
300 menu_layout = new QHBoxLayout();
302 score->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
303 line->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
304 timer_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
306 menu_layout->addWidget(score);
307 menu_layout->addWidget(line);
308 menu_layout->addWidget(timer_label);
310 menu_layout->setSizeConstraint(QLayout::SetFixedSize);
311 layout->addLayout(menu_layout);
313 scrollArea = new QScrollArea();
314 sub_window = new QWidget(scrollArea);
315 grid = new QGridLayout();
317 sub_window->setLayout(grid);
318 scrollArea->setWidget(sub_window);
320 scrollArea->setWidgetResizable(true);
321 scrollArea->setProperty("FingerScrollable", true);
322 scrollArea->setFocusPolicy(Qt::NoFocus);
324 layout->addWidget(scrollArea);
325 window->setLayout(layout);
327 QObject::connect(line, SIGNAL(textChanged(const QString&)), this, SLOT(buzz(const QString&)));
329 give_up = new QAction("Give up", window);
330 QObject::connect(give_up, SIGNAL(triggered()), this, SLOT(end()));
333 void quiz::start_dialog() {
335 QMessageBox::information(window, tr("Description"), description);
339 void quiz::buzz(const QString& buffer) {
340 map <QString, int>::iterator itr;
343 itr = index.find(buffer.toLower());
344 if (itr != index.end()) {
345 q = questions.at(itr->second);
350 q->label->setStyleSheet("QLabel { color: green }");
351 q->label->setText(q->answer);
356 if (correct == total) {
368 void quiz::display_score() {
369 QString score_text, ext;
371 score_text.setNum(correct);
372 score_text.append('/');
374 score_text.append(ext);
376 score->setText(score_text);
379 void quiz::display_grid() {
380 vector<question *>::iterator itrq;
381 int i,j, nr_col_padding;
388 QFontMetrics qfm = QFontMetrics(font);
389 example.append(max_label_length + 2, 'C');
390 pixelsWide = qfm.width(QString::fromStdString(example));
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;
399 nr_columns = DEFAULT_NR_COL;
402 itrq = questions.begin();
403 for ( i = 0; itrq != questions.end() ; i++) {
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);
410 grid->addWidget((*itrq)->label, i, j);
417 void quiz::update_timer() {
422 void quiz::display_timer() {
424 QTime t(0, current_time / 60, current_time % 60);
425 timer_label->setText(t.toString("m:ss"));
426 if (current_time == 0) {
432 vector<question *>::iterator itrq;
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);
449 QString message = "";
452 message.append(APP_NAME);
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);