Added welcome screen
[mdictionary] / trunk / src / base / gui / MainWindow.cpp
index 146b5d8..a13f93c 100644 (file)
 
 #include "MainWindow.h"
 #include "ui_MainWindow.h"
-#include "DictManagerWidget.h"
+#include <QtGui>
+#ifdef Q_WS_MAEMO_5
+    #include <QMaemo5InformationBox>
+#endif
+
 
 MainWindow::MainWindow(Backbone *backbone, QWidget *parent):
-    QMainWindow(parent),
+    GUIInterface(parent),
     ui(new Ui::MainWindow) {
 
+    #ifdef Q_WS_MAEMO_5
+        setAttribute(Qt::WA_Maemo5StackedWindow);
+    #endif
+
     this->backbone = backbone;
 
+    initializeUI();
+
+    connectBackbone();
+    connectSearchBar();
+    connectWordList();
+    connectTranslationWidget();
+    connectDictManager();
+    connectMenu();
+    connectBookmarksWidget();
+
+
+    setExactSearch(false);
+
+    setWindowTitle("mDictionary");
+
+    showMaximized();
+}
+
+MainWindow::~MainWindow() {
+    delete ui;
+}
+
+
+void MainWindow::initializeUI() {
     ui->setupUi(this);
 
+    //showFullScreen();
+    //sets attribute to maemo's stacked window
+
+
+
+    searchBarWidget = new SearchBarWidget;
+
+    wordListWidget = new WordListWidget;
+
+    //translationWidget is antoher stacked window, so we don't add it to layout
+    //only create it with this widget as parent
+    translationWidget = new TranslationWidget(this);
+
+    welcomeScreenWidget = new WelcomeScreenWidget;
+
     #ifdef Q_WS_MAEMO_5
-        setAttribute(Qt::WA_Maemo5StackedWindow);
+        ui->centralWidget->layout()->addWidget(welcomeScreenWidget);
+    #else
+        translationWidget->hide();
+        splitter = new QSplitter(Qt::Horizontal);
+        splitter->addWidget(wordListWidget);
+        splitter->addWidget(welcomeScreenWidget);
+        splitter->setStretchFactor(1, 150);
+        ui->centralWidget->layout()->addWidget(splitter);
     #endif
 
-    searchBarWidget = new SearchBarWidget(backbone);
-    wordListWidget = new WordListWidget(backbone);
-    translationWidget = new TranslationWidget(backbone, this);
-    menuWidget = new MenuWidget(this);
+    QVBoxLayout* vl = (QVBoxLayout*)(ui->centralWidget->layout());
+    vl->addWidget(searchBarWidget, 0, Qt::AlignBottom);
+
+    dictManagerWidget = new DictManagerWidget(this);
+    dictManagerWidget->hide();
 
+    settingsWidget = new SettingsWidget(this);
+    settingsWidget->hide();
 
-    menuWidget->addSubMenu(tr("Dictionaries"),
-                           new DictManagerWidget(backbone, this));
-    menuWidget->addSubMenu(tr("Settings"), new QPushButton("Settings"));
-    menuWidget->addSubMenu(tr("About"), new QPushButton("About"));
+    bookmarksWidget = new BookmarksWidget(this);
+    bookmarksWidget->hide();
 
-    ui->menuBar->addAction(menuWidget);
+    menu = new QMenu(this);
+    #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"), new QPushButton("About"));
+        menu->addAction(menuWidget);
+        ui->menuBar->addMenu(menu);
+    #else
+        dictionariesAction = ui->menuBar->addAction(tr("Dictionaries"));
+        connect(dictionariesAction, SIGNAL(triggered()),
+                dictManagerWidget, SLOT(show()));
 
+        settingsAction = ui->menuBar->addAction(tr("Settings"));
+        connect(settingsAction, SIGNAL(triggered()),
+                settingsWidget, SLOT(show()));
 
-    connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
-            translationWidget, SLOT(show(QList<Translation*>)));
+        bookmarksAction = ui->menuBar->addAction(tr("Bookmarks"));
+        connect(bookmarksAction, SIGNAL(triggered()),
+                bookmarksWidget, SLOT(show()));
+    #endif
+
+}
+
+void MainWindow::closeEvent(QCloseEvent *event) {
+    //reqest to stop all searches and close app
+        emit quit();
+        event->accept();
+}
+
+bool MainWindow::exactSearch() {
+    return _exactSearch;
+}
+
+void MainWindow::setExactSearch(bool exact) {
+    _exactSearch = exact;
+}
+
+void MainWindow::setSearchString(QString word) {
+    searchString = word;
+}
+
+void MainWindow::wordListReady() {
+    //gets results from backbone
+    QMultiHash<QString, Translation*> res = backbone->result();
+    QHash<QString, QList<Translation*> > searchResult;
+
+    #ifdef Q_WS_MAEMO_5
+    if(!wordListWidget->isVisible()) {
+        int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
+        QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
+        l->removeWidget(welcomeScreenWidget);
+        l->insertWidget(0, wordListWidget);
+        qDebug()<<"changed";
+    }
+    #endif
+
+    //if nothing was found
+    if(res.count() == 0) {
+        #ifdef Q_WS_MAEMO_5
+        QMaemo5InformationBox::information(this,
+                            tr("Can't find any matching words"),
+                            QMaemo5InformationBox::DefaultTimeout);
+        #endif
+        //show empty list to remove results of old search
+        emit showWordList(searchResult);
+    }
+    else {
+        //find translations of the same key word
+        QMultiHash<QString, Translation*>::iterator i;
+        for(i = res.begin(); i != res.end(); i++) {
+            searchResult[i.key()].push_back(i.value());
+        }
+
+
+        if(!exactSearch()) {
+            emit showWordList(searchResult);
+        }
+        else {
+            #ifndef Q_WS_MAEMO_5
+                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;
+                    emit searchTranslations(j.value());
+                    break;
+                }
+            }
+
+            if(!foundExactMatch) {
+                #ifdef Q_WS_MAEMO_5
+                QMaemo5InformationBox::information(this,
+                                    tr("Can't find exactly matching word"),
+                                    QMaemo5InformationBox::DefaultTimeout);
+                #endif
+
+                emit showWordList(searchResult);
+            }
+
+        }
+    }
+    setExactSearch(false);
+}
+
+void MainWindow::translationsReady() {
+    #ifndef Q_WS_MAEMO_5
+    if(!translationWidget->isVisible()) {
+        int i = ui->centralWidget->layout()->indexOf(welcomeScreenWidget);
+        QBoxLayout* l = (QBoxLayout*)(ui->centralWidget->layout());
+        QSplitter* s = (QSplitter*)((QWidgetItem*)(l->itemAt(0))->widget());
+        s->insertWidget(1,translationWidget);
+        s->setStretchFactor(1, 150);
+        welcomeScreenWidget->deleteLater();
+        qDebug()<<"changed";
+    }
+    #endif
+
+    emit showTranslation(backbone->htmls());
+}
+
+QList<CommonDictInterface*> MainWindow::getPlugins() {
+    return backbone->getPlugins();
+}
+
+QHash<CommonDictInterface*, bool> MainWindow::getDictionaries() {
+    return backbone->getDictionaries();
+}
+
+void MainWindow::searchExact(QString word) {
+    setExactSearch(true);
+    //searching with searchBar, not directly by emiting 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::breakSearching() {
+    //make sure to unset exact search mode
+    setExactSearch(false);
+}
+
+void MainWindow::addToHistory(QList<Translation *> trans) {
+    if(trans.count() > 0) {
+        backbone->history()->add(trans[0]->key());
+        translationWidget->setWindowTitle(trans[0]->key());
+    }
+}
+
+void MainWindow::historyNext() {
+    if(backbone->history()->nextAvailable()) {
+        QString next = backbone->history()->next();
+        #ifndef Q_WS_MAEMO_5
+            setExactSearch(true);
+        #endif
+        searchBarWidget->searchDelay(next);
+    }
+}
 
