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 /*translationWidget is another stacked window, so we don't add it to
66 layout, only create it with this widget as parent
67 it must be created as first object in main window, otherwise sometimes
68 when app starts in maemo, when trying to set stacked window attribute
70 translationWidget = new TranslationWidget(this);
75 setWindowIcon(QIcon(":/icons/64x64/mdictionary.png"));
76 setWindowTitle("mDictionary");
78 mainLayout = new QVBoxLayout();
79 QWidget* w = new QWidget();
80 w->setLayout(mainLayout);
83 menuBar = new QMenuBar();
86 notifyManager = new NotifyManager(this);
88 initializeSearchWidgets();
93 void MainWindow::initializeSearchWidgets() {
94 searchBarWidget = new SearchBarWidget();
96 wordListWidget = new WordListWidget();
98 welcomeScreenWidget = new WelcomeScreenWidget();
101 //At start we set widget as welcome screen widget
102 mainLayout->addWidget(welcomeScreenWidget);
103 mainLayout->addWidget(searchBarWidget, 0, Qt::AlignBottom);
105 translationWidget->hide();
106 //we add word list and welcome screen to splitter
107 splitter = new QSplitter(Qt::Horizontal);
108 splitter->addWidget(wordListWidget);
109 splitter->addWidget(welcomeScreenWidget);
110 splitter->setStretchFactor(1, 150);
112 mainLayout->addWidget(splitter);
113 mainLayout->addWidget(searchBarWidget);
117 void MainWindow::initializeMenu() {
118 initializeMenuWidgets();
121 menuWidget = new MenuWidget(this);
123 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
124 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
125 menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
126 menuWidget->addSubMenu(tr("About"), aboutWidget);
128 menuBar->addAction(menuWidget);
130 connect(menuWidget, SIGNAL(setApplicationMenu(QWidget*)),
131 notifyManager, SLOT(setMenu(QWidget*)));
133 dictionariesAction = menuBar->addAction(tr("Dictionaries"));
134 connect(dictionariesAction, SIGNAL(triggered()),
135 dictManagerWidget, SLOT(show()));
137 settingsAction = menuBar->addAction(tr("Settings"));
138 connect(settingsAction, SIGNAL(triggered()),
139 settingsWidget, SLOT(show()));
141 QMenu* m = menuBar->addMenu(tr("Bookmarks"));
142 bookmarksShowAllAction = new QAction(tr("Show all"), m);
144 bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
146 m->addAction(bookmarksShowAllAction);
147 m->addAction(bookmarksRemoveAllAction);
149 aboutAction = menuBar->addAction(tr("About"));
150 connect(aboutAction, SIGNAL(triggered()),
151 aboutWidget, SLOT(show()));
156 void MainWindow::initializeMenuWidgets() {
157 dictManagerWidget = new DictManagerWidget(this);
158 dictManagerWidget->hide();
160 settingsWidget = new SettingsWidget(this);
161 settingsWidget->hide();
163 bookmarksWidget = new BookmarksWidget(this);
164 bookmarksWidget->hide();
166 aboutWidget = new AboutWidget(this);
170 void MainWindow::closeEvent(QCloseEvent *event) {
171 //request to stop all searches and close app
176 bool MainWindow::isInExactSearch() {
180 void MainWindow::setExactSearch(bool exact) {
181 _exactSearch = exact;
184 void MainWindow::setExactSearchString(QString word) {
188 void MainWindow::wordListReady() {
189 //gets results from backbone
190 QMultiHash<QString, Translation*> backboneResult = backbone->result();
191 QHash<QString, QList<Translation*> > searchResult;
197 //if nothing was found
198 if(backboneResult.count() == 0) {
199 showNotification(Notify::Info, tr("Can't find any matching words"));
201 //show empty list to remove results of old search
202 Q_EMIT showWordList(searchResult);
205 //find translations of the same key word
206 QMultiHash<QString, Translation*>::iterator i;
207 for(i = backboneResult.begin(); i != backboneResult.end(); i++) {
208 searchResult[i.key()].push_back(i.value());
211 //show search results
212 Q_EMIT showWordList(searchResult);
215 if(isInExactSearch()) {
216 QList<Translation*> exactTranslation;
217 if(checkExactSearch(searchResult, exactTranslation)) {
218 Q_EMIT searchTranslations(exactTranslation);
221 showNotification(Notify::Info,
222 tr("Can't find exactly matching word"));
225 setExactSearch(false);
230 bool MainWindow::checkExactSearch(
231 QHash<QString, QList<Translation *> > searchResult,
232 QList<Translation *> &found) {
234 bool foundExactMatch = false;
235 QHash<QString, QList<Translation*> >::iterator j;
236 for(j = searchResult.begin(); j != searchResult.end(); j++) {
237 if(j.key().toLower() == searchString.toLower()
238 && !foundExactMatch) {
246 void MainWindow::translationsReady() {
251 Q_EMIT showTranslation(backbone->htmls());
252 notifyManager->screenChanged();
256 void MainWindow::hideWelcomeScreen() {
258 //switch welcome screen with word list
259 if(!wordListWidget->isVisible()) {
260 mainLayout->removeWidget(welcomeScreenWidget);
261 welcomeScreenWidget->deleteLater();
263 mainLayout->insertWidget(0, wordListWidget);
266 //switch welcome screen with translation widget
267 if(!translationWidget->isVisible()) {
268 splitter->insertWidget(1,translationWidget);
269 splitter->setStretchFactor(1, 150);
270 welcomeScreenWidget->deleteLater();
275 QList<CommonDictInterface*> MainWindow::getPlugins() {
276 return backbone->getPlugins();
279 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
280 return backbone->getDictionaries();
284 void MainWindow::search(QString word) {
285 setExactSearch(false);
286 searchBarWidget->search(word);
289 void MainWindow::searchExact(QString word) {
290 setExactSearch(true);
291 searchBarWidget->search(word);
294 void MainWindow::searchDelay(QString word) {
295 searchBarWidget->searchDelay(word);
302 void MainWindow::searchingInterrupted() {
303 //make sure to unset exact search mode
304 setExactSearch(false);
307 void MainWindow::addToHistory(QList<Translation *> trans) {
308 if(trans.count() > 0) {
309 backbone->history()->add(trans[0]->key());
313 void MainWindow::historyNext() {
314 if(backbone->history()->nextAvailable()) {
315 QString next = backbone->history()->next();
317 setExactSearch(true);
323 void MainWindow::historyPrev() {
324 if(backbone->history()->prevAvailable()) {
326 setExactSearch(true);
328 QString prev = backbone->history()->previous();
333 void MainWindow::disableMenu() {
335 if(menuBar->actions().contains(menuWidget)) {
336 menuBar->removeAction(menuWidget);
339 menuBar->setEnabled(false);
343 void MainWindow::enableMenu() {
345 if(!menuBar->actions().contains(menuWidget)) {
346 menuBar->addAction(menuWidget);
349 menuBar->setEnabled(true);
353 void MainWindow::showHistory(QPoint p) {
355 HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
358 QPoint newPos = mapFromGlobal(p);
359 newPos.setY(searchBarWidget->pos().y() -
360 historyDialog.sizeHint().height());
361 newPos.setX(width() - historyDialog.sizeHint().width());
363 historyDialog.move(newPos);
366 if(historyDialog.exec() == QDialog::Accepted) {
367 backbone->history()->setCurrentElement(historyDialog.selectedRow());
368 searchExact(historyDialog.selectedWord());
372 void MainWindow::setSettings(Settings *s) {
373 backbone->setSettings(s);
376 Settings* MainWindow::settings() {
377 return backbone->settings();
381 void MainWindow::showNotification(Notify::NotifyType type, QString text) {
382 notifyManager->showNotification(type, text);
385 void MainWindow::connectBackbone() {
387 connect(this, SIGNAL(searchWordList(QString)),
388 this, SIGNAL(setBusy()));
390 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
391 this, SIGNAL(setBusy()));
393 connect(this, SIGNAL(stopSearching()),
394 this, SIGNAL(setIdle()));
396 connect(this, SIGNAL(searchWordList(QString)),
397 this, SLOT(setExactSearchString(QString)));
399 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
400 this, SLOT(addToHistory(QList<Translation*>)));
404 connect(this, SIGNAL(quit()),
405 backbone, SLOT(quit()));
407 connect(this, SIGNAL(searchWordList(QString)),
408 backbone, SLOT(search(QString)));
410 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
411 backbone, SLOT(searchHtml(QList<Translation*>)));
413 connect(this, SIGNAL(stopSearching()),
414 backbone, SLOT(stopSearching()));
416 connect(this, SIGNAL(stopSearching()),
417 this, SLOT(searchingInterrupted()));
419 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
420 backbone, SLOT(addDictionary(CommonDictInterface*)));
422 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
423 backbone, SLOT(removeDictionary(CommonDictInterface*)));
425 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
426 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
429 connect(backbone, SIGNAL(ready()),
430 this, SIGNAL(setIdle()));
432 connect(backbone, SIGNAL(htmlReady()),
433 this, SIGNAL(setIdle()));
436 connect(backbone, SIGNAL(ready()),
437 this, SLOT(wordListReady()));
439 connect(backbone, SIGNAL(htmlReady()),
440 this, SLOT(translationsReady()));
442 connect(backbone, SIGNAL(searchCanceled()),
443 this, SIGNAL(setIdle()));
445 connect(backbone, SIGNAL(notify(Notify::NotifyType,QString)),
446 this, SLOT(showNotification(Notify::NotifyType,QString)));
449 void MainWindow::connectSearchBar() {
450 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
451 this, SIGNAL(searchWordList(QString)));
453 connect(searchBarWidget, SIGNAL(stopSearching()),
454 this, SIGNAL(stopSearching()));
456 connect(this, SIGNAL(setBusy()),
457 searchBarWidget, SLOT(setBusy()));
459 connect(this, SIGNAL(setIdle()),
460 searchBarWidget, SLOT(setIdle()));
462 connect(searchBarWidget, SIGNAL(historyNext()),
463 this, SLOT(historyNext()));
465 connect(searchBarWidget, SIGNAL(historyPrev()),
466 this, SLOT(historyPrev()));
468 connect(searchBarWidget, SIGNAL(historyShow(QPoint)),
469 this, SLOT(showHistory(QPoint)));
471 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
472 backbone->history(), SLOT(refreshStatus()));
474 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
475 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
478 void MainWindow::connectWordList() {
480 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
482 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
484 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
485 this, SIGNAL(searchTranslations(QList<Translation*>)));
490 connect(this, SIGNAL(setBusy()),
491 wordListWidget, SLOT(lockList()));
493 connect(this, SIGNAL(setIdle()),
494 wordListWidget, SLOT(unlockList()));
496 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
497 backbone, SLOT(addBookmark(QList<Translation*>)));
499 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
500 backbone, SLOT(removeBookmark(QList<Translation*>)));
503 void MainWindow::connectTranslationWidget() {
504 connect(this, SIGNAL(showTranslation(QStringList)),
505 translationWidget, SLOT(show(QStringList)));
508 connect(translationWidget, SIGNAL(search(QString)),
509 this, SLOT(search(QString)));
511 connect(translationWidget, SIGNAL(search(QString)),
512 this, SLOT(searchExact(QString)));
518 void MainWindow::connectDictManager() {
519 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
520 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
522 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
523 this, SIGNAL(removeDictionary(CommonDictInterface*)));
525 connect(dictManagerWidget,
526 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
527 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
530 void MainWindow::connectMenu() {
531 connect(this, SIGNAL(setBusy()),
532 this, SLOT(disableMenu()));
534 connect(this, SIGNAL(setIdle()),
535 this, SLOT(enableMenu()));
539 void MainWindow::connectBookmarksWidget() {
541 //after removing bookmarks we search for them once again to clear word list
542 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
543 this, SLOT(removeBookmarks()));
546 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
547 menuWidget, SLOT(hideMenu()));
549 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
550 backbone, SLOT(fetchBookmarks()));
554 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
555 this, SLOT(removeBookmarks()));
556 connect(bookmarksShowAllAction, SIGNAL(triggered()),
557 backbone, SLOT(fetchBookmarks()));
563 void MainWindow::removeBookmarks() {
566 par = bookmarksWidget;
570 if(QMessageBox::question(par, tr("Delete all bookmarks"),
571 tr("Do you want to delete all bookmarks? (This action cannot be revoked)"),
572 QMessageBox::Yes, QMessageBox::Cancel) == QMessageBox::Yes) {
573 backbone->removeAllBookmarks();
574 if(searchString.size())
575 backbone->search(searchString);