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"
27 #include <QMaemo5InformationBox>
31 MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
33 ui(new Ui::MainWindow) {
36 setAttribute(Qt::WA_Maemo5StackedWindow);
39 this->backbone = backbone;
46 connectTranslationWidget();
50 setExactSearch(false);
52 setWindowTitle("mDictionary");
57 MainWindow::~MainWindow() {
62 void MainWindow::initializeUI() {
66 //sets attribute to maemo's stacked window
70 searchBarWidget = new SearchBarWidget;
72 wordListWidget = new WordListWidget;
74 //translationWidget is antoher stacked window, so we don't add it to layout
75 //only create it with this widget as parent
76 translationWidget = new TranslationWidget(this);
79 ui->centralWidget->layout()->addWidget(wordListWidget);
81 splitter = new QSplitter(Qt::Horizontal);
82 splitter->addWidget(wordListWidget);
83 splitter->addWidget(translationWidget);
84 splitter->setStretchFactor(1, 150);
85 ui->centralWidget->layout()->addWidget(splitter);
87 ui->centralWidget->layout()->addWidget(searchBarWidget);
91 dictManagerWidget = new DictManagerWidget(this);
92 dictManagerWidget->hide();
94 settingsWidget = new SettingsWidget(this);
95 settingsWidget->hide();
98 menuWidget = new MenuWidget(this);
99 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
100 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
101 menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
102 ui->menuBar->addAction(menuWidget);
104 dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
105 connect(dictionariesAction, SIGNAL(triggered()),
106 dictManagerWidget, SLOT(show()));
108 settingsAction = ui->menuBar->addAction(tr("Settings"));
109 connect(settingsAction, SIGNAL(triggered()),
110 settingsWidget, SLOT(show()));
115 void MainWindow::closeEvent(QCloseEvent *event) {
116 //reqest to stop all searches and close app
121 bool MainWindow::exactSearch() {
125 void MainWindow::setExactSearch(bool exact) {
126 _exactSearch = exact;
129 void MainWindow::setSearchString(QString word) {
133 void MainWindow::wordListReady() {
134 //gets results from backbone
135 QMultiHash<QString, Translation*> res = backbone->result();
136 QHash<QString, QList<Translation*> > searchResult;
138 //if nothing was found
139 if(res.count() == 0) {
141 QMaemo5InformationBox::information(this,
142 tr("Can't find any matching words"),
143 QMaemo5InformationBox::DefaultTimeout);
145 //show empty list to remove results of old search
146 emit showWordList(searchResult);
149 //find translations of the same key word
150 QMultiHash<QString, Translation*>::iterator i;
151 for(i = res.begin(); i != res.end(); i++) {
152 searchResult[i.key()].push_back(i.value());
157 emit showWordList(searchResult);
161 emit showWordList(searchResult);
163 bool foundExactMatch = false;
164 QHash<QString, QList<Translation*> >::iterator j;
165 for(j = searchResult.begin(); j != searchResult.end(); j++) {
166 if(j.key() == searchString && !foundExactMatch) {
167 foundExactMatch = true;
168 emit searchTranslations(j.value());
173 if(!foundExactMatch) {
175 QMaemo5InformationBox::information(this,
176 tr("Can't find exactly matching word"),
177 QMaemo5InformationBox::DefaultTimeout);
180 emit showWordList(searchResult);
185 setExactSearch(false);
188 void MainWindow::translationsReady() {
189 emit showTranslation(backbone->htmls());
192 QList<CommonDictInterface*> MainWindow::getPlugins() {
193 return backbone->getPlugins();
196 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
197 return backbone->getDictionaries();
200 void MainWindow::searchExact(QString word) {
201 setExactSearch(true);
202 //searching with searchBar, not directly by emiting searchWordList(),
203 //because it will set search word in searchBar's edit line
204 //this function is only used by history and when searching from attributes
205 searchBarWidget->search(word);
210 void MainWindow::breakSearching() {
211 //make sure to unset exact search mode
212 setExactSearch(false);
215 void MainWindow::addToHistory(QList<Translation *> trans) {
216 if(trans.count() > 0) {
217 backbone->history()->add(trans[0]->key());
218 translationWidget->setWindowTitle(trans[0]->key());
222 void MainWindow::historyNext() {
223 if(backbone->history()->nextAvailable()) {
224 QString next = backbone->history()->next();
226 setExactSearch(true);
228 searchBarWidget->searchDelay(next);
232 void MainWindow::historyPrev() {
233 if(backbone->history()->prevAvailable()) {
235 setExactSearch(true);
237 QString prev = backbone->history()->previous();
238 searchBarWidget->searchDelay(prev);
242 void MainWindow::disableMenu() {
244 if(ui->menuBar->actions().contains(menuWidget)) {
245 ui->menuBar->removeAction(menuWidget);
248 ui->menuBar->setEnabled(false);
252 void MainWindow::enableMenu() {
254 if(!ui->menuBar->actions().contains(menuWidget)) {
255 ui->menuBar->addAction(menuWidget);
258 ui->menuBar->setEnabled(true);
262 void MainWindow::showHistory() {
263 HistoryListDialog historyDialog(backbone->history()->list(), this);
264 if(historyDialog.exec() == QDialog::Accepted) {
265 backbone->history()->setCurrentElement(historyDialog.selectedRow());
266 searchExact(historyDialog.selectedWord());
270 void MainWindow::setSettings(Settings *s) {
271 backbone->setSettings(s);
274 Settings* MainWindow::settings() {
275 return backbone->settings();
278 void MainWindow::connectBackbone() {
279 connect(this, SIGNAL(quit()),
280 backbone, SLOT(quit()));
282 connect(this, SIGNAL(searchWordList(QString)),
283 backbone, SLOT(search(QString)));
285 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
286 backbone, SLOT(searchHtml(QList<Translation*>)));
288 connect(this, SIGNAL(stopSearching()),
289 backbone, SLOT(stopSearching()));
291 connect(this, SIGNAL(stopSearching()),
292 this, SLOT(breakSearching()));
294 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
295 backbone, SLOT(addDictionary(CommonDictInterface*)));
297 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
298 backbone, SLOT(removeDictionary(CommonDictInterface*)));
300 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
301 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
304 connect(backbone, SIGNAL(ready()),
305 this, SIGNAL(setIdle()));
307 connect(backbone, SIGNAL(htmlReady()),
308 this, SIGNAL(setIdle()));
311 connect(backbone, SIGNAL(ready()),
312 this, SLOT(wordListReady()));
314 connect(backbone, SIGNAL(htmlReady()),
315 this, SLOT(translationsReady()));
317 connect(backbone, SIGNAL(searchCanceled()),
318 this, SIGNAL(setIdle()));
323 connect(this, SIGNAL(searchWordList(QString)),
324 this, SIGNAL(setBusy()));
326 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
327 this, SIGNAL(setBusy()));
329 connect(this, SIGNAL(stopSearching()),
330 this, SIGNAL(setIdle()));
332 connect(this, SIGNAL(searchWordList(QString)),
333 this, SLOT(setSearchString(QString)));
335 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
336 this, SLOT(addToHistory(QList<Translation*>)));
341 void MainWindow::connectSearchBar() {
342 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
343 this, SIGNAL(searchWordList(QString)));
345 connect(searchBarWidget, SIGNAL(stopSearching()),
346 this, SIGNAL(stopSearching()));
348 connect(this, SIGNAL(setBusy()),
349 searchBarWidget, SLOT(setBusy()));
351 connect(this, SIGNAL(setIdle()),
352 searchBarWidget, SLOT(setIdle()));
354 connect(searchBarWidget, SIGNAL(historyNext()),
355 this, SLOT(historyNext()));
357 connect(searchBarWidget, SIGNAL(historyPrev()),
358 this, SLOT(historyPrev()));
360 connect(searchBarWidget, SIGNAL(historyShow()),
361 this, SLOT(showHistory()));
363 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
364 backbone->history(), SLOT(refreshStatus()));
366 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
367 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
370 void MainWindow::connectWordList() {
372 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
374 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
376 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
377 this, SIGNAL(searchTranslations(QList<Translation*>)));
382 connect(this, SIGNAL(setBusy()),
383 wordListWidget, SLOT(lockList()));
385 connect(this, SIGNAL(setIdle()),
386 wordListWidget, SLOT(unlockList()));
388 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
389 backbone, SLOT(addBookmark(QList<Translation*>)));
391 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
392 backbone, SLOT(removeBookmark(QList<Translation*>)));
395 void MainWindow::connectTranslationWidget() {
396 connect(this, SIGNAL(showTranslation(QStringList)),
397 translationWidget, SLOT(show(QStringList)));
401 void MainWindow::connectDictManager() {
402 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
403 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
405 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
406 this, SIGNAL(removeDictionary(CommonDictInterface*)));
408 connect(dictManagerWidget,
409 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
410 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
413 void MainWindow::connectMenu() {
414 connect(this, SIGNAL(setBusy()),
415 this, SLOT(disableMenu()));
417 connect(this, SIGNAL(setIdle()),
418 this, SLOT(enableMenu()));