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"
25 #include "ui_MainWindow.h"
28 #include <QMaemo5InformationBox>
32 MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
34 ui(new Ui::MainWindow) {
37 setAttribute(Qt::WA_Maemo5StackedWindow);
40 this->backbone = backbone;
47 connectTranslationWidget();
50 connectBookmarksWidget();
53 setExactSearch(false);
55 setWindowTitle("mDictionary");
60 MainWindow::~MainWindow() {
65 void MainWindow::initializeUI() {
69 //sets attribute to maemo's stacked window
73 searchBarWidget = new SearchBarWidget;
75 wordListWidget = new WordListWidget;
77 //translationWidget is antoher stacked window, so we don't add it to layout
78 //only create it with this widget as parent
79 translationWidget = new TranslationWidget(this);
81 welcomeScreenWidget = new WelcomeScreenWidget;
84 ui->centralWidget->layout()->addWidget(welcomeScreenWidget);
86 translationWidget->hide();
87 splitter = new QSplitter(Qt::Horizontal);
88 splitter->addWidget(wordListWidget);
89 splitter->addWidget(welcomeScreenWidget);
90 splitter->setStretchFactor(1, 150);
91 ui->centralWidget->layout()->addWidget(splitter);
94 QVBoxLayout* vl = (QVBoxLayout*)(ui->centralWidget->layout());
95 vl->addWidget(searchBarWidget, 0, Qt::AlignBottom);
97 dictManagerWidget = new DictManagerWidget(this);
98 dictManagerWidget->hide();
100 settingsWidget = new SettingsWidget(this);
101 settingsWidget->hide();
103 bookmarksWidget = new BookmarksWidget(this);
104 bookmarksWidget->hide();
106 menu = new QMenu(this);
108 aboutWidget = new AboutWidget(this);
114 menuWidget = new MenuWidget(this);
115 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
116 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
117 menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
118 menuWidget->addSubMenu(tr("About"), aboutWidget);
119 ui->menuBar->addAction(menuWidget);
121 dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
122 connect(dictionariesAction, SIGNAL(triggered()),
123 dictManagerWidget, SLOT(show()));
125 settingsAction = ui->menuBar->addAction(tr("Settings"));
126 connect(settingsAction, SIGNAL(triggered()),
127 settingsWidget, SLOT(show()));
129 bookmarksAction = ui->menuBar->addAction(tr("Bookmarks"));
130 connect(bookmarksAction, SIGNAL(triggered()),
131 bookmarksWidget, SLOT(show()));
133 aboutAction = ui->menuBar->addAction(tr("About"));
134 connect(aboutAction, SIGNAL(triggered()),
135 aboutWidget, SLOT(show()));
140 void MainWindow::closeEvent(QCloseEvent *event) {
141 //reqest to stop all searches and close app
146 bool MainWindow::exactSearch() {
150 void MainWindow::setExactSearch(bool exact) {
151 _exactSearch = exact;
154 void MainWindow::setSearchString(QString word) {
158 void MainWindow::wordListReady() {
159 //gets results from backbone
160 QMultiHash<QString, Translation*> res = backbone->result();
161 QHash<QString, QList<Translation*> > searchResult;
164 if(!wordListWidget->isVisible()) {
165 int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
166 QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
167 l->removeWidget(welcomeScreenWidget);
168 l->insertWidget(0, wordListWidget);
173 //if nothing was found
174 if(res.count() == 0) {
176 QMaemo5InformationBox::information(this,
177 tr("Can't find any matching words"),
178 QMaemo5InformationBox::DefaultTimeout);
180 //show empty list to remove results of old search
181 emit showWordList(searchResult);
184 //find translations of the same key word
185 QMultiHash<QString, Translation*>::iterator i;
186 for(i = res.begin(); i != res.end(); i++) {
187 searchResult[i.key()].push_back(i.value());
192 emit showWordList(searchResult);
196 emit showWordList(searchResult);
198 bool foundExactMatch = false;
199 QHash<QString, QList<Translation*> >::iterator j;
200 for(j = searchResult.begin(); j != searchResult.end(); j++) {
201 if(j.key() == searchString && !foundExactMatch) {
202 foundExactMatch = true;
203 emit searchTranslations(j.value());
208 if(!foundExactMatch) {
210 QMaemo5InformationBox::information(this,
211 tr("Can't find exactly matching word"),
212 QMaemo5InformationBox::DefaultTimeout);
215 emit showWordList(searchResult);
220 setExactSearch(false);
223 void MainWindow::translationsReady() {
225 if(!translationWidget->isVisible()) {
226 int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
227 QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
228 QSplitter* s = (QSplitter*)((QWidgetItem*)(l->itemAt(0))->widget());
229 s->insertWidget(1,translationWidget);
230 s->setStretchFactor(1, 150);
231 welcomeScreenWidget->deleteLater();
236 emit showTranslation(backbone->htmls());
239 QList<CommonDictInterface*> MainWindow::getPlugins() {
240 return backbone->getPlugins();
243 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
244 return backbone->getDictionaries();
247 void MainWindow::searchExact(QString word) {
248 setExactSearch(true);
249 //searching with searchBar, not directly by emiting searchWordList(),
250 //because it will set search word in searchBar's edit line
251 //this function is only used by history and when searching from attributes
252 searchBarWidget->search(word);
257 void MainWindow::breakSearching() {
258 //make sure to unset exact search mode
259 setExactSearch(false);
262 void MainWindow::addToHistory(QList<Translation *> trans) {
263 if(trans.count() > 0) {
264 backbone->history()->add(trans[0]->key());
265 translationWidget->setWindowTitle(trans[0]->key());
269 void MainWindow::historyNext() {
270 if(backbone->history()->nextAvailable()) {
271 QString next = backbone->history()->next();
273 setExactSearch(true);
275 searchBarWidget->searchDelay(next);
279 void MainWindow::historyPrev() {
280 if(backbone->history()->prevAvailable()) {
282 setExactSearch(true);
284 QString prev = backbone->history()->previous();
285 searchBarWidget->searchDelay(prev);
289 void MainWindow::disableMenu() {
291 if(ui->menuBar->actions().contains(menuWidget)) {
292 ui->menuBar->removeAction(menuWidget);
295 ui->menuBar->setEnabled(false);
299 void MainWindow::enableMenu() {
301 if(!ui->menuBar->actions().contains(menuWidget)) {
302 ui->menuBar->addAction(menuWidget);
305 ui->menuBar->setEnabled(true);
309 void MainWindow::showHistory() {
310 HistoryListDialog historyDialog(backbone->history()->list(), this);
311 if(historyDialog.exec() == QDialog::Accepted) {
312 backbone->history()->setCurrentElement(historyDialog.selectedRow());
313 searchExact(historyDialog.selectedWord());
317 void MainWindow::setSettings(Settings *s) {
318 backbone->setSettings(s);
321 Settings* MainWindow::settings() {
322 return backbone->settings();
325 void MainWindow::connectBackbone() {
326 connect(this, SIGNAL(quit()),
327 backbone, SLOT(quit()));
329 connect(this, SIGNAL(searchWordList(QString)),
330 backbone, SLOT(search(QString)));
332 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
333 backbone, SLOT(searchHtml(QList<Translation*>)));
335 connect(this, SIGNAL(stopSearching()),
336 backbone, SLOT(stopSearching()));
338 connect(this, SIGNAL(stopSearching()),
339 this, SLOT(breakSearching()));
341 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
342 backbone, SLOT(addDictionary(CommonDictInterface*)));
344 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
345 backbone, SLOT(removeDictionary(CommonDictInterface*)));
347 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
348 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
351 connect(backbone, SIGNAL(ready()),
352 this, SIGNAL(setIdle()));
354 connect(backbone, SIGNAL(htmlReady()),
355 this, SIGNAL(setIdle()));
358 connect(backbone, SIGNAL(ready()),
359 this, SLOT(wordListReady()));
361 connect(backbone, SIGNAL(htmlReady()),
362 this, SLOT(translationsReady()));
364 connect(backbone, SIGNAL(searchCanceled()),
365 this, SIGNAL(setIdle()));
370 connect(this, SIGNAL(searchWordList(QString)),
371 this, SIGNAL(setBusy()));
373 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
374 this, SIGNAL(setBusy()));
376 connect(this, SIGNAL(stopSearching()),
377 this, SIGNAL(setIdle()));
379 connect(this, SIGNAL(searchWordList(QString)),
380 this, SLOT(setSearchString(QString)));
382 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
383 this, SLOT(addToHistory(QList<Translation*>)));
388 void MainWindow::connectSearchBar() {
389 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
390 this, SIGNAL(searchWordList(QString)));
392 connect(searchBarWidget, SIGNAL(stopSearching()),
393 this, SIGNAL(stopSearching()));
395 connect(this, SIGNAL(setBusy()),
396 searchBarWidget, SLOT(setBusy()));
398 connect(this, SIGNAL(setIdle()),
399 searchBarWidget, SLOT(setIdle()));
401 connect(searchBarWidget, SIGNAL(historyNext()),
402 this, SLOT(historyNext()));
404 connect(searchBarWidget, SIGNAL(historyPrev()),
405 this, SLOT(historyPrev()));
407 connect(searchBarWidget, SIGNAL(historyShow()),
408 this, SLOT(showHistory()));
410 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
411 backbone->history(), SLOT(refreshStatus()));
413 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
414 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
417 void MainWindow::connectWordList() {
419 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
421 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
423 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
424 this, SIGNAL(searchTranslations(QList<Translation*>)));
429 connect(this, SIGNAL(setBusy()),
430 wordListWidget, SLOT(lockList()));
432 connect(this, SIGNAL(setIdle()),
433 wordListWidget, SLOT(unlockList()));
435 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
436 backbone, SLOT(addBookmark(QList<Translation*>)));
438 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
439 backbone, SLOT(removeBookmark(QList<Translation*>)));
442 void MainWindow::connectTranslationWidget() {
443 connect(this, SIGNAL(showTranslation(QStringList)),
444 translationWidget, SLOT(show(QStringList)));
448 void MainWindow::connectDictManager() {
449 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
450 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
452 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
453 this, SIGNAL(removeDictionary(CommonDictInterface*)));
455 connect(dictManagerWidget,
456 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
457 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
460 void MainWindow::connectMenu() {
461 connect(this, SIGNAL(setBusy()),
462 this, SLOT(disableMenu()));
464 connect(this, SIGNAL(setIdle()),
465 this, SLOT(enableMenu()));
469 void MainWindow::showAllBookmarks() {
473 void MainWindow::connectBookmarksWidget() {
474 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
475 backbone, SLOT(removeAllBookmark()));
477 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
480 connect(menu, SIGNAL(aboutToHide()),
481 this, SLOT(showAllBookmarks()));