Merge branch 'master' of ssh://drop.maemo.org/git/mdictionary
[mdictionary] / trunk / src / base / gui / MainWindow.cpp
index 29b16e5..b706572 100644 (file)
@@ -19,7 +19,8 @@
 
 *******************************************************************************/
 
-//Created by Mateusz Półrola
+//! \file MainWindow.cpp
+//! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
 
 #include "MainWindow.h"
 #include <QtGui>
@@ -46,8 +47,6 @@ MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
 
     setExactSearch(false);
 
-    setWindowTitle("mDictionary");
-
     showMaximized();
 }
 
@@ -62,27 +61,41 @@ void MainWindow::initializeUI() {
         setAttribute(Qt::WA_Maemo5StackedWindow);
     #endif
 
-    //translationWidget is another stacked window, so we don't add it to layout
-    //only create it with this widget as parent
-    translationWidget = new TranslationWidget(this);
 
+    setWindowIcon(QIcon(":/icons/64x64/mdictionary.png"));
+    setWindowTitle("mDictionary");
+
+    /*translationWidget is another stacked window, so we don't add it to
+      layout only create it with this widget as parent
+      it must be created as first object in main window, otherwise sometimes
+      when app starts in maemo, when trying to set stacked window attribure
+      it segfaults*/
+    translationWidget = new TranslationWidget(this);
 
     mainLayout = new QVBoxLayout();
     QWidget* w = new QWidget();
     w->setLayout(mainLayout);
     setCentralWidget(w);
+
     menuBar = new QMenuBar();
     setMenuBar(menuBar);
 
+    initializeSearchWidgets();
+
+    initializeMenu();
+
+    notifyManager = new NotifyManager(this);
+}
+
+void MainWindow::initializeSearchWidgets() {
     searchBarWidget = new SearchBarWidget();
 
     wordListWidget = new WordListWidget();
 
     welcomeScreenWidget = new WelcomeScreenWidget();
 
-
     #ifdef Q_WS_MAEMO_5
-    //At start we set widget as welcome screen widget
+        //At start we set widget as welcome screen widget
         mainLayout->addWidget(welcomeScreenWidget);
         mainLayout->addWidget(searchBarWidget, 0, Qt::AlignBottom);
     #else
@@ -92,11 +105,48 @@ void MainWindow::initializeUI() {
         splitter->addWidget(wordListWidget);
         splitter->addWidget(welcomeScreenWidget);
         splitter->setStretchFactor(1, 150);
+
         mainLayout->addWidget(splitter);
         mainLayout->addWidget(searchBarWidget);
     #endif
+}
 
+void MainWindow::initializeMenu() {
+    initializeMenuWidgets();
 
+#ifdef Q_WS_MAEMO_5
+    menuWidget = new MenuWidget(this);
+
+    menuWidget->addSubMenu(tr("Settings"), settingsWidget);
+    menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
+    menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
+    menuWidget->addSubMenu(tr("About"), aboutWidget);
+
+    menuBar->addAction(menuWidget);
+#else
+    dictionariesAction = menuBar->addAction(tr("Dictionaries"));
+    connect(dictionariesAction, SIGNAL(triggered()),
+            dictManagerWidget, SLOT(show()));
+
+    settingsAction = menuBar->addAction(tr("Settings"));
+    connect(settingsAction, SIGNAL(triggered()),
+            settingsWidget, SLOT(show()));
+
+    QMenu* m = menuBar->addMenu(tr("Bookmarks"));
+    bookmarksShowAllAction = new QAction(tr("Show all"), m);
+
+    bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
+
+    m->addAction(bookmarksShowAllAction);
+    m->addAction(bookmarksRemoveAllAction);
+
+    aboutAction = menuBar->addAction(tr("About"));
+    connect(aboutAction, SIGNAL(triggered()),
+            aboutWidget, SLOT(show()));
+#endif
+}
+
+void MainWindow::initializeMenuWidgets() {
     dictManagerWidget = new DictManagerWidget(this);
     dictManagerWidget->hide();
 
@@ -108,46 +158,15 @@ void MainWindow::initializeUI() {
 
     aboutWidget = new AboutWidget(this);
     aboutWidget->hide();
-
-    //creating menus
-
-    #ifdef Q_WS_MAEMO_5
-        menuWidget = new MenuWidget(this);
-        menuWidget->addSubMenu(tr("Settings"), settingsWidget);
-        menuWidget->addSubMenu(tr("Dictionaries"), dictManagerWidget);
-        menuWidget->addSubMenu(tr("Bookmarks"), bookmarksWidget);
-        menuWidget->addSubMenu(tr("About"), aboutWidget);
-        menuBar->addAction(menuWidget);
-    #else
-        dictionariesAction = menuBar->addAction(tr("Dictionaries"));
-        connect(dictionariesAction, SIGNAL(triggered()),
-                dictManagerWidget, SLOT(show()));
-
-        settingsAction = menuBar->addAction(tr("Settings"));
-        connect(settingsAction, SIGNAL(triggered()),
-                settingsWidget, SLOT(show()));
-
-        QMenu* m = menuBar->addMenu(tr("Bookmarks"));
-        bookmarksShowAllAction = new QAction(tr("Show all"), m);
-
-        bookmarksRemoveAllAction = new QAction(tr("Remove all"), m);
-
-        m->addAction(bookmarksShowAllAction);
-        m->addAction(bookmarksRemoveAllAction);
-
-        aboutAction = menuBar->addAction(tr("About"));
-        connect(aboutAction, SIGNAL(triggered()),
-                aboutWidget, SLOT(show()));
-    #endif
 }
 
 void MainWindow::closeEvent(QCloseEvent *event) {
     //request to stop all searches and close app
-        Q_EMIT quit();
-        event->accept();
+    Q_EMIT quit();
+    event->accept();
 }
 
-bool MainWindow::exactSearch() {
+bool MainWindow::isInExactSearch() {
     return _exactSearch;
 }
 
@@ -155,28 +174,21 @@ void MainWindow::setExactSearch(bool exact) {
     _exactSearch = exact;
 }
 
-void MainWindow::setSearchString(QString word) {
+void MainWindow::setExactSearchString(QString word) {
     searchString = word;
 }
 
 void MainWindow::wordListReady() {
     //gets results from backbone
-    QMultiHash<QString, Translation*> res = backbone->result();
+    QMultiHash<QString, Translation*> backboneResult = backbone->result();
     QHash<QString, QList<Translation*> > searchResult;
 
     #ifdef Q_WS_MAEMO_5
-    //switch welcome screen with word list
-    if(!wordListWidget->isVisible()) {
-        int i = mainLayout->indexOf(welcomeScreenWidget);
-        QBoxLayout* l = (QBoxLayout*)(mainLayout);
-        l->removeWidget(welcomeScreenWidget);
-        welcomeScreenWidget->deleteLater();
-        l->insertWidget(0, wordListWidget);
-    }
+        hideWelcomeScreen();
     #endif
 
     //if nothing was found
-    if(res.count() == 0) {
+    if(backboneResult.count() == 0) {
         showNotify(Notify::Info, tr("Can't find any matching words"));
 
         //show empty list to remove results of old search
@@ -185,54 +197,73 @@ void MainWindow::wordListReady() {
     else {
         //find translations of the same key word
         QMultiHash<QString, Translation*>::iterator i;
-        for(i = res.begin(); i != res.end(); i++) {
+        for(i = backboneResult.begin(); i != backboneResult.end(); i++) {
             searchResult[i.key()].push_back(i.value());
         }
 
+        //show search results
+        Q_EMIT showWordList(searchResult);
 
-        if(!exactSearch()) {
-            Q_EMIT showWordList(searchResult);
-        }
-        else {
-            #ifndef Q_WS_MAEMO_5
-            //on desktop we show word list in exact search
-                Q_EMIT showWordList(searchResult);
-            #endif
-            bool foundExactMatch = false;
-            QHash<QString, QList<Translation*> >::iterator j;
-            for(j = searchResult.begin(); j != searchResult.end(); j++) {
-                if(j.key() == searchString && !foundExactMatch) {
-                    foundExactMatch = true;
-                    Q_EMIT searchTranslations(j.value());
-                    break;
-                }
-            }
 
-            if(!foundExactMatch) {
+        if(isInExactSearch()) {
+            QList<Translation*> exactTranslation;
+            if(checkExactSearch(searchResult, exactTranslation)) {
+                qDebug()<<exactTranslation.count();
+                Q_EMIT searchTranslations(exactTranslation);
+            }
+            else {
                 showNotify(Notify::Info,
                            tr("Can't find exactly matching word"));
-
-                Q_EMIT showWordList(searchResult);
             }
 
+            setExactSearch(false);
         }
     }
-    setExactSearch(false);
+}
+
+bool MainWindow::checkExactSearch(
+        QHash<QString, QList<Translation *> > searchResult,
+        QList<Translation *> &found) {
+
+    bool foundExactMatch = false;
+    QHash<QString, QList<Translation*> >::iterator j;
+    for(j = searchResult.begin(); j != searchResult.end(); j++) {
+        if(j.key().toLower() == searchString.toLower()
+            && !foundExactMatch) {
+            found = j.value();
+            return true;
+        }
+    }
+    return false;
 }
 
 void MainWindow::translationsReady() {
     #ifndef Q_WS_MAEMO_5
+        hideWelcomeScreen();
+    #endif
+
+    Q_EMIT showTranslation(backbone->htmls());
+    notifyManager->screenChanged();
+}
+
+
+void MainWindow::hideWelcomeScreen() {
+#ifdef Q_WS_MAEMO_5
+    //switch welcome screen with word list
+    if(!wordListWidget->isVisible()) {
+        mainLayout->removeWidget(welcomeScreenWidget);
+        welcomeScreenWidget->deleteLater();
+
+        mainLayout->insertWidget(0, wordListWidget);
+    }
+#else
     //switch welcome screen with translation widget
     if(!translationWidget->isVisible()) {
-        QBoxLayout* l = (QBoxLayout*)(mainLayout);
-        QSplitter* s = (QSplitter*)((QWidgetItem*)(l->itemAt(0))->widget());
-        s->insertWidget(1,translationWidget);
-        s->setStretchFactor(1, 150);
+        splitter->insertWidget(1,translationWidget);
+        splitter->setStretchFactor(1, 150);
         welcomeScreenWidget->deleteLater();
     }
-    #endif
-
-    Q_EMIT showTranslation(backbone->htmls());
+#endif
 }
 
 QList<CommonDictInterface*> MainWindow::getPlugins() {
@@ -251,15 +282,18 @@ void MainWindow::search(QString word) {
 
 void MainWindow::searchExact(QString word) {
     setExactSearch(true);
-    //searching with searchBar, not directly by emitting searchWordList(),
-    //because it will set search word in searchBar's edit line
-    //this function is only used by history and when searching from attributes
     searchBarWidget->search(word);
 }
 
+void MainWindow::searchDelay(QString word) {
+    searchBarWidget->searchDelay(word);
+}
+
 
 
-void MainWindow::breakSearching() {
+
+
+void MainWindow::searchingInterrupted() {
     //make sure to unset exact search mode
     setExactSearch(false);
 }
@@ -276,7 +310,7 @@ void MainWindow::historyNext() {
         #ifndef Q_WS_MAEMO_5
             setExactSearch(true);
         #endif
-        searchBarWidget->searchDelay(next);
+        searchDelay(next);
     }
 }
 
@@ -286,7 +320,7 @@ void MainWindow::historyPrev() {
             setExactSearch(true);
         #endif
         QString prev = backbone->history()->previous();
-        searchBarWidget->searchDelay(prev);
+        searchDelay(prev);
     }
 }
 
@@ -311,6 +345,7 @@ void MainWindow::enableMenu() {
 }
 
 void MainWindow::showHistory(QPoint p) {
+
     HistoryListDialog historyDialog(backbone->history()->list(), searchBarWidget);
 
     #ifndef Q_WS_MAEMO_5
@@ -338,37 +373,28 @@ Settings* MainWindow::settings() {
 
 
 void MainWindow::showNotify(Notify::NotifyType type, QString text) {
+    notifyManager->showNotify(type, text);
+}
 
-    switch(type) {
-    case Notify::Info:
-        #ifdef Q_WS_MAEMO_5
-            QMaemo5InformationBox::information(0,
-                                text,
-                                QMaemo5InformationBox::DefaultTimeout);
-        #else
-            QMessageBox::information(this, tr("Information"), text);
-        #endif
-        break;
+void MainWindow::connectBackbone() {
+
+    connect(this, SIGNAL(searchWordList(QString)),
+            this, SIGNAL(setBusy()));
+
+    connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
+            this, SIGNAL(setBusy()));
+
+    connect(this, SIGNAL(stopSearching()),
+            this, SIGNAL(setIdle()));
+
+    connect(this, SIGNAL(searchWordList(QString)),
+            this, SLOT(setExactSearchString(QString)));
+
+    connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
+            this, SLOT(addToHistory(QList<Translation*>)));
 
-    case Notify::Warning:
-        #ifndef Q_WS_MAEMO_5
-                QMessageBox::warning(this, tr("Warning"), text);
-                break;
-        #endif
 
-    case Notify::Error:
-        #ifdef Q_WS_MAEMO_5
-            QMaemo5InformationBox::information(0,
-                                text,
-                                QMaemo5InformationBox::NoTimeout);
-        #else
-            QMessageBox::critical(this, tr("Error"), text);
-        #endif
-        break;
-    }
-}
 
-void MainWindow::connectBackbone() {
     connect(this, SIGNAL(quit()),
             backbone, SLOT(quit()));
 
@@ -382,7 +408,7 @@ void MainWindow::connectBackbone() {
             backbone, SLOT(stopSearching()));
 
     connect(this, SIGNAL(stopSearching()),
-            this, SLOT(breakSearching()));
+            this, SLOT(searchingInterrupted()));
 
     connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
             backbone, SLOT(addDictionary(CommonDictInterface*)));
@@ -412,26 +438,6 @@ void MainWindow::connectBackbone() {
 
     connect(backbone, SIGNAL(notify(Notify::NotifyType,QString)),
             this, SLOT(showNotify(Notify::NotifyType,QString)));
-
-
-
-
-    connect(this, SIGNAL(searchWordList(QString)),
-            this, SIGNAL(setBusy()));
-
-    connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
-            this, SIGNAL(setBusy()));
-
-    connect(this, SIGNAL(stopSearching()),
-            this, SIGNAL(setIdle()));
-
-    connect(this, SIGNAL(searchWordList(QString)),
-            this, SLOT(setSearchString(QString)));
-
-    connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
-            this, SLOT(addToHistory(QList<Translation*>)));
-
-
 }
 
 void MainWindow::connectSearchBar() {