-    ui->centralWidget->layout()->addWidget(wordListWidget);
-    ui->centralWidget->layout()->addWidget(searchBarWidget);
+void MainWindow::historyPrev() {
+    if(backbone->history()->prevAvailable()) {
+        #ifndef Q_WS_MAEMO_5
+            setExactSearch(true);
+        #endif
+        QString prev = backbone->history()->previous();
+        searchBarWidget->searchDelay(prev);
+    }
+}
+
+void MainWindow::disableMenu() {
+    #ifdef Q_WS_MAEMO_5
+        if(ui->menuBar->actions().contains(menuWidget)) {
+              ui->menuBar->removeAction(menuWidget);
+        }
+    #else
+        ui->menuBar->setEnabled(false);
+    #endif
+}
+
+void MainWindow::enableMenu() {
+    #ifdef Q_WS_MAEMO_5
+        if(!ui->menuBar->actions().contains(menuWidget)) {
+            ui->menuBar->addAction(menuWidget);
+        }
+    #else
+        ui->menuBar->setEnabled(true);
+    #endif
+}
 
+void MainWindow::showHistory() {
+    HistoryListDialog historyDialog(backbone->history()->list(), this);
+    if(historyDialog.exec() == QDialog::Accepted) {
+        backbone->history()->setCurrentElement(historyDialog.selectedRow());
+        searchExact(historyDialog.selectedWord());
+    }
+}
+
+void MainWindow::setSettings(Settings *s) {
+    backbone->setSettings(s);
+}
+
+Settings* MainWindow::settings() {
+    return backbone->settings();
+}
 
