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) {
35 this->backbone = backbone;
42 connectTranslationWidget();
46 setExactSearch(false);
48 setWindowTitle("mDictionary");
51 MainWindow::~MainWindow() {
56 void MainWindow::initializeUI() {
59 //sets attribute to maemo's stacked window
61 setAttribute(Qt::WA_Maemo5StackedWindow);
65 searchBarWidget = new SearchBarWidget;
66 wordListWidget = new WordListWidget;
68 //translationWidget is antoher stacked window, so we don't add it to layout
69 //only create it with this widget as parent
70 translationWidget = new TranslationWidget(this);
73 ui->centralWidget->layout()->addWidget(wordListWidget);
75 splitter = new QSplitter(Qt::Horizontal);
76 splitter->addWidget(wordListWidget);
77 splitter->addWidget(translationWidget);
78 splitter->setStretchFactor(1, 150);
79 ui->centralWidget->layout()->addWidget(splitter);
81 ui->centralWidget->layout()->addWidget(searchBarWidget);
85 dictManagerWidget = new DictManagerWidget(this);
86 dictManagerWidget->hide();
88 menuWidget = new MenuWidget(this);
89 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
90 menuWidget->addSubMenu(tr("Settings"), new QPushButton("Settings"));
91 menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
92 ui->menuBar->addAction(menuWidget);
94 dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
95 connect(dictionariesAction, SIGNAL(triggered()),
96 dictManagerWidget, SLOT(show()));
101 void MainWindow::closeEvent(QCloseEvent *event) {
102 //reqest to stop all searches and close app
107 bool MainWindow::exactSearch() {
111 void MainWindow::setExactSearch(bool exact) {
112 _exactSearch = exact;
115 void MainWindow::setSearchString(QString word) {
119 void MainWindow::wordListReady() {
120 //gets results from backbone
121 QMultiHash<QString, Translation*> res = backbone->result();
122 QHash<QString, QList<Translation*> > searchResult;
124 //if nothing was found
125 if(res.count() == 0) {
127 QMaemo5InformationBox::information(this,
128 tr("Can't find any matching words"),
129 QMaemo5InformationBox::DefaultTimeout);
131 //show empty list to remove results of old search
132 emit showWordList(searchResult);
135 //find translations of the same key word
136 QMultiHash<QString, Translation*>::iterator i;
137 for(i = res.begin(); i != res.end(); i++) {
138 searchResult[i.key()].push_back(i.value());
143 emit showWordList(searchResult);
147 bool foundExactMatch = false;
148 QHash<QString, QList<Translation*> >::iterator j;
149 for(j = searchResult.begin(); j != searchResult.end(); j++) {
150 if(j.key() == searchString && !foundExactMatch) {
151 foundExactMatch = true;
152 emit searchTranslations(j.value());
157 if(!foundExactMatch) {
159 QMaemo5InformationBox::information(this,
160 tr("Can't find exactly matching word"),
161 QMaemo5InformationBox::DefaultTimeout);
164 emit showWordList(searchResult);
169 setExactSearch(false);
172 void MainWindow::translationsReady() {
173 emit showTranslation(backbone->htmls());
176 QList<CommonDictInterface*> MainWindow::getPlugins() {
177 return backbone->getPlugins();
180 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
181 return backbone->getDictionaries();
184 void MainWindow::searchExact(QString word) {
185 setExactSearch(true);
186 //searching with searchBar, not directly by emiting searchWordList(),
187 //because it will set search word in searchBar's edit line
188 //this function is only used by history and when searching from attributes
189 searchBarWidget->search(word);
194 void MainWindow::breakSearching() {
195 //make sure to unset exact search mode
196 setExactSearch(false);
199 void MainWindow::addToHistory(QList<Translation *> trans) {
200 if(trans.count() > 0) {
201 backbone->history()->add(trans[0]->key());
202 translationWidget->setWindowTitle(trans[0]->key());
206 void MainWindow::historyNext() {
207 if(backbone->history()->nextAvailable()) {
208 QString next = backbone->history()->next();
209 searchBarWidget->searchDelay(next);
213 void MainWindow::historyPrev() {
214 if(backbone->history()->prevAvailable()) {
215 QString prev = backbone->history()->previous();
216 searchBarWidget->searchDelay(prev);
220 void MainWindow::disableMenu() {
222 if(ui->menuBar->actions().contains(menuWidget)) {
223 ui->menuBar->removeAction(menuWidget);
226 ui->menuBar->setEnabled(false);
230 void MainWindow::enableMenu() {
232 if(!ui->menuBar->actions().contains(menuWidget)) {
233 ui->menuBar->addAction(menuWidget);
236 ui->menuBar->setEnabled(true);
240 void MainWindow::showHistory() {
241 HistoryListDialog historyDialog(backbone->history()->list(), this);
242 if(historyDialog.exec() == QDialog::Accepted) {
243 backbone->history()->setCurrentElement(historyDialog.selectedRow());
244 searchExact(historyDialog.selectedWord());
248 void MainWindow::connectBackbone() {
249 connect(this, SIGNAL(quit()),
250 backbone, SLOT(quit()));
252 connect(this, SIGNAL(searchWordList(QString)),
253 backbone, SLOT(search(QString)));
255 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
256 backbone, SLOT(searchHtml(QList<Translation*>)));
258 connect(this, SIGNAL(stopSearching()),
259 backbone, SLOT(stopSearching()));
261 connect(this, SIGNAL(stopSearching()),
262 this, SLOT(breakSearching()));
264 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
265 backbone, SLOT(addDictionary(CommonDictInterface*)));
267 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
268 backbone, SLOT(removeDictionary(CommonDictInterface*)));
270 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
271 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
274 connect(backbone, SIGNAL(ready()),
275 this, SIGNAL(setIdle()));
277 connect(backbone, SIGNAL(htmlReady()),
278 this, SIGNAL(setIdle()));
281 connect(backbone, SIGNAL(ready()),
282 this, SLOT(wordListReady()));
284 connect(backbone, SIGNAL(htmlReady()),
285 this, SLOT(translationsReady()));
287 connect(backbone, SIGNAL(searchCanceled()),
288 this, SIGNAL(setIdle()));
293 connect(this, SIGNAL(searchWordList(QString)),
294 this, SIGNAL(setBusy()));
296 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
297 this, SIGNAL(setBusy()));
299 connect(this, SIGNAL(stopSearching()),
300 this, SIGNAL(setIdle()));
302 connect(this, SIGNAL(searchWordList(QString)),
303 this, SLOT(setSearchString(QString)));
305 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
306 this, SLOT(addToHistory(QList<Translation*>)));
311 void MainWindow::connectSearchBar() {
312 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
313 this, SIGNAL(searchWordList(QString)));
315 connect(searchBarWidget, SIGNAL(stopSearching()),
316 this, SIGNAL(stopSearching()));
318 connect(this, SIGNAL(setBusy()),
319 searchBarWidget, SLOT(setBusy()));
321 connect(this, SIGNAL(setIdle()),
322 searchBarWidget, SLOT(setIdle()));
324 connect(searchBarWidget, SIGNAL(historyNext()),
325 this, SLOT(historyNext()));
327 connect(searchBarWidget, SIGNAL(historyPrev()),
328 this, SLOT(historyPrev()));
330 connect(searchBarWidget, SIGNAL(historyShow()),
331 this, SLOT(showHistory()));
333 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
334 backbone->history(), SLOT(refreshStatus()));
336 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
337 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
340 void MainWindow::connectWordList() {
342 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
344 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
346 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
347 this, SIGNAL(searchTranslations(QList<Translation*>)));
350 connect(this, SIGNAL(setBusy()),
351 wordListWidget, SLOT(lockList()));
353 connect(this, SIGNAL(setIdle()),
354 wordListWidget, SLOT(unlockList()));
357 void MainWindow::connectTranslationWidget() {
358 connect(this, SIGNAL(showTranslation(QStringList)),
359 translationWidget, SLOT(show(QStringList)));
363 void MainWindow::connectDictManager() {
364 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
365 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
367 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
368 this, SIGNAL(removeDictionary(CommonDictInterface*)));
370 connect(dictManagerWidget,
371 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
372 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
375 void MainWindow::connectMenu() {
376 connect(this, SIGNAL(setBusy()),
377 this, SLOT(disableMenu()));
379 connect(this, SIGNAL(setIdle()),
380 this, SLOT(enableMenu()));