Added welcome screen
[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     #ifdef Q_WS_MAEMO_5
108         menuWidget = new MenuWidget(this);
109         menuWidget->addSubMenu(tr("Settings"), settingsWidget);
110         menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
111         menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
112         menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
113         menu->addAction(menuWidget);
114         ui->menuBar->addMenu(menu);
115     #else
116         dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
117         connect(dictionariesAction, SIGNAL(triggered()),
118                 dictManagerWidget, SLOT(show()));
119
120         settingsAction = ui->menuBar->addAction(tr("Settings"));
121         connect(settingsAction, SIGNAL(triggered()),
122                 settingsWidget, SLOT(show()));
123
124         bookmarksAction = ui->menuBar->addAction(tr("Bookmarks"));
125         connect(bookmarksAction, SIGNAL(triggered()),
126                 bookmarksWidget, SLOT(show()));
127     #endif
128
129 }
130
131 void MainWindow::closeEvent(QCloseEvent *event) {
132     //reqest to stop all searches and close app
133         emit quit();
134         event->accept();
135 }
136
137 bool MainWindow::exactSearch() {
138     return _exactSearch;
139 }
140
141 void MainWindow::setExactSearch(bool exact) {
142     _exactSearch = exact;
143 }
144
145 void MainWindow::setSearchString(QString word) {
146     searchString = word;
147 }
148
149 void MainWindow::wordListReady() {
150     //gets results from backbone
151     QMultiHash<QString, Translation*> res = backbone->result();
152     QHash<QString, QList<Translation*> > searchResult;
153
154     #ifdef Q_WS_MAEMO_5
155     if(!wordListWidget->isVisible()) {
156         int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
157         QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
158         l->removeWidget(welcomeScreenWidget);
159         l->insertWidget(0, wordListWidget);
160         qDebug()<<"changed";
161     }
162     #endif
163
164     //if nothing was found
165     if(res.count() == 0) {
166         #ifdef Q_WS_MAEMO_5
167         QMaemo5InformationBox::information(this,
168                             tr("Can't find any matching words"),
169                             QMaemo5InformationBox::DefaultTimeout);
170         #endif
171         //show empty list to remove results of old search
172         emit showWordList(searchResult);
173     }
174     else {
175         //find translations of the same key word
176         QMultiHash<QString, Translation*>::iterator i;
177         for(i = res.begin(); i != res.end(); i++) {
178             searchResult[i.key()].push_back(i.value());
179         }
180
181
182         if(!exactSearch()) {
183             emit showWordList(searchResult);
184         }
185         else {
186             #ifndef Q_WS_MAEMO_5
187                 emit showWordList(searchResult);
188             #endif
189             bool foundExactMatch = false;
190             QHash<QString, QList<Translation*> >::iterator j;
191             for(j = searchResult.begin(); j != searchResult.end(); j++) {
192                 if(j.key() == searchString && !foundExactMatch) {
193                     foundExactMatch = true;
194                     emit searchTranslations(j.value());
195                     break;
196                 }
197             }
198
199             if(!foundExactMatch) {
200                 #ifdef Q_WS_MAEMO_5
201                 QMaemo5InformationBox::information(this,
202                                     tr("Can't find exactly matching word"),
203                                     QMaemo5InformationBox::DefaultTimeout);
204                 #endif
205
206                 emit showWordList(searchResult);
207             }
208
209         }
210     }
211     setExactSearch(false);
212 }
213
214 void MainWindow::translationsReady() {
215     #ifndef Q_WS_MAEMO_5
216     if(!translationWidget->isVisible()) {
217         int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
218         QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
219         QSplitter* s = (QSplitter*)((QWidgetItem*)(l->itemAt(0))->widget());
220         s->insertWidget(1,translationWidget);
221         s->setStretchFactor(1, 150);
222         welcomeScreenWidget->deleteLater();
223         qDebug()<<"changed";
224     }
225     #endif
226
227     emit showTranslation(backbone->htmls());
228 }
229
230 QList<CommonDictInterface*> MainWindow::getPlugins() {
231     return backbone->getPlugins();
232 }
233
234 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
235     return backbone->getDictionaries();
236 }
237
238 void MainWindow::searchExact(QString word) {
239     setExactSearch(true);
240     //searching with searchBar, not directly by emiting searchWordList(),
241     //because it will set search word in searchBar's edit line
242     //this function is only used by history and when searching from attributes
243     searchBarWidget->search(word);
244 }
245
246
247
248 void MainWindow::breakSearching() {
249     //make sure to unset exact search mode
250     setExactSearch(false);
251 }
252
253 void MainWindow::addToHistory(QList<Translation *> trans) {
254     if(trans.count() > 0) {
255         backbone->history()->add(trans[0]->key());
256         translationWidget->setWindowTitle(trans[0]->key());
257     }
258 }
259
260 void MainWindow::historyNext() {
261     if(backbone->history()->nextAvailable()) {
262         QString next = backbone->history()->next();
263         #ifndef Q_WS_MAEMO_5
264             setExactSearch(true);
265         #endif
266         searchBarWidget->searchDelay(next);
267     }
268 }
269
270 void MainWindow::historyPrev() {
271     if(backbone->history()->prevAvailable()) {
272         #ifndef Q_WS_MAEMO_5
273             setExactSearch(true);
274         #endif
275         QString prev = backbone->history()->previous();
276         searchBarWidget->searchDelay(prev);
277     }
278 }
279
280 void MainWindow::disableMenu() {
281     #ifdef Q_WS_MAEMO_5
282         if(ui->menuBar->actions().contains(menuWidget)) {
283               ui->menuBar->removeAction(menuWidget);
284         }
285     #else
286         ui->menuBar->setEnabled(false);
287     #endif
288 }
289
290 void MainWindow::enableMenu() {
291     #ifdef Q_WS_MAEMO_5
292         if(!ui->menuBar->actions().contains(menuWidget)) {
293             ui->menuBar->addAction(menuWidget);
294         }
295     #else
296         ui->menuBar->setEnabled(true);
297     #endif
298 }
299
300 void MainWindow::showHistory() {
301     HistoryListDialog historyDialog(backbone->history()->list(), this);
302     if(historyDialog.exec() == QDialog::Accepted) {
303         backbone->history()->setCurrentElement(historyDialog.selectedRow());
304         searchExact(historyDialog.selectedWord());
305     }
306 }
307
308 void MainWindow::setSettings(Settings *s) {
309     backbone->setSettings(s);
310 }
311
312 Settings* MainWindow::settings() {
313     return backbone->settings();
314 }
315
316 void MainWindow::connectBackbone() {
317     connect(this, SIGNAL(quit()),
318             backbone, SLOT(quit()));
319
320     connect(this, SIGNAL(searchWordList(QString)),
321             backbone, SLOT(search(QString)));
322
323     connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
324             backbone, SLOT(searchHtml(QList<Translation*>)));
325
326     connect(this, SIGNAL(stopSearching()),
327             backbone, SLOT(stopSearching()));
328
329     connect(this, SIGNAL(stopSearching()),
330             this, SLOT(breakSearching()));
331
332     connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
333             backbone, SLOT(addDictionary(CommonDictInterface*)));
334
335     connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
336             backbone, SLOT(removeDictionary(CommonDictInterface*)));
337
338     connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
339             backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
340
341
342     connect(backbone, SIGNAL(ready()),
343             this, SIGNAL(setIdle()));
344
345     connect(backbone, SIGNAL(htmlReady()),
346             this, SIGNAL(setIdle()));
347
348
349     connect(backbone, SIGNAL(ready()),
350             this, SLOT(wordListReady()));
351
352     connect(backbone, SIGNAL(htmlReady()),
353             this, SLOT(translationsReady()));
354
355     connect(backbone, SIGNAL(searchCanceled()),
356             this, SIGNAL(setIdle()));
357
358
359
360
361     connect(this, SIGNAL(searchWordList(QString)),
362             this, SIGNAL(setBusy()));
363
364     connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
365             this, SIGNAL(setBusy()));
366
367     connect(this, SIGNAL(stopSearching()),
368             this, SIGNAL(setIdle()));
369
370     connect(this, SIGNAL(searchWordList(QString)),
371             this, SLOT(setSearchString(QString)));
372
373     connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
374             this, SLOT(addToHistory(QList<Translation*>)));
375
376
377 }
378
379 void MainWindow::connectSearchBar() {
380     connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
381             this, SIGNAL(searchWordList(QString)));
382
383     connect(searchBarWidget, SIGNAL(stopSearching()),
384             this, SIGNAL(stopSearching()));
385
386     connect(this, SIGNAL(setBusy()),
387             searchBarWidget, SLOT(setBusy()));
388
389     connect(this, SIGNAL(setIdle()),
390             searchBarWidget, SLOT(setIdle()));
391
392     connect(searchBarWidget, SIGNAL(historyNext()),
393             this, SLOT(historyNext()));
394
395     connect(searchBarWidget, SIGNAL(historyPrev()),
396             this, SLOT(historyPrev()));
397
398     connect(searchBarWidget, SIGNAL(historyShow()),
399             this, SLOT(showHistory()));
400
401     connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
402             backbone->history(), SLOT(refreshStatus()));
403
404     connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
405             searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
406 }
407
408 void MainWindow::connectWordList() {
409     connect(this,
410             SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
411             wordListWidget,
412             SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
413
414     connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
415             this, SIGNAL(searchTranslations(QList<Translation*>)));
416
417
418
419
420     connect(this, SIGNAL(setBusy()),
421             wordListWidget, SLOT(lockList()));
422
423     connect(this, SIGNAL(setIdle()),
424             wordListWidget, SLOT(unlockList()));
425
426     connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
427             backbone, SLOT(addBookmark(QList<Translation*>)));
428
429     connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
430             backbone, SLOT(removeBookmark(QList<Translation*>)));
431 }
432
433 void MainWindow::connectTranslationWidget() {
434     connect(this, SIGNAL(showTranslation(QStringList)),
435             translationWidget, SLOT(show(QStringList)));
436
437 }
438
439 void MainWindow::connectDictManager() {
440     connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
441             this, SIGNAL(addNewDictionary(CommonDictInterface*)));
442
443     connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
444             this, SIGNAL(removeDictionary(CommonDictInterface*)));
445
446     connect(dictManagerWidget,
447             SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
448             this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
449 }
450
451 void MainWindow::connectMenu() {
452     connect(this, SIGNAL(setBusy()),
453             this, SLOT(disableMenu()));
454
455     connect(this, SIGNAL(setIdle()),
456             this, SLOT(enableMenu()));
457 }
458
459
460 void MainWindow::showAllBookmarks() {
461     qDebug()<<"asdas";
462 }
463
464 void MainWindow::connectBookmarksWidget() {
465     connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
466             backbone, SLOT(removeAllBookmark()));
467
468     connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
469             menu, SLOT(hide()));
470
471     connect(menu, SIGNAL(aboutToHide()),
472             this, SLOT(showAllBookmarks()));
473 }