Add missing xdxf caching dialog source files
[mdictionary] / trunk / src / base / gui / MainWindow.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //Created by Mateusz Półrola
23
24 #include "MainWindow.h"
25 #include "ui_MainWindow.h"
26 #ifdef Q_WS_MAEMO_5
27     #include <QMaemo5InformationBox>
28 #endif
29
30
31 MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
32     GUIInterface(parent),
33     ui(new Ui::MainWindow) {
34
35     this->backbone = backbone;
36
37     initializeUI();
38
39     connectBackbone();
40     connectSearchBar();
41     connectWordList();
42     connectTranslationWidget();
43     connectDictManager();
44     connectMenu();
45
46     setExactSearch(false);
47
48     setWindowTitle("mDictionary");
49 }
50
51 MainWindow::~MainWindow() {
52     delete ui;
53 }
54
55
56 void MainWindow::initializeUI() {
57     ui->setupUi(this);
58
59     //sets attribute to maemo's stacked window
60     #ifdef Q_WS_MAEMO_5
61         setAttribute(Qt::WA_Maemo5StackedWindow);
62     #endif
63
64
65     searchBarWidget = new SearchBarWidget;
66     wordListWidget = new WordListWidget;
67
68     //translationWidget is antoher stacked window, so we don't add it to layout
69     //only create it with this widget as parent
70     translationWidget = new TranslationWidget(this);
71
72     ui->centralWidget->layout()->addWidget(wordListWidget);
73     ui->centralWidget->layout()->addWidget(searchBarWidget);
74
75
76
77     dictManagerWidget = new DictManagerWidget(this);
78
79     menuWidget = new MenuWidget(this);
80     menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
81     menuWidget->addSubMenu(tr("Settings"), new QPushButton("Settings"));
82     menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
83
84     ui->menuBar->addAction(menuWidget);
85 }
86
87 void MainWindow::closeEvent(QCloseEvent *event) {
88     //reqest to stop all searches and close app
89         emit quit();
90         event->accept();
91 }
92
93 bool MainWindow::exactSearch() {
94     return _exactSearch;
95 }
96
97 void MainWindow::setExactSearch(bool exact) {
98     _exactSearch = exact;
99 }
100
101 void MainWindow::setSearchString(QString word) {
102     searchString = word;
103 }
104
105 void MainWindow::wordListReady() {
106     //gets results from backbone
107     QMultiHash<QString, Translation*> res = backbone->result();
108     QHash<QString, QList<Translation*> > searchResult;
109
110     //if nothing was found
111     if(res.count() == 0) {
112         #ifdef Q_WS_MAEMO_5
113         QMaemo5InformationBox::information(this,
114                             tr("Can't find any matching words"),
115                             QMaemo5InformationBox::DefaultTimeout);
116         #endif
117         //show empty list to remove results of old search
118         emit showWordList(searchResult);
119     }
120     else {
121         //find translations of the same key word
122         QMultiHash<QString, Translation*>::iterator i;
123         for(i = res.begin(); i != res.end(); i++) {
124             searchResult[i.key()].push_back(i.value());
125         }
126
127
128         if(!exactSearch()) {
129             emit showWordList(searchResult);
130         }
131         else {
132
133             bool foundExactMatch = false;
134             QHash<QString, QList<Translation*> >::iterator j;
135             for(j = searchResult.begin(); j != searchResult.end(); j++) {
136                 if(j.key() == searchString && !foundExactMatch) {
137                     foundExactMatch = true;
138                     emit searchTranslations(j.value());
139                     break;
140                 }
141             }
142
143             if(!foundExactMatch) {
144                 #ifdef Q_WS_MAEMO_5
145                 QMaemo5InformationBox::information(this,
146                                     tr("Can't find exactly matching word"),
147                                     QMaemo5InformationBox::DefaultTimeout);
148                 #endif
149
150                 emit showWordList(searchResult);
151             }
152
153         }
154     }
155     setExactSearch(false);
156 }
157
158 void MainWindow::translationsReady() {
159     emit showTranslation(backbone->htmls());
160 }
161
162 QList<CommonDictInterface*> MainWindow::getPlugins() {
163     return backbone->getPlugins();
164 }
165
166 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
167     return backbone->getDictionaries();
168 }
169
170 void MainWindow::searchExact(QString word) {
171     setExactSearch(true);
172     //searching with searchBar, not directly by emiting searchWordList(),
173     //because it will set search word in searchBar's edit line
174     //this function is only used by history and when searching from attributes
175     searchBarWidget->search(word);
176 }
177
178
179
180 void MainWindow::breakSearching() {
181     //make sure to unset exact search mode
182     setExactSearch(false);
183 }
184
185 void MainWindow::addToHistory(QList<Translation *> trans) {
186     if(trans.count() > 0) {
187         backbone->history()->add(trans[0]->key());
188         translationWidget->setWindowTitle(trans[0]->key());
189     }
190 }
191
192 void MainWindow::historyNext() {
193     if(backbone->history()->nextAvailable()) {
194         QString next = backbone->history()->next();
195         searchBarWidget->searchDelay(next);
196     }
197 }
198
199 void MainWindow::historyPrev() {
200     if(backbone->history()->prevAvailable()) {
201         QString prev = backbone->history()->previous();
202         searchBarWidget->searchDelay(prev);
203     }
204 }
205
206 void MainWindow::disableMenu() {
207     if(ui->menuBar->actions().contains(menuWidget)) {
208         ui->menuBar->removeAction(menuWidget);
209     }
210 }
211
212 void MainWindow::enableMenu() {
213     if(!ui->menuBar->actions().contains(menuWidget)) {
214         ui->menuBar->addAction(menuWidget);
215     }
216 }
217
218 void MainWindow::showHistory() {
219     HistoryListDialog historyDialog(backbone->history()->list(), this);
220     if(historyDialog.exec() == QDialog::Accepted) {
221         backbone->history()->setCurrentElement(historyDialog.selectedRow());
222         searchExact(historyDialog.selectedWord());
223     }
224 }
225
226 void MainWindow::connectBackbone() {
227     connect(this, SIGNAL(quit()),
228             backbone, SLOT(quit()));
229
230     connect(this, SIGNAL(searchWordList(QString)),
231             backbone, SLOT(search(QString)));
232
233     connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
234             backbone, SLOT(searchHtml(QList<Translation*>)));
235
236     connect(this, SIGNAL(stopSearching()),
237             backbone, SLOT(stopSearching()));
238
239     connect(this, SIGNAL(stopSearching()),
240             this, SLOT(breakSearching()));
241
242     connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
243             backbone, SLOT(addDictionary(CommonDictInterface*)));
244
245     connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
246             backbone, SLOT(removeDictionary(CommonDictInterface*)));
247
248     connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
249             backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
250
251
252     connect(backbone, SIGNAL(ready()),
253             this, SIGNAL(setIdle()));
254
255     connect(backbone, SIGNAL(htmlReady()),
256             this, SIGNAL(setIdle()));
257
258
259     connect(backbone, SIGNAL(ready()),
260             this, SLOT(wordListReady()));
261
262     connect(backbone, SIGNAL(htmlReady()),
263             this, SLOT(translationsReady()));
264
265     connect(backbone, SIGNAL(searchCanceled()),
266             this, SIGNAL(setIdle()));
267
268
269
270
271     connect(this, SIGNAL(searchWordList(QString)),
272             this, SIGNAL(setBusy()));
273
274     connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
275             this, SIGNAL(setBusy()));
276
277     connect(this, SIGNAL(stopSearching()),
278             this, SIGNAL(setIdle()));
279
280     connect(this, SIGNAL(searchWordList(QString)),
281             this, SLOT(setSearchString(QString)));
282
283     connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
284             this, SLOT(addToHistory(QList<Translation*>)));
285
286
287 }
288
289 void MainWindow::connectSearchBar() {
290     connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
291             this, SIGNAL(searchWordList(QString)));
292
293     connect(searchBarWidget, SIGNAL(stopSearching()),
294             this, SIGNAL(stopSearching()));
295
296     connect(this, SIGNAL(setBusy()),
297             searchBarWidget, SLOT(setBusy()));
298
299     connect(this, SIGNAL(setIdle()),
300             searchBarWidget, SLOT(setIdle()));
301
302     connect(searchBarWidget, SIGNAL(historyNext()),
303             this, SLOT(historyNext()));
304
305     connect(searchBarWidget, SIGNAL(historyPrev()),
306             this, SLOT(historyPrev()));
307
308     connect(searchBarWidget, SIGNAL(historyShow()),
309             this, SLOT(showHistory()));
310
311     connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
312             backbone->history(), SLOT(refreshStatus()));
313
314     connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
315             searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
316 }
317
318 void MainWindow::connectWordList() {
319     connect(this,
320             SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
321             wordListWidget,
322             SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
323
324     connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
325             this, SIGNAL(searchTranslations(QList<Translation*>)));
326
327
328     connect(this, SIGNAL(setBusy()),
329             wordListWidget, SLOT(lockList()));
330
331     connect(this, SIGNAL(setIdle()),
332             wordListWidget, SLOT(unlockList()));
333 }
334
335 void MainWindow::connectTranslationWidget() {
336     connect(this, SIGNAL(showTranslation(QStringList)),
337             translationWidget, SLOT(show(QStringList)));
338
339 }
340
341 void MainWindow::connectDictManager() {
342     connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
343             this, SIGNAL(addNewDictionary(CommonDictInterface*)));
344
345     connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
346             this, SIGNAL(removeDictionary(CommonDictInterface*)));
347
348     connect(dictManagerWidget,
349             SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
350             this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
351 }
352
353 void MainWindow::connectMenu() {
354     connect(this, SIGNAL(setBusy()),
355             this, SLOT(disableMenu()));
356
357     connect(this, SIGNAL(setIdle()),
358             this, SLOT(enableMenu()));
359 }