0fe31e81db515444c7b698f3d3c3e636789e5c0a
[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
38 void History::setMaxSize(int size) {
39     if(maxSize() <= 0) return;
40     if(size > _maxSize) {
41         _maxSize = size;
42     }
43     else {
44         _maxSize = size;
45         if(currentElement > 0) {
46             _history.remove(0, currentElement);
47         }
48
49         while(_history.size() > _maxSize) {
50             _history.pop_back();
51         }
52
53         setCurrentElement(0);
54     }
55 }
56
57 int History::maxSize() {
58     return _maxSize;
59 }
60
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)
65             return;
66     }
67
68     //if we are not in head, we deleted everything ahead of us
69     if(currentElement > 0) {
70         _history.remove(0, currentElement);
71     }
72
73
74     if(_history.contains(word)) {
75         _history.remove(_history.indexOf(word));
76     }
77
78     //add new word to head
79     _history.push_front(word);
80
81     //fit to max size
82     while(_history.size() > _maxSize) {
83         _history.pop_back();
84     }
85
86     currentElement = 0;
87
88     if(_history.size() > 1) {
89         _prevAvailable = true;
90         _nextAvailable = false;
91         _listAvailable = true;
92     }
93     else {
94         _prevAvailable = false;
95         _nextAvailable = false;
96         _listAvailable = true;
97     }
98
99     Q_EMIT historyChanged(_prevAvailable,
100                         _nextAvailable,
101                         _listAvailable);
102 }
103
104 QString History::previous() {
105     if(_prevAvailable) {
106         currentElement++;
107
108         _nextAvailable = true;
109
110         if(currentElement+1 == _history.size()) {
111             _prevAvailable = false;
112         }
113
114         Q_EMIT historyChanged(_prevAvailable,
115                             _nextAvailable,
116                             _listAvailable);
117
118         return _history[currentElement];
119     }
120     return QString();
121 }
122
123 QString History::next() {
124     if(_nextAvailable) {
125         currentElement--;
126
127         _prevAvailable = true;
128
129         if(currentElement == 0) {
130            _nextAvailable = false;
131         }
132
133         Q_EMIT historyChanged(_prevAvailable,
134                             _nextAvailable,
135                             _listAvailable);
136
137         return _history[currentElement];
138     }
139     return QString();
140 }
141
142 QStringList History::list() {
143     QStringList result;
144
145     if(_listAvailable) {
146         for(int i=0; i<_history.size(); i++) {
147             result << _history[i];
148         }
149     }
150     return result;
151 }
152
153 bool History::nextAvailable() {
154     return _nextAvailable;
155 }
156
157 bool History::prevAvailable() {
158     return _prevAvailable;
159 }
160
161 bool History::listAvailable() {
162     return _listAvailable;
163 }
164
165 void History::setCurrentElement(int element) {
166     if(element < 0 || element >= _history.size()) return;
167
168     currentElement = element;
169
170     if(currentElement > 0) {
171        _nextAvailable = true;
172     }
173     else {
174         _nextAvailable = false;
175     }
176
177     if(currentElement+1 < _history.size()) {
178         _prevAvailable = true;
179     }
180     else {
181         _prevAvailable = false;
182     }
183
184     Q_EMIT historyChanged(_prevAvailable,
185                         _nextAvailable,
186                         _listAvailable);
187 }
188
189 void History::refreshStatus() {
190     Q_EMIT historyChanged(_prevAvailable,
191                         _nextAvailable,
192                         _listAvailable);
193 }