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;
68 wordListWidget = new WordListWidget;
70 //translationWidget is antoher stacked window, so we don't add it to layout
71 //only create it with this widget as parent
72 translationWidget = new TranslationWidget(this);
75 ui->centralWidget->layout()->addWidget(wordListWidget);
77 splitter = new QSplitter(Qt::Horizontal);
78 splitter->addWidget(wordListWidget);
79 splitter->addWidget(translationWidget);
80 splitter->setStretchFactor(1, 150);
81 ui->centralWidget->layout()->addWidget(splitter);
83 ui->centralWidget->layout()->addWidget(searchBarWidget);
87 dictManagerWidget = new DictManagerWidget(this);
88 dictManagerWidget->hide();
90 menuWidget = new MenuWidget(this);
91 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
92 menuWidget->addSubMenu(tr("Settings"), new QPushButton("Settings"));
93 menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
94 ui->menuBar->addAction(menuWidget);
96 dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
97 connect(dictionariesAction, SIGNAL(triggered()),
98 dictManagerWidget, SLOT(show()));
103 void MainWindow::closeEvent(QCloseEvent *event) {
104 //reqest to stop all searches and close app
109 bool MainWindow::exactSearch() {
113 void MainWindow::setExactSearch(bool exact) {
114 _exactSearch = exact;
117 void MainWindow::setSearchString(QString word) {
121 void MainWindow::wordListReady() {
122 //gets results from backbone
123 QMultiHash<QString, Translation*> res = backbone->result();
124 QHash<QString, QList<Translation*> > searchResult;
126 //if nothing was found
127 if(res.count() == 0) {
129 QMaemo5InformationBox::information(this,
130 tr("Can't find any matching words"),
131 QMaemo5InformationBox::DefaultTimeout);
133 //show empty list to remove results of old search
134 emit showWordList(searchResult);
137 //find translations of the same key word
138 QMultiHash<QString, Translation*>::iterator i;
139 for(i = res.begin(); i != res.end(); i++) {
140 searchResult[i.key()].push_back(i.value());
145 emit showWordList(searchResult);
149 bool foundExactMatch = false;
150 QHash<QString, QList<Translation*> >::iterator j;
151 for(j = searchResult.begin(); j != searchResult.end(); j++) {
152 if(j.key() == searchString && !foundExactMatch) {
153 foundExactMatch = true;
154 emit searchTranslations(j.value());
159 if(!foundExactMatch) {
161 QMaemo5InformationBox::information(this,
162 tr("Can't find exactly matching word"),
163 QMaemo5InformationBox::DefaultTimeout);
166 emit showWordList(searchResult);
171 setExactSearch(false);
174 void MainWindow::translationsReady() {
175 emit showTranslation(backbone->htmls());
178 QList<CommonDictInterface*> MainWindow::getPlugins() {
179 return backbone->getPlugins();
182 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
183 return backbone->getDictionaries();
186 void MainWindow::searchExact(QString word) {
187 setExactSearch(true);
188 //searching with searchBar, not directly by emiting searchWordList(),
189 //because it will set search word in searchBar's edit line
190 //this function is only used by history and when searching from attributes
191 searchBarWidget->search(word);
196 void MainWindow::breakSearching() {
197 //make sure to unset exact search mode
198 setExactSearch(false);
201 void MainWindow::addToHistory(QList<Translation *> trans) {
202 if(trans.count() > 0) {
203 backbone->history()->add(trans[0]->key());
204 translationWidget->setWindowTitle(trans[0]->key());
208 void MainWindow::historyNext() {
209 if(backbone->history()->nextAvailable()) {
210 QString next = backbone->history()->next();
211 searchBarWidget->searchDelay(next);
215 void MainWindow::historyPrev() {
216 if(backbone->history()->prevAvailable()) {
217 QString prev = backbone->history()->previous();
218 searchBarWidget->searchDelay(prev);
222 void MainWindow::disableMenu() {
224 if(ui->menuBar->actions().contains(menuWidget)) {
225 ui->menuBar->removeAction(menuWidget);
228 ui->menuBar->setEnabled(false);
232 void MainWindow::enableMenu() {
234 if(!ui->menuBar->actions().contains(menuWidget)) {
235 ui->menuBar->addAction(menuWidget);
238 ui->menuBar->setEnabled(true);
242 void MainWindow::showHistory() {
243 HistoryListDialog historyDialog(backbone->history()->list(), this);
244 if(historyDialog.exec() == QDialog::Accepted) {
245 backbone->history()->setCurrentElement(historyDialog.selectedRow());
246 searchExact(historyDialog.selectedWord());
250 void MainWindow::connectBackbone() {
251 connect(this, SIGNAL(quit()),
252 backbone, SLOT(quit()));
254 connect(this, SIGNAL(searchWordList(QString)),
255 backbone, SLOT(search(QString)));
257 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
258 backbone, SLOT(searchHtml(QList<Translation*>)));
260 connect(this, SIGNAL(stopSearching()),
261 backbone, SLOT(stopSearching()));
263 connect(this, SIGNAL(stopSearching()),
264 this, SLOT(breakSearching()));
266 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
267 backbone, SLOT(addDictionary(CommonDictInterface*)));
269 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
270 backbone, SLOT(removeDictionary(CommonDictInterface*)));
272 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
273 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
276 connect(backbone, SIGNAL(ready()),
277 this, SIGNAL(setIdle()));
279 connect(backbone, SIGNAL(htmlReady()),
280 this, SIGNAL(setIdle()));
283 connect(backbone, SIGNAL(ready()),
284 this, SLOT(wordListReady()));
286 connect(backbone, SIGNAL(htmlReady()),
287 this, SLOT(translationsReady()));
289 connect(backbone, SIGNAL(searchCanceled()),
290 this, SIGNAL(setIdle()));
295 connect(this, SIGNAL(searchWordList(QString)),
296 this, SIGNAL(setBusy()));
298 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
299 this, SIGNAL(setBusy()));
301 connect(this, SIGNAL(stopSearching()),
302 this, SIGNAL(setIdle()));
304 connect(this, SIGNAL(searchWordList(QString)),
305 this, SLOT(setSearchString(QString)));
307 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
308 this, SLOT(addToHistory(QList<Translation*>)));
313 void MainWindow::connectSearchBar() {
314 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
315 this, SIGNAL(searchWordList(QString)));
317 connect(searchBarWidget, SIGNAL(stopSearching()),
318 this, SIGNAL(stopSearching()));
320 connect(this, SIGNAL(setBusy()),
321 searchBarWidget, SLOT(setBusy()));
323 connect(this, SIGNAL(setIdle()),
324 searchBarWidget, SLOT(setIdle()));
326 connect(searchBarWidget, SIGNAL(historyNext()),
327 this, SLOT(historyNext()));
329 connect(searchBarWidget, SIGNAL(historyPrev()),
330 this, SLOT(historyPrev()));
332 connect(searchBarWidget, SIGNAL(historyShow()),
333 this, SLOT(showHistory()));
335 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
336 backbone->history(), SLOT(refreshStatus()));
338 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
339 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
342 void MainWindow::connectWordList() {
344 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
346 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
348 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
349 this, SIGNAL(searchTranslations(QList<Translation*>)));
352 connect(this, SIGNAL(setBusy()),
353 wordListWidget, SLOT(lockList()));
355 connect(this, SIGNAL(setIdle()),
356 wordListWidget, SLOT(unlockList()));
359 void MainWindow::connectTranslationWidget() {
360 connect(this, SIGNAL(showTranslation(QStringList)),
361 translationWidget, SLOT(show(QStringList)));
365 void MainWindow::connectDictManager() {
366 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
367 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
369 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
370 this, SIGNAL(removeDictionary(CommonDictInterface*)));
372 connect(dictManagerWidget,
373 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
374 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
377 void MainWindow::connectMenu() {
378 connect(this, SIGNAL(setBusy()),
379 this, SLOT(disableMenu()));
381 connect(this, SIGNAL(setIdle()),
382 this, SLOT(enableMenu()));