Deleting xxmklink_readme.txt
[qtdict.git] / dictionary.cpp
blob7d0dd3df3f0c6e8423883725ed726ac7495beeda
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
11 #include <QtGui>
12 #include <QtNetwork>
13 //#include <QtDebug>
14 #include "dictionary.h"
16 Dictionary::Dictionary(QWidget *parent)
17 : QDialog(parent)
20 //>>version
21 appver=QString("0.3.1");
22 //<<version
24 http = new QHttp(this);
25 setupUi(this);
26 enableTranslateButton();
27 prev->setEnabled(false);
28 next->setEnabled(false);
29 deleteHistory->setEnabled(false);
32 connect(lineEdit, SIGNAL(textChanged(const QString &)),
33 this, SLOT(enableTranslateButton()));
34 connect(http, SIGNAL(requestFinished(int, bool)),
35 this, SLOT(httpRequestFinished(int, bool)));
36 connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
37 this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
38 connect(pushButton, SIGNAL(clicked()),
39 this, SLOT(translateClicked()));
40 connect(pushButton, SIGNAL(clicked()),
41 this, SLOT(statusChanged()));
42 connect(dictionaryBox, SIGNAL(currentIndexChanged( int )),
43 this, SLOT(setDic(int)));
44 connect(modeBox, SIGNAL(currentIndexChanged( int )),
45 this, SLOT(setMode(int)));
46 connect(hitBox, SIGNAL(currentIndexChanged( int )),
47 this, SLOT(setHits(int)));
48 connect(aboutButton, SIGNAL(clicked()),
49 this, SLOT(aboutClicked()));
50 connect(deleteHistory, SIGNAL(clicked()),
51 this, SLOT(deleteHistoryClicked()));
52 connect(prev, SIGNAL(clicked()),
53 this, SLOT(stepPrev()));
54 connect(next, SIGNAL(clicked()),
55 this, SLOT(stepNext()));
57 // initialize UI
60 QStringList labels;
61 labels << QString("Szó") << QString("Jelentés");
62 tableWidget->setHorizontalHeaderLabels(labels);
63 // tableWidget->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
64 tableWidget->horizontalHeader()->setResizeMode(1, QHeaderView::Stretch);
66 tableWidget->verticalHeader()->hide();
67 tableWidget->setShowGrid(true);
69 szotar="szotarE";
70 dic="E";
71 mode="0";
72 hits="20";
76 á=%E1
77 ä=%E4
78 é=%E9
79 í=%ED
80 ó=%F3
81 ö=%F6
82 ő=%F5
83 ú=%FA
84 ü=%FC
85 ű=%FB
86 ß=%DF
89 html_list << "&aacute;" << "&auml;" << "&eacute;" << "&iacute;" << "&oacute;" << "&ouml;" << "&otilde;" << "&uacute;" << "&uuml;"<< "&ucirc;" << "&szlig;";
90 latin_list << "á" << "ä" << "é" << "í" << "ó" << "ö" << "ő" << "ú" << "ü" << "ű" << "ß";
91 hex_list << "%E1" << "%E4" << "%E9" << "%ED" << "%F3" << "%F6" << "%F5" << "%FA" << "%FC" << "%FB" << "%DF";
94 void Dictionary::translate(settings &set)
96 if (history.isEmpty()) deleteHistory->setEnabled(false);
97 else deleteHistory->setEnabled(true);
98 lineEdit->selectAll();
99 enablePrevButton();
100 enableNextButton();
102 QString word((set.word.split(" ", QString::SkipEmptyParts)).join("+"));
105 for (int i = 0; i < latin_list.size(); ++i){
106 word.replace(latin_list[i],hex_list[i]);
109 QString path("/cgi-bin/"+szotar+"?search_term="+word+"&max_hits="+hits+"&mode="+mode+"&diction="+dic);
110 QString host("www.cab.u-szeged.hu");
111 QUrl url;
113 url.setHost(host);
114 url.setPath(path);
115 url.setScheme("http");
116 http->setHost(url.host());
118 buffer=new QBuffer();
119 httpGetId = http->get(url.path(), buffer);
121 pushButton->setEnabled(false);
126 QStringList Dictionary::filter(QString html,QRegExp rx)
129 QStringList list;
130 QString str;
131 int pos = 0;
132 while ((pos = rx.indexIn(html, pos)) != -1) {
133 str = rx.cap(1);
134 list.append(str);
135 pos += rx.matchedLength();
138 return list;
142 void Dictionary::updateUi(settings &set)
144 lineEdit->setText(set.word);
145 dictionaryBox->setCurrentIndex(set.dict);
146 modeBox->setCurrentIndex(set.mode);
147 hitBox->setCurrentIndex(set.hit);
150 void Dictionary::updateHistory(settings &set)
153 if ( history.isEmpty() )
155 history.prepend(set);
156 hIter = history.begin();
157 }else if ( !((*history.begin()) == set) ) {
158 history.prepend(set);
159 hIter = history.begin();
160 if (history.size()>20)
162 history.removeLast();
166 enablePrevButton();
167 enableNextButton();
170 void Dictionary::translateClicked()
173 QString text(lineEdit->text());
174 settings set(dictionaryBox->currentIndex(),modeBox->currentIndex(), hitBox->currentIndex(), text);
175 translate(set);
176 updateHistory(set);
179 void Dictionary::httpRequestFinished(int requestId, bool error)
182 if (requestId != httpGetId)
183 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(QString("Keresett szó: ")+lineEdit->text()+", listázott találatok száma: "+found);
259 if (word_list.size()==0) statusLabel->setText(QString("<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(QString("<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ó", QString("<b>Qtdict v"+appver+"</b><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);