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();
97 aboutWidget = new AboutWidget(this);
103 menuWidget = new MenuWidget(this);
104 menuWidget->addSubMenu(tr("Settings"), settingsWidget);
105 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
106 menuWidget->addSubMenu(tr("About"), aboutWidget);
107 ui->menuBar->addAction(menuWidget);
109 dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
110 connect(dictionariesAction, SIGNAL(triggered()),
111 dictManagerWidget, SLOT(show()));
113 settingsAction = ui->menuBar->addAction(tr("Settings"));
114 connect(settingsAction, SIGNAL(triggered()),
115 settingsWidget, SLOT(show()));
117 aboutAction = ui->menuBar->addAction(tr("About"));
118 connect(aboutAction, SIGNAL(triggered()),
119 aboutWidget, SLOT(show()));
124 void MainWindow::closeEvent(QCloseEvent *event) {
125 //reqest to stop all searches and close app
130 bool MainWindow::exactSearch() {
134 void MainWindow::setExactSearch(bool exact) {
135 _exactSearch = exact;
138 void MainWindow::setSearchString(QString word) {
142 void MainWindow::wordListReady() {
143 //gets results from backbone
144 QMultiHash<QString, Translation*> res = backbone->result();
145 QHash<QString, QList<Translation*> > searchResult;
147 //if nothing was found
148 if(res.count() == 0) {
150 QMaemo5InformationBox::information(this,
151 tr("Can't find any matching words"),
152 QMaemo5InformationBox::DefaultTimeout);
154 //show empty list to remove results of old search
155 emit showWordList(searchResult);
158 //find translations of the same key word
159 QMultiHash<QString, Translation*>::iterator i;
160 for(i = res.begin(); i != res.end(); i++) {
161 searchResult[i.key()].push_back(i.value());
166 emit showWordList(searchResult);
170 emit showWordList(searchResult);
172 bool foundExactMatch = false;
173 QHash<QString, QList<Translation*> >::iterator j;
174 for(j = searchResult.begin(); j != searchResult.end(); j++) {
175 if(j.key() == searchString && !foundExactMatch) {
176 foundExactMatch = true;
177 emit searchTranslations(j.value());
182 if(!foundExactMatch) {
184 QMaemo5InformationBox::information(this,
185 tr("Can't find exactly matching word"),
186 QMaemo5InformationBox::DefaultTimeout);
189 emit showWordList(searchResult);
194 setExactSearch(false);
197 void MainWindow::translationsReady() {
198 emit showTranslation(backbone->htmls());
201 QList<CommonDictInterface*> MainWindow::getPlugins() {
202 return backbone->getPlugins();
205 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
206 return backbone->getDictionaries();
209 void MainWindow::searchExact(QString word) {
210 setExactSearch(true);
211 //searching with searchBar, not directly by emiting searchWordList(),
212 //because it will set search word in searchBar's edit line
213 //this function is only used by history and when searching from attributes
214 searchBarWidget->search(word);
219 void MainWindow::breakSearching() {
220 //make sure to unset exact search mode
221 setExactSearch(false);
224 void MainWindow::addToHistory(QList<Translation *> trans) {
225 if(trans.count() > 0) {
226 backbone->history()->add(trans[0]->key());
227 translationWidget->setWindowTitle(trans[0]->key());
231 void MainWindow::historyNext() {
232 if(backbone->history()->nextAvailable()) {
233 QString next = backbone->history()->next();
235 setExactSearch(true);
237 searchBarWidget->searchDelay(next);
241 void MainWindow::historyPrev() {
242 if(backbone->history()->prevAvailable()) {
244 setExactSearch(true);
246 QString prev = backbone->history()->previous();
247 searchBarWidget->searchDelay(prev);
251 void MainWindow::disableMenu() {
253 if(ui->menuBar->actions().contains(menuWidget)) {
254 ui->menuBar->removeAction(menuWidget);
257 ui->menuBar->setEnabled(false);
261 void MainWindow::enableMenu() {
263 if(!ui->menuBar->actions().contains(menuWidget)) {
264 ui->menuBar->addAction(menuWidget);
267 ui->menuBar->setEnabled(true);
271 void MainWindow::showHistory() {
272 HistoryListDialog historyDialog(backbone->history()->list(), this);
273 if(historyDialog.exec() == QDialog::Accepted) {
274 backbone->history()->setCurrentElement(historyDialog.selectedRow());
275 searchExact(historyDialog.selectedWord());
279 void MainWindow::setSettings(Settings *s) {
280 backbone->setSettings(s);
283 Settings* MainWindow::settings() {
284 return backbone->settings();
287 void MainWindow::connectBackbone() {
288 connect(this, SIGNAL(quit()),
289 backbone, SLOT(quit()));
291 connect(this, SIGNAL(searchWordList(QString)),
292 backbone, SLOT(search(QString)));
294 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
295 backbone, SLOT(searchHtml(QList<Translation*>)));
297 connect(this, SIGNAL(stopSearching()),
298 backbone, SLOT(stopSearching()));
300 connect(this, SIGNAL(stopSearching()),
301 this, SLOT(breakSearching()));
303 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
304 backbone, SLOT(addDictionary(CommonDictInterface*)));
306 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
307 backbone, SLOT(removeDictionary(CommonDictInterface*)));
309 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
310 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
313 connect(backbone, SIGNAL(ready()),
314 this, SIGNAL(setIdle()));
316 connect(backbone, SIGNAL(htmlReady()),
317 this, SIGNAL(setIdle()));
320 connect(backbone, SIGNAL(ready()),
321 this, SLOT(wordListReady()));
323 connect(backbone, SIGNAL(htmlReady()),
324 this, SLOT(translationsReady()));
326 connect(backbone, SIGNAL(searchCanceled()),
327 this, SIGNAL(setIdle()));
332 connect(this, SIGNAL(searchWordList(QString)),
333 this, SIGNAL(setBusy()));
335 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
336 this, SIGNAL(setBusy()));
338 connect(this, SIGNAL(stopSearching()),
339 this, SIGNAL(setIdle()));
341 connect(this, SIGNAL(searchWordList(QString)),
342 this, SLOT(setSearchString(QString)));
344 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
345 this, SLOT(addToHistory(QList<Translation*>)));
350 void MainWindow::connectSearchBar() {
351 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
352 this, SIGNAL(searchWordList(QString)));
354 connect(searchBarWidget, SIGNAL(stopSearching()),
355 this, SIGNAL(stopSearching()));
357 connect(this, SIGNAL(setBusy()),
358 searchBarWidget, SLOT(setBusy()));
360 connect(this, SIGNAL(setIdle()),
361 searchBarWidget, SLOT(setIdle()));
363 connect(searchBarWidget, SIGNAL(historyNext()),
364 this, SLOT(historyNext()));
366 connect(searchBarWidget, SIGNAL(historyPrev()),
367 this, SLOT(historyPrev()));
369 connect(searchBarWidget, SIGNAL(historyShow()),
370 this, SLOT(showHistory()));
372 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
373 backbone->history(), SLOT(refreshStatus()));
375 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
376 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
379 void MainWindow::connectWordList() {
381 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
383 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
385 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
386 this, SIGNAL(searchTranslations(QList<Translation*>)));
391 connect(this, SIGNAL(setBusy()),
392 wordListWidget, SLOT(lockList()));
394 connect(this, SIGNAL(setIdle()),
395 wordListWidget, SLOT(unlockList()));
397 connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
398 backbone, SLOT(addBookmark(QList<Translation*>)));
400 connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
401 backbone, SLOT(removeBookmark(QList<Translation*>)));
404 void MainWindow::connectTranslationWidget() {
405 connect(this, SIGNAL(showTranslation(QStringList)),
406 translationWidget, SLOT(show(QStringList)));
410 void MainWindow::connectDictManager() {
411 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
412 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
414 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
415 this, SIGNAL(removeDictionary(CommonDictInterface*)));
417 connect(dictManagerWidget,
418 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
419 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
422 void MainWindow::connectMenu() {
423 connect(this, SIGNAL(setBusy()),
424 this, SLOT(disableMenu()));
426 connect(this, SIGNAL(setIdle()),
427 this, SLOT(enableMenu()));