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 menuWidget = new MenuWidget(this);
109 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
110 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
111 menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
112 menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
113 menu->addAction(menuWidget);
114 ui->menuBar->addMenu(menu);
116 dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
117 connect(dictionariesAction, SIGNAL(triggered()),
118 dictManagerWidget, SLOT(show()));
120 settingsAction = ui->menuBar->addAction(tr("Settings"));
121 connect(settingsAction, SIGNAL(triggered()),
122 settingsWidget, SLOT(show()));
124 bookmarksAction = ui->menuBar->addAction(tr("Bookmarks"));
125 connect(bookmarksAction, SIGNAL(triggered()),
126 bookmarksWidget, SLOT(show()));
131 void MainWindow::closeEvent(QCloseEvent *event) {
132 //reqest to stop all searches and close app
137 bool MainWindow::exactSearch() {
141 void MainWindow::setExactSearch(bool exact) {
142 _exactSearch = exact;
145 void MainWindow::setSearchString(QString word) {
149 void MainWindow::wordListReady() {
150 //gets results from backbone
151 QMultiHash<QString, Translation*> res = backbone->result();
152 QHash<QString, QList<Translation*> > searchResult;
155 if(!wordListWidget->isVisible()) {
156 int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
157 QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
158 l->removeWidget(welcomeScreenWidget);
159 l->insertWidget(0, wordListWidget);
164 //if nothing was found
165 if(res.count() == 0) {
167 QMaemo5InformationBox::information(this,
168 tr("Can't find any matching words"),
169 QMaemo5InformationBox::DefaultTimeout);
171 //show empty list to remove results of old search
172 emit showWordList(searchResult);
175 //find translations of the same key word
176 QMultiHash<QString, Translation*>::iterator i;
177 for(i = res.begin(); i != res.end(); i++) {
178 searchResult[i.key()].push_back(i.value());
183 emit showWordList(searchResult);
187 emit showWordList(searchResult);
189 bool foundExactMatch = false;
190 QHash<QString, QList<Translation*> >::iterator j;
191 for(j = searchResult.begin(); j != searchResult.end(); j++) {
192 if(j.key() == searchString && !foundExactMatch) {
193 foundExactMatch = true;
194 emit searchTranslations(j.value());
199 if(!foundExactMatch) {
201 QMaemo5InformationBox::information(this,
202 tr("Can't find exactly matching word"),
203 QMaemo5InformationBox::DefaultTimeout);
206 emit showWordList(searchResult);
211 setExactSearch(false);
214 void MainWindow::translationsReady() {
216 if(!translationWidget->isVisible()) {
217 int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
218 QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
219 QSplitter* s = (QSplitter*)((QWidgetItem*)(l->itemAt(0))->widget());
220 s->insertWidget(1,translationWidget);
221 s->setStretchFactor(1, 150);
222 welcomeScreenWidget->deleteLater();
227 emit showTranslation(backbone->htmls());
230 QList<CommonDictInterface*> MainWindow::getPlugins() {
231 return backbone->getPlugins();
234 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
235 return backbone->getDictionaries();
238 void MainWindow::searchExact(QString word) {
239 setExactSearch(true);
240 //searching with searchBar, not directly by emiting searchWordList(),
241 //because it will set search word in searchBar's edit line
242 //this function is only used by history and when searching from attributes
243 searchBarWidget->search(word);
248 void MainWindow::breakSearching() {
249 //make sure to unset exact search mode
250 setExactSearch(false);
253 void MainWindow::addToHistory(QList<Translation *> trans) {
254 if(trans.count() > 0) {
255 backbone->history()->add(trans[0]->key());
256 translationWidget->setWindowTitle(trans[0]->key());
260 void MainWindow::historyNext() {
261 if(backbone->history()->nextAvailable()) {
262 QString next = backbone->history()->next();
264 setExactSearch(true);
266 searchBarWidget->searchDelay(next);
270 void MainWindow::historyPrev() {
271 if(backbone->history()->prevAvailable()) {
273 setExactSearch(true);
275 QString prev = backbone->history()->previous();
276 searchBarWidget->searchDelay(prev);
280 void MainWindow::disableMenu() {
282 if(ui->menuBar->actions().contains(menuWidget)) {
283 ui->menuBar->removeAction(menuWidget);
286 ui->menuBar->setEnabled(false);
290 void MainWindow::enableMenu() {
292 if(!ui->menuBar->actions().contains(menuWidget)) {
293 ui->menuBar->addAction(menuWidget);
296 ui->menuBar->setEnabled(true);
300 void MainWindow::showHistory() {
301 HistoryListDialog historyDialog(backbone->history()->list(), this);
302 if(historyDialog.exec() == QDialog::Accepted) {
303 backbone->history()->setCurrentElement(historyDialog.selectedRow());
304 searchExact(historyDialog.selectedWord());
308 void MainWindow::setSettings(Settings *s) {
309 backbone->setSettings(s);
312 Settings* MainWindow::settings() {
313 return backbone->settings();
316 void MainWindow::connectBackbone() {
317 connect(this, SIGNAL(quit()),
318 backbone, SLOT(quit()));
320 connect(this, SIGNAL(searchWordList(QString)),
321 backbone, SLOT(search(QString)));
323 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
324 backbone, SLOT(searchHtml(QList<Translation*>)));
326 connect(this, SIGNAL(stopSearching()),
327 backbone, SLOT(stopSearching()));
329 connect(this, SIGNAL(stopSearching()),
330 this, SLOT(breakSearching()));
332 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
333 backbone, SLOT(addDictionary(CommonDictInterface*)));
335 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
336 backbone, SLOT(removeDictionary(CommonDictInterface*)));
338 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
339 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
342 connect(backbone, SIGNAL(ready()),
343 this, SIGNAL(setIdle()));
345 connect(backbone, SIGNAL(htmlReady()),
346 this, SIGNAL(setIdle()));
349 connect(backbone, SIGNAL(ready()),
350 this, SLOT(wordListReady()));
352 connect(backbone, SIGNAL(htmlReady()),
353 this, SLOT(translationsReady()));
355 connect(backbone, SIGNAL(searchCanceled()),
356 this, SIGNAL(setIdle()));
361 connect(this, SIGNAL(searchWordList(QString)),
362 this, SIGNAL(setBusy()));
364 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
365 this, SIGNAL(setBusy()));
367 connect(this, SIGNAL(stopSearching()),
368 this, SIGNAL(setIdle()));
370 connect(this, SIGNAL(searchWordList(QString)),
371 this, SLOT(setSearchString(QString)));
373 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
374 this, SLOT(addToHistory(QList<Translation*>)));
379 void MainWindow::connectSearchBar() {
380 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
381 this, SIGNAL(searchWordList(QString)));
383 connect(searchBarWidget, SIGNAL(stopSearching()),
384 this, SIGNAL(stopSearching()));
386 connect(this, SIGNAL(setBusy()),
387 searchBarWidget, SLOT(setBusy()));
389 connect(this, SIGNAL(setIdle()),
390 searchBarWidget, SLOT(setIdle()));
392 connect(searchBarWidget, SIGNAL(historyNext()),
393 this, SLOT(historyNext()));
395 connect(searchBarWidget, SIGNAL(historyPrev()),
396 this, SLOT(historyPrev()));
398 connect(searchBarWidget, SIGNAL(historyShow()),
399 this, SLOT(showHistory()));
401 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
402 backbone->history(), SLOT(refreshStatus()));
404 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
405 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
408 void MainWindow::connectWordList() {
410 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
412 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
414 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
415 this, SIGNAL(searchTranslations(QList<Translation*>)));
420 connect(this, SIGNAL(setBusy()),
421 wordListWidget, SLOT(lockList()));
423 connect(this, SIGNAL(setIdle()),
424 wordListWidget, SLOT(unlockList()));
426 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
427 backbone, SLOT(addBookmark(QList<Translation*>)));
429 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
430 backbone, SLOT(removeBookmark(QList<Translation*>)));
433 void MainWindow::connectTranslationWidget() {
434 connect(this, SIGNAL(showTranslation(QStringList)),
435 translationWidget, SLOT(show(QStringList)));
439 void MainWindow::connectDictManager() {
440 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
441 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
443 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
444 this, SIGNAL(removeDictionary(CommonDictInterface*)));
446 connect(dictManagerWidget,
447 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
448 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
451 void MainWindow::connectMenu() {
452 connect(this, SIGNAL(setBusy()),
453 this, SLOT(disableMenu()));
455 connect(this, SIGNAL(setIdle()),
456 this, SLOT(enableMenu()));
460 void MainWindow::showAllBookmarks() {
464 void MainWindow::connectBookmarksWidget() {
465 connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
466 backbone, SLOT(removeAllBookmark()));
468 connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
471 connect(menu, SIGNAL(aboutToHide()),
472 this, SLOT(showAllBookmarks()));