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());
258 notifyManager->screenChanged();
263 void MainWindow::hideWelcomeScreen() {
265 //switch welcome screen with word list
266 if(!wordListWidget->isVisible()) {
267 mainLayout->removeWidget(welcomeScreenWidget);
268 welcomeScreenWidget->deleteLater();
270 mainLayout->insertWidget(0, wordListWidget);
273 //switch welcome screen with translation widget
274 if(!translationWidget->isVisible()) {
275 splitter->insertWidget(1,translationWidget);
276 splitter->setStretchFactor(1, 150);
277 welcomeScreenWidget->deleteLater();
282 QList<CommonDictInterface*> MainWindow::getPlugins() {
283 return backbone->getPlugins();
286 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
287 return backbone->getDictionaries();
291 void MainWindow::search(QString word) {
292 setExactSearch(false);
293 searchBarWidget->search(word);
296 void MainWindow::searchExact(QString word) {
297 setExactSearch(true);
298 searchBarWidget->search(word);
301 void MainWindow::searchDelay(QString word) {
302 searchBarWidget->searchDelay(word);
309 void MainWindow::searchingInterrupted() {
310 //make sure to unset exact search mode
311 setExactSearch(false);
314 void MainWindow::addToHistory(QList<Translation *> trans) {
315 if(trans.count() > 0) {
316 backbone->history()->add(trans[0]->key());
320 void MainWindow::historyNext() {
321 if(backbone->history()->nextAvailable()) {
322 QString next = backbone->history()->next();
324 setExactSearch(true);
330 void MainWindow::historyPrev() {
331 if(backbone->history()->prevAvailable()) {
333 setExactSearch(true);
335 QString prev = backbone->history()->previous();
340 void MainWindow::disableMenu() {
342 if(menuBar->actions().contains(menuWidget)) {
343 menuBar->removeAction(menuWidget);
346 menuBar->setEnabled(false);
350 void MainWindow::enableMenu() {
352 if(!menuBar->actions().contains(menuWidget)) {
353 menuBar->addAction(menuWidget);
356 menuBar->setEnabled(true);
360 void MainWindow::showHistory(QPoint p) {
362 HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
365 QPoint newPos = mapFromGlobal(p);
366 newPos.setY(searchBarWidget->pos().y() -
367 historyDialog.sizeHint().height());
368 newPos.setX(width() - historyDialog.sizeHint().width());
370 historyDialog.move(newPos);
373 if(historyDialog.exec() == QDialog::Accepted) {
374 backbone->history()->setCurrentElement(historyDialog.selectedRow());
375 searchExact(historyDialog.selectedWord());
379 void MainWindow::setSettings(Settings *s) {
380 backbone->setSettings(s);
383 Settings* MainWindow::settings() {
384 return backbone->settings();
388 void MainWindow::showNotification(Notify::NotifyType type, QString text) {
389 notifyManager->showNotification(type, text);
392 void MainWindow::connectBackbone() {
394 connect(this, SIGNAL(searchWordList(QString)),
395 this, SIGNAL(setBusy()));
397 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
398 this, SIGNAL(setBusy()));
400 connect(this, SIGNAL(stopSearching()),
401 this, SIGNAL(setIdle()));
403 connect(this, SIGNAL(searchWordList(QString)),
404 this, SLOT(setExactSearchString(QString)));
406 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
407 this, SLOT(addToHistory(QList<Translation*>)));
411 connect(this, SIGNAL(quit()),
412 backbone, SLOT(quit()));
414 connect(this, SIGNAL(searchWordList(QString)),
415 backbone, SLOT(search(QString)));
417 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
418 backbone, SLOT(searchHtml(QList<Translation*>)));
420 connect(this, SIGNAL(stopSearching()),
421 backbone, SLOT(stopSearching()));
423 connect(this, SIGNAL(stopSearching()),
424 this, SLOT(searchingInterrupted()));
426 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
427 backbone, SLOT(addDictionary(CommonDictInterface*)));
429 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
430 backbone, SLOT(removeDictionary(CommonDictInterface*)));
432 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
433 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
436 connect(backbone, SIGNAL(ready()),
437 this, SIGNAL(setIdle()));
439 connect(backbone, SIGNAL(htmlReady()),
440 this, SIGNAL(setIdle()));
443 connect(backbone, SIGNAL(ready()),
444 this, SLOT(wordListReady()));
446 connect(backbone, SIGNAL(htmlReady()),
447 this, SLOT(translationsReady()));
449 connect(backbone, SIGNAL(searchCanceled()),
450 this, SIGNAL(setIdle()));
452 connect(backbone, SIGNAL(notify(Notify::NotifyType,QString)),
453 this, SLOT(showNotification(Notify::NotifyType,QString)));
456 void MainWindow::connectSearchBar() {
457 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
458 this, SIGNAL(searchWordList(QString)));
460 connect(searchBarWidget, SIGNAL(stopSearching()),
461 this, SIGNAL(stopSearching()));
463 connect(this, SIGNAL(setBusy()),
464 searchBarWidget, SLOT(setBusy()));
466 connect(this, SIGNAL(setIdle()),
467 searchBarWidget, SLOT(setIdle()));
469 connect(searchBarWidget, SIGNAL(historyNext()),
470 this, SLOT(historyNext()));
472 connect(searchBarWidget, SIGNAL(historyPrev()),
473 this, SLOT(historyPrev()));
475 connect(searchBarWidget, SIGNAL(historyShow(QPoint)),
476 this, SLOT(showHistory(QPoint)));
478 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
479 backbone->history(), SLOT(refreshStatus()));
481 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
482 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
485 void MainWindow::connectWordList() {
487 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
489 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
491 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
492 this, SIGNAL(searchTranslations(QList<Translation*>)));
497 connect(this, SIGNAL(setBusy()),
498 wordListWidget, SLOT(lockList()));
500 connect(this, SIGNAL(setIdle()),
501 wordListWidget, SLOT(unlockList()));
503 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
504 backbone, SLOT(addBookmark(QList<Translation*>)));
506 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
507 backbone, SLOT(removeBookmark(QList<Translation*>)));
510 void MainWindow::connectTranslationWidget() {
511 connect(this, SIGNAL(showTranslation(QStringList)),
512 translationWidget, SLOT(show(QStringList)));
515 connect(translationWidget, SIGNAL(search(QString)),
516 this, SLOT(search(QString)));
518 connect(translationWidget, SIGNAL(search(QString)),
519 this, SLOT(searchExact(QString)));
525 void MainWindow::connectDictManager() {
526 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
527 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
529 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
530 this, SIGNAL(removeDictionary(CommonDictInterface*)));
532 connect(dictManagerWidget,
533 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
534 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
537 void MainWindow::connectMenu() {
538 connect(this, SIGNAL(setBusy()),
539 this, SLOT(disableMenu()));
541 connect(this, SIGNAL(setIdle()),
542 this, SLOT(enableMenu()));
546 void MainWindow::connectBookmarksWidget() {
548 //after removing bookmarks we search for them once again to clear the words list
549 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
550 this, SLOT(removeBookmarks()));
553 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
554 menuWidget, SLOT(hideMenu()));
556 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
557 backbone, SLOT(fetchBookmarks()));
561 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
562 this, SLOT(removeBookmarks()));
563 connect(bookmarksShowAllAction, SIGNAL(triggered()),
564 backbone, SLOT(fetchBookmarks()));
570 void MainWindow::removeBookmarks() {
573 par = bookmarksWidget;
577 if(QMessageBox::question(par, tr("Delete all bookmarks"),
578 tr("Do you want to delete all bookmarks? (This action cannot be revoked)"),
579 QMessageBox::Yes, QMessageBox::Cancel) == QMessageBox::Yes) {
580 backbone->removeAllBookmarks();