From 5d6eb8557be0bed708e074e415d76034a9b80330 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mateusz=20P=C3=B3=C5=82rola?= Date: Wed, 11 Aug 2010 11:59:51 +0200 Subject: [PATCH] Added history --- trunk/src/base/backbone/History.cpp | 164 ++++++++++++++++++++++++++++++ trunk/src/base/backbone/backbone.cpp | 7 +- trunk/src/base/backbone/backbone.h | 5 +- trunk/src/base/base.pro | 8 +- trunk/src/base/gui/HistoryListDialog.cpp | 67 ++++++++++++ trunk/src/base/gui/HistoryListDialog.h | 49 +++++++++ trunk/src/base/gui/SearchBarWidget.cpp | 44 ++++++-- trunk/src/base/gui/SearchBarWidget.h | 5 +- trunk/src/includes/History.h | 63 ++++++++++++ 9 files changed, 395 insertions(+), 17 deletions(-) create mode 100644 trunk/src/base/backbone/History.cpp create mode 100644 trunk/src/base/gui/HistoryListDialog.cpp create mode 100644 trunk/src/base/gui/HistoryListDialog.h create mode 100644 trunk/src/includes/History.h diff --git a/trunk/src/base/backbone/History.cpp b/trunk/src/base/backbone/History.cpp new file mode 100644 index 0000000..4163f4d --- /dev/null +++ b/trunk/src/base/backbone/History.cpp @@ -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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +//Created by Mateusz Półrola + +#include "../../includes/History.h" +#include + +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); +} diff --git a/trunk/src/base/backbone/backbone.cpp b/trunk/src/base/backbone/backbone.cpp index d311c98..220682c 100644 --- a/trunk/src/base/backbone/backbone.cpp +++ b/trunk/src/base/backbone/backbone.cpp @@ -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 Backbone::getPlugins() { -QList Backbone::getHistory() { - //TODO code needed +History* Backbone::history() { + return _history; } diff --git a/trunk/src/base/backbone/backbone.h b/trunk/src/base/backbone/backbone.h index 2e185f2..749b8de 100644 --- a/trunk/src/base/backbone/backbone.h +++ b/trunk/src/base/backbone/backbone.h @@ -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 getPlugins(); //! \return history of performed searches - QList getHistory(); //TODO implementation needed (in future) + History* history(); //! \return return search fesult QMultiHash result(); @@ -172,6 +173,8 @@ private: void addInternalDictionary(CommonDictInterface*, bool); //void writeConfig(QString key, QString value); + History* _history; + }; #endif // BACKBONE_H diff --git a/trunk/src/base/base.pro b/trunk/src/base/base.pro index 2c6a578..fe9c278 100644 --- a/trunk/src/base/base.pro +++ b/trunk/src/base/base.pro @@ -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 index 0000000..62f5d3e --- /dev/null +++ b/trunk/src/base/gui/HistoryListDialog.cpp @@ -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 . + + 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; iaddItem(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 index 0000000..4a60faa --- /dev/null +++ b/trunk/src/base/gui/HistoryListDialog.h @@ -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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +//Created by Mateusz Półrola + +#ifndef HISTORYLISTDIALOG_H +#define HISTORYLISTDIALOG_H + +#include +#include "SearchBarWidget.h" +#include "../../includes/History.h" +#include + +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 diff --git a/trunk/src/base/gui/SearchBarWidget.cpp b/trunk/src/base/gui/SearchBarWidget.cpp index 94b0045..476ec26 100644 --- a/trunk/src/base/gui/SearchBarWidget.cpp +++ b/trunk/src/base/gui/SearchBarWidget.cpp @@ -25,6 +25,7 @@ #include "SearchBarWidget.h" #include #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); + } +} diff --git a/trunk/src/base/gui/SearchBarWidget.h b/trunk/src/base/gui/SearchBarWidget.h index fbceb03..ca8ebb4 100644 --- a/trunk/src/base/gui/SearchBarWidget.h +++ b/trunk/src/base/gui/SearchBarWidget.h @@ -28,7 +28,7 @@ #include #include #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 index 0000000..ac0b1bf --- /dev/null +++ b/trunk/src/includes/History.h @@ -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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +//Created by Mateusz Półrola + +#ifndef HISTORY_H +#define HISTORY_H + +#include +#include +#include + +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 _history; + int _maxSize; + int currentElement; + bool _prevAvailable; + bool _nextAvailable; + bool _listAvailable; +}; + +#endif // HISTORY_H -- 1.7.9.5