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