From d7583d643c2d93c27043f15ea201fcd312e22db6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Charles=20Cl=C3=A9ment?= Date: Tue, 16 Feb 2010 14:18:42 -0800 Subject: [PATCH] Initial commit --- AUTHORS | 1 + COPYING | 339 +++++++++++++++++++++++++++++++++++++++++++++++ README | 17 +++ data/beatles_albums | 15 +++ data/constants | 9 ++ data/radio_code | 29 ++++ qquiz.pro | 9 ++ src/main.cpp | 33 +++++ src/quiz.cpp | 366 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/quiz.h | 100 ++++++++++++++ src/src.pro | 50 +++++++ 11 files changed, 968 insertions(+) create mode 100644 AUTHORS create mode 100644 COPYING create mode 100644 README create mode 100644 data/beatles_albums create mode 100644 data/constants create mode 100644 data/radio_code create mode 100644 qquiz.pro create mode 100644 src/main.cpp create mode 100644 src/quiz.cpp create mode 100644 src/quiz.h create mode 100644 src/src.pro diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..44cd2d1 --- /dev/null +++ b/AUTHORS @@ -0,0 +1 @@ +Charles Clément diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..d511905 --- /dev/null +++ b/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/README b/README new file mode 100644 index 0000000..db6d6cf --- /dev/null +++ b/README @@ -0,0 +1,17 @@ +Qquiz + +Charles Clément + +qquiz is an application to play quizzes. Each quiz file contains a number of +questions and answers and are displayed on screen. The user enters the answer +for each question and tries to complete in the minimum amount of time. + +Here's the syntax for the quiz files : +Quiz title +# This is a comment +# Author : name email_address +Default amount of time in minutes +Question 1 ; Answer 1 | Another possible answer to question 1 +Question 2 ; Answer 1 | Answer 2 +... + diff --git a/data/beatles_albums b/data/beatles_albums new file mode 100644 index 0000000..44f18f3 --- /dev/null +++ b/data/beatles_albums @@ -0,0 +1,15 @@ +Beatles discography +# Author Charles Clément +3 +1963 ; Please Please Me +1963 ; With The Beatles +1964 ; A Hard Day's Night | A Hard Day s Night +1964 ; Beatles for Sale +1965 ; Help! | Help +1965 ; Rubber Soul +1966 ; Revolver +1967 ; Sgt. Pepper's Lonely Hearts Club Band | Sgt. Pepper s Lonely Hearts Club Band | Sergeant Pepper's Lonely Hearts Club Band +1968 ; The Beatles (aka White Album) | White Album +1969 ; Yellow Submarine +1969 ; Abbey Road +1970 ; Let It Be diff --git a/data/constants b/data/constants new file mode 100644 index 0000000..5e05cb0 --- /dev/null +++ b/data/constants @@ -0,0 +1,9 @@ +Constants +# Author: Charles Clément +5 +3.14159265 ; Pi +1.61803 ; Golden ratio +6.022 10^23 ; Avogadro +6.674 ; Gravitational +2.71828 ; e +299,792,458 ; Speed of light | Lightspeed diff --git a/data/radio_code b/data/radio_code new file mode 100644 index 0000000..6bcabd3 --- /dev/null +++ b/data/radio_code @@ -0,0 +1,29 @@ +Radio Code +# Author: Charles Clément +3 +A ; Alpha +B ; Bravo +C ; Charlie +D ; Delta +E ; Echo +F ; Foxtrot +G ; Golf +H ; Hotel +I ; India +J ; Juliet +K ; Kilo +L ; Lima +M ; Mike +N ; November +O ; Oscar +P ; Papa +Q ; Quebec +R ; Romeo +S ; Sierra +T ; Tango +U ; Uniform +V ; Victor +W ; Whiskey +X ; XRay +Y ; Yankee +Z ; Zulu diff --git a/qquiz.pro b/qquiz.pro new file mode 100644 index 0000000..36119cd --- /dev/null +++ b/qquiz.pro @@ -0,0 +1,9 @@ +QMAKEVERSION = $$[QMAKE_VERSION] +ISQT4 = $$find(QMAKEVERSION, ^[2-9]) +isEmpty( ISQT4 ) { +error("Use the qmake include with Qt4.4 or greater, on Debian that is qmake-qt4"); +} + +TEMPLATE = subdirs +SUBDIRS = src + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b581f2a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2010 Charles Clement + * + * This file is part of qquiz. + * + * qquiz is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ + +#include "quiz.h" + +int main(int argc, char **argv){ + QApplication app(argc, argv); + quiz *q; + + q = new quiz(); + + return app.exec(); + +} diff --git a/src/quiz.cpp b/src/quiz.cpp new file mode 100644 index 0000000..76d5ac8 --- /dev/null +++ b/src/quiz.cpp @@ -0,0 +1,366 @@ +/* + * Copyright (C) 2010 Charles Clement + * + * This file is part of qquiz. + * + * qquiz is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ + +#include "quiz.h" + +question::question(QString h, QString r) : hint(h), answer(r), answered(0) { +} + +question::~question() { + delete(label); +} + +quiz::quiz() : current(NULL), correct(0) { + QAction *choose; + + window = new QWidget(); + window->setWindowTitle(QApplication::translate("Qtquiz", "Qtquiz")); + + menu = new QMenuBar(window); + choose = new QAction("Open", window); + QObject::connect(choose, SIGNAL(triggered()), this, SLOT(choose_quiz())); + + menu->addAction(choose); + window->show(); + + retrieve_quizzes(); + + choose_quiz(); +}; + +void quiz::retrieve_quizzes() { + QDir dir; + QFileInfoList list; + quiz_file *q; + QString path(""); + QString buf; + QString datadir(PKGDATADIR); + + if(dir.cd(datadir)) { + list = dir.entryInfoList(QDir::Files); + } else { + cerr << "Can't find directory " << datadir.toStdString() << endl; + } + path.append(dir.homePath()); + path.append("/."); + path.append(APP_NAME); + if (dir.cd(path)) { + list.append(dir.entryInfoList(QDir::Files)); + } else { + cerr << "Can't find directory " << path.toStdString() << endl; + } + for (int i = 0; i < list.size(); ++i) { + q = new quiz_file(); + + QFileInfo fileInfo = list.at(i); + q->path = fileInfo.absoluteFilePath(); + QFile file(fileInfo.absoluteFilePath()); + file.open(QFile::ReadOnly); + QTextStream stream(&file); + do { + buf = stream.readLine(); + } while (buf[0] == '#'); + q->title = buf; + q->id = i; + files.push_back(q); + } +} + +void quiz::choose_quiz() { + QStringList items; + vector::iterator itr; + bool ok; + + for (itr = files.begin(); itr != files.end() ; itr++) { + items << (*itr)->title; + } + + //QString item = QInputDialog::getItem(window, tr("Choose quiz"), + // tr("Available quizzes:"), items, 0, false, &ok, QInputDialog::UseListViewForComboBoxItems); + QString item = QInputDialog::getItem(window, tr("Choose quiz"), + tr("Available quizzes:"), items, 0, false, &ok); + + if (ok && !item.isEmpty()) { + if (current) { + vector::iterator itrq; + + index.clear(); + for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) { + (*itrq)->label->hide(); + grid->removeWidget((*itrq)->label); + } + delete(timer); + questions.clear(); + correct = 0; + } else { + init_gui(); + } + current = files.at(items.indexOf(item)); + if(read_quiz(current->path.absolutePath().toStdString().c_str())) { + menu->addAction(give_up); + display_score(); + display_grid(); + if (total_time) { + current_time = total_time; + timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), this, SLOT(update_timer())); + timer->start(1000); + } + } + } +} + + +int quiz::read_quiz(const char *filename) { + string parse_line, hint, answer; + string buffer; + string::size_type loc, mloc; + QString qanswer; + question *q; + ifstream ifs (filename); + max_label_length = 0; + int i = 0; + + total_time = 0; + if (!ifs) { + cerr << "Failed to open file " << filename << endl; + return 0; + } else { + do { + getline(ifs, buffer); + } while (buffer[0] == '#'); + title = QString::fromStdString(buffer); + do { + getline(ifs, buffer); + } while (buffer[0] == '#'); + total_time = atoi(buffer.c_str()); + /* convert minutes to seconds */ + total_time *= 60; + while (getline(ifs, parse_line)){ + if (parse_line[0] == '#') + continue; + + loc = parse_line.find(CHAR_DELIM); + if (loc == string::npos) { + cerr << "Wrong format in file " << filename << endl; + return 0 ; + } + + hint = parse_line.substr(0, loc); + if (hint.length() > max_label_length) + max_label_length = hint.length(); + parse_line = parse_line.substr(loc); + mloc = parse_line.find(ANSWER_DELIM); + + if (mloc == string::npos) { + answer = parse_line.substr(1); + } else { + answer = parse_line.substr(1, mloc - 1); + } + if (answer.length() > max_label_length ) + max_label_length = answer.length(); + qanswer = QString::fromStdString(answer); + qanswer = qanswer.trimmed(); + q = new question(QString::fromStdString(hint), qanswer ); + questions.push_back(q); + index[qanswer.toLower()] = i; + + while (mloc != string::npos) { + parse_line = parse_line.substr(mloc + 1); + mloc = parse_line.find(ANSWER_DELIM); + if (mloc != string::npos) { + answer = parse_line.substr(0, mloc); + } else { + answer = parse_line.substr(0); + } + qanswer = QString::fromStdString(answer); + qanswer = qanswer.trimmed(); + index[qanswer.toLower()] = i; + mloc = parse_line.find(ANSWER_DELIM); + } + i++; + } + ifs.close(); + total = i; + } + return 1; +} + +void quiz::init_gui() { + QHBoxLayout *menu_layout; + + line = new QLineEdit(window); + score = new QLabel("", window); + timer_label = new QLabel("0:00", window); + + /* Disable auto-completion + */ + //line->setCompleter((QCompleter *)0); + //line->setCompleter(0); + +#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0) + // supposed to work in Qt 4.6 + line->setInputMethodHints(Qt::ImhNoPredictiveText); +#endif + + //line->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + line->setMinimumWidth(window->width() / 2); + score->setAlignment(Qt::AlignHCenter); + timer_label->setAlignment(Qt::AlignHCenter); + + layout = new QVBoxLayout(); + menu_layout = new QHBoxLayout(); + + score->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + line->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + timer_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + + menu_layout->addWidget(score); + menu_layout->addWidget(line); + menu_layout->addWidget(timer_label); + + menu_layout->setSizeConstraint(QLayout::SetFixedSize); + layout->addLayout(menu_layout); + + scrollArea = new QScrollArea(); + sub_window = new QWidget(scrollArea); + grid = new QGridLayout(); + + sub_window->setLayout(grid); + scrollArea->setWidget(sub_window); + + scrollArea->setWidgetResizable(true); + scrollArea->setProperty("FingerScrollable", true); + scrollArea->setFocusPolicy(Qt::NoFocus); + + layout->addWidget(scrollArea); + window->setLayout(layout); + + QObject::connect(line, SIGNAL(textChanged(const QString&)), this, SLOT(buzz(const QString&))); + + give_up = new QAction("Give up", window); + QObject::connect(give_up, SIGNAL(triggered()), this, SLOT(end())); +} + +void quiz::buzz(const QString& buffer) { + map ::iterator itr; + question *q; + + itr = index.find(buffer.toLower()); + if (itr != index.end()) { + q = questions.at(itr->second); + + if (q->answered) { + return ; + } else { + q->label->setStyleSheet("QLabel { color: green }"); + q->label->setText(q->answer); + correct++; + line->clear(); + display_score(); + q->answered = 1; + if (correct == total) { + end(); + } + } + } +} + +quiz::~quiz() { + index.clear(); + questions.clear(); +} + +void quiz::display_score() { + QString score_text, ext; + + score_text.setNum(correct); + score_text.append('/'); + ext.setNum(total); + score_text.append(ext); + + score->setText(score_text); +} + +void quiz::display_grid() { + vector::iterator itrq; + int i,j, nr_columns, nr_col_padding; + int padding = 3; + int pixelsWide ; + QFont font; + string example; + + QFontMetrics qfm = QFontMetrics(font); + example.append(max_label_length + 2, 'C'); + pixelsWide = qfm.width(QString::fromStdString(example)); + + if (pixelsWide) { + nr_columns = window->width() / pixelsWide; + nr_col_padding = (window->width() - + (2 * padding + nr_columns * 2 * padding) ) / pixelsWide; + nr_columns = nr_col_padding; + } + if (!nr_columns) { + nr_columns = DEFAULT_NR_COL; + } + + itrq = questions.begin(); + for ( i = 0; itrq != questions.end() ; i++) { + + for ( j = 0 ; j < nr_columns && itrq != questions.end() ; j++) { + (*itrq)->label = new QLabel((*itrq)->hint); + // Doesn't do anything on maemo + //(*itrq)->label->setFrameStyle(QFrame::Panel | QFrame::Raised); + + grid->addWidget((*itrq)->label, i, j); + itrq++; + } + } + +} + +void quiz::update_timer() { + + current_time--; + QTime t(0, current_time / 60, current_time % 60); + timer_label->setText(t.toString("m:ss")); + if (current_time == 0) { + end(); + } +} + +void quiz::end() { + vector::iterator itrq; + + if(!current) + return; + + timer->stop(); + + menu->removeAction(give_up); + for ( itrq = questions.begin() ; itrq != questions.end() ; itrq++) { + if (!(*itrq)->answered) { + (*itrq)->label->setStyleSheet("QLabel { color: red }"); + (*itrq)->label->setText((*itrq)->answer); + } + } +} diff --git a/src/quiz.h b/src/quiz.h new file mode 100644 index 0000000..fe5e9eb --- /dev/null +++ b/src/quiz.h @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2010 Charles Clement + * + * This file is part of qquiz. + * + * qquiz is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#define APP_NAME "qquiz" + +#define CHAR_DELIM ';' +#define ANSWER_DELIM '|' + +#define DEFAULT_NR_COL 2 + +using namespace std; + +class question { + public: + question(); + ~question(); + question(QString s, QString r); + QString hint; + QString answer; + QLabel *label; + int answered; +}; + +class quiz_file { + public: + QDir path; + QString title; + int id; +}; + +class quiz : public QObject{ + Q_OBJECT + public: + quiz(); + ~quiz(); + void retrieve_quizzes(); + int read_quiz(const char *filename); + void init_gui(); + void display_score(); + void display_grid(); + void clean_gui(); + public slots: + void buzz(const QString& text); + void update_timer(); + void choose_quiz(); + void end(); + private: + vector questions; + vector files; + quiz_file * current; + QString title; + int total; + int correct; + unsigned int total_time; + unsigned int current_time; + unsigned int max_label_length; + map < QString, int > index; + QWidget *window; + QMenuBar *menu; + QAction *give_up; + QGridLayout *grid; + QVBoxLayout *layout; + QLineEdit *line; + QScrollArea *scrollArea; + QWidget *sub_window; + QLabel *score; + QLabel *timer_label; + QTimer *timer; +}; + diff --git a/src/src.pro b/src/src.pro new file mode 100644 index 0000000..17bfeb5 --- /dev/null +++ b/src/src.pro @@ -0,0 +1,50 @@ +###################################################################### +# Automatically generated by qmake (2.01a) ven. janv. 29 19:13:59 2010 +###################################################################### + +TEMPLATE = app +TARGET = qquiz +DEPENDPATH += . src +INCLUDEPATH += . + +CONFIG += qt +# CONFIG += debug +# Input +HEADERS += quiz.h +SOURCES += main.cpp quiz.cpp + + unix { + #VARIABLES + isEmpty(PREFIX) { + PREFIX = /usr/local + } + +DATADIR =$$PREFIX/share +PKGDATADIR = $$DATADIR/$${TARGET} + +DEFINES += DATADIR=\"$$DATADIR\" PKGDATADIR=\\\"$$PKGDATADIR\\\" + +BINDIR = $$PREFIX/bin + +#MAKE INSTALL + +INSTALLS += target desktop iconxpm icon26 icon48 icon64 + + target.path =$$BINDIR + + desktop.path = $$DATADIR/applications/hildon + desktop.files += $${TARGET}.desktop + + iconxpm.path = $$DATADIR/pixmap + iconxpm.files += ../data/maemo/$${TARGET}.xpm + + icon26.path = $$DATADIR/icons/hicolor/26x26/apps + icon26.files += ../data/26x26/$${TARGET}.png + + icon48.path = $$DATADIR/icons/hicolor/48x48/apps + icon48.files += ../data/48x48/$${TARGET}.png + + icon64.path = $$DATADIR/icons/hicolor/64x64/apps + icon64.files += ../data/64x64/$${TARGET}.png + + } -- 1.7.9.5