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 dictionariesAction = menuBar->addAction(tr("Dictionaries"));
128 connect(dictionariesAction, SIGNAL(triggered()),
129 dictManagerWidget, SLOT(show()));
131 settingsAction = menuBar->addAction(tr("Settings"));
132 connect(settingsAction, SIGNAL(triggered()),
133 settingsWidget, SLOT(show()));
135 QMenu* m = menuBar->addMenu(tr("Bookmarks"));
136 bookmarksShowAllAction = new QAction(tr("Show all"), m);
138 bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
140 m->addAction(bookmarksShowAllAction);
141 m->addAction(bookmarksRemoveAllAction);
143 aboutAction = menuBar->addAction(tr("About"));
144 connect(aboutAction, SIGNAL(triggered()),
145 aboutWidget, SLOT(show()));
149 void MainWindow::initializeMenuWidgets() {
150 dictManagerWidget = new DictManagerWidget(this);
151 dictManagerWidget->hide();
153 settingsWidget = new SettingsWidget(this);
154 settingsWidget->hide();
156 bookmarksWidget = new BookmarksWidget(this);
157 bookmarksWidget->hide();
159 aboutWidget = new AboutWidget(this);
163 void MainWindow::closeEvent(QCloseEvent *event) {
164 //request to stop all searches and close app
169 bool MainWindow::isInExactSearch() {
173 void MainWindow::setExactSearch(bool exact) {
174 _exactSearch = exact;
177 void MainWindow::setExactSearchString(QString word) {
181 void MainWindow::wordListReady() {
182 //gets results from backbone
183 QMultiHash<QString, Translation*> backboneResult = backbone->result();
184 QHash<QString, QList<Translation*> > searchResult;
190 //if nothing was found
191 if(backboneResult.count() == 0) {
192 showNotification(Notify::Info, tr("Can't find any matching words"));
194 //show empty list to remove results of old search
195 Q_EMIT showWordList(searchResult);
198 //find translations of the same key word
199 QMultiHash<QString, Translation*>::iterator i;
200 for(i = backboneResult.begin(); i != backboneResult.end(); i++) {
201 searchResult[i.key()].push_back(i.value());
204 //show search results
205 Q_EMIT showWordList(searchResult);
208 if(isInExactSearch()) {
209 QList<Translation*> exactTranslation;
210 if(checkExactSearch(searchResult, exactTranslation)) {
211 Q_EMIT searchTranslations(exactTranslation);
214 showNotification(Notify::Info,
215 tr("Can't find exactly matching word"));
218 setExactSearch(false);
223 bool MainWindow::checkExactSearch(
224 QHash<QString, QList<Translation *> > searchResult,
225 QList<Translation *> &found) {
227 bool foundExactMatch = false;
228 QHash<QString, QList<Translation*> >::iterator j;
229 for(j = searchResult.begin(); j != searchResult.end(); j++) {
230 if(j.key().toLower() == searchString.toLower()
231 && !foundExactMatch) {
239 void MainWindow::translationsReady() {
244 Q_EMIT showTranslation(backbone->htmls());
245 notifyManager->screenChanged();
249 void MainWindow::hideWelcomeScreen() {
251 //switch welcome screen with word list
252 if(!wordListWidget->isVisible()) {
253 mainLayout->removeWidget(welcomeScreenWidget);
254 welcomeScreenWidget->deleteLater();
256 mainLayout->insertWidget(0, wordListWidget);
259 //switch welcome screen with translation widget
260 if(!translationWidget->isVisible()) {
261 splitter->insertWidget(1,translationWidget);
262 splitter->setStretchFactor(1, 150);
263 welcomeScreenWidget->deleteLater();
268 QList<CommonDictInterface*> MainWindow::getPlugins() {
269 return backbone->getPlugins();
272 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
273 return backbone->getDictionaries();
277 void MainWindow::search(QString word) {
278 setExactSearch(false);
279 searchBarWidget->search(word);
282 void MainWindow::searchExact(QString word) {
283 setExactSearch(true);
284 searchBarWidget->search(word);
287 void MainWindow::searchDelay(QString word) {
288 searchBarWidget->searchDelay(word);
295 void MainWindow::searchingInterrupted() {
296 //make sure to unset exact search mode
297 setExactSearch(false);
300 void MainWindow::addToHistory(QList<Translation *> trans) {
301 if(trans.count() > 0) {
302 backbone->history()->add(trans[0]->key());
306 void MainWindow::historyNext() {
307 if(backbone->history()->nextAvailable()) {
308 QString next = backbone->history()->next();
310 setExactSearch(true);
316 void MainWindow::historyPrev() {
317 if(backbone->history()->prevAvailable()) {
319 setExactSearch(true);
321 QString prev = backbone->history()->previous();
326 void MainWindow::disableMenu() {
328 if(menuBar->actions().contains(menuWidget)) {
329 menuBar->removeAction(menuWidget);
332 menuBar->setEnabled(false);
336 void MainWindow::enableMenu() {
338 if(!menuBar->actions().contains(menuWidget)) {
339 menuBar->addAction(menuWidget);
342 menuBar->setEnabled(true);
346 void MainWindow::showHistory(QPoint p) {
348 HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
351 QPoint newPos = mapFromGlobal(p);
352 newPos.setY(searchBarWidget->pos().y() -
353 historyDialog.sizeHint().height());
354 newPos.setX(width() - historyDialog.sizeHint().width());
356 historyDialog.move(newPos);
359 if(historyDialog.exec() == QDialog::Accepted) {
360 backbone->history()->setCurrentElement(historyDialog.selectedRow());
361 searchExact(historyDialog.selectedWord());
365 void MainWindow::setSettings(Settings *s) {
366 backbone->setSettings(s);
369 Settings* MainWindow::settings() {
370 return backbone->settings();
374 void MainWindow::showNotification(Notify::NotifyType type, QString text) {
375 notifyManager->showNotification(type, text);
378 void MainWindow::connectBackbone() {
380 connect(this, SIGNAL(searchWordList(QString)),
381 this, SIGNAL(setBusy()));
383 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
384 this, SIGNAL(setBusy()));
386 connect(this, SIGNAL(stopSearching()),
387 this, SIGNAL(setIdle()));
389 connect(this, SIGNAL(searchWordList(QString)),
390 this, SLOT(setExactSearchString(QString)));
392 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
393 this, SLOT(addToHistory(QList<Translation*>)));
397 connect(this, SIGNAL(quit()),
398 backbone, SLOT(quit()));
400 connect(this, SIGNAL(searchWordList(QString)),
401 backbone, SLOT(search(QString)));
403 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
404 backbone, SLOT(searchHtml(QList<Translation*>)));
406 connect(this, SIGNAL(stopSearching()),
407 backbone, SLOT(stopSearching()));
409 connect(this, SIGNAL(stopSearching()),
410 this, SLOT(searchingInterrupted()));
412 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
413 backbone, SLOT(addDictionary(CommonDictInterface*)));
415 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
416 backbone, SLOT(removeDictionary(CommonDictInterface*)));
418 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
419 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
422 connect(backbone, SIGNAL(ready()),
423 this, SIGNAL(setIdle()));
425 connect(backbone, SIGNAL(htmlReady()),
426 this, SIGNAL(setIdle()));
429 connect(backbone, SIGNAL(ready()),
430 this, SLOT(wordListReady()));
432 connect(backbone, SIGNAL(htmlReady()),
433 this, SLOT(translationsReady()));
435 connect(backbone, SIGNAL(searchCanceled()),
436 this, SIGNAL(setIdle()));
438 connect(backbone, SIGNAL(notify(Notify::NotifyType,QString)),
439 this, SLOT(showNotification(Notify::NotifyType,QString)));
442 void MainWindow::connectSearchBar() {
443 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
444 this, SIGNAL(searchWordList(QString)));
446 connect(searchBarWidget, SIGNAL(stopSearching()),
447 this, SIGNAL(stopSearching()));
449 connect(this, SIGNAL(setBusy()),
450 searchBarWidget, SLOT(setBusy()));
452 connect(this, SIGNAL(setIdle()),
453 searchBarWidget, SLOT(setIdle()));
455 connect(searchBarWidget, SIGNAL(historyNext()),
456 this, SLOT(historyNext()));
458 connect(searchBarWidget, SIGNAL(historyPrev()),
459 this, SLOT(historyPrev()));
461 connect(searchBarWidget, SIGNAL(historyShow(QPoint)),
462 this, SLOT(showHistory(QPoint)));
464 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
465 backbone->history(), SLOT(refreshStatus()));
467 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
468 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
471 void MainWindow::connectWordList() {
473 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
475 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
477 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
478 this, SIGNAL(searchTranslations(QList<Translation*>)));
483 connect(this, SIGNAL(setBusy()),
484 wordListWidget, SLOT(lockList()));
486 connect(this, SIGNAL(setIdle()),
487 wordListWidget, SLOT(unlockList()));
489 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
490 backbone, SLOT(addBookmark(QList<Translation*>)));
492 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
493 backbone, SLOT(removeBookmark(QList<Translation*>)));
496 void MainWindow::connectTranslationWidget() {
497 connect(this, SIGNAL(showTranslation(QStringList)),
498 translationWidget, SLOT(show(QStringList)));
501 connect(translationWidget, SIGNAL(search(QString)),
502 this, SLOT(search(QString)));
504 connect(translationWidget, SIGNAL(search(QString)),
505 this, SLOT(searchExact(QString)));
511 void MainWindow::connectDictManager() {
512 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
513 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
515 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
516 this, SIGNAL(removeDictionary(CommonDictInterface*)));
518 connect(dictManagerWidget,
519 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
520 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
523 void MainWindow::connectMenu() {
524 connect(this, SIGNAL(setBusy()),
525 this, SLOT(disableMenu()));
527 connect(this, SIGNAL(setIdle()),
528 this, SLOT(enableMenu()));
532 void MainWindow::connectBookmarksWidget() {
534 //after removing bookmarks we search for them once again to clear word list
535 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
536 this, SLOT(removeBookmarks()));
539 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
540 menuWidget, SLOT(hideMenu()));
542 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
543 backbone, SLOT(fetchBookmarks()));
547 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
548 this, SLOT(removeBookmarks()));
549 connect(bookmarksShowAllAction, SIGNAL(triggered()),
550 backbone, SLOT(fetchBookmarks()));
556 void MainWindow::removeBookmarks() {
559 par = bookmarksWidget;
563 if(QMessageBox::question(par, tr("Delete all bookmarks"),
564 tr("Do you want to delete all bookmarks? (This action cannot be revoked)"),
565 QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
566 backbone->removeAllBookmarks();
567 if(searchString.size())
568 backbone->search(searchString);