Little change in search bar icons
[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 oryginal, qreal rotation) {
77     QPixmap p = oryginal.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     p.setMask(p.createHeuristicMask());
92    /* #ifdef Q_WS_MAEMO_5
93         QPainter painter(&p);
94         QColor c =  palette().background().color();
95        // c.setAlpha(192);
96         painter.fillRect(p.rect(), c);
97
98         newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
99     #endif*/
100
101     return newIcon;
102 }
103
104
105 void SearchBarWidget::initializeUI() {
106
107     #ifdef Q_WS_MAEMO_5
108         setMaximumHeight(150);
109     #else
110         setMaximumHeight(100);
111     #endif
112
113
114     horizontalLayout = new QHBoxLayout;
115     verticalLayout = new QVBoxLayout;
116
117
118     searchPushButton = new QPushButton(tr("Search"));
119     searchPushButton->setMinimumWidth(125);
120
121
122     searchWordLineEdit = new QLineEdit;
123     searchWordLineEdit->setMinimumWidth(250);
124
125
126
127     completerModel = new QStringListModel(this);
128
129
130     lineEditCompleter = new QCompleter(searchWordLineEdit);
131     lineEditCompleter->setModel(completerModel);
132     lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
133     lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
134     searchWordLineEdit->setCompleter(lineEditCompleter);
135
136
137     #ifndef Q_WS_MAEMO_5
138         searchWordLineEdit->setMinimumHeight(
139                 searchWordLineEdit->sizeHint().height()*3/2);
140     #endif
141
142
143     //create layout for lineEdit to have clear button on it
144     QHBoxLayout* lineEditLayout = new QHBoxLayout;
145     searchWordLineEdit->setLayout(lineEditLayout);
146
147
148     clearSearchWordToolButton = new QToolButton;
149     #ifdef Q_WS_MAEMO_5
150         clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
151         clearSearchWordToolButton->setMaximumSize(
152                 clearSearchWordToolButton->sizeHint().height()/2,
153                 clearSearchWordToolButton->sizeHint().height()/2);
154         lineEditLayout->setContentsMargins(0,0,10,0);
155     #else
156         clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
157         clearSearchWordToolButton->setMinimumSize(
158                 searchWordLineEdit->sizeHint().height()*1.2,
159                 searchWordLineEdit->sizeHint().height()*1.2);
160         lineEditLayout->setContentsMargins(0,0,5,0);
161     #endif
162
163
164     historyNextToolButton = new QToolButton;
165     #ifdef Q_WS_MAEMO_5
166         historyNextToolButton->setIcon(
167                 generateIcon(QIcon::fromTheme("general_forward")));
168     #else
169         historyNextToolButton->setIcon(
170                 generateIcon(QIcon::fromTheme("go-next")));
171     #endif
172
173
174
175     historyPrevToolButton = new QToolButton;
176     #ifdef Q_WS_MAEMO_5
177         historyPrevToolButton->setIcon(
178                 generateIcon(QIcon::fromTheme("general_back")));
179     #else
180         historyPrevToolButton->setIcon(
181                 generateIcon(QIcon::fromTheme("go-previous")));
182     #endif
183
184
185
186     historyShowToolButton = new QToolButton;
187     #ifdef Q_WS_MAEMO_5
188         historyShowToolButton->setIcon(
189                 generateIcon(QIcon::fromTheme("general_back"), 90));
190     #else
191         historyShowToolButton->setIcon(
192                 generateIcon(QIcon::fromTheme("go-up")));
193     #endif
194
195     searchingProgressBar = new QProgressBar;
196     //progress bar has minimum and maximum values set to 0, which will effect
197     //with "I'm alive" bar
198     searchingProgressBar->setMinimum(0);
199     searchingProgressBar->setMaximum(0);
200     #ifdef Q_WS_MAEMO_5
201         searchingProgressBar->setMaximumHeight(50);
202     #endif
203     searchingProgressBar->hide();
204
205
206     setLayout(verticalLayout);
207
208     verticalLayout->addWidget(searchingProgressBar);
209
210     //adding widgets to layout
211     horizontalLayout->addWidget(searchWordLineEdit);
212     horizontalLayout->addWidget(searchPushButton);
213     horizontalLayout->addWidget(historyPrevToolButton);
214     horizontalLayout->addWidget(historyShowToolButton);
215     horizontalLayout->addWidget(historyNextToolButton);
216
217     //adding clear toolButton to textEdit with right alignment
218     lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
219
220
221     verticalLayout->addLayout(horizontalLayout);
222 }
223
224
225 void SearchBarWidget::searchPushButtonClicked() {
226     if(busy) {
227         Q_EMIT stopSearching();
228     }
229     else {
230         search(searchWordLineEdit->text());
231     }
232 }
233
234
235 void SearchBarWidget::search(QString word) {
236     if(!busy && !word.isEmpty()) {
237         completerModel->insertRow(completerModel->rowCount());
238         QModelIndex index =
239                 completerModel->index(completerModel->rowCount() -1);
240
241         completerModel->setData(index, word);
242
243
244         searchWordLineEdit->setText(word);
245         Q_EMIT searchForTranslations(word);
246     }
247 }
248
249 void SearchBarWidget::searchDelay(QString word) {
250     if(!busy && !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(!busy) {
266         Q_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(busy) return;
282     searchingProgressBar->show();
283     searchPushButton->setText(tr("Stop"));
284     setEnabled(false);
285     busy = true;
286 }
287
288 void SearchBarWidget::setIdle() {
289     if(!busy) return;
290     searchingProgressBar->hide();
291     searchPushButton->setText(tr("Search"));
292     setEnabled(true);
293     busy = false;
294     Q_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(!busy) {
306         historyPrevToolButton->setEnabled(prev);
307         historyNextToolButton->setEnabled(next);
308         historyShowToolButton->setEnabled(list);
309     }
310 }
311
312 void SearchBarWidget::showHistoryButtonClicked() {
313     #ifdef Q_WS_MAEMO_5
314         emit historyShow();
315     #else
316         QPoint p = historyShowToolButton->pos();
317         p.setY(p.y());
318         emit historyShow(mapToGlobal(p));
319     #endif
320 }