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 *******************************************************************************/
22 //Created by Mateusz Półrola
24 #include "../../include/History.h"
27 History::History(int maxSize, QObject *parent) :
32 _prevAvailable = false;
33 _nextAvailable = false;
34 _listAvailable = false;
38 void History::setMaxSize(int size) {
39 if(maxSize() <= 0) return;
45 if(currentElement > 0) {
46 _history.remove(0, currentElement);
49 while(_history.size() > _maxSize) {
57 int History::maxSize() {
61 void History::add(QString word) {
62 if(currentElement != -1) {
63 //we search the same word so we don't add it again
64 if(_history[currentElement] == word)
68 //if we are not in head, we deleted everything ahead of us
69 if(currentElement > 0) {
70 _history.remove(0, currentElement);
74 if(_history.contains(word)) {
75 _history.remove(_history.indexOf(word));
78 //add new word to head
79 _history.push_front(word);
82 while(_history.size() > _maxSize) {
88 if(_history.size() > 1) {
89 _prevAvailable = true;
90 _nextAvailable = false;
91 _listAvailable = true;
94 _prevAvailable = false;
95 _nextAvailable = false;
96 _listAvailable = true;
99 Q_EMIT historyChanged(_prevAvailable,
104 QString History::previous() {
108 _nextAvailable = true;
110 if(currentElement+1 == _history.size()) {
111 _prevAvailable = false;
114 Q_EMIT historyChanged(_prevAvailable,
118 return _history[currentElement];
123 QString History::next() {
127 _prevAvailable = true;
129 if(currentElement == 0) {
130 _nextAvailable = false;
133 Q_EMIT historyChanged(_prevAvailable,
137 return _history[currentElement];
142 QStringList History::list() {
146 for(int i=0; i<_history.size(); i++) {
147 result << _history[i];
153 bool History::nextAvailable() {
154 return _nextAvailable;
157 bool History::prevAvailable() {
158 return _prevAvailable;
161 bool History::listAvailable() {
162 return _listAvailable;
165 void History::setCurrentElement(int element) {
166 if(element < 0 || element >= _history.size()) return;
168 currentElement = element;
170 if(currentElement > 0) {
171 _nextAvailable = true;
174 _nextAvailable = false;
177 if(currentElement+1 < _history.size()) {
178 _prevAvailable = true;
181 _prevAvailable = false;
184 Q_EMIT historyChanged(_prevAvailable,
189 void History::refreshStatus() {
190 Q_EMIT historyChanged(_prevAvailable,