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