Clean and order documentation in source files. Source ready to beta 2 release
[mdictionary] / src / mdictionary / 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 /*! \file SearchBarWidget.cpp
23     \brief Displays search bar
24
25     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 */
27
28
29 #include "SearchBarWidget.h"
30 #include <QDebug>
31 #include "../../include/DictDialog.h"
32 #include "HistoryListDialog.h"
33
34
35 SearchBarWidget::SearchBarWidget(QWidget *parent) :
36     QWidget(parent) {
37
38     initializeUI();
39
40
41     busy = false;
42
43     connect(searchPushButton, SIGNAL(clicked()),
44             this, SLOT(searchPushButtonClicked()));
45
46     connect(searchWordLineEdit, SIGNAL(returnPressed()),
47             this, SLOT(searchPushButtonClicked()));
48
49     connect(historyNextToolButton, SIGNAL(clicked()),
50             this, SIGNAL(historyNext()));
51
52     connect(historyPrevToolButton, SIGNAL(clicked()),
53             this, SIGNAL(historyPrev()));
54
55     connect(historyShowToolButton, SIGNAL(clicked()),
56             this, SLOT(showHistoryButtonClicked()));
57
58     connect(clearSearchWordToolButton, SIGNAL(clicked()),
59             this, SLOT(clearSearchWordToolButtonClicked()));
60
61
62     connect(&delayTimer, SIGNAL(timeout()),
63             this, SLOT(delaySearchTimeout()));
64
65
66     searchWordLineEdit->setFocus();
67
68     historyPrevToolButton->setEnabled(false);
69     historyNextToolButton->setEnabled(false);
70     historyShowToolButton->setEnabled(false);
71
72     setEnabled(true);
73 }
74
75 SearchBarWidget::~SearchBarWidget() {
76
77 }
78
79 QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
80     QPixmap p = original.pixmap(64);
81
82
83     if(rotation != 0) {
84         QMatrix m;
85         m.rotate(rotation);
86
87         p = p.transformed(m);
88     }
89
90     QIcon newIcon;
91     newIcon.addPixmap(p);
92
93
94     #ifdef Q_WS_MAEMO_5
95         QImage img = p.toImage();
96
97         for(int i=0; i < img.width(); i++) {
98             for(int j=0; j < img.height(); j++) {
99                 QColor c = img.pixel(i,j);
100                 if(c != QColor(0,0,0,255)) {
101                     c.setRed(c.red()/2);
102                     c.setGreen(c.green()/2);
103                     c.setBlue(c.blue()/2);
104                     img.setPixel(i, j, c.rgb());
105                 }
106             }
107         }
108         p = p.fromImage(img);
109
110         newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
111     #endif
112
113     return newIcon;
114 }
115
116
117 void SearchBarWidget::setFocus() {
118     searchWordLineEdit->setFocus();
119 }
120
121 void SearchBarWidget::initializeUI() {
122
123     #ifdef Q_WS_MAEMO_5
124         setMaximumHeight(150);
125     #else
126         setMaximumHeight(100);
127     #endif
128
129
130     horizontalLayout = new QHBoxLayout;
131     verticalLayout = new QVBoxLayout;
132
133
134     searchPushButton = new QPushButton(tr("Search"));
135     searchPushButton->setMinimumWidth(125);
136
137
138     searchWordLineEdit = new QLineEdit;
139     searchWordLineEdit->setMinimumWidth(250);
140
141
142
143     completerModel = new QStringListModel(this);
144
145
146     lineEditCompleter = new QCompleter(searchWordLineEdit);
147     lineEditCompleter->setModel(completerModel);
148     lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
149     lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
150     searchWordLineEdit->setCompleter(lineEditCompleter);
151
152
153     #ifndef Q_WS_MAEMO_5
154         searchWordLineEdit->setMinimumHeight(
155                 searchWordLineEdit->sizeHint().height()*3/2);
156     #endif
157
158
159     //create layout for lineEdit to have clear button on it
160     QHBoxLayout* lineEditLayout = new QHBoxLayout;
161     searchWordLineEdit->setLayout(lineEditLayout);
162
163
164     clearSearchWordToolButton = new QToolButton;
165     #ifdef Q_WS_MAEMO_5
166         clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
167         clearSearchWordToolButton->setMaximumSize(
168                 clearSearchWordToolButton->sizeHint().height()/2,
169                 clearSearchWordToolButton->sizeHint().height()/2);
170         lineEditLayout->setContentsMargins(0,0,15,0);
171     #else
172         clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
173         clearSearchWordToolButton->setMinimumSize(
174                 searchWordLineEdit->sizeHint().height()*1.2,
175                 searchWordLineEdit->sizeHint().height()*1.2);
176         lineEditLayout->setContentsMargins(0,0,5,0);
177     #endif
178
179
180     historyNextToolButton = new QToolButton;
181     #ifdef Q_WS_MAEMO_5
182         historyNextToolButton->setIcon(
183                 generateIcon(QIcon::fromTheme("general_forward")));
184     #else
185         historyNextToolButton->setIcon(
186                 generateIcon(QIcon::fromTheme("go-next")));
187     #endif
188
189
190
191     historyPrevToolButton = new QToolButton;
192     #ifdef Q_WS_MAEMO_5
193         historyPrevToolButton->setIcon(
194                 generateIcon(QIcon::fromTheme("general_back")));
195     #else
196         historyPrevToolButton->setIcon(
197                 generateIcon(QIcon::fromTheme("go-previous")));
198     #endif
199
200
201
202     historyShowToolButton = new QToolButton;
203     #ifdef Q_WS_MAEMO_5
204         historyShowToolButton->setIcon(
205                 generateIcon(QIcon::fromTheme("general_back"), 90));
206     #else
207         historyShowToolButton->setIcon(
208                 generateIcon(QIcon::fromTheme("go-up")));
209     #endif
210
211     searchingProgressBar = new QProgressBar;
212     //progress bar has minimum and maximum values set to 0, which will effect
213     //with "I'm alive" bar
214     searchingProgressBar->setMinimum(0);
215     searchingProgressBar->setMaximum(0);
216     #ifdef Q_WS_MAEMO_5
217         searchingProgressBar->setMaximumHeight(50);
218     #endif
219     searchingProgressBar->hide();
220
221
222     setLayout(verticalLayout);
223
224     verticalLayout->addWidget(searchingProgressBar);
225
226     //adding widgets to layout
227     horizontalLayout->addWidget(searchWordLineEdit);
228     horizontalLayout->addWidget(searchPushButton);
229     horizontalLayout->addWidget(historyPrevToolButton);
230     horizontalLayout->addWidget(historyShowToolButton);
231     horizontalLayout->addWidget(historyNextToolButton);
232
233     //adding clear toolButton to textEdit with right alignment
234     lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
235
236
237     verticalLayout->addLayout(horizontalLayout);
238 }
239
240
241 void SearchBarWidget::searchPushButtonClicked() {
242     if(busy) {
243         Q_EMIT stopSearching();
244     }
245     else {
246         search(searchWordLineEdit->text());
247     }
248 }
249
250
251 void SearchBarWidget::search(QString word) {
252     if(!busy && !word.isEmpty()) {
253         completerModel->insertRow(completerModel->rowCount());
254         QModelIndex index =
255                 completerModel->index(completerModel->rowCount() -1);
256
257         completerModel->setData(index, word);
258
259
260         searchWordLineEdit->setText(word);
261         Q_EMIT searchForTranslations(word);
262     }
263 }
264
265 void SearchBarWidget::searchDelay(QString word) {
266     if(!busy && !word.isEmpty()) {
267         searchWordLineEdit->setText(word);
268
269
270         if(delayTimer.isActive()) {
271             delayTimer.stop();
272         }
273
274         delayString = word;
275         delayTimer.start(500);
276     }
277 }
278
279 void SearchBarWidget::delaySearchTimeout() {
280     delayTimer.stop();
281     if(!busy) {
282         Q_EMIT searchForTranslations(delayString);
283     }
284 }
285
286 void SearchBarWidget::setEnabled(bool enabled) {
287     searchWordLineEdit->setEnabled(enabled);
288
289     if(!enabled) {
290         historyPrevToolButton->setEnabled(false);
291         historyNextToolButton->setEnabled(false);
292         historyShowToolButton->setEnabled(false);
293     }
294 }
295
296 void SearchBarWidget::setBusy() {
297     if(busy) return;
298     searchingProgressBar->show();
299     searchPushButton->setText(tr("Stop"));
300     setEnabled(false);
301     busy = true;
302 }
303
304 void SearchBarWidget::setIdle() {
305     if(!busy) return;
306     searchingProgressBar->hide();
307     searchPushButton->setText(tr("Search"));
308     setEnabled(true);
309     busy = false;
310     Q_EMIT refreshHistoryButtons();
311 }
312
313
314 void SearchBarWidget::clearSearchWordToolButtonClicked() {
315     searchWordLineEdit->clear();
316 }
317
318
319
320 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
321     if(!busy) {
322         historyPrevToolButton->setEnabled(prev);
323         historyNextToolButton->setEnabled(next);
324         historyShowToolButton->setEnabled(list);
325     }
326 }
327
328 void SearchBarWidget::showHistoryButtonClicked() {
329     #ifdef Q_WS_MAEMO_5
330         emit historyShow();
331     #else
332         QPoint p = historyShowToolButton->pos();
333         p.setY(p.y());
334         emit historyShow(mapToGlobal(p));
335     #endif
336 }