Added documentation
[mdictionary] / trunk / src / base / gui / SearchBarWidget.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
25 #include "SearchBarWidget.h"
26 #include <QDebug>
27 #include "../../includes/DictDialog.h"
28 #include "HistoryListDialog.h"
29
30
31 SearchBarWidget::SearchBarWidget(QWidget *parent) :
32     QWidget(parent) {
33
34     initializeUI();
35
36
37     _isSearching = false;
38
39     connect(searchPushButton, SIGNAL(clicked()),
40             this, SLOT(searchPushButtonClicked()));
41
42     connect(searchWordLineEdit, SIGNAL(returnPressed()),
43             this, SLOT(searchPushButtonClicked()));
44
45     connect(historyNextToolButton, SIGNAL(clicked()),
46             this, SIGNAL(historyNext()));
47
48     connect(historyPrevToolButton, SIGNAL(clicked()),
49             this, SIGNAL(historyPrev()));
50
51     connect(historyShowToolButton, SIGNAL(clicked()),
52             this, SIGNAL(historyShow()));
53
54     connect(clearSearchWordToolButton, SIGNAL(clicked()),
55             this, SLOT(clearSearchWordToolButtonClicked()));
56
57
58     connect(&delayTimer, SIGNAL(timeout()),
59             this, SLOT(delaySearchTimeout()));
60
61
62     searchWordLineEdit->setFocus();
63
64     historyPrevToolButton->setEnabled(false);
65     historyNextToolButton->setEnabled(false);
66     historyShowToolButton->setEnabled(false);
67     setEnabled(true);
68 }
69
70 SearchBarWidget::~SearchBarWidget() {
71
72 }
73
74 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
75     QPixmap p = oryginal.pixmap(64);
76
77     if(rotation != 0) {
78         QMatrix m;
79         m.rotate(rotation);
80
81         p = p.transformed(m);
82     }
83
84
85     QIcon newIcon;
86     newIcon.addPixmap(p);
87
88     #ifdef Q_WS_MAEMO_5
89         QPainter painter(&p);
90         painter.fillRect(p.rect(), QColor(0,0,0,192));
91
92         newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
93     #endif
94
95     return newIcon;
96 }
97
98
99 void SearchBarWidget::initializeUI() {
100
101     #ifdef Q_WS_MAEMO_5
102         setMaximumHeight(150);
103     #else
104         setMaximumHeight(100);
105     #endif
106
107
108     horizontalLayout = new QHBoxLayout();
109     verticalLayout = new QVBoxLayout();
110
111
112     searchPushButton = new QPushButton(tr("Search"));
113     searchPushButton->setMinimumWidth(125);
114     #ifndef Q_WS_MAEMO_5
115         searchPushButton->setMinimumHeight(
116                 searchPushButton->sizeHint().height()*2);
117     #endif
118
119     searchWordLineEdit = new QLineEdit();
120     searchWordLineEdit->setMinimumWidth(250);
121
122     #ifndef Q_WS_MAEMO_5
123         searchWordLineEdit->setMinimumHeight(
124                 searchWordLineEdit->sizeHint().height()*2);
125     #endif
126
127
128     //create layout for lineEdit to have clear button on it
129     QHBoxLayout* lineEditLayout = new QHBoxLayout;
130     searchWordLineEdit->setLayout(lineEditLayout);
131
132
133     clearSearchWordToolButton = new QToolButton();
134     #ifdef Q_WS_MAEMO_5
135         clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
136         //tool buttons will have size 2 times smaller
137         clearSearchWordToolButton->setMaximumSize(
138                 clearSearchWordToolButton->sizeHint().height()/2,
139                 clearSearchWordToolButton->sizeHint().height()/2);
140     #else
141         clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
142         clearSearchWordToolButton->setMinimumSize(
143                 clearSearchWordToolButton->sizeHint().height(),
144                 clearSearchWordToolButton->sizeHint().height());
145     #endif
146
147
148     historyNextToolButton = new QToolButton();
149     #ifdef Q_WS_MAEMO_5
150         historyNextToolButton->setIcon(
151                 generateIcon(QIcon::fromTheme("general_forward")));
152     #else
153         historyNextToolButton->setIcon(
154                 generateIcon(QIcon::fromTheme("go-next")));
155         historyNextToolButton->setMinimumSize(
156                 clearSearchWordToolButton->sizeHint().height()*2,
157                 clearSearchWordToolButton->sizeHint().height()*2);
158     #endif
159
160
161
162     historyPrevToolButton = new QToolButton();
163     #ifdef Q_WS_MAEMO_5
164         historyPrevToolButton->setIcon(
165                 generateIcon(QIcon::fromTheme("general_back")));
166     #else
167         historyPrevToolButton->setIcon(
168                 generateIcon(QIcon::fromTheme("go-previous")));
169         historyPrevToolButton->setMinimumSize(
170                 clearSearchWordToolButton->sizeHint().height()*2,
171                 clearSearchWordToolButton->sizeHint().height()*2);
172     #endif
173
174
175
176     historyShowToolButton = new QToolButton();
177     #ifdef Q_WS_MAEMO_5
178         historyShowToolButton->setIcon(
179                 generateIcon(QIcon::fromTheme("general_back"), 90));
180     #else
181         historyShowToolButton->setIcon(
182                 generateIcon(QIcon::fromTheme("go-up")));
183         historyShowToolButton->setMinimumSize(
184                 clearSearchWordToolButton->sizeHint().height()*2,
185                 clearSearchWordToolButton->sizeHint().height()*2);
186     #endif
187
188     fullScreenToolButton = new QToolButton();
189     #ifdef Q_WS_MAEMO_5
190         fullScreenToolButton->setIcon(
191                 generateIcon(QIcon::fromTheme("general_fullsize")));
192     #else
193         fullScreenToolButton->setIcon(
194                 generateIcon(QIcon::fromTheme("view-fullscreen")));
195         fullScreenToolButton->setMinimumSize(
196                 fullScreenToolButton->sizeHint().height()*2,
197                 fullScreenToolButton->sizeHint().height()*2);
198     #endif
199
200
201     searchingProgressBar = new QProgressBar();
202     //progress bar have minimum and maximum values set to 0, which will effect
203     //with "I'm alive" bar
204     searchingProgressBar->setMinimum(0);
205     searchingProgressBar->setMaximum(0);
206     searchingProgressBar->hide();
207     #ifdef Q_WS_MAEMO_5
208         searchingProgressBar->setMaximumHeight(50);
209     #endif
210
211
212     setLayout(verticalLayout);
213
214     verticalLayout->addWidget(searchingProgressBar);
215
216     //adding widgets to layout
217     horizontalLayout->addWidget(searchWordLineEdit);
218     horizontalLayout->addWidget(searchPushButton);
219     horizontalLayout->addWidget(historyPrevToolButton);
220     horizontalLayout->addWidget(historyShowToolButton);
221     horizontalLayout->addWidget(historyNextToolButton);
222     horizontalLayout->addWidget(fullScreenToolButton);
223
224     //adding clear toolButton to textEdit with right alignment
225     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
226                               Qt::AlignRight | Qt::AlignVCenter);
227
228     verticalLayout->addLayout(horizontalLayout);
229 }
230
231
232 void SearchBarWidget::searchPushButtonClicked() {
233     if(_isSearching) {
234         emit stopSearching();
235     }
236     else {
237         search(searchWordLineEdit->text());
238     }
239 }
240
241
242 void SearchBarWidget::search(QString word) {
243     if(!_isSearching && !word.isEmpty()) {
244         searchWordLineEdit->setText(word);
245         emit searchForTranslations(word);
246     }
247 }
248
249 void SearchBarWidget::searchDelay(QString word) {
250     if(!_isSearching && !word.isEmpty()) {
251         searchWordLineEdit->setText(word);
252
253
254         if(delayTimer.isActive()) {
255             delayTimer.stop();
256         }
257
258         delayString = word;
259         delayTimer.start(500);
260     }
261 }
262
263 void SearchBarWidget::delaySearchTimeout() {
264     delayTimer.stop();
265     if(!_isSearching) {
266         emit searchForTranslations(delayString);
267     }
268 }
269
270 void SearchBarWidget::setEnabled(bool enabled) {
271     searchWordLineEdit->setEnabled(enabled);
272
273     if(!enabled) {
274         historyPrevToolButton->setEnabled(false);
275         historyNextToolButton->setEnabled(false);
276         historyShowToolButton->setEnabled(false);
277     }
278 }
279
280 void SearchBarWidget::setBusy() {
281     if(_isSearching) return;
282     searchingProgressBar->show();
283     searchPushButton->setText(tr("Stop"));
284     setEnabled(false);
285     _isSearching = true;
286 }
287
288 void SearchBarWidget::setIdle() {
289     if(!_isSearching) return;
290     searchingProgressBar->hide();
291     searchPushButton->setText(tr("Search"));
292     setEnabled(true);
293     _isSearching = false;
294     emit refreshHistoryButtons();
295 }
296
297
298 void SearchBarWidget::clearSearchWordToolButtonClicked() {
299     searchWordLineEdit->clear();
300 }
301
302
303
304 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
305     if(!_isSearching) {
306         historyPrevToolButton->setEnabled(prev);
307         historyNextToolButton->setEnabled(next);
308         historyShowToolButton->setEnabled(list);
309     }
310 }