+void MainWindow::connectBackbone() {
     connect(this, SIGNAL(quit()),
             backbone, SLOT(quit()));
 
-    connect(backbone, SIGNAL(closeOk()),
-            this, SLOT(closeOk()));
+    connect(this, SIGNAL(searchWordList(QString)),
+            backbone, SLOT(search(QString)));
+
+    connect(this, SIGNAL(searchTranslations(QList<Translation*>)),
+            backbone, SLOT(searchHtml(QList<Translation*>)));
+
+    connect(this, SIGNAL(stopSearching()),
+            backbone, SLOT(stopSearching()));
+
+    connect(this, SIGNAL(stopSearching()),
+            this, SLOT(breakSearching()));
+
+    connect(this, SIGNAL(addNewDictionary(CommonDictInterface*)),
+            backbone, SLOT(addDictionary(CommonDictInterface*)));
+
+    connect(this, SIGNAL(removeDictionary(CommonDictInterface*)),
+            backbone, SLOT(removeDictionary(CommonDictInterface*)));
+
+    connect(this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
+            backbone, SLOT(selectedDictionaries(QList<CommonDictInterface*>)));
+
+
+    connect(backbone, SIGNAL(ready()),
+            this, SIGNAL(setIdle()));
+
+    connect(backbone, SIGNAL(htmlReady()),
+            this, SIGNAL(setIdle()));
+
+
+    connect(backbone, SIGNAL(ready()),
+            this, SLOT(wordListReady()));
+
+    connect(backbone, SIGNAL(htmlReady()),
+            this, SLOT(translationsReady()));
+
+    connect(backbone, SIGNAL(searchCanceled()),
+            this, SIGNAL(setIdle()));
+
+
+
+
+    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*>)));
 
-    closingApplication = false;
 
-    setWindowTitle("mDictionary");
 }
 
