Added history
authorMateusz Półrola <mateusz.polrola@comarch.pl>
Wed, 11 Aug 2010 09:59:51 +0000 (11:59 +0200)
committerMateusz Półrola <mateusz.polrola@comarch.pl>
Wed, 11 Aug 2010 09:59:51 +0000 (11:59 +0200)
trunk/src/base/backbone/History.cpp [new file with mode: 0644]
trunk/src/base/backbone/backbone.cpp
trunk/src/base/backbone/backbone.h
trunk/src/base/base.pro
trunk/src/base/gui/HistoryListDialog.cpp [new file with mode: 0644]
trunk/src/base/gui/HistoryListDialog.h [new file with mode: 0644]
trunk/src/base/gui/SearchBarWidget.cpp
trunk/src/base/gui/SearchBarWidget.h
trunk/src/includes/History.h [new file with mode: 0644]

diff --git a/trunk/src/base/backbone/History.cpp b/trunk/src/base/backbone/History.cpp
new file mode 100644 (file)
index 0000000..4163f4d
--- /dev/null
@@ -0,0 +1,164 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#include "../../includes/History.h"
+#include <QDebug>
+
+History::History(QObject *parent) :
+    QObject(parent)
+{
+    _maxSize = 5;
+    currentElement = -1;
+    _prevAvailable = false;
+    _nextAvailable = false;
+    _listAvailable = false;
+}
+
+
+void History::add(QString word) {
+    if(currentElement != -1) {
+        //we search the same word so we don't add it again
+        if(_history[currentElement] == word)
+            return;
+    }
+
+    //if we are not in head, we deleted everything ahed us
+    if(currentElement > 0) {
+        _history.remove(0, currentElement);
+    }
+
+
+    if(_history.contains(word)) {
+        _history.remove(_history.indexOf(word));
+    }
+
+    //add new word to head
+    _history.push_front(word);
+
+    //fit to max size
+    while(_history.size() > _maxSize) {
+        _history.pop_back();
+    }
+
+    currentElement = 0;
+
+    if(_history.size() > 1) {
+        _prevAvailable = true;
+        _nextAvailable = false;
+        _listAvailable = true;
+    }
+    else {
+        _prevAvailable = false;
+        _nextAvailable = false;
+        _listAvailable = true;
+    }
+
+    emit historyChanged(_prevAvailable,
+                        _nextAvailable,
+                        _listAvailable);
+}
+
+QString History::previous() {
+    if(_prevAvailable) {
+        currentElement++;
+
+        _nextAvailable = true;
+
+        if(currentElement+1 == _history.size()) {
+            _prevAvailable = false;
+        }
+
+        emit historyChanged(_prevAvailable,
+                            _nextAvailable,
+                            _listAvailable);
+
+        return _history[currentElement];
+    }
+    return QString();
+}
+
+QString History::next() {
+    if(_nextAvailable) {
+        currentElement--;
+
+        _prevAvailable = true;
+
+        if(currentElement == 0) {
+           _nextAvailable = false;
+        }
+
+        emit historyChanged(_prevAvailable,
+                            _nextAvailable,
+                            _listAvailable);
+
+        return _history[currentElement];
+    }
+    return QString();
+}
+
+QStringList History::list() {
+    QStringList result;
+
+    if(_listAvailable) {
+        for(int i=0; i<_history.size(); i++) {
+            result << _history[i];
+        }
+    }
+    return result;
+}
+
+bool History::nextAvailable() {
+    return _nextAvailable;
+}
+
+bool History::prevAvailable() {
+    return _prevAvailable;
+}
+
+bool History::listAvailable() {
+    return _listAvailable;
+}
+
+void History::setCurrentElement(int element) {
+    if(element < 0 || element >= _history.size()) return;
+
+    currentElement = element;
+
+    if(currentElement > 0) {
+       _nextAvailable = true;
+    }
+    else {
+        _nextAvailable = false;
+    }
+
+    if(currentElement+1 < _history.size()) {
+        _prevAvailable = true;
+    }
+    else {
+        _prevAvailable = false;
+    }
+
+    emit historyChanged(_prevAvailable,
+                        _nextAvailable,
+                        _listAvailable);
+}
index d311c98..220682c 100644 (file)
@@ -42,6 +42,9 @@ void Backbone::init() {
    connect(&_timerSearch, SIGNAL(timeout()), this, SLOT(translationReady()));
    connect(&_timerHtmlSearch, SIGNAL(timeout()), this,
            SLOT(htmlTranslationReady()));
+
+
+   _history = new History(this);
 }
 
 Backbone::Backbone(QString pluginPath, QString configPath, QObject *parent)
