1 /*******************************************************************************
3 This file is part of mDictionary.
5 mDictionary is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 mDictionary is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mDictionary. If not, see <http://www.gnu.org/licenses/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
23 \brief Object with search history
25 \author Mateusz Półrola <mateusz.polrola@comarch.pl>
28 #include "../../include/History.h"
31 History::History(int maxSize, QObject *parent) :
36 _prevAvailable = false;
37 _nextAvailable = false;
38 _listAvailable = false;
41 void History::setMaxSize(int size) {
42 if(maxSize() <= 0) return;
48 if(currentElement > 0) {
49 _history.remove(0, currentElement);
52 while(_history.size() > _maxSize) {
60 int History::maxSize() {
64 void History::add(QString word) {
65 if(currentElement != -1) {
66 //we search for the same word so we don't add it again
67 if(_history[currentElement] == word)
71 //if we are not in head, we deleted everything ahead of us
72 if(currentElement > 0) {
73 _history.remove(0, currentElement);
77 if(_history.contains(word)) {
78 _history.remove(_history.indexOf(word));
81 //add new word to head
82 _history.push_front(word);
85 while(_history.size() > _maxSize) {
91 if(_history.size() > 1) {
92 _prevAvailable = true;
93 _nextAvailable = false;
94 _listAvailable = true;
97 _prevAvailable = false;
98 _nextAvailable = false;
99 _listAvailable = true;
102 Q_EMIT historyChanged(_prevAvailable,
107 QString History::previous() {
111 _nextAvailable = true;
113 if(currentElement+1 == _history.size()) {
114 _prevAvailable = false;
117 Q_EMIT historyChanged(_prevAvailable,
121 return _history[currentElement];
126 QString History::next() {
130 _prevAvailable = true;
132 if(currentElement == 0) {
133 _nextAvailable = false;
136 Q_EMIT historyChanged(_prevAvailable,
140 return _history[currentElement];
145 QStringList History::list() {
149 for(int i=0; i<_history.size(); i++) {
150 result << _history[i];
156 bool History::nextAvailable() {
157 return _nextAvailable;
160 bool History::prevAvailable() {
161 return _prevAvailable;
164 bool History::listAvailable() {
165 return _listAvailable;
168 void History::setCurrentElement(int element) {
169 if(element < 0 || element >= _history.size()) return;
171 currentElement = element;
173 if(currentElement > 0) {
174 _nextAvailable = true;
177 _nextAvailable = false;
180 if(currentElement+1 < _history.size()) {
181 _prevAvailable = true;
184 _prevAvailable = false;
187 Q_EMIT historyChanged(_prevAvailable,
192 void History::refreshStatus() {
193 Q_EMIT historyChanged(_prevAvailable,