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 connect(settingsWidget, SIGNAL(notify(Notify::NotifyType,QString)),
164 this, SLOT(showNotification(Notify::NotifyType,QString)));
166 bookmarksWidget = new BookmarksWidget(this);
167 bookmarksWidget->hide();
169 aboutWidget = new AboutWidget(this);
173 void MainWindow::closeEvent(QCloseEvent *event) {
174 //request to stop all searches and close app
179 bool MainWindow::isInExactSearch() {
183 void MainWindow::setExactSearch(bool exact) {
184 _exactSearch = exact;
187 void MainWindow::setExactSearchString(QString word) {
191 void MainWindow::wordListReady() {
192 //gets results from backbone
193 QMultiHash<QString, Translation*> backboneResult = backbone->result();
194 QHash<QString, QList<Translation*> > searchResult;
200 //if nothing was found
201 if(backboneResult.count() == 0) {
202 showNotification(Notify::Info, tr("Can't find any matching words"));
204 //show empty list to remove results of old search
205 Q_EMIT showWordList(searchResult);
208 //find translations of the same key word
209 QMultiHash<QString, Translation*>::iterator i;
210 for(i = backboneResult.begin(); i != backboneResult.end(); i++) {
211 searchResult[i.key()].push_back(i.value());
214 //show search results
215 Q_EMIT showWordList(searchResult);
218 if(isInExactSearch()) {
219 QList<Translation*> exactTranslation;
220 if(checkExactSearch(searchResult, exactTranslation)) {
221 Q_EMIT searchTranslations(exactTranslation);
224 showNotification(Notify::Info,
225 tr("Can't find exactly matching word"));
228 setExactSearch(false);
233 bool MainWindow::checkExactSearch(
234 QHash<QString, QList<Translation *> > searchResult,
235 QList<Translation *> &found) {
237 bool foundExactMatch = false;
238 QHash<QString, QList<Translation*> >::iterator j;
239 for(j = searchResult.begin(); j != searchResult.end(); j++) {
240 if(j.key().toLower() == searchString.toLower()
241 && !foundExactMatch) {
249 void MainWindow::translationsReady() {
254 Q_EMIT showTranslation(backbone->htmls());
255 notifyManager->screenChanged();
259 void MainWindow::hideWelcomeScreen() {
261 //switch welcome screen with word list
262 if(!wordListWidget->isVisible()) {
263 mainLayout->removeWidget(welcomeScreenWidget);
264 welcomeScreenWidget->deleteLater();
266 mainLayout->insertWidget(0, wordListWidget);
269 //switch welcome screen with translation widget
270 if(!translationWidget->isVisible()) {
271 splitter->insertWidget(1,translationWidget);
272 splitter->setStretchFactor(1, 150);
273 welcomeScreenWidget->deleteLater();
278 QList<CommonDictInterface*> MainWindow::getPlugins() {
279 return backbone->getPlugins();
282 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
283 return backbone->getDictionaries();
287 void MainWindow::search(QString word) {
288 setExactSearch(false);
289 searchBarWidget->search(word);
292 void MainWindow::searchExact(QString word) {
293 setExactSearch(true);
294 searchBarWidget->search(word);
297 void MainWindow::searchDelay(QString word) {
298 searchBarWidget->searchDelay(word);
305 void MainWindow::searchingInterrupted() {
306 //make sure to unset exact search mode
307 setExactSearch(false);
310 void MainWindow::addToHistory(QList<Translation *> trans) {
311 if(trans.count() > 0) {
312 backbone->history()->add(trans[0]->key());
316 void MainWindow::historyNext() {
317 if(backbone->history()->nextAvailable()) {
318 QString next = backbone->history()->next();
320 setExactSearch(true);
326 void MainWindow::historyPrev() {
327 if(backbone->history()->prevAvailable()) {
329 setExactSearch(true);
331 QString prev = backbone->history()->previous();
336 void MainWindow::disableMenu() {
338 if(menuBar->actions().contains(menuWidget)) {
339 menuBar->removeAction(menuWidget);
342 menuBar->setEnabled(false);
346 void MainWindow::enableMenu() {
348 if(!menuBar->actions().contains(menuWidget)) {
349 menuBar->addAction(menuWidget);
352 menuBar->setEnabled(true);
356 void MainWindow::showHistory(QPoint p) {
358 HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
361 QPoint newPos = mapFromGlobal(p);
362 newPos.setY(searchBarWidget->pos().y() -
363 historyDialog.sizeHint().height());
364 newPos.setX(width() - historyDialog.sizeHint().width());
366 historyDialog.move(newPos);
369 if(historyDialog.exec() == QDialog::Accepted) {
370 backbone->history()->setCurrentElement(historyDialog.selectedRow());
371 searchExact(historyDialog.selectedWord());
375 void MainWindow::setSettings(Settings *s) {
376 backbone->setSettings(s);
379 Settings* MainWindow::settings() {
380 return backbone->settings();
384 void MainWindow::showNotification(Notify::NotifyType type, QString text) {
385 notifyManager->showNotification(type, text);
388 void MainWindow::connectBackbone() {
390 connect(this, SIGNAL(searchWordList(QString)),
391 this, SIGNAL(setBusy()));
393 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
394 this, SIGNAL(setBusy()));
396 connect(this, SIGNAL(stopSearching()),
397 this, SIGNAL(setIdle()));
399 connect(this, SIGNAL(searchWordList(QString)),
400 this, SLOT(setExactSearchString(QString)));
402 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
403 this, SLOT(addToHistory(QList<Translation*>)));
407 connect(this, SIGNAL(quit()),
408 backbone, SLOT(quit()));
410 connect(this, SIGNAL(searchWordList(QString)),
411 backbone, SLOT(search(QString)));
413 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
414 backbone, SLOT(searchHtml(QList<Translation*>)));
416 connect(this, SIGNAL(stopSearching()),
417 backbone, SLOT(stopSearching()));
419 connect(this, SIGNAL(stopSearching()),
420 this, SLOT(searchingInterrupted()));
422 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
423 backbone, SLOT(addDictionary(CommonDictInterface*)));
425 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
426 backbone, SLOT(removeDictionary(CommonDictInterface*)));
428 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
429 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
432 connect(backbone, SIGNAL(ready()),
433 this, SIGNAL(setIdle()));
435 connect(backbone, SIGNAL(htmlReady()),
436 this, SIGNAL(setIdle()));
439 connect(backbone, SIGNAL(ready()),
440 this, SLOT(wordListReady()));
442 connect(backbone, SIGNAL(htmlReady()),
443 this, SLOT(translationsReady()));
445 connect(backbone, SIGNAL(searchCanceled()),
446 this, SIGNAL(setIdle()));
448 connect(backbone, SIGNAL(notify(Notify::NotifyType,QString)),
449 this, SLOT(showNotification(Notify::NotifyType,QString)));
452 void MainWindow::connectSearchBar() {
453 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
454 this, SIGNAL(searchWordList(QString)));
456 connect(searchBarWidget, SIGNAL(stopSearching()),
457 this, SIGNAL(stopSearching()));
459 connect(this, SIGNAL(setBusy()),
460 searchBarWidget, SLOT(setBusy()));
462 connect(this, SIGNAL(setIdle()),
463 searchBarWidget, SLOT(setIdle()));
465 connect(searchBarWidget, SIGNAL(historyNext()),
466 this, SLOT(historyNext()));
468 connect(searchBarWidget, SIGNAL(historyPrev()),
469 this, SLOT(historyPrev()));
471 connect(searchBarWidget, SIGNAL(historyShow(QPoint)),
472 this, SLOT(showHistory(QPoint)));
474 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
475 backbone->history(), SLOT(refreshStatus()));
477 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
478 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
481 void MainWindow::connectWordList() {
483 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
485 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
487 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
488 this, SIGNAL(searchTranslations(QList<Translation*>)));
493 connect(this, SIGNAL(setBusy()),
494 wordListWidget, SLOT(lockList()));
496 connect(this, SIGNAL(setIdle()),
497 wordListWidget, SLOT(unlockList()));
499 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
500 backbone, SLOT(addBookmark(QList<Translation*>)));
502 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
503 backbone, SLOT(removeBookmark(QList<Translation*>)));
506 void MainWindow::connectTranslationWidget() {
507 connect(this, SIGNAL(showTranslation(QStringList)),
508 translationWidget, SLOT(show(QStringList)));
511 connect(translationWidget, SIGNAL(search(QString)),
512 this, SLOT(search(QString)));
514 connect(translationWidget, SIGNAL(search(QString)),
515 this, SLOT(searchExact(QString)));
521 void MainWindow::connectDictManager() {
522 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
523 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
525 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
526 this, SIGNAL(removeDictionary(CommonDictInterface*)));
528 connect(dictManagerWidget,
529 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
530 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
533 void MainWindow::connectMenu() {
534 connect(this, SIGNAL(setBusy()),
535 this, SLOT(disableMenu()));
537 connect(this, SIGNAL(setIdle()),
538 this, SLOT(enableMenu()));
542 void MainWindow::connectBookmarksWidget() {
544 //after removing bookmarks we search for them once again to clear word list
545 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
546 this, SLOT(removeBookmarks()));
549 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
550 menuWidget, SLOT(hideMenu()));
552 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
553 backbone, SLOT(fetchBookmarks()));
557 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
558 this, SLOT(removeBookmarks()));
559 connect(bookmarksShowAllAction, SIGNAL(triggered()),
560 backbone, SLOT(fetchBookmarks()));
566 void MainWindow::removeBookmarks() {
569 par = bookmarksWidget;
573 if(QMessageBox::question(par, tr("Delete all bookmarks"),
574 tr("Do you want to delete all bookmarks? (This action cannot be revoked)"),
575 QMessageBox::Yes, QMessageBox::Cancel) == QMessageBox::Yes) {
576 backbone->removeAllBookmarks();
577 if(searchString.size())
578 backbone->search(searchString);