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