@@ -106,8 +109,8 @@ QList<CommonDictInterface* > Backbone::getPlugins() {
 
 
 
-QList<QString> Backbone::getHistory() {
-    //TODO code needed
+History* Backbone::history() {
+    return _history;
 }
 
 
index 2e185f2..749b8de 100644 (file)
@@ -37,6 +37,7 @@
 #include "../../includes/CommonDictInterface.h"
 #include "../../includes/settings.h"
 #include "../../includes/translation.h"
+#include "../../includes/History.h"
 
 
 /*! Inner part of dictionary - glues together GUI and plugins
@@ -67,7 +68,7 @@ public:
     QList<CommonDictInterface*> getPlugins();
 
     //! \return history of performed searches
-    QList<QString> getHistory(); //TODO implementation needed (in future)
+    History* history();
 
     //! \return return search fesult
     QMultiHash<QString, Translation*> result();
@@ -172,6 +173,8 @@ private:
     void addInternalDictionary(CommonDictInterface*, bool);
     //void writeConfig(QString key, QString value);
 
+    History* _history;
+
 };
 
 #endif // BACKBONE_H
index 2c6a578..fe9c278 100644 (file)
@@ -30,7 +30,9 @@ SOURCES += gui/main.cpp\
     gui/MenuWidget.cpp \
     gui/MenuTabWidget.cpp \
     gui/DictManagerWidget.cpp \
-    gui/DictTypeSelectDialog.cpp
+    gui/DictTypeSelectDialog.cpp \
+    backbone/History.cpp \
+    gui/HistoryListDialog.cpp
 
 HEADERS  += gui/MainWindow.h \
     gui/SearchBarWidget.h \
@@ -44,7 +46,9 @@ HEADERS  += gui/MainWindow.h \
     gui/MenuTabWidget.h \
     gui/DictManagerWidget.h \
     gui/DictTypeSelectDialog.h \
-    gui/TranslationWidgetAutoResizer.h
+    gui/TranslationWidgetAutoResizer.h \
+    ../includes/History.h \
+    gui/HistoryListDialog.h
 
 FORMS    += gui/MainWindow.ui
 
diff --git a/trunk/src/base/gui/HistoryListDialog.cpp b/trunk/src/base/gui/HistoryListDialog.cpp
new file mode 100644 (file)
index 0000000..62f5d3e
--- /dev/null
@@ -0,0 +1,67 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#include "HistoryListDialog.h"
+
+HistoryListDialog::HistoryListDialog(History *history, SearchBarWidget *parent):
+        QDialog(parent)
+{
+    this->history = history;
+    searchBar = parent;
+
+    verticalLayout = new QVBoxLayout;
+    setLayout(verticalLayout);
+
+    historyListWidget = new QListWidget;
+    verticalLayout->addWidget(historyListWidget);
+
+
+    QStringList words = history->list();
+
+    for(int i=0; i<words.count(); i++) {
+        QListWidgetItem* item = new QListWidgetItem(
+                QString::number(i+1) + ". " + words[i]);
+        historyListWidget->addItem(item);
+    }
+
+     setModal(true);
+
+     setWindowTitle(tr("History"));
+
+     setMinimumHeight(300);
+
+     connect(historyListWidget, SIGNAL(clicked(QModelIndex)),
+             this, SLOT(itemClicked(QModelIndex)));
+}
+
+
+void HistoryListDialog::itemClicked(QModelIndex index) {
+    QString selectedWord = history->list()[index.row()];
+
+    history->setCurrentElement(index.row());
+
+    searchBar->search(selectedWord);
+
+    accept();
+}
+
diff --git a/trunk/src/base/gui/HistoryListDialog.h b/trunk/src/base/gui/HistoryListDialog.h
new file mode 100644 (file)
index 0000000..4a60faa
--- /dev/null
@@ -0,0 +1,49 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#ifndef HISTORYLISTDIALOG_H
+#define HISTORYLISTDIALOG_H
+
+#include <QDialog>
+#include "SearchBarWidget.h"
+#include "../../includes/History.h"
+#include <QtGui>
+
+class HistoryListDialog : public QDialog
+{
+    Q_OBJECT
+public:
+    explicit HistoryListDialog(History* history, SearchBarWidget*parent);
+
+private Q_SLOTS:
+    void itemClicked(QModelIndex);
+private:
+    History* history;
+    SearchBarWidget* searchBar;
+
+    QListWidget* historyListWidget;
+    QVBoxLayout* verticalLayout;
+
+};
+
+#endif // HISTORYLISTDIALOG_H
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);
+    }
+}
index fbceb03..ca8ebb4 100644 (file)
@@ -28,7 +28,7 @@
 #include <QWidget>
 #include <QtGui>
 #include "../backbone/backbone.h"
-
+#include "../../includes/History.h"
 
 //! Displays search bar
 /*!
@@ -87,10 +87,12 @@ private Q_SLOTS:
     void historyPrevToolButtonClicked();
     void historyNextToolButtonClicked();
     void historyShowToolButtonClicked();
+    void updateHistoryButtons(bool prev, bool next, bool list);
 
 
 private:
     Backbone* backbone;
+    History* history;
     QLineEdit* searchWordLineEdit;
     QToolButton* clearSearchWordToolButton;
     QPushButton* searchPushButton;
@@ -107,7 +109,6 @@ private:
     bool _isSearching;
 
     void initializeUI();
-    void showHistoryListDialog();
 };
 
 #endif // SEARCHBARWIDGET_H
diff --git a/trunk/src/includes/History.h b/trunk/src/includes/History.h
new file mode 100644 (file)
index 0000000..ac0b1bf
--- /dev/null
@@ -0,0 +1,63 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#ifndef HISTORY_H
+#define HISTORY_H
+
+#include <QObject>
+#include <QVector>
+#include <QStringList>
+
+class History : public QObject
+{
+    Q_OBJECT
+public:
+    explicit History(QObject *parent = 0);
+
+Q_SIGNALS:
+    void historyChanged(bool prevAvailable,
+                        bool nextAvailable,
+                        bool listAvailable);
+
+public:
+    void add(QString);
+    QString previous();
+    QString next();
+    QStringList list();
+
+    bool prevAvailable();
+    bool nextAvailable();
+    bool listAvailable();
+
+    void setCurrentElement(int element);
+
+private:
+    QVector<QString> _history;
+    int _maxSize;
+    int currentElement;
+    bool _prevAvailable;
+    bool _nextAvailable;
+    bool _listAvailable;
+};
+
+#endif // HISTORY_H