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 //Created by Mateusz Półrola
24 #include "MainWindow.h"
27 #include <QMaemo5InformationBox>
31 MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
32 GUIInterface(parent) {
34 this->backbone = backbone;
42 connectTranslationWidget();
45 connectBookmarksWidget();
47 setExactSearch(false);
49 setWindowTitle("mDictionary");
54 MainWindow::~MainWindow() {
59 void MainWindow::initializeUI() {
62 setAttribute(Qt::WA_Maemo5StackedWindow);
65 //translationWidget is another stacked window, so we don't add it to layout
66 //only create it with this widget as parent
67 translationWidget = new TranslationWidget(this);
70 mainLayout = new QVBoxLayout();
71 QWidget* w = new QWidget();
72 w->setLayout(mainLayout);
74 menuBar = new QMenuBar();
77 searchBarWidget = new SearchBarWidget();
79 wordListWidget = new WordListWidget();
81 welcomeScreenWidget = new WelcomeScreenWidget();
85 //At start we set widget as welcome screen widget
86 mainLayout->addWidget(welcomeScreenWidget);
87 mainLayout->addWidget(searchBarWidget, 0, Qt::AlignBottom);
89 translationWidget->hide();
90 //we add to splitter word list and welcome screen
91 splitter = new QSplitter(Qt::Horizontal);
92 splitter->addWidget(wordListWidget);
93 splitter->addWidget(welcomeScreenWidget);
94 splitter->setStretchFactor(1, 150);
95 mainLayout->addWidget(splitter);
96 mainLayout->addWidget(searchBarWidget);
100 dictManagerWidget = new DictManagerWidget(this);
101 dictManagerWidget->hide();
103 settingsWidget = new SettingsWidget(this);
104 settingsWidget->hide();
106 bookmarksWidget = new BookmarksWidget(this);
107 bookmarksWidget->hide();
109 aboutWidget = new AboutWidget(this);
115 menuWidget = new MenuWidget(this);
116 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
117 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
118 menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
119 menuWidget->addSubMenu(tr("About"), aboutWidget);
120 menuBar->addAction(menuWidget);
122 dictionariesAction = menuBar->addAction(tr("Dictionaries"));
123 connect(dictionariesAction, SIGNAL(triggered()),
124 dictManagerWidget, SLOT(show()));
126 settingsAction = menuBar->addAction(tr("Settings"));
127 connect(settingsAction, SIGNAL(triggered()),
128 settingsWidget, SLOT(show()));
130 QMenu* m = menuBar->addMenu(tr("Bookmarks"));
131 bookmarksShowAllAction = new QAction(tr("Show all"), m);
133 bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
135 m->addAction(bookmarksShowAllAction);
136 m->addAction(bookmarksRemoveAllAction);
138 aboutAction = menuBar->addAction(tr("About"));
139 connect(aboutAction, SIGNAL(triggered()),
140 aboutWidget, SLOT(show()));
144 void MainWindow::closeEvent(QCloseEvent *event) {
145 //request to stop all searches and close app
150 bool MainWindow::exactSearch() {
154 void MainWindow::setExactSearch(bool exact) {
155 _exactSearch = exact;
158 void MainWindow::setSearchString(QString word) {
162 void MainWindow::wordListReady() {
163 //gets results from backbone
164 QMultiHash<QString, Translation*> res = backbone->result();
165 QHash<QString, QList<Translation*> > searchResult;
168 //switch welcome screen with word list
169 if(!wordListWidget->isVisible()) {
170 int i = mainLayout->indexOf(welcomeScreenWidget);
171 QBoxLayout* l = (QBoxLayout*)(mainLayout);
172 l->removeWidget(welcomeScreenWidget);
173 welcomeScreenWidget->deleteLater();
174 l->insertWidget(0, wordListWidget);
178 //if nothing was found
179 if(res.count() == 0) {
180 showNotify(Notify::Info, tr("Can't find any matching words"));
182 //show empty list to remove results of old search
183 Q_EMIT showWordList(searchResult);
186 //find translations of the same key word
187 QMultiHash<QString, Translation*>::iterator i;
188 for(i = res.begin(); i != res.end(); i++) {
189 searchResult[i.key()].push_back(i.value());
194 Q_EMIT showWordList(searchResult);
198 //on desktop we show word list in exact search
199 Q_EMIT showWordList(searchResult);
201 bool foundExactMatch = false;
202 QHash<QString, QList<Translation*> >::iterator j;
203 for(j = searchResult.begin(); j != searchResult.end(); j++) {
204 if(j.key() == searchString && !foundExactMatch) {
205 foundExactMatch = true;
206 Q_EMIT searchTranslations(j.value());
211 if(!foundExactMatch) {
212 showNotify(Notify::Info,
213 tr("Can't find exactly matching word"));
215 Q_EMIT showWordList(searchResult);
220 setExactSearch(false);
223 void MainWindow::translationsReady() {
225 //switch welcome screen with translation widget
226 if(!translationWidget->isVisible()) {
227 QBoxLayout* l = (QBoxLayout*)(mainLayout);
228 QSplitter* s = (QSplitter*)((QWidgetItem*)(l->itemAt(0))->widget());
229 s->insertWidget(1,translationWidget);
230 s->setStretchFactor(1, 150);
231 welcomeScreenWidget->deleteLater();
235 Q_EMIT showTranslation(backbone->htmls());
238 QList<CommonDictInterface*> MainWindow::getPlugins() {
239 return backbone->getPlugins();
242 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
243 return backbone->getDictionaries();
247 void MainWindow::search(QString word) {
248 setExactSearch(false);
249 searchBarWidget->search(word);
252 void MainWindow::searchExact(QString word) {
253 setExactSearch(true);
254 //searching with searchBar, not directly by emitting searchWordList(),
255 //because it will set search word in searchBar's edit line
256 //this function is only used by history and when searching from attributes
257 searchBarWidget->search(word);
262 void MainWindow::breakSearching() {
263 //make sure to unset exact search mode
264 setExactSearch(false);
267 void MainWindow::addToHistory(QList<Translation *> trans) {
268 if(trans.count() > 0) {
269 backbone->history()->add(trans[0]->key());
273 void MainWindow::historyNext() {
274 if(backbone->history()->nextAvailable()) {
275 QString next = backbone->history()->next();
277 setExactSearch(true);
279 searchBarWidget->searchDelay(next);
283 void MainWindow::historyPrev() {
284 if(backbone->history()->prevAvailable()) {
286 setExactSearch(true);
288 QString prev = backbone->history()->previous();
289 searchBarWidget->searchDelay(prev);
293 void MainWindow::disableMenu() {
295 if(menuBar->actions().contains(menuWidget)) {
296 menuBar->removeAction(menuWidget);
299 menuBar->setEnabled(false);
303 void MainWindow::enableMenu() {
305 if(!menuBar->actions().contains(menuWidget)) {
306 menuBar->addAction(menuWidget);
309 menuBar->setEnabled(true);
313 void MainWindow::showHistory(QPoint p) {
314 HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
317 QPoint newPos = mapFromGlobal(p);
318 newPos.setY(searchBarWidget->pos().y() -
319 historyDialog.sizeHint().height());
320 newPos.setX(width() - historyDialog.sizeHint().width());
322 historyDialog.move(newPos);
325 if(historyDialog.exec() == QDialog::Accepted) {
326 backbone->history()->setCurrentElement(historyDialog.selectedRow());
327 searchExact(historyDialog.selectedWord());
331 void MainWindow::setSettings(Settings *s) {
332 backbone->setSettings(s);
335 Settings* MainWindow::settings() {
336 return backbone->settings();
340 void MainWindow::showNotify(Notify::NotifyType type, QString text) {
345 QMaemo5InformationBox::information(0,
347 QMaemo5InformationBox::DefaultTimeout);
349 QMessageBox::information(this, "Information", text);
353 case Notify::Warning:
355 QMessageBox::warning(this, "Warning", text);
361 QMaemo5InformationBox::information(0,
363 QMaemo5InformationBox::NoTimeout);
365 QMessageBox::critical(this, "Error", text);
371 void MainWindow::connectBackbone() {
372 connect(this, SIGNAL(quit()),
373 backbone, SLOT(quit()));
375 connect(this, SIGNAL(searchWordList(QString)),
376 backbone, SLOT(search(QString)));
378 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
379 backbone, SLOT(searchHtml(QList<Translation*>)));
381 connect(this, SIGNAL(stopSearching()),
382 backbone, SLOT(stopSearching()));
384 connect(this, SIGNAL(stopSearching()),
385 this, SLOT(breakSearching()));
387 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
388 backbone, SLOT(addDictionary(CommonDictInterface*)));
390 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
391 backbone, SLOT(removeDictionary(CommonDictInterface*)));
393 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
394 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
397 connect(backbone, SIGNAL(ready()),
398 this, SIGNAL(setIdle()));
400 connect(backbone, SIGNAL(htmlReady()),
401 this, SIGNAL(setIdle()));
404 connect(backbone, SIGNAL(ready()),
405 this, SLOT(wordListReady()));
407 connect(backbone, SIGNAL(htmlReady()),
408 this, SLOT(translationsReady()));
410 connect(backbone, SIGNAL(searchCanceled()),
411 this, SIGNAL(setIdle()));
413 connect(backbone, SIGNAL(notify(Notify::NotifyType,QString)),
414 this, SLOT(showNotify(Notify::NotifyType,QString)));
419 connect(this, SIGNAL(searchWordList(QString)),
420 this, SIGNAL(setBusy()));
422 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
423 this, SIGNAL(setBusy()));
425 connect(this, SIGNAL(stopSearching()),
426 this, SIGNAL(setIdle()));
428 connect(this, SIGNAL(searchWordList(QString)),
429 this, SLOT(setSearchString(QString)));
431 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
432 this, SLOT(addToHistory(QList<Translation*>)));
437 void MainWindow::connectSearchBar() {
438 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
439 this, SIGNAL(searchWordList(QString)));
441 connect(searchBarWidget, SIGNAL(stopSearching()),
442 this, SIGNAL(stopSearching()));
444 connect(this, SIGNAL(setBusy()),
445 searchBarWidget, SLOT(setBusy()));
447 connect(this, SIGNAL(setIdle()),
448 searchBarWidget, SLOT(setIdle()));
450 connect(searchBarWidget, SIGNAL(historyNext()),
451 this, SLOT(historyNext()));
453 connect(searchBarWidget, SIGNAL(historyPrev()),
454 this, SLOT(historyPrev()));
456 connect(searchBarWidget, SIGNAL(historyShow(QPoint)),
457 this, SLOT(showHistory(QPoint)));
459 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
460 backbone->history(), SLOT(refreshStatus()));
462 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
463 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
466 void MainWindow::connectWordList() {
468 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
470 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
472 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
473 this, SIGNAL(searchTranslations(QList<Translation*>)));
478 connect(this, SIGNAL(setBusy()),
479 wordListWidget, SLOT(lockList()));
481 connect(this, SIGNAL(setIdle()),
482 wordListWidget, SLOT(unlockList()));
484 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
485 backbone, SLOT(addBookmark(QList<Translation*>)));
487 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
488 backbone, SLOT(removeBookmark(QList<Translation*>)));
491 void MainWindow::connectTranslationWidget() {
492 connect(this, SIGNAL(showTranslation(QStringList)),
493 translationWidget, SLOT(show(QStringList)));
496 connect(translationWidget, SIGNAL(search(QString)),
497 this, SLOT(search(QString)));
499 connect(translationWidget, SIGNAL(search(QString)),
500 this, SLOT(searchExact(QString)));
506 void MainWindow::connectDictManager() {
507 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
508 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
510 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
511 this, SIGNAL(removeDictionary(CommonDictInterface*)));
513 connect(dictManagerWidget,
514 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
515 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
518 void MainWindow::connectMenu() {
519 connect(this, SIGNAL(setBusy()),
520 this, SLOT(disableMenu()));
522 connect(this, SIGNAL(setIdle()),
523 this, SLOT(enableMenu()));
527 void MainWindow::connectBookmarksWidget() {
529 //after removing bookmarks we search for it once again to clear word list
530 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
531 this, SLOT(removeBookmarks()));
534 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
535 menuWidget, SLOT(hideMenu()));
537 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
538 backbone, SLOT(fetchBookmarks()));
542 connect(bookmarksRemoveAllAction, SIGNAL(triggered()),
543 this, SLOT(removeBookmarks()));
544 connect(bookmarksShowAllAction, SIGNAL(triggered()),
545 backbone, SLOT(fetchBookmarks()));
551 void MainWindow::removeBookmarks() {
554 par = bookmarksWidget;
558 if(QMessageBox::question(par, "Delete all bookmarks",
559 "Do you want to delete all bookmarks? (This action cannot be revoked)",
560 QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
561 backbone->removeAllBookmarks();
562 if(searchString.size())
563 backbone->search(searchString);