Added history
[mdictionary] / trunk / src / base / gui / SearchBarWidget.cpp
index 94b0045..476ec26 100644 (file)
@@ -25,6 +25,7 @@
 #include "SearchBarWidget.h"
 #include <QDebug>
 #include "../../includes/DictDialog.h"
+#include "HistoryListDialog.h"
 
 
 SearchBarWidget::SearchBarWidget(Backbone* backbone, QWidget *parent) :
@@ -32,6 +33,8 @@ SearchBarWidget::SearchBarWidget(Backbone* backbone, QWidget *parent) :
 
     this->backbone = backbone;
 
+    history = backbone->history();
+
     initializeUI();
 
     setMaximumHeight(150);
@@ -70,7 +73,13 @@ SearchBarWidget::SearchBarWidget(Backbone* backbone, QWidget *parent) :
     connect(backbone, SIGNAL(htmlReady()),
             this, SLOT(setIdle()));
 
+
+    connect(history, SIGNAL(historyChanged(bool,bool,bool)),
+            this, SLOT(updateHistoryButtons(bool,bool,bool)));
+
     searchWordLineEdit->setFocus();
+
+    setEnabled(true);
 }
 
 SearchBarWidget::~SearchBarWidget() {
@@ -162,7 +171,6 @@ void SearchBarWidget::initializeUI() {
                               Qt::AlignRight | Qt::AlignVCenter);
 
     verticalLayout->addLayout(horizontalLayout);
-
 }
 
 
@@ -181,15 +189,19 @@ void SearchBarWidget::search(QString word) {
     if(!_isSearching && !word.isEmpty()) {
         searchWordLineEdit->setText(word);
         setBusy();
+        history->add(word);
         emit searchForTranslations(word);
     }
 }
 
 void SearchBarWidget::setEnabled(bool enabled) {
     searchWordLineEdit->setEnabled(enabled);
-    historyNextToolButton->setEnabled(enabled);
-    historyPrevToolButton->setEnabled(enabled);
-    historyShowToolButton->setEnabled(enabled);
+
+    if(enabled) {
+        historyNextToolButton->setEnabled(history->nextAvailable());
+        historyPrevToolButton->setEnabled(history->prevAvailable());
+        historyShowToolButton->setEnabled(history->listAvailable());
+    }
 }
 
 void SearchBarWidget::setBusy() {
@@ -211,25 +223,37 @@ void SearchBarWidget::setIdle() {
 }
 
 void SearchBarWidget::historyNextToolButtonClicked() {
-
+    QString next = history->next();
+    if(!next.isEmpty()) {
+        searchWordLineEdit->setText(next);
+    }
 }
 
 void SearchBarWidget::historyPrevToolButtonClicked() {
-
+    QString prev = history->previous();
+    if(!prev.isEmpty()) {
+        searchWordLineEdit->setText(prev);
+    }
 }
 
 void SearchBarWidget::historyShowToolButtonClicked() {
-
+    HistoryListDialog listDialog(history, this);
+    listDialog.exec();
 }
 
 void SearchBarWidget::clearSearchWordToolButtonClicked() {
     searchWordLineEdit->clear();
 }
 
-void SearchBarWidget::showHistoryListDialog() {
-
-}
 
 bool SearchBarWidget::isSearching() const {
     return _isSearching;
 }
+
+void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
+    if(!isSearching()) {
+        historyPrevToolButton->setEnabled(prev);
+        historyNextToolButton->setEnabled(next);
+        historyShowToolButton->setEnabled(list);
+    }
+}