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) {
35 window = new QWidget();
36 window->setWindowTitle(QApplication::translate("Qtquiz", "Qtquiz"));
38 menu = new QMenuBar(window);
39 choose = new QAction("Open", window);
40 QObject::connect(choose, SIGNAL(triggered()), this, SLOT(choose_quiz()));
42 menu->addAction(choose);
50 void quiz::retrieve_quizzes() {
56 QString datadir(PKGDATADIR);
59 list = dir.entryInfoList(QDir::Files);
61 cerr << "Can't find directory " << datadir.toStdString() << endl;
63 path.append(dir.homePath());
65 path.append(APP_NAME);
67 list.append(dir.entryInfoList(QDir::Files));
69 cerr << "Can't find directory " << path.toStdString() << endl;
71 for (int i = 0; i < list.size(); ++i) {
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);
80 buf = stream.readLine();
81 } while (buf[0] == '#');
88 void quiz::choose_quiz() {
90 vector<quiz_file *>::iterator itr;
93 for (itr = files.begin(); itr != files.end() ; itr++) {
94 items << (*itr)->title;
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);
102 if (ok && !item.isEmpty()) {
104 vector<question *>::iterator itrq;
107 for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) {
108 (*itrq)->label->hide();
109 grid->removeWidget((*itrq)->label);
118 current = files.at(items.indexOf(item));
119 if(read_quiz(current->path.absolutePath().toStdString().c_str())) {
124 menu->addAction(give_up);
127 current_time = total_time;
131 timer = new QTimer(this);
132 connect(timer, SIGNAL(timeout()), this, SLOT(update_timer()));
136 QString message = "Can't read file :\n";
138 message.append(current->path.absolutePath());
139 QMessageBox::warning (window, tr("Input file error"), message);
145 int quiz::read_quiz(const char *filename) {
146 string parse_line, hint, answer;
148 string::size_type loc, mloc;
151 ifstream ifs (filename);
152 max_label_length = 0;
158 cerr << "Failed to open file " << filename << endl;
162 getline(ifs, buffer);
163 } while (buffer[0] == '#');
164 title = QString::fromStdString(buffer);
166 getline(ifs, buffer);
167 } while (buffer[0] == '#');
168 description = QString::fromStdString(buffer);
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);
178 getline(ifs, buffer);
179 } while (buffer[0] == '#');
181 total_time = QString::fromStdString(buffer).toInt(&ok);
185 /* convert minutes to seconds */
187 while (getline(ifs, parse_line)){
188 if (parse_line[0] == '#')
191 loc = parse_line.find(CHAR_DELIM);
192 if (loc == string::npos) {
193 cerr << "Wrong format in file " << filename << endl;
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);
203 if (mloc == string::npos) {
204 answer = parse_line.substr(1);
206 answer = parse_line.substr(1, mloc - 1);
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);
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);
221 answer = parse_line.substr(0);
223 qanswer = QString::fromStdString(answer);
224 qanswer = qanswer.trimmed();
225 q->alternate_answers.append(qanswer);
226 mloc = parse_line.find(ANSWER_DELIM);
236 void quiz::trim_questions() {
237 vector<question *> new_questions;
241 qsrand(QDateTime::currentDateTime().toTime_t());
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);
251 questions = new_questions;
255 void quiz::build_index() {
256 vector<question *>::iterator itr;
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;
264 for (list_itr = (*itr)->alternate_answers.begin();
265 list_itr != (*itr)->alternate_answers.end(); list_itr++)
266 index[(*list_itr).toLower()] = position;
271 void quiz::init_gui() {
272 QHBoxLayout *menu_layout;
274 line = new QLineEdit(window);
275 score = new QLabel("", window);
276 timer_label = new QLabel("0:00", window);
278 /* Disable auto-completion
280 //line->setCompleter((QCompleter *)0);
281 //line->setCompleter(0);
283 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
284 // supposed to work in Qt 4.6
285 line->setInputMethodHints(Qt::ImhNoPredictiveText);
288 //line->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
289 line->setMinimumWidth(window->width() / 2);
290 score->setAlignment(Qt::AlignHCenter);
291 timer_label->setAlignment(Qt::AlignHCenter);
293 layout = new QVBoxLayout();
294 menu_layout = new QHBoxLayout();
296 score->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
297 line->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
298 timer_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
300 menu_layout->addWidget(score);
301 menu_layout->addWidget(line);
302 menu_layout->addWidget(timer_label);
304 menu_layout->setSizeConstraint(QLayout::SetFixedSize);
305 layout->addLayout(menu_layout);
307 scrollArea = new QScrollArea();
308 sub_window = new QWidget(scrollArea);
309 grid = new QGridLayout();
311 sub_window->setLayout(grid);
312 scrollArea->setWidget(sub_window);
314 scrollArea->setWidgetResizable(true);
315 scrollArea->setProperty("FingerScrollable", true);
316 scrollArea->setFocusPolicy(Qt::NoFocus);
318 layout->addWidget(scrollArea);
319 window->setLayout(layout);
321 QObject::connect(line, SIGNAL(textChanged(const QString&)), this, SLOT(buzz(const QString&)));
323 give_up = new QAction("Give up", window);
324 QObject::connect(give_up, SIGNAL(triggered()), this, SLOT(end()));
327 void quiz::start_dialog() {
329 QMessageBox::information(window, tr("Description"), description);
333 void quiz::buzz(const QString& buffer) {
334 map <QString, int>::iterator itr;
337 itr = index.find(buffer.toLower());
338 if (itr != index.end()) {
339 q = questions.at(itr->second);
344 q->label->setStyleSheet("QLabel { color: green }");
345 q->label->setText(q->answer);
350 if (correct == total) {
362 void quiz::display_score() {
363 QString score_text, ext;
365 score_text.setNum(correct);
366 score_text.append('/');
368 score_text.append(ext);
370 score->setText(score_text);
373 void quiz::display_grid() {
374 vector<question *>::iterator itrq;
375 int i,j, nr_col_padding;
382 QFontMetrics qfm = QFontMetrics(font);
383 example.append(max_label_length + 2, 'C');
384 pixelsWide = qfm.width(QString::fromStdString(example));
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;
393 nr_columns = DEFAULT_NR_COL;
396 itrq = questions.begin();
397 for ( i = 0; itrq != questions.end() ; i++) {
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);
404 grid->addWidget((*itrq)->label, i, j);
411 void quiz::update_timer() {
416 void quiz::display_timer() {
418 QTime t(0, current_time / 60, current_time % 60);
419 timer_label->setText(t.toString("m:ss"));
420 if (current_time == 0) {
426 vector<question *>::iterator itrq;
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);