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);
52 searchBarWidget->setFocus();
55 MainWindow::~MainWindow() {
60 void MainWindow::initializeUI() {
63 setAttribute(Qt::WA_Maemo5StackedWindow);
67 /*translationWidget is another stacked window, so we don't add it to
68 layout, only create it with this widget as parent
69 it must be created as first object in main window, otherwise sometimes
70 when app starts in maemo, when trying to set stacked window attribute
72 translationWidget = new TranslationWidget(this);
77 setWindowIcon(QIcon(":/icons/64x64/mdictionary.png"));
78 setWindowTitle("mDictionary");
80 mainLayout = new QVBoxLayout();
81 QWidget* w = new QWidget();
82 w->setLayout(mainLayout);
85 menuBar = new QMenuBar();
88 notifyManager = new NotifyManager(this);
90 initializeSearchWidgets();
95 void MainWindow::initializeSearchWidgets() {
96 searchBarWidget = new SearchBarWidget();
98 wordListWidget = new WordListWidget();
100 welcomeScreenWidget = new WelcomeScreenWidget();
103 //At start we set widget as welcome screen widget
104 mainLayout->addWidget(welcomeScreenWidget);
105 mainLayout->addWidget(searchBarWidget, 0, Qt::AlignBottom);
107 translationWidget->hide();
108 //we add word list and welcome screen to splitter
109 splitter = new QSplitter(Qt::Horizontal);
110 splitter->addWidget(wordListWidget);
111 splitter->addWidget(welcomeScreenWidget);
112 splitter->setStretchFactor(1, 150);
114 mainLayout->addWidget(splitter);
115 mainLayout->addWidget(searchBarWidget);
119 void MainWindow::initializeMenu() {
120 initializeMenuWidgets();
123 menuWidget = new MenuWidget(this);
125 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
126 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
127 menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
128 menuWidget->addSubMenu(tr("About"), aboutWidget);
130 menuBar->addAction(menuWidget);
132 connect(menuWidget, SIGNAL(setApplicationMenu(QWidget*)),
133 notifyManager, SLOT(setMenu(QWidget*)));
135 dictionariesAction = menuBar->addAction(tr("Dictionaries"));
136 connect(dictionariesAction, SIGNAL(triggered()),
137 dictManagerWidget, SLOT(show()));
139 settingsAction = menuBar->addAction(tr("Settings"));
140 connect(settingsAction, SIGNAL(triggered()),
141 settingsWidget, SLOT(show()));
143 QMenu* m = menuBar->addMenu(tr("Bookmarks"));
144 bookmarksShowAllAction = new QAction(tr("Show all"), m);
146 bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
148 m->addAction(bookmarksShowAllAction);
149 m->addAction(bookmarksRemoveAllAction);
151 aboutAction = menuBar->addAction(tr("About"));
152 connect(aboutAction, SIGNAL(triggered()),
153 aboutWidget, SLOT(show()));
158 void MainWindow::initializeMenuWidgets() {
159 dictManagerWidget = new DictManagerWidget(this);
160 dictManagerWidget->hide();
162 settingsWidget = new SettingsWidget(this);
163 settingsWidget->hide();
165 connect(settingsWidget, SIGNAL(notify(Notify::NotifyType,QString)),
166 this, SLOT(showNotification(Notify::NotifyType,QString)));
168 bookmarksWidget = new BookmarksWidget(this);
169 bookmarksWidget->hide();
171 aboutWidget = new AboutWidget(this);
175 void MainWindow::closeEvent(QCloseEvent *event) {
176 //request to stop all searches and close app
181 bool MainWindow::isInExactSearch() {
185 void MainWindow::setExactSearch(bool exact) {
186 _exactSearch = exact;
189 void MainWindow::setExactSearchString(QString word) {
193 void MainWindow::wordListReady() {
194 //gets results from backbone
195 QMultiHash<QString, Translation*> backboneResult = backbone->result();
196 QHash<QString, QList<Translation*> > searchResult;
202 //if nothing was found
203 if(backboneResult.count() == 0) {
204 showNotification(Notify::Info, tr("Can't find any matching words"));
206 //show empty list to remove results of old search
207 Q_EMIT showWordList(searchResult);
210 //find translations of the same key word
211 QMultiHash<QString, Translation*>::iterator i;
212 for(i = backboneResult.begin(); i != backboneResult.end(); i++) {
213 searchResult[i.key()].push_back(i.value());
216 //show search results
217 Q_EMIT showWordList(searchResult);
220 if(isInExactSearch()) {
221 QList<Translation*> exactTranslation;
222 if(checkExactSearch(searchResult, exactTranslation)) {
223 Q_EMIT searchTranslations(exactTranslation);
226 showNotification(Notify::Info,
227 tr("Can't find exactly matching word"));
230 setExactSearch(false);
235 bool MainWindow::checkExactSearch(
236 QHash<QString, QList<Translation *> > searchResult,
237 QList<Translation *> &found) {
239 bool foundExactMatch = false;
240 QHash<QString, QList<Translation*> >::iterator j;
241 for(j = searchResult.begin(); j != searchResult.end(); j++) {
242 if(j.key().toLower() == searchString.toLower()
243 && !foundExactMatch) {
251 void MainWindow::translationsReady() {
256 Q_EMIT showTranslation(backbone->htmls());
257 notifyManager->screenChanged();
261 void MainWindow::hideWelcomeScreen() {
263 //switch welcome screen with word list
264 if(!wordListWidget->isVisible()) {
265 mainLayout->removeWidget(welcomeScreenWidget);
266 welcomeScreenWidget->deleteLater();
268 mainLayout->insertWidget(0, wordListWidget);
271 //switch welcome screen with translation widget
272 if(!translationWidget->isVisible()) {
273 splitter->insertWidget(1,translationWidget);
274 splitter->setStretchFactor(1, 150);
275 welcomeScreenWidget->deleteLater();
280 QList<CommonDictInterface*> MainWindow::getPlugins() {
281 return backbone->getPlugins();
284 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
285 return backbone->getDictionaries();
289 void MainWindow::search(QString word) {
290 setExactSearch(false);
291 searchBarWidget->search(word);
294 void MainWindow::searchExact(QString word) {
295 setExactSearch(true);
296 searchBarWidget->search(word);
299 void MainWindow::searchDelay(QString word) {
300 searchBarWidget->searchDelay(word);
307 void MainWindow::searchingInterrupted() {
308 //make sure to unset exact search mode
309 setExactSearch(false);
312 void MainWindow::addToHistory(QList<Translation *> trans) {
313 if(trans.count() > 0) {
314 backbone->history()->add(trans[0]->key());
318 void MainWindow::historyNext() {
319 if(backbone->history()->nextAvailable()) {
320 QString next = backbone->history()->next();
322 setExactSearch(true);
328 void MainWindow::historyPrev() {
329 if(backbone->history()->prevAvailable()) {
331 setExactSearch(true);
333 QString prev = backbone->history()->previous();
338 void MainWindow::disableMenu() {
340 if(menuBar->actions().contains(menuWidget)) {
341 menuBar->removeAction(menuWidget);
344 menuBar->setEnabled(false);
348 void MainWindow::enableMenu() {
350 if(!menuBar->actions().contains(menuWidget)) {
351 menuBar->addAction(menuWidget);
354 menuBar->setEnabled(true);
358 void MainWindow::showHistory(QPoint p) {
360 HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
363 QPoint newPos = mapFromGlobal(p);
364 newPos.setY(searchBarWidget->pos().y() -
365 historyDialog.sizeHint().height());
366 newPos.setX(width() - historyDialog.sizeHint().width());
368 historyDialog.move(newPos);
371 if(historyDialog.exec() == QDialog::Accepted) {
372 backbone->history()->setCurrentElement(historyDialog.selectedRow());
373 searchExact(historyDialog.selectedWord());
377 void MainWindow::setSettings(Settings *s) {
378 backbone->setSettings(s);
381 Settings* MainWindow::settings() {
382 return backbone->settings();
386 void MainWindow::showNotification(Notify::NotifyType type, QString text) {
387 notifyManager->showNotification(type, text);
390 void MainWindow::connectBackbone() {
392 connect(this, SIGNAL(searchWordList(QString)),
393 this, SIGNAL(setBusy()));
395 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
396 this, SIGNAL(setBusy()));
398 connect(this, SIGNAL(stopSearching()),
399 this, SIGNAL(setIdle()));
401 connect(this, SIGNAL(searchWordList(QString)),
402 this, SLOT(setExactSearchString(QString)));
404 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
405 this, SLOT(addToHistory(QList<Translation*>)));
409 connect(this, SIGNAL(quit()),
410 backbone, SLOT(quit()));
412 connect(this, SIGNAL(searchWordList(QString)),
413 backbone, SLOT(search(QString)));
415 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
416 backbone, SLOT(searchHtml(QList<Translation*>)));
418 connect(this, SIGNAL(stopSearching()),
419 backbone, SLOT(stopSearching()));
421 connect(this, SIGNAL(stopSearching()),
422 this, SLOT(searchingInterrupted()));
424 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
425 backbone, SLOT(addDictionary(CommonDictInterface*)));
427 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
428 backbone, SLOT(removeDictionary(CommonDictInterface*)));
430 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
431 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
434 connect(backbone, SIGNAL(ready()),
435 this, SIGNAL(setIdle()));
437 connect(backbone, SIGNAL(htmlReady()),
438 this, SIGNAL(setIdle()));
441 connect(backbone, SIGNAL(ready()),
442 this, SLOT(wordListReady()));
444 connect(backbone, SIGNAL(htmlReady()),
445 this, SLOT(translationsReady()));
447 connect(backbone, SIGNAL(searchCanceled()),
448 this, SIGNAL(setIdle()));
450 connect(backbone, SIGNAL(notify(Notify::NotifyType,QString)),
451 this, SLOT(showNotification(Notify::NotifyType,QString)));
454 void MainWindow::connectSearchBar() {
455 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
456 this, SIGNAL(searchWordList(QString)));
458 connect(searchBarWidget, SIGNAL(stopSearching()),
459 this, SIGNAL(stopSearching()));
461 connect(this, SIGNAL(setBusy()),
462 searchBarWidget, SLOT(setBusy()));
464 connect(this, SIGNAL(setIdle()),
465 searchBarWidget, SLOT(setIdle()));
467 connect(searchBarWidget, SIGNAL(historyNext()),
468 this, SLOT(historyNext()));
470 connect(searchBarWidget, SIGNAL(historyPrev()),
471 this, SLOT(historyPrev()));
473 connect(searchBarWidget, SIGNAL(historyShow(QPoint)),
474 this, SLOT(showHistory(QPoint)));
476 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
477 backbone->history(), SLOT(refreshStatus()));
479 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
480 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
483 void MainWindow::connectWordList() {
485 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
487 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
489 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
490 this, SIGNAL(searchTranslations(QList<Translation*>)));
495 connect(this, SIGNAL(setBusy()),
496 wordListWidget, SLOT(lockList()));
498 connect(this, SIGNAL(setIdle()),
499 wordListWidget, SLOT(unlockList()));
501 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
502 backbone, SLOT(addBookmark(QList<Translation*>)));
504 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
505 backbone, SLOT(removeBookmark(QList<Translation*>)));
508 void MainWindow::connectTranslationWidget() {
509 connect(this, SIGNAL(showTranslation(QStringList)),
510 translationWidget, SLOT(show(QStringList)));
513 connect(translationWidget, SIGNAL(search(QString)),
514 this, SLOT(search(QString)));
516 connect(translationWidget, SIGNAL(search(QString)),
517 this, SLOT(searchExact(QString)));
523 void MainWindow::connectDictManager() {
524 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
525 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
527 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
528 this, SIGNAL(removeDictionary(CommonDictInterface*)));
530 connect(dictManagerWidget,
531 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
532 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
535 void MainWindow::connectMenu() {
536 connect(this, SIGNAL(setBusy()),
537 this, SLOT(disableMenu()));
539 connect(this, SIGNAL(setIdle()),
540 this, SLOT(enableMenu()));
544 void MainWindow::connectBookmarksWidget() {
546 //after removing bookmarks we search for them once again to clear word list
547 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
548 this, SLOT(removeBookmarks()));
551 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
552 menuWidget, SLOT(hideMenu()));
554 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
555 backbone, SLOT(fetchBookmarks()));
559 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
560 this, SLOT(removeBookmarks()));
561 connect(bookmarksShowAllAction, SIGNAL(triggered()),
562 backbone, SLOT(fetchBookmarks()));
568 void MainWindow::removeBookmarks() {
571 par = bookmarksWidget;
575 if(QMessageBox::question(par, tr("Delete all bookmarks"),
576 tr("Do you want to delete all bookmarks? (This action cannot be revoked)"),
577 QMessageBox::Yes, QMessageBox::Cancel) == QMessageBox::Yes) {
578 backbone->removeAllBookmarks();
579 if(searchString.size())
580 backbone->search(searchString);