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;
37 void History::setMaxSize(int size) {
38 if(maxSize() <= 0) return;
44 if(currentElement > 0) {
45 _history.remove(0, currentElement);
48 while(_history.size() > _maxSize) {
56 int History::maxSize() {
60 void History::add(QString word) {
61 if(currentElement != -1) {
62 //we search for the same word so we don't add it again
63 if(_history[currentElement] == word)
67 //if we are not in head, we deleted everything ahead of us
68 if(currentElement > 0) {
69 _history.remove(0, currentElement);
73 if(_history.contains(word)) {
74 _history.remove(_history.indexOf(word));
77 //add new word to head
78 _history.push_front(word);
81 while(_history.size() > _maxSize) {
87 if(_history.size() > 1) {
88 _prevAvailable = true;
89 _nextAvailable = false;
90 _listAvailable = true;
93 _prevAvailable = false;
94 _nextAvailable = false;
95 _listAvailable = true;
98 Q_EMIT historyChanged(_prevAvailable,
103 QString History::previous() {
107 _nextAvailable = true;
109 if(currentElement+1 == _history.size()) {
110 _prevAvailable = false;
113 Q_EMIT historyChanged(_prevAvailable,
117 return _history[currentElement];
122 QString History::next() {
126 _prevAvailable = true;
128 if(currentElement == 0) {
129 _nextAvailable = false;
132 Q_EMIT historyChanged(_prevAvailable,
136 return _history[currentElement];
141 QStringList History::list() {
145 for(int i=0; i<_history.size(); i++) {
146 result << _history[i];
152 bool History::nextAvailable() {
153 return _nextAvailable;
156 bool History::prevAvailable() {
157 return _prevAvailable;
160 bool History::listAvailable() {
161 return _listAvailable;
164 void History::setCurrentElement(int element) {
165 if(element < 0 || element >= _history.size()) return;
167 currentElement = element;
169 if(currentElement > 0) {
170 _nextAvailable = true;
173 _nextAvailable = false;
176 if(currentElement+1 < _history.size()) {
177 _prevAvailable = true;
180 _prevAvailable = false;
183 Q_EMIT historyChanged(_prevAvailable,
188 void History::refreshStatus() {
189 Q_EMIT historyChanged(_prevAvailable,