Can Add New Words in ScannerWidget
[QFreeRecite.git] / src / gui / MainWindow.cpp
blob136af86b8dfc3951ebcc952fcf33cda2a1a8a015
1 #include <Dict.h>
2 #include <Task.h>
3 #include <Reciter.h>
4 #include <Tester.h>
5 #include <ConfigHolder.h>
6 #include <Manager.h>
8 #include "MainWidget.h"
9 #include "ReciterWidget.h"
10 #include "TesterWidget.h"
11 #include "MainWindow.h"
12 #include "ResultWidget.h"
14 MainWindow::MainWindow()
15 :scannerWidget(0)
17 setupUi(this);
18 if(!freeRecite::manager.load()) {
19 #ifdef DEBUG
20 #include <iostream>
21 std::cerr << "Can't load "
22 << freeRecite::configHolder.mgrFile() << std::endl
23 << "The program exit!\n";
24 #endif //DEBUG
25 close();
27 if(!freeRecite::dictionary.load()) {
28 #ifdef DEBUG
29 #include <iostream>
30 std::cerr << "Can't load dictionary.\n"
31 << "The program exit!\n";
32 #endif //DEBUG
33 close();
35 stackedWidget = new QStackedWidget(this);
36 setCentralWidget(stackedWidget);
37 mainWidget = new MainWidget(this);
38 stackedWidget->addWidget(mainWidget);
39 connect(mainWidget->reciteButton,SIGNAL(clicked()),
40 this,SLOT(recite()));
41 connect(mainWidget->testButton,SIGNAL(clicked()),
42 this,SLOT(test()));
43 connect(mainWidget->quitButton,SIGNAL(clicked()),
44 this,SLOT(close()));
47 MainWindow::~MainWindow() {
48 if(mainWidget != 0)
49 mainWidget->deleteLater();
50 if(scannerWidget != 0)
51 scannerWidget->deleteLater();
54 void MainWindow::recite() {
55 if(mainWidget->getCurrentTaskID() == 0)
56 return;
57 scannerWidget = new ReciterWidget(this);
58 stackedWidget->addWidget(scannerWidget);
59 stackedWidget->setCurrentWidget(scannerWidget);
60 connect(scannerWidget,SIGNAL(showResult(ResultWidget *)),
61 this,SLOT(showResult(ResultWidget *)));
62 connect(scannerWidget->stopButton,SIGNAL(clicked()),
63 this,SLOT(showMain()));
64 scannerWidget->start(mainWidget->getCurrentTaskID());
67 void MainWindow::test() {
68 if(mainWidget->getCurrentTaskID() == 0)
69 return;
70 scannerWidget = new TesterWidget(this);
71 stackedWidget->addWidget(scannerWidget);
72 stackedWidget->setCurrentWidget(scannerWidget);
73 connect(scannerWidget,SIGNAL(showResult(ResultWidget *)),
74 this,SLOT(showResult(ResultWidget *)));
75 connect(scannerWidget->stopButton,SIGNAL(clicked()),
76 this,SLOT(showMain()));
77 scannerWidget->start(mainWidget->getCurrentTaskID());
80 void MainWindow::showMain(ResultWidget *resultWidget) {
81 stackedWidget->setCurrentWidget(mainWidget);
82 stackedWidget->removeWidget(scannerWidget);
83 stackedWidget->removeWidget(resultWidget);
84 scannerWidget->deleteLater();
85 if(resultWidget != 0)
86 resultWidget->deleteLater();
87 scannerWidget = 0;
89 #ifdef DEBUG
90 #include <iostream>
91 std::cerr << stackedWidget->count();
92 #endif //DEBUG
95 void MainWindow::showResult(ResultWidget *resultWidget) {
96 if(resultWidget->isTester()) {
97 int testResult =
98 freeRecite::manager.test(resultWidget->id(),resultWidget->score());
99 if(testResult == 0) {
100 resultWidget->setTailer(tr("You passed it!"));
101 }else if(testResult == -1){
102 resultWidget->setTailer(tr("You haven't passed it!"));
103 }else { // testResult == 1
104 resultWidget->setTailer(tr("You have complished this task!"));
107 stackedWidget->addWidget(resultWidget);
108 stackedWidget->setCurrentWidget(resultWidget);
109 connect(resultWidget,SIGNAL(confirmed(ResultWidget *)),
110 this,SLOT(showMain(ResultWidget *)));