Fixed gui: search progress bar, result list
[mdictionary] / trunk / src / base / gui / SearchBarWidget.cpp
index 3ff3a7e..b9f674c 100644 (file)
 #include "SearchBarWidget.h"
 #include <QDebug>
 #include "../../includes/DictDialog.h"
+#include "HistoryListDialog.h"
 
 
-SearchBarWidget::SearchBarWidget(Backbone* backbone, QWidget *parent) :
+SearchBarWidget::SearchBarWidget(QWidget *parent) :
     QWidget(parent) {
 
-    this->backbone = backbone;
-
     initializeUI();
 
-    setMaximumHeight(150);
 
     _isSearching = false;
 
     connect(searchPushButton, SIGNAL(clicked()),
             this, SLOT(searchPushButtonClicked()));
 
+    connect(searchWordLineEdit, SIGNAL(returnPressed()),
+            this, SLOT(searchPushButtonClicked()));
+
     connect(historyNextToolButton, SIGNAL(clicked()),
-            this, SLOT(historyNextToolButtonClicked()));
+            this, SIGNAL(historyNext()));
 
     connect(historyPrevToolButton, SIGNAL(clicked()),
-            this, SLOT(historyPrevToolButtonClicked()));
+            this, SIGNAL(historyPrev()));
 
     connect(historyShowToolButton, SIGNAL(clicked()),
-            this, SLOT(historyShowToolButtonClicked()));
+            this, SIGNAL(historyShow()));
 
     connect(clearSearchWordToolButton, SIGNAL(clicked()),
             this, SLOT(clearSearchWordToolButtonClicked()));
 
 
-    //connects request to backbone
-    connect(this, SIGNAL(searchForTranslations(QString)),
-            backbone, SLOT(search(QString)));
+    connect(&delayTimer, SIGNAL(timeout()),
+            this, SLOT(delaySearchTimeout()));
 
-    connect(this, SIGNAL(stopSearching()),
-            backbone, SLOT(stopSearching()));
 
-    connect(backbone, SIGNAL(ready()),
-            this, SLOT(searchFinished()));
+    searchWordLineEdit->setFocus();
 
+    historyPrevToolButton->setEnabled(false);
+    historyNextToolButton->setEnabled(false);
+    historyShowToolButton->setEnabled(false);
+    setEnabled(true);
 }
 
 SearchBarWidget::~SearchBarWidget() {
 
 }
 
+QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
+    QPixmap p = oryginal.pixmap(64);
+
+    if(rotation != 0) {
+        QMatrix m;
+        m.rotate(rotation);
+
+        p = p.transformed(m);
+    }
+
+    QIcon newIcon;
+    newIcon.addPixmap(p);
+
+
+    #ifdef Q_WS_MAEMO_5
+        QPainter painter(&p);
+        painter.fillRect(p.rect(), QColor(0,0,0,192));
+
+        newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
+    #endif
+
+    return newIcon;
+}
+
 
 void SearchBarWidget::initializeUI() {
+
+    #ifdef Q_WS_MAEMO_5
+        setMaximumHeight(150);
+    #else
+        setMaximumHeight(70);
+    #endif
+
+
     horizontalLayout = new QHBoxLayout();
     verticalLayout = new QVBoxLayout();
 
 
     searchPushButton = new QPushButton(tr("Search"));
-    searchPushButton->setMinimumWidth(200);
+    searchPushButton->setMinimumWidth(125);
 
 
     searchWordLineEdit = new QLineEdit();
-    searchWordLineEdit->setMinimumWidth(350);
+    searchWordLineEdit->setMinimumWidth(250);
+
+    #ifndef Q_WS_MAEMO_5
+        searchWordLineEdit->setMinimumHeight(
+                searchWordLineEdit->sizeHint().height()*3/2);
+    #endif
+
+
     //create layout for lineEdit to have clear button on it
-    QHBoxLayout* lineEditLayout = new QHBoxLayout;
+    QHBoxLayout* lineEditLayout = new QHBoxLayout();
     searchWordLineEdit->setLayout(lineEditLayout);
 
 
     clearSearchWordToolButton = new QToolButton();
-    clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
-    //tool buttons will have size 2 times smaller
-    clearSearchWordToolButton->setMaximumSize(
-            clearSearchWordToolButton->sizeHint().width()/2,
-            clearSearchWordToolButton->sizeHint().height()/2);
+    #ifdef Q_WS_MAEMO_5
+        clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
+        //tool buttons will have size 2 times smaller
+        clearSearchWordToolButton->setMaximumSize(
+                clearSearchWordToolButton->sizeHint().height()/2,
+                clearSearchWordToolButton->sizeHint().height()/2);
+        lineEditLayout->setContentsMargins(0,0,10,0);
+    #else
+        clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
+        clearSearchWordToolButton->setMinimumSize(
+                searchWordLineEdit->sizeHint().height()*1.2,
+                searchWordLineEdit->sizeHint().height()*1.2);
+        lineEditLayout->setContentsMargins(0,0,5,0);
+    #endif
 
 
     historyNextToolButton = new QToolButton();
-    historyNextToolButton->setIcon(QIcon::fromTheme("general_forward"));
-    historyNextToolButton->setMaximumSize(
-            historyNextToolButton->sizeHint().width()/2,
-            historyNextToolButton->sizeHint().height()/2);
+    #ifdef Q_WS_MAEMO_5
+        historyNextToolButton->setIcon(
+                generateIcon(QIcon::fromTheme("general_forward")));
+    #else
+        historyNextToolButton->setIcon(
+                generateIcon(QIcon::fromTheme("go-next")));
+    #endif
 
 
-    historyPrevToolButton = new QToolButton();
-    historyPrevToolButton->setIcon(QIcon::fromTheme("general_back"));
-    historyPrevToolButton->setMaximumSize(
-            historyPrevToolButton->sizeHint().width()/2,
-            historyPrevToolButton->sizeHint().height()/2);
 
+    historyPrevToolButton = new QToolButton();
+    #ifdef Q_WS_MAEMO_5
+        historyPrevToolButton->setIcon(
+                generateIcon(QIcon::fromTheme("general_back")));
+    #else
+        historyPrevToolButton->setIcon(
+                generateIcon(QIcon::fromTheme("go-previous")));
+    #endif
 
-    historyShowToolButton = new QToolButton();
-    historyShowToolButton->setIcon(QIcon::fromTheme("general_back"));
-    historyShowToolButton->setMaximumSize(
-            historyShowToolButton->sizeHint().width()/2,
-            historyShowToolButton->sizeHint().height()/2);
-
-    QPixmap p = historyShowToolButton->icon().pixmap(256);
-    QMatrix m;
-    m.rotate(90);
 
 
-    QIcon temp;
-    temp.addPixmap(p.transformed(m));
-    historyShowToolButton->setIcon(temp);
+    historyShowToolButton = new QToolButton();
+    #ifdef Q_WS_MAEMO_5
+        historyShowToolButton->setIcon(
+                generateIcon(QIcon::fromTheme("general_back"), 90));
+    #else
+        historyShowToolButton->setIcon(
+                generateIcon(QIcon::fromTheme("go-up")));
+    #endif
+
+    /*fullScreenToolButton = new QToolButton();
+    #ifdef Q_WS_MAEMO_5
+        fullScreenToolButton->setIcon(
+                generateIcon(QIcon::fromTheme("general_fullsize")));
+    #else
+        fullScreenToolButton->setIcon(
+                generateIcon(QIcon::fromTheme("view-fullscreen")));
+        fullScreenToolButton->setMinimumSize(
+                fullScreenToolButton->sizeHint().height()*2,
+                fullScreenToolButton->sizeHint().height()*2);
+    #endif*/
 
 
     searchingProgressBar = new QProgressBar();
@@ -131,8 +194,9 @@ void SearchBarWidget::initializeUI() {
     searchingProgressBar->setMinimum(0);
     searchingProgressBar->setMaximum(0);
     searchingProgressBar->hide();
-    searchingProgressBar->setMaximumHeight(50);
-
+    #ifdef Q_WS_MAEMO_5
+        searchingProgressBar->setMaximumHeight(50);
+    #endif
 
 
     setLayout(verticalLayout);
@@ -145,10 +209,11 @@ void SearchBarWidget::initializeUI() {
     horizontalLayout->addWidget(historyPrevToolButton);
     horizontalLayout->addWidget(historyShowToolButton);
     horizontalLayout->addWidget(historyNextToolButton);
+   // horizontalLayout->addWidget(fullScreenToolButton);
 
     //adding clear toolButton to textEdit with right alignment
-    lineEditLayout->addWidget(clearSearchWordToolButton, 0,
-                              Qt::AlignRight | Qt::AlignVCenter);
+    lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
+
 
     verticalLayout->addLayout(horizontalLayout);
 }
@@ -156,58 +221,80 @@ void SearchBarWidget::initializeUI() {
 
 void SearchBarWidget::searchPushButtonClicked() {
     if(_isSearching) {
-        searchingProgressBar->hide();
-        searchPushButton->setText(tr("Search"));
-        setEnabled(true);
-        _isSearching = false;
         emit stopSearching();
     }
     else {
-        searchingProgressBar->show();
-        searchPushButton->setText(tr("Stop"));
-        setEnabled(false);
-        _isSearching = true;
-        emit searchForTranslations(searchWordLineEdit->text());
+        search(searchWordLineEdit->text());
     }
 }
 
-void SearchBarWidget::setEnabled(bool enabled) {
-    searchWordLineEdit->setEnabled(enabled);
-    historyNextToolButton->setEnabled(enabled);
-    historyPrevToolButton->setEnabled(enabled);
-    historyShowToolButton->setEnabled(enabled);
+
+void SearchBarWidget::search(QString word) {
+    if(!_isSearching && !word.isEmpty()) {
+        searchWordLineEdit->setText(word);
+        emit searchForTranslations(word);
+    }
 }
 
+void SearchBarWidget::searchDelay(QString word) {
+    if(!_isSearching && !word.isEmpty()) {
+        searchWordLineEdit->setText(word);
 
-void SearchBarWidget::searchFinished() {
-        searchingProgressBar->hide();
-        searchPushButton->setText(tr("Search"));
-        setEnabled(true);
-        _isSearching = false;
-}
 
-void SearchBarWidget::historyNextToolButtonClicked() {
+        if(delayTimer.isActive()) {
+            delayTimer.stop();
+        }
+
+        delayString = word;
+        delayTimer.start(500);
+    }
+}
 
-    CommonDictInterface*p = backbone->getPlugins()[0];
-    p->dictDialog()->addNewDictionary(this);
+void SearchBarWidget::delaySearchTimeout() {
+    delayTimer.stop();
+    if(!_isSearching) {
+        emit searchForTranslations(delayString);
+    }
 }
 
-void SearchBarWidget::historyPrevToolButtonClicked() {
+void SearchBarWidget::setEnabled(bool enabled) {
+    searchWordLineEdit->setEnabled(enabled);
 
+    if(!enabled) {
+        historyPrevToolButton->setEnabled(false);
+        historyNextToolButton->setEnabled(false);
+        historyShowToolButton->setEnabled(false);
+    }
 }
 
-void SearchBarWidget::historyShowToolButtonClicked() {
+void SearchBarWidget::setBusy() {
+    if(_isSearching) return;
+    searchingProgressBar->show();
+    searchPushButton->setText(tr("Stop"));
+    setEnabled(false);
+    _isSearching = true;
+}
 
+void SearchBarWidget::setIdle() {
+    if(!_isSearching) return;
+    searchingProgressBar->hide();
+    searchPushButton->setText(tr("Search"));
+    setEnabled(true);
+    _isSearching = false;
+    emit refreshHistoryButtons();
 }
 
+
 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);
+    }
 }