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);
72 ui->centralWidget->layout()->addWidget(wordListWidget);
73 ui->centralWidget->layout()->addWidget(searchBarWidget);
77 dictManagerWidget = new DictManagerWidget(this);
79 menuWidget = new MenuWidget(this);
80 menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
81 menuWidget->addSubMenu(tr("Settings"), new QPushButton("Settings"));
82 menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
84 ui->menuBar->addAction(menuWidget);
87 void MainWindow::closeEvent(QCloseEvent *event) {
88 //reqest to stop all searches and close app
93 bool MainWindow::exactSearch() {
97 void MainWindow::setExactSearch(bool exact) {
101 void MainWindow::setSearchString(QString word) {
105 void MainWindow::wordListReady() {
106 //gets results from backbone
107 QMultiHash<QString, Translation*> res = backbone->result();
108 QHash<QString, QList<Translation*> > searchResult;
110 //if nothing was found
111 if(res.count() == 0) {
113 QMaemo5InformationBox::information(this,
114 tr("Can't find any matching words"),
115 QMaemo5InformationBox::DefaultTimeout);
117 //show empty list to remove results of old search
118 emit showWordList(searchResult);
121 //find translations of the same key word
122 QMultiHash<QString, Translation*>::iterator i;
123 for(i = res.begin(); i != res.end(); i++) {
124 searchResult[i.key()].push_back(i.value());
129 emit showWordList(searchResult);
133 bool foundExactMatch = false;
134 QHash<QString, QList<Translation*> >::iterator j;
135 for(j = searchResult.begin(); j != searchResult.end(); j++) {
136 if(j.key() == searchString && !foundExactMatch) {
137 foundExactMatch = true;
138 emit searchTranslations(j.value());
143 if(!foundExactMatch) {
145 QMaemo5InformationBox::information(this,
146 tr("Can't find exactly matching word"),
147 QMaemo5InformationBox::DefaultTimeout);
150 emit showWordList(searchResult);
155 setExactSearch(false);
158 void MainWindow::translationsReady() {
159 emit showTranslation(backbone->htmls());
162 QList<CommonDictInterface*> MainWindow::getPlugins() {
163 return backbone->getPlugins();
166 QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
167 return backbone->getDictionaries();
170 void MainWindow::searchExact(QString word) {
171 setExactSearch(true);
172 //searching with searchBar, not directly by emiting searchWordList(),
173 //because it will set search word in searchBar's edit line
174 //this function is only used by history and when searching from attributes
175 searchBarWidget->search(word);
180 void MainWindow::breakSearching() {
181 //make sure to unset exact search mode
182 setExactSearch(false);
185 void MainWindow::addToHistory(QList<Translation *> trans) {
186 if(trans.count() > 0) {
187 backbone->history()->add(trans[0]->key());
188 translationWidget->setWindowTitle(trans[0]->key());
192 void MainWindow::historyNext() {
193 if(backbone->history()->nextAvailable()) {
194 QString next = backbone->history()->next();
195 searchBarWidget->searchDelay(next);
199 void MainWindow::historyPrev() {
200 if(backbone->history()->prevAvailable()) {
201 QString prev = backbone->history()->previous();
202 searchBarWidget->searchDelay(prev);
206 void MainWindow::disableMenu() {
207 if(ui->menuBar->actions().contains(menuWidget)) {
208 ui->menuBar->removeAction(menuWidget);
212 void MainWindow::enableMenu() {
213 if(!ui->menuBar->actions().contains(menuWidget)) {
214 ui->menuBar->addAction(menuWidget);
218 void MainWindow::showHistory() {
219 HistoryListDialog historyDialog(backbone->history()->list(), this);
220 if(historyDialog.exec() == QDialog::Accepted) {
221 backbone->history()->setCurrentElement(historyDialog.selectedRow());
222 searchExact(historyDialog.selectedWord());
226 void MainWindow::connectBackbone() {
227 connect(this, SIGNAL(quit()),
228 backbone, SLOT(quit()));
230 connect(this, SIGNAL(searchWordList(QString)),
231 backbone, SLOT(search(QString)));
233 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
234 backbone, SLOT(searchHtml(QList<Translation*>)));
236 connect(this, SIGNAL(stopSearching()),
237 backbone, SLOT(stopSearching()));
239 connect(this, SIGNAL(stopSearching()),
240 this, SLOT(breakSearching()));
242 connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
243 backbone, SLOT(addDictionary(CommonDictInterface*)));
245 connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
246 backbone, SLOT(removeDictionary(CommonDictInterface*)));
248 connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
249 backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
252 connect(backbone, SIGNAL(ready()),
253 this, SIGNAL(setIdle()));
255 connect(backbone, SIGNAL(htmlReady()),
256 this, SIGNAL(setIdle()));
259 connect(backbone, SIGNAL(ready()),
260 this, SLOT(wordListReady()));
262 connect(backbone, SIGNAL(htmlReady()),
263 this, SLOT(translationsReady()));
265 connect(backbone, SIGNAL(searchCanceled()),
266 this, SIGNAL(setIdle()));
271 connect(this, SIGNAL(searchWordList(QString)),
272 this, SIGNAL(setBusy()));
274 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
275 this, SIGNAL(setBusy()));
277 connect(this, SIGNAL(stopSearching()),
278 this, SIGNAL(setIdle()));
280 connect(this, SIGNAL(searchWordList(QString)),
281 this, SLOT(setSearchString(QString)));
283 connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
284 this, SLOT(addToHistory(QList<Translation*>)));
289 void MainWindow::connectSearchBar() {
290 connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
291 this, SIGNAL(searchWordList(QString)));
293 connect(searchBarWidget, SIGNAL(stopSearching()),
294 this, SIGNAL(stopSearching()));
296 connect(this, SIGNAL(setBusy()),
297 searchBarWidget, SLOT(setBusy()));
299 connect(this, SIGNAL(setIdle()),
300 searchBarWidget, SLOT(setIdle()));
302 connect(searchBarWidget, SIGNAL(historyNext()),
303 this, SLOT(historyNext()));
305 connect(searchBarWidget, SIGNAL(historyPrev()),
306 this, SLOT(historyPrev()));
308 connect(searchBarWidget, SIGNAL(historyShow()),
309 this, SLOT(showHistory()));
311 connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
312 backbone->history(), SLOT(refreshStatus()));
314 connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
315 searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
318 void MainWindow::connectWordList() {
320 SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
322 SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
324 connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
325 this, SIGNAL(searchTranslations(QList<Translation*>)));
328 connect(this, SIGNAL(setBusy()),
329 wordListWidget, SLOT(lockList()));
331 connect(this, SIGNAL(setIdle()),
332 wordListWidget, SLOT(unlockList()));
335 void MainWindow::connectTranslationWidget() {
336 connect(this, SIGNAL(showTranslation(QStringList)),
337 translationWidget, SLOT(show(QStringList)));
341 void MainWindow::connectDictManager() {
342 connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
343 this, SIGNAL(addNewDictionary(CommonDictInterface*)));
345 connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
346 this, SIGNAL(removeDictionary(CommonDictInterface*)));
348 connect(dictManagerWidget,
349 SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
350 this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
353 void MainWindow::connectMenu() {
354 connect(this, SIGNAL(setBusy()),
355 this, SLOT(disableMenu()));
357 connect(this, SIGNAL(setIdle()),
358 this, SLOT(enableMenu()));