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 \brief Implements interface for GUI
25 \author Mateusz Półrola <mateusz.polrola@comarch.pl>
28 #include "MainWindow.h"
31 #include <QMaemo5InformationBox>
35 MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
36 GUIInterface(parent) {
38 this->backbone = backbone;
46 connectTranslationWidget();
49 connectBookmarksWidget();
51 setExactSearch(false);
53 setMinimumSize(750, 400);
57 searchBarWidget->setFocus();
60 MainWindow::~MainWindow() {
64 void MainWindow::initializeUI() {
67 setAttribute(Qt::WA_Maemo5StackedWindow);
71 /*translationWidget is another stacked window, so we don't add it to
72 layout, only create it with this widget as parent
73 it must be created as first object in main window, otherwise sometimes
74 when app starts in maemo, when trying to set stacked window attribute
76 translationWidget = new TranslationWidget(this);
81 setWindowIcon(QIcon(":/icons/64x64/mdictionary.png"));
82 setWindowTitle("mDictionary");
84 mainLayout = new QVBoxLayout();
85 QWidget* w = new QWidget();
86 w->setLayout(mainLayout);
89 menuBar = new QMenuBar();
92 notifyManager = new NotifyManager(this);
94 initializeSearchWidgets();
99 void MainWindow::initializeSearchWidgets() {
100 searchBarWidget = new SearchBarWidget();
102 wordListWidget = new WordListWidget();
104 welcomeScreenWidget = new WelcomeScreenWidget();
107 //At start we set widget as welcome screen widget
108 mainLayout->addWidget(welcomeScreenWidget);
109 mainLayout->addWidget(searchBarWidget, 0, Qt::AlignBottom);
111 translationWidget->hide();
112 //we add word list and welcome screen to splitter
113 splitter = new QSplitter(Qt::Horizontal);
114 splitter->addWidget(wordListWidget);
115 splitter->addWidget(welcomeScreenWidget);
116 splitter->setStretchFactor(1, 150);
118 mainLayout->addWidget(splitter, 1);
120 mainLayout->addWidget(searchBarWidget,0, Qt::AlignBottom);
124 void MainWindow::initializeMenu() {
125 initializeMenuWidgets();
128 menuWidget = new MenuWidget(this);
130 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
131 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
132 menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
133 menuWidget->addSubMenu(tr("About"), aboutWidget);
135 menuBar->addAction(menuWidget);
137 connect(menuWidget, SIGNAL(setApplicationMenu(QWidget*)),
138 notifyManager, SLOT(setMenu(QWidget*)));
140 dictionariesAction = menuBar->addAction(tr("&Dictionaries"));
141 connect(dictionariesAction, SIGNAL(triggered()),
142 dictManagerWidget, SLOT(show()));
144 settingsAction = menuBar->addAction(tr("&Settings"));
145 connect(settingsAction, SIGNAL(triggered()),
146 settingsWidget, SLOT(show()));
148 QMenu* m = menuBar->addMenu(tr("&Bookmarks"));
149 bookmarksShowAllAction = new QAction(tr("Show all"), m);
151 bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
153 m->addAction(bookmarksShowAllAction);
154 m->addAction(bookmarksRemoveAllAction);
156 aboutAction = menuBar->addAction(tr("&About"));
157 connect(aboutAction, SIGNAL(triggered()),
158 aboutWidget, SLOT(show()));
163 void MainWindow::initializeMenuWidgets() {
164 dictManagerWidget = new DictManagerWidget(this);
165 dictManagerWidget->hide();
167 settingsWidget = new SettingsWidget(this);
168 settingsWidget->hide();
170 connect(settingsWidget, SIGNAL(notify(Notify::NotifyType,QString)),
171 this, SLOT(showNotification(Notify::NotifyType,QString)));
173 bookmarksWidget = new BookmarksWidget(this);
174 bookmarksWidget->hide();
176 aboutWidget = new AboutWidget(this);
180 void MainWindow::closeEvent(QCloseEvent *event) {
181 //request to stop all searches and close app
186 bool MainWindow::isInExactSearch() {
190 void MainWindow::setExactSearch(bool exact) {
191 _exactSearch = exact;
194 void MainWindow::setExactSearchString(QString word) {
198 void MainWindow::wordListReady() {
199 //gets results from backbone
200 QMultiHash<QString, Translation*> backboneResult = backbone->result();
201 QHash<QString, QList<Translation*> > searchResult;
207 //if nothing was found
208 if(backboneResult.count() == 0) {
209 showNotification(Notify::Info, tr("Can't find any matching words"));
211 //show empty list to remove results of old search
212 Q_EMIT showWordList(searchResult);
215 //find translations of the same key word
216 QMultiHash<QString, Translation*>::iterator i;
217 for(i = backboneResult.begin(); i != backboneResult.end(); i++) {
218 searchResult[i.key()].push_back(i.value());
221 //show search results
222 Q_EMIT showWordList(searchResult);
225 if(isInExactSearch()) {
226 QList<Translation*> exactTranslation;
227 if(checkExactSearch(searchResult, exactTranslation)) {
228 Q_EMIT searchTranslations(exactTranslation);
231 showNotification(Notify::Info,
232 tr("Can't find exactly matching word"));
235 setExactSearch(false);
239 wordListWidget->setFocus();
242 bool MainWindow::checkExactSearch(
243 QHash<QString, QList<Translation *> > searchResult,
244 QList<Translation *> &found) {
246 bool foundExactMatch = false;
247 QHash<QString, QList<Translation*> >::iterator j;
248 for(j = searchResult.begin(); j != searchResult.end(); j++) {
249 if(j.key().toLower() == searchString.toLower()
250 && !foundExactMatch) {
258 void MainWindow::translationsReady() {
263 Q_EMIT showTranslation(backbone->xmls());
264 wordListWidget->setFocus();
266 notifyManager->screenChanged();
271 void MainWindow::hideWelcomeScreen() {
273 //switch welcome screen with word list
274 if(!wordListWidget->isVisible()) {
275 mainLayout->removeWidget(welcomeScreenWidget);
276 welcomeScreenWidget->deleteLater();
278 mainLayout->insertWidget(0, wordListWidget);
281 //switch welcome screen with translation widget
282 if(!translationWidget->isVisible()) {
283 splitter->insertWidget(1,translationWidget);
284 splitter->setStretchFactor(1, 150);
285 welcomeScreenWidget->deleteLater();
290 QList<CommonDictInterface*> MainWindow::getPlugins() {
291 return backbone->getPlugins();
294 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
295 return backbone->getDictionaries();
299 void MainWindow::search(QString word) {
300 setExactSearch(false);
301 searchBarWidget->search(word);
303 if(translationWidget->isVisible()) {
304 translationWidget->hide();
310 void MainWindow::searchExact(QString word) {
311 setExactSearch(true);
312 searchBarWidget->search(word);
315 void MainWindow::searchDelay(QString word) {
316 searchBarWidget->searchDelay(word);
323 void MainWindow::searchingInterrupted() {
324 //make sure to unset exact search mode
325 setExactSearch(false);
328 void MainWindow::addToHistory(QList<Translation *> trans) {
329 if(trans.count() > 0) {
330 backbone->history()->add(trans[0]->key());
334 void MainWindow::historyNext() {
335 if(backbone->history()->nextAvailable()) {
336 QString next = backbone->history()->next();
338 setExactSearch(true);
344 void MainWindow::historyPrev() {
345 if(backbone->history()->prevAvailable()) {
347 setExactSearch(true);
349 QString prev = backbone->history()->previous();
354 void MainWindow::disableMenu() {
356 if(menuBar->actions().contains(menuWidget)) {
357 menuBar->removeAction(menuWidget);
360 menuBar->setEnabled(false);
364 void MainWindow::enableMenu() {
366 if(!menuBar->actions().contains(menuWidget)) {
367 menuBar->addAction(menuWidget);
370 menuBar->setEnabled(true);
374 void MainWindow::showHistory(QPoint p) {
376 HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
379 QPoint newPos = mapFromGlobal(p);
380 newPos.setY(searchBarWidget->pos().y() -
381 historyDialog.sizeHint().height());
382 newPos.setX(width() - historyDialog.sizeHint().width());
384 historyDialog.move(newPos);
387 if(historyDialog.exec() == QDialog::Accepted) {
388 backbone->history()->setCurrentElement(historyDialog.selectedRow());
389 searchExact(historyDialog.selectedWord());
393 void MainWindow::setSettings(Settings *s) {
394 backbone->setSettings(s);
397 Settings* MainWindow::settings() {
398 return backbone->settings();
402 void MainWindow::showNotification(Notify::NotifyType type, QString text) {
403 notifyManager->showNotification(type, text);
406 void MainWindow::connectBackbone() {
408 connect(this, SIGNAL(searchWordList(QString)),
409 this, SIGNAL(setBusy()));
411 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
412 this, SIGNAL(setBusy()));
414 connect(this, SIGNAL(stopSearching()),
415 this, SIGNAL(setIdle()));
417 connect(this, SIGNAL(searchWordList(QString)),
418 this, SLOT(setExactSearchString(QString)));
420 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
421 this, SLOT(addToHistory(QList<Translation*>)));
425 connect(this, SIGNAL(quit()),
426 backbone, SLOT(quit()));
428 connect(this, SIGNAL(searchWordList(QString)),
429 backbone, SLOT(search(QString)));
431 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
432 backbone, SLOT(searchXml(QList<Translation*>)));
434 connect(this, SIGNAL(stopSearching()),
435 backbone, SLOT(stopSearching()));
437 connect(this, SIGNAL(stopSearching()),
438 this, SLOT(searchingInterrupted()));
440 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
441 backbone, SLOT(addDictionary(CommonDictInterface*)));
443 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
444 backbone, SLOT(removeDictionary(CommonDictInterface*)));
446 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
447 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
450 connect(backbone, SIGNAL(ready()),
451 this, SIGNAL(setIdle()));
453 connect(backbone, SIGNAL(xmlReady()),
454 this, SIGNAL(setIdle()));
457 connect(backbone, SIGNAL(ready()),
458 this, SLOT(wordListReady()));
460 connect(backbone, SIGNAL(xmlReady()),
461 this, SLOT(translationsReady()));
463 connect(backbone, SIGNAL(searchCanceled()),
464 this, SIGNAL(setIdle()));
466 connect(backbone, SIGNAL(notify(Notify::NotifyType,QString)),
467 this, SLOT(showNotification(Notify::NotifyType,QString)));
469 connect(backbone, SIGNAL(closeOk()),
470 this, SLOT(close()));
473 //connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
474 // this, SIGNAL(setBusy()));
476 //connect(backbone, SIGNAL(bookmarkReady()),
477 // this, SIGNAL(setIdle()));
480 void MainWindow::connectSearchBar() {
481 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
482 this, SIGNAL(searchWordList(QString)));
484 connect(searchBarWidget, SIGNAL(stopSearching()),
485 this, SIGNAL(stopSearching()));
487 connect(this, SIGNAL(setBusy()),
488 searchBarWidget, SLOT(setBusy()));
490 connect(this, SIGNAL(setIdle()),
491 searchBarWidget, SLOT(setIdle()));
493 connect(searchBarWidget, SIGNAL(historyNext()),
494 this, SLOT(historyNext()));
496 connect(searchBarWidget, SIGNAL(historyPrev()),
497 this, SLOT(historyPrev()));
499 connect(searchBarWidget, SIGNAL(historyShow(QPoint)),
500 this, SLOT(showHistory(QPoint)));
502 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
503 backbone->history(), SLOT(refreshStatus()));
505 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
506 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
509 void MainWindow::connectWordList() {
511 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
513 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
515 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
516 this, SIGNAL(searchTranslations(QList<Translation*>)));
521 connect(this, SIGNAL(setBusy()),
522 wordListWidget, SLOT(lockList()));
524 connect(this, SIGNAL(setIdle()),
525 wordListWidget, SLOT(unlockList()));
527 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
528 backbone, SLOT(addBookmark(QList<Translation*>)));
530 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
531 backbone, SLOT(removeBookmark(QList<Translation*>)));
534 void MainWindow::connectTranslationWidget() {
535 connect(this, SIGNAL(showTranslation(QStringList)),
536 translationWidget, SLOT(show(QStringList)));
539 connect(translationWidget, SIGNAL(search(QString)),
540 this, SLOT(search(QString)));
542 connect(translationWidget, SIGNAL(notify(Notify::NotifyType, QString)),
543 this, SLOT(showNotification(Notify::NotifyType,QString)));
545 connect(translationWidget, SIGNAL(search(QString)),
546 this, SLOT(searchExact(QString)));
551 void MainWindow::connectDictManager() {
552 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
553 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
555 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
556 this, SIGNAL(removeDictionary(CommonDictInterface*)));
558 connect(dictManagerWidget,
559 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
560 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
563 void MainWindow::connectMenu() {
564 connect(this, SIGNAL(setBusy()),
565 this, SLOT(disableMenu()));
567 connect(this, SIGNAL(setIdle()),
568 this, SLOT(enableMenu()));
572 void MainWindow::connectBookmarksWidget() {
574 //after removing bookmarks we search for them once again to clear the words list
575 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
576 this, SLOT(removeBookmarks()));
579 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
580 menuWidget, SLOT(hideMenu()));
582 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
583 backbone, SLOT(fetchBookmarks()));
587 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
588 this, SLOT(removeBookmarks()));
589 connect(bookmarksShowAllAction, SIGNAL(triggered()),
590 backbone, SLOT(fetchBookmarks()));
596 void MainWindow::removeBookmarks() {
599 par = bookmarksWidget;
603 if(QMessageBox::question(par, tr("Delete all bookmarks"),
604 tr("Do you want to delete all bookmarks? (This action cannot be revoked, and will clear current word list)"),
605 QMessageBox::Yes, QMessageBox::Cancel) == QMessageBox::Yes) {
606 backbone->removeAllBookmarks();
607 ((WordListWidget*)wordListWidget)->clear();