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