Cleaning up the code and adding includes.h
[qtdict.git] / dictionary.cpp
blob1f26eb57f8b8fb1ac122b3df9c1fb288871cca74
1 // Copyright (c) 2008, Andrew Bécsi (andrewbecsi @ yahoo . co . uk)
2 //
3 // This program is free software, released under the GNU General Public License
4 // and you are welcome to redistribute it under certain conditions. It comes with
5 // ABSOLUTELY NO WARRANTY; for details read the GNU General Public License to be
6 // found in the file "COPYING" distributed with this program, or online at:
7 // http://www.gnu.org/copyleft/gpl.html
8 //
9 // encodig=UTF-8
13 #include "dictionary.h"
15 Dictionary::Dictionary(QWidget *parent)
16 : QDialog(parent)
19 //>>version
20 appver=QString("0.3.5");
21 //<<version
23 http = new QHttp(this);
24 setupUi(this);
25 enableTranslateButton();
26 prev->setEnabled(false);
27 next->setEnabled(false);
28 deleteHistory->setEnabled(false);
31 connect(lineEdit, SIGNAL(textChanged(const QString &)),
32 this, SLOT(enableTranslateButton()));
33 connect(http, SIGNAL(requestFinished(int, bool)),
34 this, SLOT(httpRequestFinished(int, bool)));
35 connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
36 this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
37 connect(pushButton, SIGNAL(clicked()),
38 this, SLOT(translateClicked()));
39 connect(pushButton, SIGNAL(clicked()),
40 this, SLOT(statusChanged()));
41 connect(dictionaryBox, SIGNAL(currentIndexChanged( int )),
42 this, SLOT(setDic(int)));
43 connect(modeBox, SIGNAL(currentIndexChanged( int )),
44 this, SLOT(setMode(int)));
45 connect(hitBox, SIGNAL(currentIndexChanged( int )),
46 this, SLOT(setHits(int)));
47 connect(aboutButton, SIGNAL(clicked()),
48 this, SLOT(aboutClicked()));
49 connect(deleteHistory, SIGNAL(clicked()),
50 this, SLOT(deleteHistoryClicked()));
51 connect(prev, SIGNAL(clicked()),
52 this, SLOT(stepPrev()));
53 connect(next, SIGNAL(clicked()),
54 this, SLOT(stepNext()));
56 // initialize UI
59 QStringList labels;
60 labels << tr("Szó") << tr("Jelentés");
61 tableWidget->setHorizontalHeaderLabels(labels);
62 // tableWidget->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
63 tableWidget->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
65 tableWidget->verticalHeader()->hide();
66 tableWidget->setShowGrid(true);
68 szotar="szotarE";
69 dic="E";
70 mode="0";
71 hits="20";
75 á=%E1
76 ä=%E4
77 é=%E9
78 í=%ED
79 ó=%F3
80 ö=%F6
81 ő=%F5
82 ú=%FA
83 ü=%FC
84 ű=%FB
85 ß=%DF
88 html_list << "&aacute;" << "&auml;" << "&eacute;" << "&iacute;" << "&oacute;" << "&ouml;" << "&otilde;" << "&uacute;" << "&uuml;"<< "&ucirc;" << "&szlig;";
89 latin_list << "á" << "ä" << "é" << "í" << "ó" << "ö" << "ő" << "ú" << "ü" << "ű" << "ß";
90 hex_list << "%E1" << "%E4" << "%E9" << "%ED" << "%F3" << "%F6" << "%F5" << "%FA" << "%FC" << "%FB" << "%DF";
93 void Dictionary::translate(settings &set)
95 if (history.isEmpty()) deleteHistory->setEnabled(false);
96 else deleteHistory->setEnabled(true);
97 lineEdit->selectAll();
98 enablePrevButton();
99 enableNextButton();
101 QString word((set.word.split(" ", QString::SkipEmptyParts)).join("+"));
104 for (int i = 0; i < latin_list.size(); ++i){
105 word.replace(latin_list[i],hex_list[i]);
108 QString path("/cgi-bin/"+szotar+"?search_term="+word+"&max_hits="+hits+"&mode="+mode+"&diction="+dic);
109 QString host("www.cab.u-szeged.hu");
110 QUrl url;
112 url.setHost(host);
113 url.setPath(path);
114 url.setScheme("http");
115 http->setHost(url.host());
117 buffer=new QBuffer();
118 httpGetId = http->get(url.path(), buffer);
120 pushButton->setEnabled(false);
125 QStringList Dictionary::filter(QString html,QRegExp rx)
128 QStringList list;
129 QString str;
130 int pos = 0;
131 while ((pos = rx.indexIn(html, pos)) != -1) {
132 str = rx.cap(1);
133 list.append(str);
134 pos += rx.matchedLength();
137 return list;
141 void Dictionary::updateUi(settings &set)
143 lineEdit->setText(set.word);
144 dictionaryBox->setCurrentIndex(set.dict);
145 modeBox->setCurrentIndex(set.mode);
146 hitBox->setCurrentIndex(set.hit);
149 void Dictionary::updateHistory(settings &set)
152 if ( history.isEmpty() )
154 history.prepend(set);
155 hIter = history.begin();
156 }else if ( !((*history.begin()) == set) ) {
157 history.prepend(set);
158 hIter = history.begin();
159 if (history.size()>20)
161 history.removeLast();
165 enablePrevButton();
166 enableNextButton();
169 void Dictionary::translateClicked()
172 QString text(lineEdit->text());
173 settings set(dictionaryBox->currentIndex(),modeBox->currentIndex(), hitBox->currentIndex(), text);
174 translate(set);
175 updateHistory(set);
178 void Dictionary::httpRequestFinished(int requestId, bool error)
181 if (requestId != httpGetId)
182 return;
184 buffer->close();
186 QString str;
187 QString html;
188 QStringList word_list;
189 QStringList meaning_list;
192 QByteArray result=buffer->data();
194 QTextStream in(&result);
195 in.setCodec("ISO 8859-2");
198 // remove multiple whitespace and newline characters
200 while(!in.atEnd()){
201 in>>str;
202 html.append(str+" ");
207 //filtering word data from html string
210 QRegExp rx( "<LI>(.+)--&gt;" );
211 rx.setCaseSensitivity(Qt::CaseSensitive);
212 rx.setMinimal( true );
214 word_list=filter(html,rx);
217 //filtering meaning data from html string
220 rx.setPattern("=[HEG]\">(.+)</A></LI>");
221 meaning_list=filter(html,rx);
223 for (int i = 0; i < word_list.size(); ++i){
225 QString tmpWord=word_list[i];
226 QString tmpMeaning=meaning_list[i];
227 for (int j = 0; j < html_list.size(); ++j){
228 tmpWord.replace(html_list[j],latin_list[j]);
229 tmpMeaning.replace(html_list[j],latin_list[j]);
232 tmpWord.remove(QString("<B>"), Qt::CaseInsensitive);
233 tmpWord.remove(QString("</B>"), Qt::CaseInsensitive);
234 tmpMeaning.remove(QString("<B>"), Qt::CaseInsensitive);
235 tmpMeaning.remove(QString("</B>"), Qt::CaseInsensitive);
237 word_list.replace(i,tmpWord.toUtf8());
238 meaning_list.replace(i,tmpMeaning.toUtf8());
241 fillTable( word_list, meaning_list);
243 if (error) {
244 QMessageBox::information(this, tr("HTTP"),
245 tr("<font color=red>A letöltés meghiúsult: %1.</font>")
246 .arg(http->errorString())+tr("<br/>Ellenőrizd az internetkapcsolatod!"));
249 enableTranslateButton();
252 void Dictionary::fillTable(QStringList word_list,QStringList meaning_list)
255 QString found;
256 found.setNum(word_list.size());
257 statusLabel->setText(tr("Keresett szó: ")+lineEdit->text()+tr(", listázott találatok száma: ")+found);
259 if (word_list.size()==0) statusLabel->setText(tr("<font color=red>Nincs találat!</font>"));
260 tableWidget->setRowCount(word_list.size());
263 for (int i = 0; i < word_list.size();++i){
264 // QStringList meanings;
266 tableWidget->setItem(i, 0, new QTableWidgetItem(word_list[i]));
267 //meanings << meaning_list[i];
268 tableWidget->setItem(i, 1, new QTableWidgetItem(meaning_list[i]));
270 // while( (i+1 < word_list.size()) && word_list[i+1] == word_list[i] ){
271 // ++i;
272 // meanings << meaning_list[i];
273 // }
274 // tableWidget->setItem(row, 1, new QTableWidgetItem(meanings.join("; ")));
278 tableWidget->resizeColumnToContents( 0 );
279 tableWidget->resizeColumnToContents( 1 );
280 tableWidget->setSortingEnabled(false);
283 void Dictionary::readResponseHeader(const QHttpResponseHeader &responseHeader)
285 if (responseHeader.statusCode() != 200) {
286 QMessageBox::information(this, tr("HTTP"),
287 tr("<font color=red>A letöltés meghiúsult: %1.")
288 .arg(responseHeader.reasonPhrase())+tr("<br/>Az adatbázis nem elérhető. Próbáld meg később."));
289 http->abort();
290 return;
294 void Dictionary::setMode(int index)
296 mode.setNum(index);
299 void Dictionary::setHits(int index)
301 switch(index)
303 case 0: hits="20";break;
304 case 1: hits="50";break;
305 case 2: hits="100";break;
306 case 3: hits="200";break;
310 void Dictionary::setDic(int index)
312 switch(index)
314 case 0: dic="E";szotar="szotarE";break;
315 case 1: dic="H";szotar="szotarE";break;
316 case 2: dic="G";szotar="szotarG";break;
317 case 3: dic="H";szotar="szotarG";break;
321 void Dictionary::statusChanged()
323 statusLabel->setText(tr("<i>Lekérdezés...</i>"));
326 void Dictionary::enableTranslateButton()
328 pushButton->setEnabled(!lineEdit->text().isEmpty());
331 void Dictionary::enablePrevButton()
333 prev->setEnabled( false );
335 if ( !(history.isEmpty()) && ( history.indexOf(*hIter) < history.size()-1 ) ){
336 prev->setEnabled( true );
340 void Dictionary::enableNextButton()
342 next->setEnabled( false );
344 if ( !(history.isEmpty()) && ( history.indexOf(*hIter) > 0 ) ){
345 next->setEnabled( true );
349 void Dictionary::stepPrev()
351 ++hIter;
352 updateUi(*hIter);
353 translate(*hIter);
356 void Dictionary::stepNext()
358 --hIter;
359 updateUi(*hIter);
360 translate(*hIter);
363 void Dictionary::deleteHistoryClicked()
365 history.clear();
366 prev->setEnabled(false);
367 next->setEnabled(false);
368 deleteHistory->setEnabled(false);
370 void Dictionary::aboutClicked()
372 aboutButton->setEnabled(false);
373 QMessageBox::information(this,"Qtdict Információ", "<b>Qtdict v"+appver+"</b>"+tr("<br/><i>© Bécsi András, 2008</i><br/><br/>Egyszerű internetes szótár program, mely a http://www.cab.u-szeged.hu online szótár adatbázist használja.<br/><br/><i>Ez a program a GPL v2 jogi feltételeinek megfelelően használható.</i>"));
374 aboutButton->setEnabled(true);