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);
128 current_time = total_time;
129 timer = new QTimer(this);
130 connect(timer, SIGNAL(timeout()), this, SLOT(update_timer()));
138 int quiz::read_quiz(const char *filename) {
139 string parse_line, hint, answer;
141 string::size_type loc, mloc;
144 ifstream ifs (filename);
145 max_label_length = 0;
150 cerr << "Failed to open file " << filename << endl;
154 getline(ifs, buffer);
155 } while (buffer[0] == '#');
156 title = QString::fromStdString(buffer);
158 getline(ifs, buffer);
159 } while (buffer[0] == '#');
160 if (! buffer.compare(0, strlen(SUBSET_PATTERN), SUBSET_PATTERN)) {
161 subset = atoi(buffer.substr(strlen(SUBSET_PATTERN)).c_str());
163 getline(ifs, buffer);
164 } while (buffer[0] == '#');
166 total_time = atoi(buffer.c_str());
167 /* convert minutes to seconds */
169 while (getline(ifs, parse_line)){
170 if (parse_line[0] == '#')
173 loc = parse_line.find(CHAR_DELIM);
174 if (loc == string::npos) {
175 cerr << "Wrong format in file " << filename << endl;
179 hint = parse_line.substr(0, loc);
180 if (hint.length() > max_label_length)
181 max_label_length = hint.length();
182 parse_line = parse_line.substr(loc);
183 mloc = parse_line.find(ANSWER_DELIM);
185 if (mloc == string::npos) {
186 answer = parse_line.substr(1);
188 answer = parse_line.substr(1, mloc - 1);
190 if (answer.length() > max_label_length )
191 max_label_length = answer.length();
192 qanswer = QString::fromStdString(answer);
193 qanswer = qanswer.trimmed();
194 q = new question(QString::fromStdString(hint), qanswer );
195 questions.push_back(q);
197 while (mloc != string::npos) {
198 parse_line = parse_line.substr(mloc + 1);
199 mloc = parse_line.find(ANSWER_DELIM);
200 if (mloc != string::npos) {
201 answer = parse_line.substr(0, mloc);
203 answer = parse_line.substr(0);
205 qanswer = QString::fromStdString(answer);
206 qanswer = qanswer.trimmed();
207 q->alternate_answers.append(qanswer);
208 mloc = parse_line.find(ANSWER_DELIM);
218 void quiz::trim_questions() {
219 vector<question *> new_questions;
223 qsrand(QDateTime::currentDateTime().toTime_t());
225 nr_questions = subset;
226 while (nr_questions) {
227 number = qrand() % questions.size();
228 new_questions.push_back(questions.at(number));
229 questions.erase(questions.begin() + number);
233 questions = new_questions;
237 void quiz::build_index() {
238 vector<question *>::iterator itr;
241 for (itr = questions.begin(); itr != questions.end() ; itr++, position++) {
242 index[(*itr)->answer.toLower()] = position;
243 if (!(*itr)->alternate_answers.isEmpty()) {
244 QList<QString>::iterator list_itr;
246 for (list_itr = (*itr)->alternate_answers.begin();
247 list_itr != (*itr)->alternate_answers.end(); list_itr++)
248 index[(*list_itr).toLower()] = position;
253 void quiz::init_gui() {
254 QHBoxLayout *menu_layout;
256 line = new QLineEdit(window);
257 score = new QLabel("", window);
258 timer_label = new QLabel("0:00", window);
260 /* Disable auto-completion
262 //line->setCompleter((QCompleter *)0);
263 //line->setCompleter(0);
265 #if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
266 // supposed to work in Qt 4.6
267 line->setInputMethodHints(Qt::ImhNoPredictiveText);
270 //line->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
271 line->setMinimumWidth(window->width() / 2);
272 score->setAlignment(Qt::AlignHCenter);
273 timer_label->setAlignment(Qt::AlignHCenter);
275 layout = new QVBoxLayout();
276 menu_layout = new QHBoxLayout();
278 score->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
279 line->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
280 timer_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
282 menu_layout->addWidget(score);
283 menu_layout->addWidget(line);
284 menu_layout->addWidget(timer_label);
286 menu_layout->setSizeConstraint(QLayout::SetFixedSize);
287 layout->addLayout(menu_layout);
289 scrollArea = new QScrollArea();
290 sub_window = new QWidget(scrollArea);
291 grid = new QGridLayout();
293 sub_window->setLayout(grid);
294 scrollArea->setWidget(sub_window);
296 scrollArea->setWidgetResizable(true);
297 scrollArea->setProperty("FingerScrollable", true);
298 scrollArea->setFocusPolicy(Qt::NoFocus);
300 layout->addWidget(scrollArea);
301 window->setLayout(layout);
303 QObject::connect(line, SIGNAL(textChanged(const QString&)), this, SLOT(buzz(const QString&)));
305 give_up = new QAction("Give up", window);
306 QObject::connect(give_up, SIGNAL(triggered()), this, SLOT(end()));
309 void quiz::buzz(const QString& buffer) {
310 map <QString, int>::iterator itr;
313 itr = index.find(buffer.toLower());
314 if (itr != index.end()) {
315 q = questions.at(itr->second);
320 q->label->setStyleSheet("QLabel { color: green }");
321 q->label->setText(q->answer);
326 if (correct == total) {
338 void quiz::display_score() {
339 QString score_text, ext;
341 score_text.setNum(correct);
342 score_text.append('/');
344 score_text.append(ext);
346 score->setText(score_text);
349 void quiz::display_grid() {
350 vector<question *>::iterator itrq;
351 int i,j, nr_col_padding;
358 QFontMetrics qfm = QFontMetrics(font);
359 example.append(max_label_length + 2, 'C');
360 pixelsWide = qfm.width(QString::fromStdString(example));
363 nr_columns = window->width() / pixelsWide;
364 nr_col_padding = (window->width() -
365 (2 * padding + nr_columns * 2 * padding) ) / pixelsWide;
366 nr_columns = nr_col_padding;
369 nr_columns = DEFAULT_NR_COL;
372 itrq = questions.begin();
373 for ( i = 0; itrq != questions.end() ; i++) {
375 for ( j = 0 ; j < nr_columns && itrq != questions.end() ; j++) {
376 (*itrq)->label = new QLabel((*itrq)->hint);
377 // Doesn't do anything on maemo
378 //(*itrq)->label->setFrameStyle(QFrame::Panel | QFrame::Raised);
380 grid->addWidget((*itrq)->label, i, j);
387 void quiz::update_timer() {
390 QTime t(0, current_time / 60, current_time % 60);
391 timer_label->setText(t.toString("m:ss"));
392 if (current_time == 0) {
398 vector<question *>::iterator itrq;
405 menu->removeAction(give_up);
406 for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) {
407 if (!(*itrq)->answered) {
408 (*itrq)->label->setStyleSheet("QLabel { color: red }");
409 (*itrq)->label->setText((*itrq)->answer);