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