update dict
[QFreeRecite.git] / src / gui / ScannerWidget.cpp
blob6ac88b64ff9e1eadeba1d9a6297b069dbce311f6
1 #include <QSettings>
2 #include <QMessageBox>
3 #include <QScrollBar>
4 #include <QChar>
5 #include <QUrl>
6 #include <QFont>
8 #include <FCore.h>
9 #include "ScannerWidget.h"
10 #include "ListModel.h"
11 #include "Speaker.h"
13 using namespace freeRecite;
15 ScannerWidget::ScannerWidget(QWidget *parent)
16 :BaseScannerWidget(parent)
18 setupUi(this);
19 QSettings settings("QFreeRecite");
20 listModel = new ListModel(this);
21 listView->setModel(listModel);
22 listView->setAlternatingRowColors(true);
23 QFont font(settings.value("FontFamily").toString(),
24 settings.value("FontSize").toInt(),
25 ( settings.value("FontBold").toBool()
26 ? QFont::Bold : QFont::Normal ),
27 settings.value("FontItalic").toBool() );
28 textBrowser->setCurrentFont(font);
29 connect(lineEdit,SIGNAL(returnPressed()),
30 this,SLOT(lineReturned()));
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(hintButton,SIGNAL(clicked()),
40 this,SLOT(hint()));
41 connect(stopButton,SIGNAL(clicked()),
42 this,SIGNAL(finished()));
45 ScannerWidget::~ScannerWidget() {
46 D_OUTPUT("~ScannerWidget()")
49 void ScannerWidget::lineReturned() {
50 setInfo();
51 if(lineEdit->isFreeze()) {
52 showNext();
53 }else {
54 showAnswer();
58 void ScannerWidget::showNext() {
59 QString text;
60 if(!scanner->isValid()){
61 displayResult();
62 return;
64 lineEdit->setFreeze(false);
65 answerLabel->setText(QString(""));
66 if(dictionary.lookUp(scanner->getWord())) {
67 text = QString::fromUtf8(dictionary.translation().c_str());
68 if(!dictionary.example().empty())
69 text += "<br /><br />Examples:<br />" + QString::fromUtf8(dictionary.example().c_str());
70 }else {
71 text = QString(scanner->getWord().c_str())
72 + QString(" can't be found!");
74 text.replace(scanner->getWord().c_str(),"~",Qt::CaseInsensitive);
75 textBrowser->setText(text);
78 void ScannerWidget::showAnswer() {
79 lineEdit->setFreeze(true);
80 if(lineEdit->text().toStdString() != scanner->getWord()) {
81 Speaker::play("fail");
82 answerLabel->setText("<font color=red>" +
83 QString(scanner->getWord().c_str()) +
84 "</font>");
85 } else {
86 Speaker::play("ok");
87 answerLabel->setText("<font color=green>" +
88 QString(scanner->getWord().c_str()) +
89 "</font>");
91 QString text;
92 if(dictionary.lookUp(scanner->getWord())) {
93 if(!dictionary.phonetics().empty()) {
94 text.append(dictionary.word().c_str())
95 .append(QString::fromUtf8("<br />["))
96 .append(QString::fromUtf8(dictionary.phonetics().c_str()))
97 .append(QString::fromUtf8("]"));
98 text.append("<br />");
101 text.append(QString::fromUtf8(dictionary.translation().c_str()));
102 if(!dictionary.example().empty())
103 text.append("<br /><br />Examples:<br />")
104 .append(QString::fromUtf8(dictionary.example().c_str()));
105 }else {
106 text = QString(scanner->getWord().c_str())
107 + QString(tr(" can't be found!\n"));
109 textBrowser->clearHistory();
110 if(lineEdit->text().toStdString() != scanner->getWord())
111 textBrowser->setText("<font color=red>" + text + "</font>");
112 else
113 textBrowser->setText(text);
114 unsigned tmp = scanner->size();
115 scanner->test(lineEdit->text().toStdString() == scanner->getWord());
116 if(tmp != scanner->size()) {
117 listModel->addToList( dictionary.word().c_str());
118 listView->scrollTo(listModel->index(listModel->rowCount() - 1));
120 setInfo();
123 void ScannerWidget::setInfo(){
124 amountLabel->setText(QVariant(scanner->capability()).toString());
125 rawLabel->setText(QVariant(scanner->size()).toString());
126 r_timesLabel->setText(QVariant(scanner->times()).toString());
129 void ScannerWidget::pronounce(){
130 Speaker::speak(dictionary.word().c_str());
133 void ScannerWidget::hint() {
134 if(!scanner->isValid() || lineEdit->isFreeze())
135 return;
136 QString hintText("");
137 for(unsigned i=0,j=4; i < scanner->getWord().size(); ++i,++j) {
138 if(j == 4 || scanner->getWord()[i] == ' ') {
139 hintText.append(QChar(scanner->getWord().at(i)));
140 if(scanner->getWord()[i] == ' ')
141 hintText.append(QChar(scanner->getWord().at(++i)));
142 j = 0;
144 else
145 hintText.append(QChar('-'));
147 answerLabel->setText("<font color=blue>" +
148 hintText +
149 "</font>");