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