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 notifyManager = new NotifyManager(this);
85 initializeSearchWidgets();
92 void MainWindow::initializeSearchWidgets() {
93 searchBarWidget = new SearchBarWidget();
95 wordListWidget = new WordListWidget();
97 welcomeScreenWidget = new WelcomeScreenWidget();
100 //At start we set widget as welcome screen widget
101 mainLayout->addWidget(welcomeScreenWidget);
102 mainLayout->addWidget(searchBarWidget, 0, Qt::AlignBottom);
104 translationWidget->hide();
105 //we add word list and welcome screen to splitter
106 splitter = new QSplitter(Qt::Horizontal);
107 splitter->addWidget(wordListWidget);
108 splitter->addWidget(welcomeScreenWidget);
109 splitter->setStretchFactor(1, 150);
111 mainLayout->addWidget(splitter);
112 mainLayout->addWidget(searchBarWidget);
116 void MainWindow::initializeMenu() {
117 initializeMenuWidgets();
120 menuWidget = new MenuWidget(this);
122 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
123 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
124 menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
125 menuWidget->addSubMenu(tr("About"), aboutWidget);
127 menuBar->addAction(menuWidget);
129 connect(menuWidget, SIGNAL(setApplicationMenu(QWidget*)),
130 notifyManager, SLOT(setMenu(QWidget*)));
132 dictionariesAction = menuBar->addAction(tr("Dictionaries"));
133 connect(dictionariesAction, SIGNAL(triggered()),
134 dictManagerWidget, SLOT(show()));
136 settingsAction = menuBar->addAction(tr("Settings"));
137 connect(settingsAction, SIGNAL(triggered()),
138 settingsWidget, SLOT(show()));
140 QMenu* m = menuBar->addMenu(tr("Bookmarks"));
141 bookmarksShowAllAction = new QAction(tr("Show all"), m);
143 bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
145 m->addAction(bookmarksShowAllAction);
146 m->addAction(bookmarksRemoveAllAction);
148 aboutAction = menuBar->addAction(tr("About"));
149 connect(aboutAction, SIGNAL(triggered()),
150 aboutWidget, SLOT(show()));
155 void MainWindow::initializeMenuWidgets() {
156 dictManagerWidget = new DictManagerWidget(this);
157 dictManagerWidget->hide();
159 settingsWidget = new SettingsWidget(this);
160 settingsWidget->hide();
162 bookmarksWidget = new BookmarksWidget(this);
163 bookmarksWidget->hide();
165 aboutWidget = new AboutWidget(this);
169 void MainWindow::closeEvent(QCloseEvent *event) {
170 //request to stop all searches and close app
175 bool MainWindow::isInExactSearch() {
179 void MainWindow::setExactSearch(bool exact) {
180 _exactSearch = exact;
183 void MainWindow::setExactSearchString(QString word) {
187 void MainWindow::wordListReady() {
188 //gets results from backbone
189 QMultiHash<QString, Translation*> backboneResult = backbone->result();
190 QHash<QString, QList<Translation*> > searchResult;
196 //if nothing was found
197 if(backboneResult.count() == 0) {
198 showNotification(Notify::Info, tr("Can't find any matching words"));
200 //show empty list to remove results of old search
201 Q_EMIT showWordList(searchResult);
204 //find translations of the same key word
205 QMultiHash<QString, Translation*>::iterator i;
206 for(i = backboneResult.begin(); i != backboneResult.end(); i++) {
207 searchResult[i.key()].push_back(i.value());
210 //show search results
211 Q_EMIT showWordList(searchResult);
214 if(isInExactSearch()) {
215 QList<Translation*> exactTranslation;
216 if(checkExactSearch(searchResult, exactTranslation)) {
217 Q_EMIT searchTranslations(exactTranslation);
220 showNotification(Notify::Info,
221 tr("Can't find exactly matching word"));
224 setExactSearch(false);
229 bool MainWindow::checkExactSearch(
230 QHash<QString, QList<Translation *> > searchResult,
231 QList<Translation *> &found) {
233 bool foundExactMatch = false;
234 QHash<QString, QList<Translation*> >::iterator j;
235 for(j = searchResult.begin(); j != searchResult.end(); j++) {
236 if(j.key().toLower() == searchString.toLower()
237 && !foundExactMatch) {
245 void MainWindow::translationsReady() {
250 Q_EMIT showTranslation(backbone->htmls());
251 notifyManager->screenChanged();
255 void MainWindow::hideWelcomeScreen() {
257 //switch welcome screen with word list
258 if(!wordListWidget->isVisible()) {
259 mainLayout->removeWidget(welcomeScreenWidget);
260 welcomeScreenWidget->deleteLater();
262 mainLayout->insertWidget(0, wordListWidget);
265 //switch welcome screen with translation widget
266 if(!translationWidget->isVisible()) {
267 splitter->insertWidget(1,translationWidget);
268 splitter->setStretchFactor(1, 150);
269 welcomeScreenWidget->deleteLater();
274 QList<CommonDictInterface*> MainWindow::getPlugins() {
275 return backbone->getPlugins();
278 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
279 return backbone->getDictionaries();
283 void MainWindow::search(QString word) {
284 setExactSearch(false);
285 searchBarWidget->search(word);
288 void MainWindow::searchExact(QString word) {
289 setExactSearch(true);
290 searchBarWidget->search(word);
293 void MainWindow::searchDelay(QString word) {
294 searchBarWidget->searchDelay(word);
301 void MainWindow::searchingInterrupted() {
302 //make sure to unset exact search mode
303 setExactSearch(false);
306 void MainWindow::addToHistory(QList<Translation *> trans) {
307 if(trans.count() > 0) {
308 backbone->history()->add(trans[0]->key());
312 void MainWindow::historyNext() {
313 if(backbone->history()->nextAvailable()) {
314 QString next = backbone->history()->next();
316 setExactSearch(true);
322 void MainWindow::historyPrev() {
323 if(backbone->history()->prevAvailable()) {
325 setExactSearch(true);
327 QString prev = backbone->history()->previous();
332 void MainWindow::disableMenu() {
334 if(menuBar->actions().contains(menuWidget)) {
335 menuBar->removeAction(menuWidget);
338 menuBar->setEnabled(false);
342 void MainWindow::enableMenu() {
344 if(!menuBar->actions().contains(menuWidget)) {
345 menuBar->addAction(menuWidget);
348 menuBar->setEnabled(true);
352 void MainWindow::showHistory(QPoint p) {
354 HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
357 QPoint newPos = mapFromGlobal(p);
358 newPos.setY(searchBarWidget->pos().y() -
359 historyDialog.sizeHint().height());
360 newPos.setX(width() - historyDialog.sizeHint().width());
362 historyDialog.move(newPos);
365 if(historyDialog.exec() == QDialog::Accepted) {
366 backbone->history()->setCurrentElement(historyDialog.selectedRow());
367 searchExact(historyDialog.selectedWord());
371 void MainWindow::setSettings(Settings *s) {
372 backbone->setSettings(s);
375 Settings* MainWindow::settings() {
376 return backbone->settings();
380 void MainWindow::showNotification(Notify::NotifyType type, QString text) {
381 notifyManager->showNotification(type, text);
384 void MainWindow::connectBackbone() {
386 connect(this, SIGNAL(searchWordList(QString)),
387 this, SIGNAL(setBusy()));
389 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
390 this, SIGNAL(setBusy()));
392 connect(this, SIGNAL(stopSearching()),
393 this, SIGNAL(setIdle()));
395 connect(this, SIGNAL(searchWordList(QString)),
396 this, SLOT(setExactSearchString(QString)));
398 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
399 this, SLOT(addToHistory(QList<Translation*>)));
403 connect(this, SIGNAL(quit()),
404 backbone, SLOT(quit()));
406 connect(this, SIGNAL(searchWordList(QString)),
407 backbone, SLOT(search(QString)));
409 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
410 backbone, SLOT(searchHtml(QList<Translation*>)));
412 connect(this, SIGNAL(stopSearching()),
413 backbone, SLOT(stopSearching()));
415 connect(this, SIGNAL(stopSearching()),
416 this, SLOT(searchingInterrupted()));
418 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
419 backbone, SLOT(addDictionary(CommonDictInterface*)));
421 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
422 backbone, SLOT(removeDictionary(CommonDictInterface*)));
424 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
425 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
428 connect(backbone, SIGNAL(ready()),
429 this, SIGNAL(setIdle()));
431 connect(backbone, SIGNAL(htmlReady()),
432 this, SIGNAL(setIdle()));
435 connect(backbone, SIGNAL(ready()),
436 this, SLOT(wordListReady()));
438 connect(backbone, SIGNAL(htmlReady()),
439 this, SLOT(translationsReady()));
441 connect(backbone, SIGNAL(searchCanceled()),
442 this, SIGNAL(setIdle()));
444 connect(backbone, SIGNAL(notify(Notify::NotifyType,QString)),
445 this, SLOT(showNotification(Notify::NotifyType,QString)));
448 void MainWindow::connectSearchBar() {
449 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
450 this, SIGNAL(searchWordList(QString)));
452 connect(searchBarWidget, SIGNAL(stopSearching()),
453 this, SIGNAL(stopSearching()));
455 connect(this, SIGNAL(setBusy()),
456 searchBarWidget, SLOT(setBusy()));
458 connect(this, SIGNAL(setIdle()),
459 searchBarWidget, SLOT(setIdle()));
461 connect(searchBarWidget, SIGNAL(historyNext()),
462 this, SLOT(historyNext()));
464 connect(searchBarWidget, SIGNAL(historyPrev()),
465 this, SLOT(historyPrev()));
467 connect(searchBarWidget, SIGNAL(historyShow(QPoint)),
468 this, SLOT(showHistory(QPoint)));
470 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
471 backbone->history(), SLOT(refreshStatus()));
473 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
474 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
477 void MainWindow::connectWordList() {
479 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
481 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
483 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
484 this, SIGNAL(searchTranslations(QList<Translation*>)));
489 connect(this, SIGNAL(setBusy()),
490 wordListWidget, SLOT(lockList()));
492 connect(this, SIGNAL(setIdle()),
493 wordListWidget, SLOT(unlockList()));
495 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
496 backbone, SLOT(addBookmark(QList<Translation*>)));
498 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
499 backbone, SLOT(removeBookmark(QList<Translation*>)));
502 void MainWindow::connectTranslationWidget() {
503 connect(this, SIGNAL(showTranslation(QStringList)),
504 translationWidget, SLOT(show(QStringList)));
507 connect(translationWidget, SIGNAL(search(QString)),
508 this, SLOT(search(QString)));
510 connect(translationWidget, SIGNAL(search(QString)),
511 this, SLOT(searchExact(QString)));
517 void MainWindow::connectDictManager() {
518 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
519 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
521 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
522 this, SIGNAL(removeDictionary(CommonDictInterface*)));
524 connect(dictManagerWidget,
525 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
526 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
529 void MainWindow::connectMenu() {
530 connect(this, SIGNAL(setBusy()),
531 this, SLOT(disableMenu()));
533 connect(this, SIGNAL(setIdle()),
534 this, SLOT(enableMenu()));
538 void MainWindow::connectBookmarksWidget() {
540 //after removing bookmarks we search for them once again to clear word list
541 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
542 this, SLOT(removeBookmarks()));
545 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
546 menuWidget, SLOT(hideMenu()));
548 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
549 backbone, SLOT(fetchBookmarks()));
553 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
554 this, SLOT(removeBookmarks()));
555 connect(bookmarksShowAllAction, SIGNAL(triggered()),
556 backbone, SLOT(fetchBookmarks()));
562 void MainWindow::removeBookmarks() {
565 par = bookmarksWidget;
569 if(QMessageBox::question(par, tr("Delete all bookmarks"),
570 tr("Do you want to delete all bookmarks? (This action cannot be revoked)"),
571 QMessageBox::Yes, QMessageBox::Cancel) == QMessageBox::Yes) {
572 backbone->removeAllBookmarks();
573 if(searchString.size())
574 backbone->search(searchString);