Added history
[mdictionary] / trunk / src / base / 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 "../../includes/History.h"
25 #include <QDebug>
26
27 History::History(QObject *parent) :
28     QObject(parent)
29 {
30     _maxSize = 5;
31     currentElement = -1;
32     _prevAvailable = false;
33     _nextAvailable = false;
34     _listAvailable = false;
35 }
36
37
38 void History::add(QString word) {
39     if(currentElement != -1) {
40         //we search the same word so we don't add it again
41         if(_history[currentElement] == word)
42             return;
43     }
44
45     //if we are not in head, we deleted everything ahed us
46     if(currentElement > 0) {
47         _history.remove(0, currentElement);
48     }
49
50
51     if(_history.contains(word)) {
52         _history.remove(_history.indexOf(word));
53     }
54
55     //add new word to head
56     _history.push_front(word);
57
58     //fit to max size
59     while(_history.size() > _maxSize) {
60         _history.pop_back();
61     }
62
63     currentElement = 0;
64
65     if(_history.size() > 1) {
66         _prevAvailable = true;
67         _nextAvailable = false;
68         _listAvailable = true;
69     }
70     else {
71         _prevAvailable = false;
72         _nextAvailable = false;
73         _listAvailable = true;
74     }
75
76     emit historyChanged(_prevAvailable,
77                         _nextAvailable,
78                         _listAvailable);
79 }
80
81 QString History::previous() {
82     if(_prevAvailable) {
83         currentElement++;
84
85         _nextAvailable = true;
86
87         if(currentElement+1 == _history.size()) {
88             _prevAvailable = false;
89         }
90
91         emit historyChanged(_prevAvailable,
92                             _nextAvailable,
93                             _listAvailable);
94
95         return _history[currentElement];
96     }
97     return QString();
98 }
99
100 QString History::next() {
101     if(_nextAvailable) {
102         currentElement--;
103
104         _prevAvailable = true;
105
106         if(currentElement == 0) {
107            _nextAvailable = false;
108         }
109
110         emit historyChanged(_prevAvailable,
111                             _nextAvailable,
112                             _listAvailable);
113
114         return _history[currentElement];
115     }
116     return QString();
117 }
118
119 QStringList History::list() {
120     QStringList result;
121
122     if(_listAvailable) {
123         for(int i=0; i<_history.size(); i++) {
124             result << _history[i];
125         }
126     }
127     return result;
128 }
129
130 bool History::nextAvailable() {
131     return _nextAvailable;
132 }
133
134 bool History::prevAvailable() {
135     return _prevAvailable;
136 }
137
138 bool History::listAvailable() {
139     return _listAvailable;
140 }
141
142 void History::setCurrentElement(int element) {
143     if(element < 0 || element >= _history.size()) return;
144
145     currentElement = element;
146
147     if(currentElement > 0) {
148        _nextAvailable = true;
149     }
150     else {
151         _nextAvailable = false;
152     }
153
154     if(currentElement+1 < _history.size()) {
155         _prevAvailable = true;
156     }
157     else {
158         _prevAvailable = false;
159     }
160
161     emit historyChanged(_prevAvailable,
162                         _nextAvailable,
163                         _listAvailable);
164 }