154abfad56f9351804b904aed94a59ac9fde3cb1
[mdictionary] / src / mdictionary / backbone / History.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
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.
9
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.
14
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/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //Created by Mateusz Półrola
23
24 #include "../../include/History.h"
25 #include <QDebug>
26
27 History::History(int maxSize, QObject *parent) :
28     QObject(parent)
29 {
30     _maxSize = maxSize;
31     currentElement = -1;
32     _prevAvailable = false;
33     _nextAvailable = false;
34     _listAvailable = false;
35 }
36
37 void History::setMaxSize(int size) {
38     if(maxSize() <= 0) return;
39     if(size > _maxSize) {
40         _maxSize = size;
41     }
42     else {
43         _maxSize = size;
44         if(currentElement > 0) {
45             _history.remove(0, currentElement);
46         }
47
48         while(_history.size() > _maxSize) {
49             _history.pop_back();
50         }
51
52         setCurrentElement(0);
53     }
54 }
55
56 int History::maxSize() {
57     return _maxSize;
58 }
59
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)
64             return;
65     }
66
67     //if we are not in head, we deleted everything ahead of us
68     if(currentElement > 0) {
69         _history.remove(0, currentElement);
70     }
71
72
73     if(_history.contains(word)) {
74         _history.remove(_history.indexOf(word));
75     }
76
77     //add new word to head
78     _history.push_front(word);
79
80     //fit to max size
81     while(_history.size() > _maxSize) {
82         _history.pop_back();
83     }
84
85     currentElement = 0;
86
87     if(_history.size() > 1) {
88         _prevAvailable = true;
89         _nextAvailable = false;
90         _listAvailable = true;
91     }
92     else {
93         _prevAvailable = false;
94         _nextAvailable = false;
95         _listAvailable = true;
96     }
97
98     Q_EMIT historyChanged(_prevAvailable,
99                         _nextAvailable,
100                         _listAvailable);
101 }
102
103 QString History::previous() {
104     if(_prevAvailable) {
105         currentElement++;
106
107         _nextAvailable = true;
108
109         if(currentElement+1 == _history.size()) {
110             _prevAvailable = false;
111         }
112
113         Q_EMIT historyChanged(_prevAvailable,
114                             _nextAvailable,
115                             _listAvailable);
116
117         return _history[currentElement];
118     }
119     return QString();
120 }
121
122 QString History::next() {
123     if(_nextAvailable) {
124         currentElement--;
125
126         _prevAvailable = true;
127
128         if(currentElement == 0) {
129            _nextAvailable = false;
130         }
131
132         Q_EMIT historyChanged(_prevAvailable,
133                             _nextAvailable,
134                             _listAvailable);
135
136         return _history[currentElement];
137     }
138     return QString();
139 }
140
141 QStringList History::list() {
142     QStringList result;
143
144     if(_listAvailable) {
145         for(int i=0; i<_history.size(); i++) {
146             result << _history[i];
147         }
148     }
149     return result;
150 }
151
152 bool History::nextAvailable() {
153     return _nextAvailable;
154 }
155
156 bool History::prevAvailable() {
157     return _prevAvailable;
158 }
159
160 bool History::listAvailable() {
161     return _listAvailable;
162 }
163
164 void History::setCurrentElement(int element) {
165     if(element < 0 || element >= _history.size()) return;
166
167     currentElement = element;
168
169     if(currentElement > 0) {
170        _nextAvailable = true;
171     }
172     else {
173         _nextAvailable = false;
174     }
175
176     if(currentElement+1 < _history.size()) {
177         _prevAvailable = true;
178     }
179     else {
180         _prevAvailable = false;
181     }
182
183     Q_EMIT historyChanged(_prevAvailable,
184                         _nextAvailable,
185                         _listAvailable);
186 }
187
188 void History::refreshStatus() {
189     Q_EMIT historyChanged(_prevAvailable,
190                         _nextAvailable,
191                         _listAvailable);
192 }