update dict
[QFreeRecite.git] / src / gui / RScannerWidget.cpp
blobc6c268575af021e1fb93e1db1360f8ae79639d2c
1 #include <QScrollBar>
2 #include <QSettings>
3 #include <QMessageBox>
4 #include <QUrl>
5 #include <QChar>
6 #include <QKeyEvent>
8 #include <FCore.h>
9 #include "RScannerWidget.h"
10 #include "ListModel.h"
11 #include "Speaker.h"
13 using namespace freeRecite;
15 RScannerWidget::RScannerWidget(QWidget *parent)
16 :BaseScannerWidget(parent)
18 setupUi(this);
19 QSettings settings("QFreeRecite");
20 setFocus();
21 textBrowser->setFontFamily(settings.value("BrowserFont").toString());
22 listModel = new ListModel(this);
23 listView->setModel(listModel);
24 listView->setAlternatingRowColors(true);
25 connect(yesButton,SIGNAL(clicked()),
26 this,SLOT(testYes()));
27 connect(noButton,SIGNAL(clicked()),
28 this,SLOT(testNo()));
29 connect(showAnswerButton,SIGNAL(clicked()),
30 this,SLOT(showAnswer()));
31 connect(addButton,SIGNAL(clicked()),
32 this,SLOT(add()));
33 connect(deleteButton,SIGNAL(clicked()),
34 this,SLOT(remove()));
35 connect(modifyButton,SIGNAL(clicked()),
36 this,SLOT(modify()));
37 connect(pronounceButton,SIGNAL(clicked()),
38 this,SLOT(pronounce()));
39 connect(stopButton,SIGNAL(clicked()),
40 this,SIGNAL(finished()));
43 RScannerWidget::~RScannerWidget() {
44 D_OUTPUT("~RScannerWidget()")
47 void RScannerWidget::keyPressEvent(QKeyEvent * event) {
48 if(event->key() == Qt::Key_Down && showAnswerButton->isVisible()) {
49 Speaker::play("buttondown");
50 showAnswer();
51 Speaker::play("buttonup");
52 }else if(event->key() == Qt::Key_Left && yesButton->isVisible()) {
53 Speaker::play("buttondown");
54 testYes();
55 Speaker::play("buttonup");
56 }else if(event->key() == Qt::Key_Right && noButton->isVisible()) {
57 Speaker::play("buttondown");
58 testNo();
59 Speaker::play("buttonup");
60 }else if(event->key() == Qt::Key_Up) {
61 pronounce();
62 }else {
63 QWidget::keyPressEvent(event);
67 void RScannerWidget::showNext() {
68 if(!scanner->isValid()){
69 displayResult();
70 return;
72 textBrowser->clear();
73 textBrowser->clearHistory();
74 textBrowser->setStyleSheet("background-image: url(:/icon/quote.png)");
75 answerLabel->setText(scanner->getWord().c_str());
76 yesButton->hide();
77 noButton->hide();
78 showAnswerButton->show();
81 void RScannerWidget::testYes() {
82 unsigned tmp = scanner->size();
83 scanner->test(true);
84 if(tmp != scanner->size()) {
85 listModel->addToList( dictionary.word().c_str());
86 listView->scrollTo(listModel->index(listModel->rowCount() - 1));
88 showNext();
89 setInfo();
92 void RScannerWidget::testNo() {
93 unsigned tmp = scanner->size();
94 scanner->test(false);
95 //Call this to skip the current word in reverse mode.
96 scanner->test(true);
97 if(tmp != scanner->size()) {
98 listModel->addToList( dictionary.word().c_str());
99 listView->scrollTo(listModel->index(listModel->rowCount() - 1));
101 showNext();
102 setInfo();
105 void RScannerWidget::showAnswer() {
106 yesButton->show();
107 noButton->show();
108 showAnswerButton->hide();
109 QString text = scanner->getWord().c_str();
110 answerLabel->setText(text);
111 if(dictionary.lookUp(scanner->getWord())) {
112 if(!dictionary.phonetics().empty()) {
113 text.append(QString::fromUtf8("<br />["))
114 .append(QString::fromUtf8(dictionary.phonetics().c_str()))
115 .append(QString::fromUtf8("]"));
117 text.append("<br />");
118 text.append(QString::fromUtf8(dictionary.translation().c_str()));
119 if(!dictionary.example().empty())
120 text += "<br /><br />Examples:<br />" + QString::fromUtf8(dictionary.example().c_str());
122 }else { //The word can't be found!
123 text = QString(scanner->getWord().c_str()) + QString(" can't be found!\n");
125 textBrowser->clearHistory();
126 textBrowser->setStyleSheet("");
127 textBrowser->setText(text);
130 void RScannerWidget::pronounce(){
131 Speaker::speak(scanner->getWord().c_str());
135 void RScannerWidget::setInfo(){
136 amountLabel->setText(QVariant(scanner->capability()).toString());
137 rawLabel->setText(QVariant(scanner->size()).toString());
138 r_timesLabel->setText(QVariant(scanner->times()).toString());