Commit before weekend
[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 <QtGui>
26 #ifdef Q_WS_MAEMO_5
27     #include <QMaemo5InformationBox>
28 #endif
29
30
31 MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
32     GUIInterface(parent) {
33
34     this->backbone = backbone;
35
36
37     initializeUI();
38
39     connectBackbone();
40     connectSearchBar();
41     connectWordList();
42     connectTranslationWidget();
43     connectDictManager();
44     connectMenu();
45     connectBookmarksWidget();
46
47     setExactSearch(false);
48
49     setWindowTitle("mDictionary");
50
51     showMaximized();
52 }
53
54 MainWindow::~MainWindow() {
55
56 }
57
58
59 void MainWindow::initializeUI() {
60
61     #ifdef Q_WS_MAEMO_5
62         setAttribute(Qt::WA_Maemo5StackedWindow);
63     #endif
64
65     //translationWidget is antoher stacked window, so we don't add it to layout
66     //only create it with this widget as parent
67     translationWidget = new TranslationWidget(this);
68
69
70     mainLayout = new QVBoxLayout;
71     QWidget* w = new QWidget;
72     w->setLayout(mainLayout);
73     setCentralWidget(w);
74     menuBar = new QMenuBar;
75     setMenuBar(menuBar);
76
77     searchBarWidget = new SearchBarWidget;
78
79     wordListWidget = new WordListWidget;
80
81     welcomeScreenWidget = new WelcomeScreenWidget;
82
83
84     #ifdef Q_WS_MAEMO_5
85     //At start we set widget as welcome screen widget
86         mainLayout->addWidget(welcomeScreenWidget);
87         mainLayout->addWidget(searchBarWidget, 0, Qt::AlignBottom);
88     #else
89         translationWidget->hide();
90         //we add to splitter word list and welcome screen
91         splitter = new QSplitter(Qt::Horizontal);
92         splitter->addWidget(wordListWidget);
93         splitter->addWidget(welcomeScreenWidget);
94         splitter->setStretchFactor(1, 150);
95         mainLayout->addWidget(splitter);
96         mainLayout->addWidget(searchBarWidget);
97     #endif
98
99
100     dictManagerWidget = new DictManagerWidget(this);
101     dictManagerWidget->hide();
102
103     settingsWidget = new SettingsWidget(this);
104     settingsWidget->hide();
105
106     bookmarksWidget = new BookmarksWidget(this);
107     bookmarksWidget->hide();
108
109     aboutWidget = new AboutWidget(this);
110     aboutWidget->hide();
111
112     //creating menus
113
114     #ifdef Q_WS_MAEMO_5
115         menuWidget = new MenuWidget(this);
116         menuWidget->addSubMenu(tr("Settings"), settingsWidget);
117         menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
118         menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
119         menuWidget->addSubMenu(tr("About"), aboutWidget);
120         menuBar->addAction(menuWidget);
121     #else
122         dictionariesAction = menuBar->addAction(tr("Dictionaries"));
123         connect(dictionariesAction, SIGNAL(triggered()),
124                 dictManagerWidget, SLOT(show()));
125
126         settingsAction = menuBar->addAction(tr("Settings"));
127         connect(settingsAction, SIGNAL(triggered()),
128                 settingsWidget, SLOT(show()));
129
130         QMenu* m = menuBar->addMenu(tr("Bookmarks"));
131         bookmarksShowAllAction = new QAction(tr("Show all"), m);
132
133         bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
134
135         m->addAction(bookmarksShowAllAction);
136         m->addAction(bookmarksRemoveAllAction);
137
138         aboutAction = menuBar->addAction(tr("About"));
139         connect(aboutAction, SIGNAL(triggered()),
140                 aboutWidget, SLOT(show()));
141     #endif
142 }
143
144 void MainWindow::closeEvent(QCloseEvent *event) {
145     //reqest to stop all searches and close app
146         emit quit();
147         event->accept();
148 }
149
150 bool MainWindow::exactSearch() {
151     return _exactSearch;
152 }
153
154 void MainWindow::setExactSearch(bool exact) {
155     _exactSearch = exact;
156 }
157
158 void MainWindow::setSearchString(QString word) {
159     searchString = word;
160 }
161
162 void MainWindow::wordListReady() {
163     //gets results from backbone
164     QMultiHash<QString, Translation*> res = backbone->result();
165     QHash<QString, QList<Translation*> > searchResult;
166
167     #ifdef Q_WS_MAEMO_5
168     //switch welcome screen with word list
169     if(!wordListWidget->isVisible()) {
170         int i = mainLayout->indexOf(welcomeScreenWidget);
171         QBoxLayout* l = (QBoxLayout*)(mainLayout);
172         l->removeWidget(welcomeScreenWidget);
173         welcomeScreenWidget->deleteLater();
174         l->insertWidget(0, wordListWidget);
175     }
176     #endif
177
178     //if nothing was found
179     if(res.count() == 0) {
180         showNotify(Notify::Info, tr("Can't find any matching words"));
181
182         //show empty list to remove results of old search
183         emit showWordList(searchResult);
184     }
185     else {
186         //find translations of the same key word
187         QMultiHash<QString, Translation*>::iterator i;
188         for(i = res.begin(); i != res.end(); i++) {
189             searchResult[i.key()].push_back(i.value());
190         }
191
192
193         if(!exactSearch()) {
194             emit showWordList(searchResult);
195         }
196         else {
197             #ifndef Q_WS_MAEMO_5
198             //on desktop we show word list in exact search
199                 emit showWordList(searchResult);
200             #endif
201             bool foundExactMatch = false;
202             QHash<QString, QList<Translation*> >::iterator j;
203             for(j = searchResult.begin(); j != searchResult.end(); j++) {
204                 if(j.key() == searchString && !foundExactMatch) {
205                     foundExactMatch = true;
206                     emit searchTranslations(j.value());
207                     break;
208                 }
209             }
210
211             if(!foundExactMatch) {
212                 showNotify(Notify::Info,
213                            tr("Can't find exactly matching word"));
214
215                 emit showWordList(searchResult);
216             }
217
218         }
219     }
220     setExactSearch(false);
221 }
222
223 void MainWindow::translationsReady() {
224     #ifndef Q_WS_MAEMO_5
225     //switch welcome screen with translation widget
226     if(!translationWidget->isVisible()) {
227         QBoxLayout* l = (QBoxLayout*)(mainLayout);
228         QSplitter* s = (QSplitter*)((QWidgetItem*)(l->itemAt(0))->widget());
229         s->insertWidget(1,translationWidget);
230         s->setStretchFactor(1, 150);
231         welcomeScreenWidget->deleteLater();
232     }
233     #endif
234
235     emit showTranslation(backbone->htmls());
236 }
237
238 QList<CommonDictInterface*> MainWindow::getPlugins() {
239     return backbone->getPlugins();
240 }
241
242 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
243     return backbone->getDictionaries();
244 }
245
246
247 void MainWindow::search(QString word) {
248     setExactSearch(false);
249     searchBarWidget->search(word);
250 }
251
252 void MainWindow::searchExact(QString word) {
253     setExactSearch(true);
254     //searching with searchBar, not directly by emiting searchWordList(),
255     //because it will set search word in searchBar's edit line
256     //this function is only used by history and when searching from attributes
257     searchBarWidget->search(word);
258 }
259
260
261
262 void MainWindow::breakSearching() {
263     //make sure to unset exact search mode
264     setExactSearch(false);
265 }
266
267 void MainWindow::addToHistory(QList<Translation *> trans) {
268     if(trans.count() > 0) {
269         backbone->history()->add(trans[0]->key());
270     }
271 }
272
273 void MainWindow::historyNext() {
274     if(backbone->history()->nextAvailable()) {
275         QString next = backbone->history()->next();
276         #ifndef Q_WS_MAEMO_5
277             setExactSearch(true);
278         #endif
279         searchBarWidget->searchDelay(next);
280     }
281 }
282
283 void MainWindow::historyPrev() {
284     if(backbone->history()->prevAvailable()) {
285         #ifndef Q_WS_MAEMO_5
286             setExactSearch(true);
287         #endif
288         QString prev = backbone->history()->previous();
289         searchBarWidget->searchDelay(prev);
290     }
291 }
292
293 void MainWindow::disableMenu() {
294     #ifdef Q_WS_MAEMO_5
295         if(menuBar->actions().contains(menuWidget)) {
296               menuBar->removeAction(menuWidget);
297         }
298     #else
299         menuBar->setEnabled(false);
300     #endif
301 }
302
303 void MainWindow::enableMenu() {
304     #ifdef Q_WS_MAEMO_5
305         if(!menuBar->actions().contains(menuWidget)) {
306             menuBar->addAction(menuWidget);
307         }
308     #else
309         menuBar->setEnabled(true);
310     #endif
311 }
312
313 void MainWindow::showHistory() {
314     HistoryListDialog historyDialog(backbone->history()->list(), this);
315     if(historyDialog.exec() == QDialog::Accepted) {
316         backbone->history()->setCurrentElement(historyDialog.selectedRow());
317         searchExact(historyDialog.selectedWord());
318     }
319 }
320
321 void MainWindow::setSettings(Settings *s) {
322     backbone->setSettings(s);
323 }
324
325 Settings* MainWindow::settings() {
326     return backbone->settings();
327 }
328
329
330 void MainWindow::showNotify(Notify::NotifyType type, QString text) {
331     switch(type) {
332     case Notify::Info:
333         #ifdef Q_WS_MAEMO_5
334             QMaemo5InformationBox::information(this,
335                                 text,
336                                 QMaemo5InformationBox::DefaultTimeout);
337         #else
338             QMessageBox::information(this, "Information", text);
339         #endif
340         break;
341
342     case Notify::Warning:
343         #ifndef Q_WS_MAEMO_5
344                 QMessageBox::warning(this, "Warning", text);
345                 break;
346         #endif
347
348     case Notify::Error:
349         #ifdef Q_WS_MAEMO_5
350             QMaemo5InformationBox::information(this,
351                                 text,
352                                 QMaemo5InformationBox::NoTimeout);
353         #else
354             QMessageBox::critical(this, "Error", text);
355         #endif
356         break;
357     }
358 }
359
360 void MainWindow::connectBackbone() {
361     connect(this, SIGNAL(quit()),
362             backbone, SLOT(quit()));
363
364     connect(this, SIGNAL(searchWordList(QString)),
365             backbone, SLOT(search(QString)));
366
367     connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
368             backbone, SLOT(searchHtml(QList<Translation*>)));
369
370     connect(this, SIGNAL(stopSearching()),
371             backbone, SLOT(stopSearching()));
372
373     connect(this, SIGNAL(stopSearching()),
374             this, SLOT(breakSearching()));
375
376     connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
377             backbone, SLOT(addDictionary(CommonDictInterface*)));
378
379     connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
380             backbone, SLOT(removeDictionary(CommonDictInterface*)));
381
382     connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
383             backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
384
385
386     connect(backbone, SIGNAL(ready()),
387             this, SIGNAL(setIdle()));
388
389     connect(backbone, SIGNAL(htmlReady()),
390             this, SIGNAL(setIdle()));
391
392
393     connect(backbone, SIGNAL(ready()),
394             this, SLOT(wordListReady()));
395
396     connect(backbone, SIGNAL(htmlReady()),
397             this, SLOT(translationsReady()));
398
399     connect(backbone, SIGNAL(searchCanceled()),
400             this, SIGNAL(setIdle()));
401
402
403
404
405     connect(this, SIGNAL(searchWordList(QString)),
406             this, SIGNAL(setBusy()));
407
408     connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
409             this, SIGNAL(setBusy()));
410
411     connect(this, SIGNAL(stopSearching()),
412             this, SIGNAL(setIdle()));
413
414     connect(this, SIGNAL(searchWordList(QString)),
415             this, SLOT(setSearchString(QString)));
416
417     connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
418             this, SLOT(addToHistory(QList<Translation*>)));
419
420
421 }
422
423 void MainWindow::connectSearchBar() {
424     connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
425             this, SIGNAL(searchWordList(QString)));
426
427     connect(searchBarWidget, SIGNAL(stopSearching()),
428             this, SIGNAL(stopSearching()));
429
430     connect(this, SIGNAL(setBusy()),
431             searchBarWidget, SLOT(setBusy()));
432
433     connect(this, SIGNAL(setIdle()),
434             searchBarWidget, SLOT(setIdle()));
435
436     connect(searchBarWidget, SIGNAL(historyNext()),
437             this, SLOT(historyNext()));
438
439     connect(searchBarWidget, SIGNAL(historyPrev()),
440             this, SLOT(historyPrev()));
441
442     connect(searchBarWidget, SIGNAL(historyShow()),
443             this, SLOT(showHistory()));
444
445     connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
446             backbone->history(), SLOT(refreshStatus()));
447
448     connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
449             searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
450 }
451
452 void MainWindow::connectWordList() {
453     connect(this,
454             SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
455             wordListWidget,
456             SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
457
458     connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
459             this, SIGNAL(searchTranslations(QList<Translation*>)));
460
461
462
463
464     connect(this, SIGNAL(setBusy()),
465             wordListWidget, SLOT(lockList()));
466
467     connect(this, SIGNAL(setIdle()),
468             wordListWidget, SLOT(unlockList()));
469
470     connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
471             backbone, SLOT(addBookmark(QList<Translation*>)));
472
473     connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
474             backbone, SLOT(removeBookmark(QList<Translation*>)));
475 }
476
477 void MainWindow::connectTranslationWidget() {
478     connect(this, SIGNAL(showTranslation(QStringList)),
479             translationWidget, SLOT(show(QStringList)));
480
481      #ifdef Q_WS_MAEMO_5
482         connect(translationWidget, SIGNAL(search(QString)),
483                 this, SLOT(search(QString)));
484     #else
485         connect(translationWidget, SIGNAL(search(QString)),
486                 this, SLOT(searchExact(QString)));
487     #endif
488
489
490 }
491
492 void MainWindow::connectDictManager() {
493     connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
494             this, SIGNAL(addNewDictionary(CommonDictInterface*)));
495
496     connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
497             this, SIGNAL(removeDictionary(CommonDictInterface*)));
498
499     connect(dictManagerWidget,
500             SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
501             this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
502 }
503
504 void MainWindow::connectMenu() {
505     connect(this, SIGNAL(setBusy()),
506             this, SLOT(disableMenu()));
507
508     connect(this, SIGNAL(setIdle()),
509             this, SLOT(enableMenu()));
510 }
511
512
513 void MainWindow::connectBookmarksWidget() {
514     #ifdef Q_WS_MAEMO_5
515         //after removing bookmarks we search for it once again to clear word list
516         connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
517                 backbone, SLOT(removeAllBookmark()));
518
519         connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
520                 backbone, SLOT(fetchBookmarks()));
521
522
523         connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
524                 menuWidget, SLOT(hideMenu()));
525
526         connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
527                 backbone, SLOT(fetchBookmarks()));
528
529
530     #else
531         connect(bookmarksShowAllAction, SIGNAL(triggered()),
532                 backbone, SLOT(fetchBookmarks()));
533
534         connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
535                 backbone, SLOT(removeAllBookmark()));
536
537         connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
538                 backbone, SLOT(fetchBookmarks()));
539     #endif
540 }