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 //! \file MainWindow.cpp
23 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
25 #include "MainWindow.h"
28 #include <QMaemo5InformationBox>
32 MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
33 GUIInterface(parent) {
35 this->backbone = backbone;
43 connectTranslationWidget();
46 connectBookmarksWidget();
48 setExactSearch(false);
53 MainWindow::~MainWindow() {
58 void MainWindow::initializeUI() {
61 setAttribute(Qt::WA_Maemo5StackedWindow);
65 setWindowIcon(QIcon(":/icons/64x64/mdictionary.png"));
66 setWindowTitle("mDictionary");
68 /*translationWidget is another stacked window, so we don't add it to
69 layout, only create it with this widget as parent
70 it must be created as first object in main window, otherwise sometimes
71 when app starts in maemo, when trying to set stacked window attribute
73 translationWidget = new TranslationWidget(this);
75 mainLayout = new QVBoxLayout();
76 QWidget* w = new QWidget();
77 w->setLayout(mainLayout);
80 menuBar = new QMenuBar();
83 initializeSearchWidgets();
87 notifyManager = new NotifyManager(this);
90 void MainWindow::initializeSearchWidgets() {
91 searchBarWidget = new SearchBarWidget();
93 wordListWidget = new WordListWidget();
95 welcomeScreenWidget = new WelcomeScreenWidget();
98 //At start we set widget as welcome screen widget
99 mainLayout->addWidget(welcomeScreenWidget);
100 mainLayout->addWidget(searchBarWidget, 0, Qt::AlignBottom);
102 translationWidget->hide();
103 //we add word list and welcome screen to splitter
104 splitter = new QSplitter(Qt::Horizontal);
105 splitter->addWidget(wordListWidget);
106 splitter->addWidget(welcomeScreenWidget);
107 splitter->setStretchFactor(1, 150);
109 mainLayout->addWidget(splitter);
110 mainLayout->addWidget(searchBarWidget);
114 void MainWindow::initializeMenu() {
115 initializeMenuWidgets();
118 menuWidget = new MenuWidget(this);
120 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
121 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
122 menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
123 menuWidget->addSubMenu(tr("About"), aboutWidget);
125 menuBar->addAction(menuWidget);
127 dictManagerWidget->setMenuWidget(menuWidget);
129 dictionariesAction = menuBar->addAction(tr("Dictionaries"));
130 connect(dictionariesAction, SIGNAL(triggered()),
131 dictManagerWidget, SLOT(show()));
133 settingsAction = menuBar->addAction(tr("Settings"));
134 connect(settingsAction, SIGNAL(triggered()),
135 settingsWidget, SLOT(show()));
137 QMenu* m = menuBar->addMenu(tr("Bookmarks"));
138 bookmarksShowAllAction = new QAction(tr("Show all"), m);
140 bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
142 m->addAction(bookmarksShowAllAction);
143 m->addAction(bookmarksRemoveAllAction);
145 aboutAction = menuBar->addAction(tr("About"));
146 connect(aboutAction, SIGNAL(triggered()),
147 aboutWidget, SLOT(show()));
152 void MainWindow::initializeMenuWidgets() {
153 dictManagerWidget = new DictManagerWidget(this);
154 dictManagerWidget->hide();
156 settingsWidget = new SettingsWidget(this);
157 settingsWidget->hide();
159 bookmarksWidget = new BookmarksWidget(this);
160 bookmarksWidget->hide();
162 aboutWidget = new AboutWidget(this);
166 void MainWindow::closeEvent(QCloseEvent *event) {
167 //request to stop all searches and close app
172 bool MainWindow::isInExactSearch() {
176 void MainWindow::setExactSearch(bool exact) {
177 _exactSearch = exact;
180 void MainWindow::setExactSearchString(QString word) {
184 void MainWindow::wordListReady() {
185 //gets results from backbone
186 QMultiHash<QString, Translation*> backboneResult = backbone->result();
187 QHash<QString, QList<Translation*> > searchResult;
193 //if nothing was found
194 if(backboneResult.count() == 0) {
195 showNotification(Notify::Info, tr("Can't find any matching words"));
197 //show empty list to remove results of old search
198 Q_EMIT showWordList(searchResult);
201 //find translations of the same key word
202 QMultiHash<QString, Translation*>::iterator i;
203 for(i = backboneResult.begin(); i != backboneResult.end(); i++) {
204 searchResult[i.key()].push_back(i.value());
207 //show search results
208 Q_EMIT showWordList(searchResult);
211 if(isInExactSearch()) {
212 QList<Translation*> exactTranslation;
213 if(checkExactSearch(searchResult, exactTranslation)) {
214 Q_EMIT searchTranslations(exactTranslation);
217 showNotification(Notify::Info,
218 tr("Can't find exactly matching word"));
221 setExactSearch(false);
226 bool MainWindow::checkExactSearch(
227 QHash<QString, QList<Translation *> > searchResult,
228 QList<Translation *> &found) {
230 bool foundExactMatch = false;
231 QHash<QString, QList<Translation*> >::iterator j;
232 for(j = searchResult.begin(); j != searchResult.end(); j++) {
233 if(j.key().toLower() == searchString.toLower()
234 && !foundExactMatch) {
242 void MainWindow::translationsReady() {
247 Q_EMIT showTranslation(backbone->htmls());
248 notifyManager->screenChanged();
252 void MainWindow::hideWelcomeScreen() {
254 //switch welcome screen with word list
255 if(!wordListWidget->isVisible()) {
256 mainLayout->removeWidget(welcomeScreenWidget);
257 welcomeScreenWidget->deleteLater();
259 mainLayout->insertWidget(0, wordListWidget);
262 //switch welcome screen with translation widget
263 if(!translationWidget->isVisible()) {
264 splitter->insertWidget(1,translationWidget);
265 splitter->setStretchFactor(1, 150);
266 welcomeScreenWidget->deleteLater();
271 QList<CommonDictInterface*> MainWindow::getPlugins() {
272 return backbone->getPlugins();
275 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
276 return backbone->getDictionaries();
280 void MainWindow::search(QString word) {
281 setExactSearch(false);
282 searchBarWidget->search(word);
285 void MainWindow::searchExact(QString word) {
286 setExactSearch(true);
287 searchBarWidget->search(word);
290 void MainWindow::searchDelay(QString word) {
291 searchBarWidget->searchDelay(word);
298 void MainWindow::searchingInterrupted() {
299 //make sure to unset exact search mode
300 setExactSearch(false);
303 void MainWindow::addToHistory(QList<Translation *> trans) {
304 if(trans.count() > 0) {
305 backbone->history()->add(trans[0]->key());
309 void MainWindow::historyNext() {
310 if(backbone->history()->nextAvailable()) {
311 QString next = backbone->history()->next();
313 setExactSearch(true);
319 void MainWindow::historyPrev() {
320 if(backbone->history()->prevAvailable()) {
322 setExactSearch(true);
324 QString prev = backbone->history()->previous();
329 void MainWindow::disableMenu() {
331 if(menuBar->actions().contains(menuWidget)) {
332 menuBar->removeAction(menuWidget);
335 menuBar->setEnabled(false);
339 void MainWindow::enableMenu() {
341 if(!menuBar->actions().contains(menuWidget)) {
342 menuBar->addAction(menuWidget);
345 menuBar->setEnabled(true);
349 void MainWindow::showHistory(QPoint p) {
351 HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
354 QPoint newPos = mapFromGlobal(p);
355 newPos.setY(searchBarWidget->pos().y() -
356 historyDialog.sizeHint().height());
357 newPos.setX(width() - historyDialog.sizeHint().width());
359 historyDialog.move(newPos);
362 if(historyDialog.exec() == QDialog::Accepted) {
363 backbone->history()->setCurrentElement(historyDialog.selectedRow());
364 searchExact(historyDialog.selectedWord());
368 void MainWindow::setSettings(Settings *s) {
369 backbone->setSettings(s);
372 Settings* MainWindow::settings() {
373 return backbone->settings();
377 void MainWindow::showNotification(Notify::NotifyType type, QString text, QWidget* parent) {
378 notifyManager->showNotification(type, text, parent);
381 void MainWindow::connectBackbone() {
383 connect(this, SIGNAL(searchWordList(QString)),
384 this, SIGNAL(setBusy()));
386 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
387 this, SIGNAL(setBusy()));
389 connect(this, SIGNAL(stopSearching()),
390 this, SIGNAL(setIdle()));
392 connect(this, SIGNAL(searchWordList(QString)),
393 this, SLOT(setExactSearchString(QString)));
395 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
396 this, SLOT(addToHistory(QList<Translation*>)));
400 connect(this, SIGNAL(quit()),
401 backbone, SLOT(quit()));
403 connect(this, SIGNAL(searchWordList(QString)),
404 backbone, SLOT(search(QString)));
406 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
407 backbone, SLOT(searchHtml(QList<Translation*>)));
409 connect(this, SIGNAL(stopSearching()),
410 backbone, SLOT(stopSearching()));
412 connect(this, SIGNAL(stopSearching()),
413 this, SLOT(searchingInterrupted()));
415 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
416 backbone, SLOT(addDictionary(CommonDictInterface*)));
418 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
419 backbone, SLOT(removeDictionary(CommonDictInterface*)));
421 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
422 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
425 connect(backbone, SIGNAL(ready()),
426 this, SIGNAL(setIdle()));
428 connect(backbone, SIGNAL(htmlReady()),
429 this, SIGNAL(setIdle()));
432 connect(backbone, SIGNAL(ready()),
433 this, SLOT(wordListReady()));
435 connect(backbone, SIGNAL(htmlReady()),
436 this, SLOT(translationsReady()));
438 connect(backbone, SIGNAL(searchCanceled()),
439 this, SIGNAL(setIdle()));
441 connect(backbone, SIGNAL(notify(Notify::NotifyType,QString, QWidget*)),
442 this, SLOT(showNotification(Notify::NotifyType,QString, QWidget*)));
445 void MainWindow::connectSearchBar() {
446 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
447 this, SIGNAL(searchWordList(QString)));
449 connect(searchBarWidget, SIGNAL(stopSearching()),
450 this, SIGNAL(stopSearching()));
452 connect(this, SIGNAL(setBusy()),
453 searchBarWidget, SLOT(setBusy()));
455 connect(this, SIGNAL(setIdle()),
456 searchBarWidget, SLOT(setIdle()));
458 connect(searchBarWidget, SIGNAL(historyNext()),
459 this, SLOT(historyNext()));
461 connect(searchBarWidget, SIGNAL(historyPrev()),
462 this, SLOT(historyPrev()));
464 connect(searchBarWidget, SIGNAL(historyShow(QPoint)),
465 this, SLOT(showHistory(QPoint)));
467 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
468 backbone->history(), SLOT(refreshStatus()));
470 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
471 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
474 void MainWindow::connectWordList() {
476 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
478 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
480 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
481 this, SIGNAL(searchTranslations(QList<Translation*>)));
486 connect(this, SIGNAL(setBusy()),
487 wordListWidget, SLOT(lockList()));
489 connect(this, SIGNAL(setIdle()),
490 wordListWidget, SLOT(unlockList()));
492 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
493 backbone, SLOT(addBookmark(QList<Translation*>)));
495 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
496 backbone, SLOT(removeBookmark(QList<Translation*>)));
499 void MainWindow::connectTranslationWidget() {
500 connect(this, SIGNAL(showTranslation(QStringList)),
501 translationWidget, SLOT(show(QStringList)));
504 connect(translationWidget, SIGNAL(search(QString)),
505 this, SLOT(search(QString)));
507 connect(translationWidget, SIGNAL(search(QString)),
508 this, SLOT(searchExact(QString)));
514 void MainWindow::connectDictManager() {
515 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
516 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
518 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
519 this, SIGNAL(removeDictionary(CommonDictInterface*)));
521 connect(dictManagerWidget,
522 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
523 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
526 void MainWindow::connectMenu() {
527 connect(this, SIGNAL(setBusy()),
528 this, SLOT(disableMenu()));
530 connect(this, SIGNAL(setIdle()),
531 this, SLOT(enableMenu()));
535 void MainWindow::connectBookmarksWidget() {
537 //after removing bookmarks we search for them once again to clear word list
538 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
539 this, SLOT(removeBookmarks()));
542 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
543 menuWidget, SLOT(hideMenu()));
545 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
546 backbone, SLOT(fetchBookmarks()));
550 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
551 this, SLOT(removeBookmarks()));
552 connect(bookmarksShowAllAction, SIGNAL(triggered()),
553 backbone, SLOT(fetchBookmarks()));
559 void MainWindow::removeBookmarks() {
562 par = bookmarksWidget;
566 if(QMessageBox::question(par, tr("Delete all bookmarks"),
567 tr("Do you want to delete all bookmarks? (This action cannot be revoked)"),
568 QMessageBox::Yes, QMessageBox::Cancel) == QMessageBox::Yes) {
569 backbone->removeAllBookmarks();
570 if(searchString.size())
571 backbone->search(searchString);