1 // Copyright (c) 2008, Andrew Bécsi (andrewbecsi @ yahoo . co . uk)
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
14 #include "dictionary.h"
16 Dictionary::Dictionary(QWidget
*parent
)
21 appver
=QString("0.3.1");
24 http
= new QHttp(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()));
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);
89 html_list
<< "á" << "ä" << "é" << "í" << "ó" << "ö" << "õ" << "ú" << "ü"<< "û" << "ß";
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();
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");
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
)
132 while ((pos
= rx
.indexIn(html
, pos
)) != -1) {
135 pos
+= rx
.matchedLength();
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();
170 void Dictionary::translateClicked()
173 QString
text(lineEdit
->text());
174 settings
set(dictionaryBox
->currentIndex(),modeBox
->currentIndex(), hitBox
->currentIndex(), text
);
179 void Dictionary::httpRequestFinished(int requestId
, bool error
)
182 if (requestId
!= httpGetId
)
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
202 html
.append(str
+" ");
207 //filtering word data from html string
210 QRegExp
rx( "<LI>(.+)-->" );
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
);
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
)
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] ){
272 // meanings << meaning_list[i];
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."));
294 void Dictionary::setMode(int index
)
299 void Dictionary::setHits(int 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
)
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()
356 void Dictionary::stepNext()
363 void Dictionary::deleteHistoryClicked()
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);