-MainWindow::~MainWindow() {
-    delete ui;
+void MainWindow::connectSearchBar() {
+    connect(searchBarWidget, SIGNAL(searchForTranslations(QString)),
+            this, SIGNAL(searchWordList(QString)));
+
+    connect(searchBarWidget, SIGNAL(stopSearching()),
+            this, SIGNAL(stopSearching()));
+
+    connect(this, SIGNAL(setBusy()),
+            searchBarWidget, SLOT(setBusy()));
+
+    connect(this, SIGNAL(setIdle()),
+            searchBarWidget, SLOT(setIdle()));
+
+    connect(searchBarWidget, SIGNAL(historyNext()),
+            this, SLOT(historyNext()));
+
+    connect(searchBarWidget, SIGNAL(historyPrev()),
+            this, SLOT(historyPrev()));
+
+    connect(searchBarWidget, SIGNAL(historyShow()),
+            this, SLOT(showHistory()));
+
+    connect(searchBarWidget, SIGNAL(refreshHistoryButtons()),
+            backbone->history(), SLOT(refreshStatus()));
+
+    connect(backbone->history(), SIGNAL(historyChanged(bool,bool,bool)),
+            searchBarWidget, SLOT(updateHistoryButtons(bool,bool,bool)));
 }
 
+void MainWindow::connectWordList() {
+    connect(this,
+            SIGNAL(showWordList(QHash<QString, QList<Translation*> >)),
+            wordListWidget,
+            SLOT(showSearchResults(QHash<QString,QList<Translation*> >)));
 
-void MainWindow::closeEvent(QCloseEvent *event) {
-        emit quit();
-        event->accept();
+    connect(wordListWidget, SIGNAL(showTranslation(QList<Translation*>)),
+            this, SIGNAL(searchTranslations(QList<Translation*>)));
+
+
+
+
+    connect(this, SIGNAL(setBusy()),
+            wordListWidget, SLOT(lockList()));
+
+    connect(this, SIGNAL(setIdle()),
+            wordListWidget, SLOT(unlockList()));
+
+    connect(wordListWidget, SIGNAL(addBookmark(QList<Translation*>)),
+            backbone, SLOT(addBookmark(QList<Translation*>)));
+
+    connect(wordListWidget, SIGNAL(removeBookmark(QList<Translation*>)),
+            backbone, SLOT(removeBookmark(QList<Translation*>)));
+}
+
+void MainWindow::connectTranslationWidget() {
+    connect(this, SIGNAL(showTranslation(QStringList)),
+            translationWidget, SLOT(show(QStringList)));
+
+}
+
+void MainWindow::connectDictManager() {
+    connect(dictManagerWidget, SIGNAL(addDictionary(CommonDictInterface*)),
+            this, SIGNAL(addNewDictionary(CommonDictInterface*)));
+
+    connect(dictManagerWidget, SIGNAL(removeDictionary(CommonDictInterface*)),
+            this, SIGNAL(removeDictionary(CommonDictInterface*)));
+
+    connect(dictManagerWidget,
+            SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)),
+            this, SIGNAL(selectedDictionaries(QList<CommonDictInterface*>)));
 }
 
-void MainWindow::closeOk() {
-    /*QMessageBox::warning(this, "", "close");
-    closingApplication = true;
-    close();*/
+void MainWindow::connectMenu() {
+    connect(this, SIGNAL(setBusy()),
+            this, SLOT(disableMenu()));
+
+    connect(this, SIGNAL(setIdle()),
+            this, SLOT(enableMenu()));
+}
+
+
+void MainWindow::showAllBookmarks() {
+    qDebug()<<"asdas";
+}
+
+void MainWindow::connectBookmarksWidget() {
+    connect(bookmarksWidget, SIGNAL(removeAllBookmarks()),
+            backbone, SLOT(removeAllBookmark()));
+
+    connect(bookmarksWidget, SIGNAL(showAllBookmarks()),
+            menu, SLOT(hide()));
+
+    connect(menu, SIGNAL(aboutToHide()),
+            this, SLOT(showAllBookmarks()));
 }