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");
53 MainWindow::~MainWindow() {
58 void MainWindow::initializeUI() {
61 //sets attribute to maemo's stacked window
63 setAttribute(Qt::WA_Maemo5StackedWindow);
67 searchBarWidget = new SearchBarWidget;
69 wordListWidget = new WordListWidget;
71 //translationWidget is antoher stacked window, so we don't add it to layout
72 //only create it with this widget as parent
73 translationWidget = new TranslationWidget(this);
76 ui->centralWidget->layout()->addWidget(wordListWidget);
78 splitter = new QSplitter(Qt::Horizontal);
79 splitter->addWidget(wordListWidget);
80 splitter->addWidget(translationWidget);
81 splitter->setStretchFactor(1, 150);
82 ui->centralWidget->layout()->addWidget(splitter);
84 ui->centralWidget->layout()->addWidget(searchBarWidget);
88 dictManagerWidget = new DictManagerWidget(this);
89 dictManagerWidget->hide();
91 menuWidget = new MenuWidget(this);
92 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
93 menuWidget->addSubMenu(tr("Settings"), new QPushButton("Settings"));
94 menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
95 ui->menuBar->addAction(menuWidget);
97 dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
98 connect(dictionariesAction, SIGNAL(triggered()),
99 dictManagerWidget, SLOT(show()));
104 void MainWindow::closeEvent(QCloseEvent *event) {
105 //reqest to stop all searches and close app
110 bool MainWindow::exactSearch() {
114 void MainWindow::setExactSearch(bool exact) {
115 _exactSearch = exact;
118 void MainWindow::setSearchString(QString word) {
122 void MainWindow::wordListReady() {
123 //gets results from backbone
124 QMultiHash<QString, Translation*> res = backbone->result();
125 QHash<QString, QList<Translation*> > searchResult;
127 //if nothing was found
128 if(res.count() == 0) {
130 QMaemo5InformationBox::information(this,
131 tr("Can't find any matching words"),
132 QMaemo5InformationBox::DefaultTimeout);
134 //show empty list to remove results of old search
135 emit showWordList(searchResult);
138 //find translations of the same key word
139 QMultiHash<QString, Translation*>::iterator i;
140 for(i = res.begin(); i != res.end(); i++) {
141 searchResult[i.key()].push_back(i.value());
146 emit showWordList(searchResult);
150 bool foundExactMatch = false;
151 QHash<QString, QList<Translation*> >::iterator j;
152 for(j = searchResult.begin(); j != searchResult.end(); j++) {
153 if(j.key() == searchString && !foundExactMatch) {
154 foundExactMatch = true;
155 emit searchTranslations(j.value());
160 if(!foundExactMatch) {
162 QMaemo5InformationBox::information(this,
163 tr("Can't find exactly matching word"),
164 QMaemo5InformationBox::DefaultTimeout);
167 emit showWordList(searchResult);
172 setExactSearch(false);
175 void MainWindow::translationsReady() {
176 emit showTranslation(backbone->htmls());
179 QList<CommonDictInterface*> MainWindow::getPlugins() {
180 return backbone->getPlugins();
183 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
184 return backbone->getDictionaries();
187 void MainWindow::searchExact(QString word) {
188 setExactSearch(true);
189 //searching with searchBar, not directly by emiting searchWordList(),
190 //because it will set search word in searchBar's edit line
191 //this function is only used by history and when searching from attributes
192 searchBarWidget->search(word);
197 void MainWindow::breakSearching() {
198 //make sure to unset exact search mode
199 setExactSearch(false);
202 void MainWindow::addToHistory(QList<Translation *> trans) {
203 if(trans.count() > 0) {
204 backbone->history()->add(trans[0]->key());
205 translationWidget->setWindowTitle(trans[0]->key());
209 void MainWindow::historyNext() {
210 if(backbone->history()->nextAvailable()) {
211 QString next = backbone->history()->next();
212 searchBarWidget->searchDelay(next);
216 void MainWindow::historyPrev() {
217 if(backbone->history()->prevAvailable()) {
218 QString prev = backbone->history()->previous();
219 searchBarWidget->searchDelay(prev);
223 void MainWindow::disableMenu() {
225 if(ui->menuBar->actions().contains(menuWidget)) {
226 ui->menuBar->removeAction(menuWidget);
229 ui->menuBar->setEnabled(false);
233 void MainWindow::enableMenu() {
235 if(!ui->menuBar->actions().contains(menuWidget)) {
236 ui->menuBar->addAction(menuWidget);
239 ui->menuBar->setEnabled(true);
243 void MainWindow::showHistory() {
244 HistoryListDialog historyDialog(backbone->history()->list(), this);
245 if(historyDialog.exec() == QDialog::Accepted) {
246 backbone->history()->setCurrentElement(historyDialog.selectedRow());
247 searchExact(historyDialog.selectedWord());
251 void MainWindow::connectBackbone() {
252 connect(this, SIGNAL(quit()),
253 backbone, SLOT(quit()));
255 connect(this, SIGNAL(searchWordList(QString)),
256 backbone, SLOT(search(QString)));
258 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
259 backbone, SLOT(searchHtml(QList<Translation*>)));
261 connect(this, SIGNAL(stopSearching()),
262 backbone, SLOT(stopSearching()));
264 connect(this, SIGNAL(stopSearching()),
265 this, SLOT(breakSearching()));
267 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
268 backbone, SLOT(addDictionary(CommonDictInterface*)));
270 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
271 backbone, SLOT(removeDictionary(CommonDictInterface*)));
273 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
274 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
277 connect(backbone, SIGNAL(ready()),
278 this, SIGNAL(setIdle()));
280 connect(backbone, SIGNAL(htmlReady()),
281 this, SIGNAL(setIdle()));
284 connect(backbone, SIGNAL(ready()),
285 this, SLOT(wordListReady()));
287 connect(backbone, SIGNAL(htmlReady()),
288 this, SLOT(translationsReady()));
290 connect(backbone, SIGNAL(searchCanceled()),
291 this, SIGNAL(setIdle()));
296 connect(this, SIGNAL(searchWordList(QString)),
297 this, SIGNAL(setBusy()));
299 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
300 this, SIGNAL(setBusy()));
302 connect(this, SIGNAL(stopSearching()),
303 this, SIGNAL(setIdle()));
305 connect(this, SIGNAL(searchWordList(QString)),
306 this, SLOT(setSearchString(QString)));
308 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
309 this, SLOT(addToHistory(QList<Translation*>)));
314 void MainWindow::connectSearchBar() {
315 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
316 this, SIGNAL(searchWordList(QString)));
318 connect(searchBarWidget, SIGNAL(stopSearching()),
319 this, SIGNAL(stopSearching()));
321 connect(this, SIGNAL(setBusy()),
322 searchBarWidget, SLOT(setBusy()));
324 connect(this, SIGNAL(setIdle()),
325 searchBarWidget, SLOT(setIdle()));
327 connect(searchBarWidget, SIGNAL(historyNext()),
328 this, SLOT(historyNext()));
330 connect(searchBarWidget, SIGNAL(historyPrev()),
331 this, SLOT(historyPrev()));
333 connect(searchBarWidget, SIGNAL(historyShow()),
334 this, SLOT(showHistory()));
336 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
337 backbone->history(), SLOT(refreshStatus()));
339 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
340 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
343 void MainWindow::connectWordList() {
345 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
347 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
349 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
350 this, SIGNAL(searchTranslations(QList<Translation*>)));
353 connect(this, SIGNAL(setBusy()),
354 wordListWidget, SLOT(lockList()));
356 connect(this, SIGNAL(setIdle()),
357 wordListWidget, SLOT(unlockList()));
360 void MainWindow::connectTranslationWidget() {
361 connect(this, SIGNAL(showTranslation(QStringList)),
362 translationWidget, SLOT(show(QStringList)));
366 void MainWindow::connectDictManager() {
367 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
368 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
370 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
371 this, SIGNAL(removeDictionary(CommonDictInterface*)));
373 connect(dictManagerWidget,
374 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
375 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
378 void MainWindow::connectMenu() {
379 connect(this, SIGNAL(setBusy()),
380 this, SLOT(disableMenu()));
382 connect(this, SIGNAL(setIdle()),
383 this, SLOT(enableMenu()));