1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 //Created by Mateusz Półrola
24 #include "MainWindow.h"
25 #include "ui_MainWindow.h"
28 #include <QMaemo5InformationBox>
32 MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
34 ui(new Ui::MainWindow) {
36 this->backbone = backbone;
43 connectTranslationWidget();
46 connectBookmarksWidget();
49 setExactSearch(false);
51 setWindowTitle("mDictionary");
56 MainWindow::~MainWindow() {
61 void MainWindow::initializeUI() {
65 setAttribute(Qt::WA_Maemo5StackedWindow);
68 searchBarWidget = new SearchBarWidget;
70 wordListWidget = new WordListWidget;
72 //translationWidget is antoher stacked window, so we don't add it to layout
73 //only create it with this widget as parent
74 translationWidget = new TranslationWidget(this);
76 welcomeScreenWidget = new WelcomeScreenWidget;
80 //At start we set widget as welcome screen widget
81 ui->centralWidget->layout()->addWidget(welcomeScreenWidget);
82 QVBoxLayout* vl = (QVBoxLayout*)(ui->centralWidget->layout());
83 vl->addWidget(searchBarWidget, 0, Qt::AlignBottom);
85 translationWidget->hide();
86 //we add to splitter word list and welcome screen
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 ui->centralWidget->layout()->addWidget(searchBarWidget);
97 dictManagerWidget = new DictManagerWidget(this);
98 dictManagerWidget->hide();
100 settingsWidget = new SettingsWidget(this);
101 settingsWidget->hide();
103 bookmarksWidget = new BookmarksWidget(this);
104 bookmarksWidget->hide();
106 aboutWidget = new AboutWidget(this);
113 menuWidget = new MenuWidget(this);
114 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
115 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
116 menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
117 menuWidget->addSubMenu(tr("About"), aboutWidget);
118 ui->menuBar->addAction(menuWidget);
120 dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
121 connect(dictionariesAction, SIGNAL(triggered()),
122 dictManagerWidget, SLOT(show()));
124 settingsAction = ui->menuBar->addAction(tr("Settings"));
125 connect(settingsAction, SIGNAL(triggered()),
126 settingsWidget, SLOT(show()));
128 QMenu* m = ui->menuBar->addMenu(tr("Bookmarks"));
129 bookmarksShowAllAction = new QAction(tr("Show all"), m);
131 bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
133 m->addAction(bookmarksShowAllAction);
134 m->addAction(bookmarksRemoveAllAction);
136 aboutAction = ui->menuBar->addAction(tr("About"));
137 connect(aboutAction, SIGNAL(triggered()),
138 aboutWidget, SLOT(show()));
143 void MainWindow::closeEvent(QCloseEvent *event) {
144 //reqest to stop all searches and close app
149 bool MainWindow::exactSearch() {
153 void MainWindow::setExactSearch(bool exact) {
154 _exactSearch = exact;
157 void MainWindow::setSearchString(QString word) {
161 void MainWindow::wordListReady() {
162 //gets results from backbone
163 QMultiHash<QString, Translation*> res = backbone->result();
164 QHash<QString, QList<Translation*> > searchResult;
167 //switch welcome screen with word list
168 if(!wordListWidget->isVisible()) {
169 int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
170 QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
171 l->removeWidget(welcomeScreenWidget);
172 welcomeScreenWidget->deleteLater();
173 l->insertWidget(0, wordListWidget);
177 //if nothing was found
178 if(res.count() == 0) {
180 QMaemo5InformationBox::information(this,
181 tr("Can't find any matching words"),
182 QMaemo5InformationBox::DefaultTimeout);
184 //show empty list to remove results of old search
185 emit showWordList(searchResult);
188 //find translations of the same key word
189 QMultiHash<QString, Translation*>::iterator i;
190 for(i = res.begin(); i != res.end(); i++) {
191 searchResult[i.key()].push_back(i.value());
196 emit showWordList(searchResult);
200 //on desktop we show word list in exact search
201 emit showWordList(searchResult);
203 bool foundExactMatch = false;
204 QHash<QString, QList<Translation*> >::iterator j;
205 for(j = searchResult.begin(); j != searchResult.end(); j++) {
206 if(j.key() == searchString && !foundExactMatch) {
207 foundExactMatch = true;
208 emit searchTranslations(j.value());
213 if(!foundExactMatch) {
215 QMaemo5InformationBox::information(this,
216 tr("Can't find exactly matching word"),
217 QMaemo5InformationBox::DefaultTimeout);
220 emit showWordList(searchResult);
225 setExactSearch(false);
228 void MainWindow::translationsReady() {
230 //switch welcome screen with translation widget
231 if(!translationWidget->isVisible()) {
232 int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
233 QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
234 QSplitter* s = (QSplitter*)((QWidgetItem*)(l->itemAt(0))->widget());
235 s->insertWidget(1,translationWidget);
236 s->setStretchFactor(1, 150);
237 welcomeScreenWidget->deleteLater();
241 emit showTranslation(backbone->htmls());
244 QList<CommonDictInterface*> MainWindow::getPlugins() {
245 return backbone->getPlugins();
248 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
249 return backbone->getDictionaries();
252 void MainWindow::searchExact(QString word) {
253 setExactSearch(true);
254 //searching with searchBar, not directly by emiting searchWordList(),
255 //because it will set search word in searchBar's edit line
256 //this function is only used by history and when searching from attributes
257 searchBarWidget->search(word);
262 void MainWindow::breakSearching() {
263 //make sure to unset exact search mode
264 setExactSearch(false);
267 void MainWindow::addToHistory(QList<Translation *> trans) {
268 if(trans.count() > 0) {
269 backbone->history()->add(trans[0]->key());
270 translationWidget->setWindowTitle(trans[0]->key());
274 void MainWindow::historyNext() {
275 if(backbone->history()->nextAvailable()) {
276 QString next = backbone->history()->next();
278 setExactSearch(true);
280 searchBarWidget->searchDelay(next);
284 void MainWindow::historyPrev() {
285 if(backbone->history()->prevAvailable()) {
287 setExactSearch(true);
289 QString prev = backbone->history()->previous();
290 searchBarWidget->searchDelay(prev);
294 void MainWindow::disableMenu() {
296 if(ui->menuBar->actions().contains(menuWidget)) {
297 ui->menuBar->removeAction(menuWidget);
300 ui->menuBar->setEnabled(false);
304 void MainWindow::enableMenu() {
306 if(!ui->menuBar->actions().contains(menuWidget)) {
307 ui->menuBar->addAction(menuWidget);
310 ui->menuBar->setEnabled(true);
314 void MainWindow::showHistory() {
315 HistoryListDialog historyDialog(backbone->history()->list(), this);
316 if(historyDialog.exec() == QDialog::Accepted) {
317 backbone->history()->setCurrentElement(historyDialog.selectedRow());
318 searchExact(historyDialog.selectedWord());
322 void MainWindow::setSettings(Settings *s) {
323 backbone->setSettings(s);
326 Settings* MainWindow::settings() {
327 return backbone->settings();
330 void MainWindow::connectBackbone() {
331 connect(this, SIGNAL(quit()),
332 backbone, SLOT(quit()));
334 connect(this, SIGNAL(searchWordList(QString)),
335 backbone, SLOT(search(QString)));
337 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
338 backbone, SLOT(searchHtml(QList<Translation*>)));
340 connect(this, SIGNAL(stopSearching()),
341 backbone, SLOT(stopSearching()));
343 connect(this, SIGNAL(stopSearching()),
344 this, SLOT(breakSearching()));
346 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
347 backbone, SLOT(addDictionary(CommonDictInterface*)));
349 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
350 backbone, SLOT(removeDictionary(CommonDictInterface*)));
352 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
353 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
356 connect(backbone, SIGNAL(ready()),
357 this, SIGNAL(setIdle()));
359 connect(backbone, SIGNAL(htmlReady()),
360 this, SIGNAL(setIdle()));
363 connect(backbone, SIGNAL(ready()),
364 this, SLOT(wordListReady()));
366 connect(backbone, SIGNAL(htmlReady()),
367 this, SLOT(translationsReady()));
369 connect(backbone, SIGNAL(searchCanceled()),
370 this, SIGNAL(setIdle()));
375 connect(this, SIGNAL(searchWordList(QString)),
376 this, SIGNAL(setBusy()));
378 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
379 this, SIGNAL(setBusy()));
381 connect(this, SIGNAL(stopSearching()),
382 this, SIGNAL(setIdle()));
384 connect(this, SIGNAL(searchWordList(QString)),
385 this, SLOT(setSearchString(QString)));
387 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
388 this, SLOT(addToHistory(QList<Translation*>)));
393 void MainWindow::connectSearchBar() {
394 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
395 this, SIGNAL(searchWordList(QString)));
397 connect(searchBarWidget, SIGNAL(stopSearching()),
398 this, SIGNAL(stopSearching()));
400 connect(this, SIGNAL(setBusy()),
401 searchBarWidget, SLOT(setBusy()));
403 connect(this, SIGNAL(setIdle()),
404 searchBarWidget, SLOT(setIdle()));
406 connect(searchBarWidget, SIGNAL(historyNext()),
407 this, SLOT(historyNext()));
409 connect(searchBarWidget, SIGNAL(historyPrev()),
410 this, SLOT(historyPrev()));
412 connect(searchBarWidget, SIGNAL(historyShow()),
413 this, SLOT(showHistory()));
415 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
416 backbone->history(), SLOT(refreshStatus()));
418 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
419 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
422 void MainWindow::connectWordList() {
424 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
426 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
428 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
429 this, SIGNAL(searchTranslations(QList<Translation*>)));
434 connect(this, SIGNAL(setBusy()),
435 wordListWidget, SLOT(lockList()));
437 connect(this, SIGNAL(setIdle()),
438 wordListWidget, SLOT(unlockList()));
440 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
441 backbone, SLOT(addBookmark(QList<Translation*>)));
443 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
444 backbone, SLOT(removeBookmark(QList<Translation*>)));
447 void MainWindow::connectTranslationWidget() {
448 connect(this, SIGNAL(showTranslation(QStringList)),
449 translationWidget, SLOT(show(QStringList)));
453 void MainWindow::connectDictManager() {
454 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
455 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
457 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
458 this, SIGNAL(removeDictionary(CommonDictInterface*)));
460 connect(dictManagerWidget,
461 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
462 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
465 void MainWindow::connectMenu() {
466 connect(this, SIGNAL(setBusy()),
467 this, SLOT(disableMenu()));
469 connect(this, SIGNAL(setIdle()),
470 this, SLOT(enableMenu()));
474 void MainWindow::connectBookmarksWidget() {
476 //after removing bookmarks we search for it once again to clear word list
477 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
478 backbone, SLOT(removeAllBookmark()));
480 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
481 backbone, SLOT(fetchBookmarks()));
484 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
485 menuWidget, SLOT(hideMenu()));
487 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
488 backbone, SLOT(fetchBookmarks()));
492 connect(bookmarksShowAllAction, SIGNAL(triggered()),
493 backbone, SLOT(fetchBookmarks()));
495 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
496 backbone, SLOT(removeAllBookmark()));
498 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
499 backbone, SLOT(fetchBookmarks()));