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