draft of spinbox
[mdictionary] / src / mdictionary / gui / SearchBarWidget.cpp
index 62ee99a..9dc1450 100644 (file)
 
 *******************************************************************************/
 
-//! \file SearchBarWidget.cpp
-//! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
+/*! \file SearchBarWidget.cpp
+    \brief Displays search bar
+
+    \author Mateusz Półrola <mateusz.polrola@comarch.pl>
+*/
 
 
 #include "SearchBarWidget.h"
 #include <QDebug>
-#include "../../common/DictDialog.h"
+#include "../../include/DictDialog.h"
 #include "HistoryListDialog.h"
 
 
-SearchBarWidget::SearchBarWidget(QWidget *parent) :
-    QWidget(parent) {
+SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
+    qDebug()<<"test1";
+#ifndef Q_WS_MAEMO_5
+    this->setMaximumHeight(50);
+    busyTimer=new QTimer;
 
-    initializeUI();
+    progressBar = new QDeclarativeView();
+    progressBar->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/ProgressBar.qml"));
+    progressBar->setResizeMode(QDeclarativeView::SizeRootObjectToView);
+    progressBar->setAlignment(Qt::AlignCenter);
+    progressBar->hide();
 
+    view= new QDeclarativeView();
+    view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/SearchBarWidget.qml"));
+    view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
+    view->setAlignment(Qt::AlignCenter);
+    view->show();
 
-    busy = false;
+    mainLayout = new QVBoxLayout;
+    mainLayout->addWidget(progressBar);
+    mainLayout->addWidget(view);
+    setLayout(mainLayout);
+
+    QGraphicsObject *rootObject = view->rootObject();
+    QGraphicsObject *rootObject2 = progressBar->rootObject();
+
+    connect(rootObject, SIGNAL(searchButtonClicked(QString)),
+            this, SLOT(searchButtonClicked(QString)));
+    connect(rootObject, SIGNAL(historyNextToolButtonClicked()),
+            this, SIGNAL(historyNext()));
+    connect(rootObject, SIGNAL(historyPrevToolButtonClicked()),
+            this, SIGNAL(historyPrev()));
+    connect(rootObject, SIGNAL(historyShowToolButtonClicked()),
+            this, SLOT(showHistoryButtonClicked()));
 
+    connect(this, SIGNAL(progresSetMax(QVariant)),
+            rootObject2, SLOT(setMax(QVariant)));
+    connect(this, SIGNAL(progresSetMin(QVariant)),
+            rootObject2, SLOT(setMin(QVariant)));
+    connect(this, SIGNAL(progresSetValue(QVariant)),
+            rootObject2, SLOT(setValue(QVariant)));
+    connect(this, SIGNAL(progresSetValue2(QVariant)),
+            rootObject2, SLOT(setValue2(QVariant)));
+
+    connect(this, SIGNAL(setEnableHistoryNext(QVariant)),
+            rootObject, SLOT(setEnableHistoryNext(QVariant)));
+    connect(this, SIGNAL(setEnableHistoryShow(QVariant)),
+            rootObject, SLOT(setEnableHistoryShow(QVariant)));
+    connect(this, SIGNAL(setEnableHistoryPrev(QVariant)),
+            rootObject, SLOT(setEnableHistoryPrev(QVariant)));
+    connect(this, SIGNAL(setButtonText(QVariant)),
+            rootObject, SLOT(setButtonText(QVariant)));
+    connect(this, SIGNAL(setLineEditText(QVariant)),
+            rootObject, SLOT(setLineEditText(QVariant)));
+    connect(this, SIGNAL(setLineEditEnables(QVariant)),
+            rootObject, SLOT(setEnableLineEdit(QVariant)));
+
+    connect(busyTimer, SIGNAL(timeout()),
+            this, SLOT(updateBusyTimer()));
+
+    emit setEnableHistoryNext(false);
+    emit setEnableHistoryShow(false);
+    emit setEnableHistoryPrev(false);
+
+    completerModel = new QStringListModel(this);
+    connect(&delayTimer, SIGNAL(timeout()),
+            this, SLOT(delaySearchTimeout()));
+
+    view->setFocus();
+
+#else
+    initializeUI();
     connect(searchPushButton, SIGNAL(clicked()),
             this, SLOT(searchPushButtonClicked()));
-
     connect(searchWordLineEdit, SIGNAL(returnPressed()),
             this, SLOT(searchPushButtonClicked()));
-
     connect(historyNextToolButton, SIGNAL(clicked()),
             this, SIGNAL(historyNext()));
-
     connect(historyPrevToolButton, SIGNAL(clicked()),
             this, SIGNAL(historyPrev()));
-
     connect(historyShowToolButton, SIGNAL(clicked()),
             this, SLOT(showHistoryButtonClicked()));
-
     connect(clearSearchWordToolButton, SIGNAL(clicked()),
             this, SLOT(clearSearchWordToolButtonClicked()));
-
-
     connect(&delayTimer, SIGNAL(timeout()),
             this, SLOT(delaySearchTimeout()));
 
 
     searchWordLineEdit->setFocus();
+#endif
 
-    historyPrevToolButton->setEnabled(false);
-    historyNextToolButton->setEnabled(false);
-    historyShowToolButton->setEnabled(false);
-
+    busy = false;
     setEnabled(true);
+    updateHistoryButtons(false,false,false);
 }
 
 SearchBarWidget::~SearchBarWidget() {
 
 }
 
-QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
-    QPixmap p = oryginal.pixmap(64);
+QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
+    qDebug()<<"test2";
+    QPixmap p = original.pixmap(64);
 
     if(rotation != 0) {
         QMatrix m;
@@ -88,8 +148,20 @@ QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
 
 
     #ifdef Q_WS_MAEMO_5
-        QPainter painter(&p);
-        painter.fillRect(p.rect(), QColor(0,0,0,192));
+        QImage img = p.toImage();
+
+        for(int i=0; i < img.width(); i++) {
+            for(int j=0; j < img.height(); j++) {
+                QColor c = img.pixel(i,j);
+                if(c != QColor(0,0,0,255)) {
+                    c.setRed(c.red()/2);
+                    c.setGreen(c.green()/2);
+                    c.setBlue(c.blue()/2);
+                    img.setPixel(i, j, c.rgb());
+                }
+            }
+        }
+        p = p.fromImage(img);
 
         newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
     #endif
@@ -97,45 +169,42 @@ QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
     return newIcon;
 }
 
+void SearchBarWidget::setFocus() {
+    qDebug()<<"test3";
+#ifndef Q_WS_MAEMO_5
+        view->setFocus();
+#else
+        searchWordLineEdit->setFocus();
+#endif
+}
 
 void SearchBarWidget::initializeUI() {
-
-    #ifdef Q_WS_MAEMO_5
-        setMaximumHeight(150);
-    #else
-        setMaximumHeight(100);
-    #endif
-
+    qDebug()<<"test4";
+#ifdef Q_WS_MAEMO_5
+    setMaximumHeight(150);
 
     horizontalLayout = new QHBoxLayout;
     verticalLayout = new QVBoxLayout;
 
-
     searchPushButton = new QPushButton(tr("Search"));
     searchPushButton->setMinimumWidth(125);
 
-
     searchWordLineEdit = new QLineEdit;
     searchWordLineEdit->setMinimumWidth(250);
 
-
-
     completerModel = new QStringListModel(this);
 
-
     lineEditCompleter = new QCompleter(searchWordLineEdit);
     lineEditCompleter->setModel(completerModel);
     lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
     lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
     searchWordLineEdit->setCompleter(lineEditCompleter);
 
-
     #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;
     searchWordLineEdit->setLayout(lineEditLayout);
@@ -147,7 +216,7 @@ void SearchBarWidget::initializeUI() {
         clearSearchWordToolButton->setMaximumSize(
                 clearSearchWordToolButton->sizeHint().height()/2,
                 clearSearchWordToolButton->sizeHint().height()/2);
-        lineEditLayout->setContentsMargins(0,0,10,0);
+        lineEditLayout->setContentsMargins(0,0,15,0);
     #else
         clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
         clearSearchWordToolButton->setMinimumSize(
@@ -189,7 +258,7 @@ void SearchBarWidget::initializeUI() {
     #endif
 
     searchingProgressBar = new QProgressBar;
-    //progress bar have minimum and maximum values set to 0, which will effect
+    //progress bar has minimum and maximum values set to 0, which will effect
     //with "I'm alive" bar
     searchingProgressBar->setMinimum(0);
     searchingProgressBar->setMaximum(0);
@@ -198,7 +267,6 @@ void SearchBarWidget::initializeUI() {
     #endif
     searchingProgressBar->hide();
 
-
     setLayout(verticalLayout);
 
     verticalLayout->addWidget(searchingProgressBar);
@@ -213,50 +281,65 @@ void SearchBarWidget::initializeUI() {
     //adding clear toolButton to textEdit with right alignment
     lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
 
-
     verticalLayout->addLayout(horizontalLayout);
+#endif
 }
 
+void SearchBarWidget::searchButtonClicked(QString text) {
+    qDebug()<<"test5";
+    if(busy)
+        Q_EMIT stopSearching();
+    else
+        search(text);
+}
 
 void SearchBarWidget::searchPushButtonClicked() {
+    qDebug()<<"test6";
+#ifdef Q_WS_MAEMO_5
     if(busy) {
         Q_EMIT stopSearching();
     }
     else {
         search(searchWordLineEdit->text());
     }
+#endif
 }
 
-
 void SearchBarWidget::search(QString word) {
+    qDebug()<<"test7";
+    qDebug()<<word;
     if(!busy && !word.isEmpty()) {
         completerModel->insertRow(completerModel->rowCount());
-        QModelIndex index =
-                completerModel->index(completerModel->rowCount() -1);
-
+        QModelIndex index=completerModel->index(completerModel->rowCount() -1);
         completerModel->setData(index, word);
 
-
+#ifndef Q_WS_MAEMO_5
+        emit setLineEditText(word);
+#else
         searchWordLineEdit->setText(word);
+#endif
         Q_EMIT searchForTranslations(word);
     }
 }
 
 void SearchBarWidget::searchDelay(QString word) {
+    qDebug()<<"test8";
     if(!busy && !word.isEmpty()) {
-        searchWordLineEdit->setText(word);
+        #ifndef Q_WS_MAEMO_5
+            emit setLineEditText(word);
+        #else
+            searchWordLineEdit->setText(word);
+        #endif
+            if(delayTimer.isActive())
+                delayTimer.stop();
+            delayString = word;
+            delayTimer.start(500);
 
-
-        if(delayTimer.isActive()) {
-            delayTimer.stop();
-        }
-
-        delayString = word;
-        delayTimer.start(500);
     }
 }
 
 void SearchBarWidget::delaySearchTimeout() {
+    qDebug()<<"test9";
     delayTimer.stop();
     if(!busy) {
         Q_EMIT searchForTranslations(delayString);
@@ -264,27 +347,75 @@ void SearchBarWidget::delaySearchTimeout() {
 }
 
 void SearchBarWidget::setEnabled(bool enabled) {
+    qDebug()<<"test10";
+#ifndef Q_WS_MAEMO_5
+    emit setLineEditEnables(enabled);
+    if(!enabled) {
+        qDebug()<<"tu???";
+        emit setEnableHistoryNext(false);
+        emit setEnableHistoryShow(false);
+        emit setEnableHistoryPrev(false);
+    }
+#else
     searchWordLineEdit->setEnabled(enabled);
-
     if(!enabled) {
         historyPrevToolButton->setEnabled(false);
         historyNextToolButton->setEnabled(false);
         historyShowToolButton->setEnabled(false);
     }
+#endif
+qDebug()<<"tu2???";
 }
 
 void SearchBarWidget::setBusy() {
+    qDebug()<<"test11";
     if(busy) return;
+
+#ifndef Q_WS_MAEMO_5
+    busyTimer->start(50);
+    emit setButtonText(tr("Stop"));
+    this->setMaximumHeight(88);
+    progressBar->show();
+    emit progresSetMax(100);
+    emit progresSetMin(0);
+    emit progresSetValue(-1);
+    emit progresSetValue2(100);
+    progressMax=true;
+#else
     searchingProgressBar->show();
     searchPushButton->setText(tr("Stop"));
+#endif
+
     setEnabled(false);
     busy = true;
 }
 
+void SearchBarWidget::updateBusyTimer(){
+    qDebug()<<"test12";
+    if(progressMax==true){
+        emit progresSetValue2(0);
+        progressMax=false;
+    }
+    else{
+        emit progresSetValue2(100);
+        progressMax=true;
+    }
+    busyTimer->start(50);
+}
+
 void SearchBarWidget::setIdle() {
+    qDebug()<<"test13";
     if(!busy) return;
+#ifndef Q_WS_MAEMO_5
+    progressBar->hide();
+    busyTimer->stop();
+    emit progresSetValue2(0);
+    this->setMaximumHeight(50);
+    emit setButtonText(tr("Search"));
+#else
     searchingProgressBar->hide();
     searchPushButton->setText(tr("Search"));
+#endif
     setEnabled(true);
     busy = false;
     Q_EMIT refreshHistoryButtons();
@@ -292,25 +423,37 @@ void SearchBarWidget::setIdle() {
 
 
 void SearchBarWidget::clearSearchWordToolButtonClicked() {
+    qDebug()<<"test14";
+#ifdef Q_WS_MAEMO_5
     searchWordLineEdit->clear();
+#endif
 }
 
 
 
 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
+    qDebug()<<"test15";
     if(!busy) {
-        historyPrevToolButton->setEnabled(prev);
-        historyNextToolButton->setEnabled(next);
-        historyShowToolButton->setEnabled(list);
+        #ifndef Q_WS_MAEMO_5
+            emit setEnableHistoryNext(next);
+            emit setEnableHistoryShow(list);
+            emit setEnableHistoryPrev(prev);
+        #else
+            historyPrevToolButton->setEnabled(prev);
+            historyNextToolButton->setEnabled(next);
+            historyShowToolButton->setEnabled(list);
+        #endif
     }
 }
 
 void SearchBarWidget::showHistoryButtonClicked() {
-    #ifdef Q_WS_MAEMO_5
-        emit historyShow();
-    #else
-        QPoint p = historyShowToolButton->pos();
-        p.setY(p.y());
-        emit historyShow(mapToGlobal(p));
-    #endif
+    qDebug()<<"test16";
+#ifndef Q_WS_MAEMO_5
+    QPoint p=view->pos(); // = historyShowToolButton->pos();
+    p=mapToGlobal(p);
+    p.setX(p.x()+view->width());
+    emit historyShow(p);
+#else
+    emit historyShow();
+#endif
 }