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;
39 //sets attribute to maemo's stacked window
41 setAttribute(Qt::WA_Maemo5StackedWindow);
45 searchBarWidget = new SearchBarWidget;
46 wordListWidget = new WordListWidget;
48 //translationWidget is antoher stacked window, so we don't add it to layout
49 //only create it with this widget as parent
50 translationWidget = new TranslationWidget(this);
52 ui->centralWidget->layout()->addWidget(wordListWidget);
53 ui->centralWidget->layout()->addWidget(searchBarWidget);
57 dictManagerWidget = new DictManagerWidget(this);
59 menuWidget = new MenuWidget(this);
60 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
61 menuWidget->addSubMenu(tr("Settings"), new QPushButton("Settings"));
62 menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
64 ui->menuBar->addAction(menuWidget);
69 connectTranslationWidget();
73 setExactSearch(false);
75 setWindowTitle("mDictionary");
78 MainWindow::~MainWindow() {
83 void MainWindow::closeEvent(QCloseEvent *event) {
84 //reqest to stop all searches and close app
89 bool MainWindow::exactSearch() {
93 void MainWindow::setExactSearch(bool exact) {
97 void MainWindow::setSearchString(QString word) {
101 void MainWindow::wordListReady() {
102 //gets results from backbone
103 QMultiHash<QString, Translation*> res = backbone->result();
104 QHash<QString, QList<Translation*> > searchResult;
106 //if nothing was found
107 if(res.count() == 0) {
109 QMaemo5InformationBox::information(this,
110 tr("Can't find any matching words"),
111 QMaemo5InformationBox::DefaultTimeout);
113 //show empty list to remove results of old search
114 emit showWordList(searchResult);
117 //find translations of the same key word
118 QMultiHash<QString, Translation*>::iterator i;
119 for(i = res.begin(); i != res.end(); i++) {
120 searchResult[i.key()].push_back(i.value());
125 emit showWordList(searchResult);
129 bool foundExactMatch = false;
130 QHash<QString, QList<Translation*> >::iterator j;
131 for(j = searchResult.begin(); j != searchResult.end(); j++) {
132 if(j.key() == searchString && !foundExactMatch) {
133 foundExactMatch = true;
134 emit searchTranslations(j.value());
139 if(!foundExactMatch) {
141 QMaemo5InformationBox::information(this,
142 tr("Can't find exactly matching word"),
143 QMaemo5InformationBox::DefaultTimeout);
146 emit showWordList(searchResult);
151 setExactSearch(false);
154 void MainWindow::translationsReady() {
155 emit showTranslation(backbone->htmls());
158 QList<CommonDictInterface*> MainWindow::getPlugins() {
159 return backbone->getPlugins();
162 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
163 return backbone->getDictionaries();
166 void MainWindow::searchExact(QString word) {
167 setExactSearch(true);
168 //searching with searchBar, not directly by emiting searchWordList(),
169 //because it will set search word in searchBar's edit line
170 //this function is only used by history and when searching from attributes
171 searchBarWidget->search(word);
176 void MainWindow::breakSearching() {
177 //make sure to unset exact search mode
178 setExactSearch(false);
181 void MainWindow::addToHistory(QList<Translation *> trans) {
182 if(trans.count() > 0) {
183 backbone->history()->add(trans[0]->key());
184 translationWidget->setWindowTitle(trans[0]->key());
188 void MainWindow::historyNext() {
189 if(backbone->history()->nextAvailable()) {
190 QString next = backbone->history()->next();
191 searchBarWidget->searchDelay(next);
195 void MainWindow::historyPrev() {
196 if(backbone->history()->prevAvailable()) {
197 QString prev = backbone->history()->previous();
198 searchBarWidget->searchDelay(prev);
202 void MainWindow::disableMenu() {
203 if(ui->menuBar->actions().contains(menuWidget)) {
204 ui->menuBar->removeAction(menuWidget);
208 void MainWindow::enableMenu() {
209 if(!ui->menuBar->actions().contains(menuWidget)) {
210 ui->menuBar->addAction(menuWidget);
214 void MainWindow::showHistory() {
215 HistoryListDialog historyDialog(backbone->history()->list(), this);
216 if(historyDialog.exec() == QDialog::Accepted) {
217 backbone->history()->setCurrentElement(historyDialog.selectedRow());
218 searchExact(historyDialog.selectedWord());
222 void MainWindow::connectBackbone() {
223 connect(this, SIGNAL(quit()),
224 backbone, SLOT(quit()));
226 connect(this, SIGNAL(searchWordList(QString)),
227 backbone, SLOT(search(QString)));
229 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
230 backbone, SLOT(searchHtml(QList<Translation*>)));
232 connect(this, SIGNAL(stopSearching()),
233 backbone, SLOT(stopSearching()));
235 connect(this, SIGNAL(stopSearching()),
236 this, SLOT(breakSearching()));
238 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
239 backbone, SLOT(addDictionary(CommonDictInterface*)));
241 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
242 backbone, SLOT(removeDictionary(CommonDictInterface*)));
244 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
245 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
248 connect(backbone, SIGNAL(ready()),
249 this, SIGNAL(setIdle()));
251 connect(backbone, SIGNAL(htmlReady()),
252 this, SIGNAL(setIdle()));
255 connect(backbone, SIGNAL(ready()),
256 this, SLOT(wordListReady()));
258 connect(backbone, SIGNAL(htmlReady()),
259 this, SLOT(translationsReady()));
261 connect(backbone, SIGNAL(searchCanceled()),
262 this, SIGNAL(setIdle()));
267 connect(this, SIGNAL(searchWordList(QString)),
268 this, SIGNAL(setBusy()));
270 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
271 this, SIGNAL(setBusy()));
273 connect(this, SIGNAL(stopSearching()),
274 this, SIGNAL(setIdle()));
276 connect(this, SIGNAL(searchWordList(QString)),
277 this, SLOT(setSearchString(QString)));
279 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
280 this, SLOT(addToHistory(QList<Translation*>)));
285 void MainWindow::connectSearchBar() {
286 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
287 this, SIGNAL(searchWordList(QString)));
289 connect(searchBarWidget, SIGNAL(stopSearching()),
290 this, SIGNAL(stopSearching()));
292 connect(this, SIGNAL(setBusy()),
293 searchBarWidget, SLOT(setBusy()));
295 connect(this, SIGNAL(setIdle()),
296 searchBarWidget, SLOT(setIdle()));
298 connect(searchBarWidget, SIGNAL(historyNext()),
299 this, SLOT(historyNext()));
301 connect(searchBarWidget, SIGNAL(historyPrev()),
302 this, SLOT(historyPrev()));
304 connect(searchBarWidget, SIGNAL(historyShow()),
305 this, SLOT(showHistory()));
307 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
308 backbone->history(), SLOT(refreshStatus()));
310 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
311 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
314 void MainWindow::connectWordList() {
316 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
318 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
320 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
321 this, SIGNAL(searchTranslations(QList<Translation*>)));
324 connect(this, SIGNAL(setBusy()),
325 wordListWidget, SLOT(lockList()));
327 connect(this, SIGNAL(setIdle()),
328 wordListWidget, SLOT(unlockList()));
331 void MainWindow::connectTranslationWidget() {
332 connect(this, SIGNAL(showTranslation(QStringList)),
333 translationWidget, SLOT(show(QStringList)));
337 void MainWindow::connectDictManager() {
338 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
339 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
341 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
342 this, SIGNAL(removeDictionary(CommonDictInterface*)));
344 connect(dictManagerWidget,
345 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
346 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
349 void MainWindow::connectMenu() {
350 connect(this, SIGNAL(setBusy()),
351 this, SLOT(disableMenu()));
353 connect(this, SIGNAL(setIdle()),
354 this, SLOT(enableMenu()));