Changed repo structure
[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 "../../common/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     if(rotation != 0) {
80         QMatrix m;
81         m.rotate(rotation);
82
83         p = p.transformed(m);
84     }
85
86     QIcon newIcon;
87     newIcon.addPixmap(p);
88
89
90     #ifdef Q_WS_MAEMO_5
91         QPainter painter(&p);
92         painter.fillRect(p.rect(), QColor(0,0,0,192));
93
94         newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
95     #endif
96
97     return newIcon;
98 }
99
100
101 void SearchBarWidget::initializeUI() {
102
103     #ifdef Q_WS_MAEMO_5
104         setMaximumHeight(150);
105     #else
106         setMaximumHeight(100);
107     #endif
108
109
110     horizontalLayout = new QHBoxLayout;
111     verticalLayout = new QVBoxLayout;
112
113
114     searchPushButton = new QPushButton(tr("Search"));
115     searchPushButton->setMinimumWidth(125);
116
117
118     searchWordLineEdit = new QLineEdit;
119     searchWordLineEdit->setMinimumWidth(250);
120
121
122
123     completerModel = new QStringListModel;
124
125
126     lineEditCompleter = new QCompleter(searchWordLineEdit);
127     lineEditCompleter->setModel(completerModel);
128     lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
129     lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
130     searchWordLineEdit->setCompleter(lineEditCompleter);
131
132
133     #ifndef Q_WS_MAEMO_5
134         searchWordLineEdit->setMinimumHeight(
135                 searchWordLineEdit->sizeHint().height()*3/2);
136     #endif
137
138
139     //create layout for lineEdit to have clear button on it
140     QHBoxLayout* lineEditLayout = new QHBoxLayout;
141     searchWordLineEdit->setLayout(lineEditLayout);
142
143
144     clearSearchWordToolButton = new QToolButton;
145     #ifdef Q_WS_MAEMO_5
146         clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
147         clearSearchWordToolButton->setMaximumSize(
148                 clearSearchWordToolButton->sizeHint().height()/2,
149                 clearSearchWordToolButton->sizeHint().height()/2);
150         lineEditLayout->setContentsMargins(0,0,10,0);
151     #else
152         clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
153         clearSearchWordToolButton->setMinimumSize(
154                 searchWordLineEdit->sizeHint().height()*1.2,
155                 searchWordLineEdit->sizeHint().height()*1.2);
156         lineEditLayout->setContentsMargins(0,0,5,0);
157     #endif
158
159
160     historyNextToolButton = new QToolButton;
161     #ifdef Q_WS_MAEMO_5
162         historyNextToolButton->setIcon(
163                 generateIcon(QIcon::fromTheme("general_forward")));
164     #else
165         historyNextToolButton->setIcon(
166                 generateIcon(QIcon::fromTheme("go-next")));
167     #endif
168
169
170
171     historyPrevToolButton = new QToolButton;
172     #ifdef Q_WS_MAEMO_5
173         historyPrevToolButton->setIcon(
174                 generateIcon(QIcon::fromTheme("general_back")));
175     #else
176         historyPrevToolButton->setIcon(
177                 generateIcon(QIcon::fromTheme("go-previous")));
178     #endif
179
180
181
182     historyShowToolButton = new QToolButton;
183     #ifdef Q_WS_MAEMO_5
184         historyShowToolButton->setIcon(
185                 generateIcon(QIcon::fromTheme("general_back"), 90));
186     #else
187         historyShowToolButton->setIcon(
188                 generateIcon(QIcon::fromTheme("go-up")));
189     #endif
190
191     searchingProgressBar = new QProgressBar;
192     //progress bar have minimum and maximum values set to 0, which will effect
193     //with "I'm alive" bar
194     searchingProgressBar->setMinimum(0);
195     searchingProgressBar->setMaximum(0);
196     #ifdef Q_WS_MAEMO_5
197         searchingProgressBar->setMaximumHeight(50);
198     #endif
199     searchingProgressBar->hide();
200
201
202     setLayout(verticalLayout);
203
204     verticalLayout->addWidget(searchingProgressBar);
205
206     //adding widgets to layout
207     horizontalLayout->addWidget(searchWordLineEdit);
208     horizontalLayout->addWidget(searchPushButton);
209     horizontalLayout->addWidget(historyPrevToolButton);
210     horizontalLayout->addWidget(historyShowToolButton);
211     horizontalLayout->addWidget(historyNextToolButton);
212
213     //adding clear toolButton to textEdit with right alignment
214     lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
215
216
217     verticalLayout->addLayout(horizontalLayout);
218 }
219
220
221 void SearchBarWidget::searchPushButtonClicked() {
222     if(busy) {
223         Q_EMIT stopSearching();
224     }
225     else {
226         search(searchWordLineEdit->text());
227     }
228 }
229
230
231 void SearchBarWidget::search(QString word) {
232     if(!busy && !word.isEmpty()) {
233         completerModel->insertRow(completerModel->rowCount());
234         QModelIndex index =
235                 completerModel->index(completerModel->rowCount() -1);
236
237         completerModel->setData(index, word);
238
239
240         searchWordLineEdit->setText(word);
241         Q_EMIT searchForTranslations(word);
242     }
243 }
244
245 void SearchBarWidget::searchDelay(QString word) {
246     if(!busy && !word.isEmpty()) {
247         searchWordLineEdit->setText(word);
248
249
250         if(delayTimer.isActive()) {
251             delayTimer.stop();
252         }
253
254         delayString = word;
255         delayTimer.start(500);
256     }
257 }
258
259 void SearchBarWidget::delaySearchTimeout() {
260     delayTimer.stop();
261     if(!busy) {
262         Q_EMIT searchForTranslations(delayString);
263     }
264 }
265
266 void SearchBarWidget::setEnabled(bool enabled) {
267     searchWordLineEdit->setEnabled(enabled);
268
269     if(!enabled) {
270         historyPrevToolButton->setEnabled(false);
271         historyNextToolButton->setEnabled(false);
272         historyShowToolButton->setEnabled(false);
273     }
274 }
275
276 void SearchBarWidget::setBusy() {
277     if(busy) return;
278     searchingProgressBar->show();
279     searchPushButton->setText(tr("Stop"));
280     setEnabled(false);
281     busy = true;
282 }
283
284 void SearchBarWidget::setIdle() {
285     if(!busy) return;
286     searchingProgressBar->hide();
287     searchPushButton->setText(tr("Search"));
288     setEnabled(true);
289     busy = false;
290     Q_EMIT refreshHistoryButtons();
291 }
292
293
294 void SearchBarWidget::clearSearchWordToolButtonClicked() {
295     searchWordLineEdit->clear();
296 }
297
298
299
300 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
301     if(!busy) {
302         historyPrevToolButton->setEnabled(prev);
303         historyNextToolButton->setEnabled(next);
304         historyShowToolButton->setEnabled(list);
305     }
306 }
307
308 void SearchBarWidget::showHistoryButtonClicked() {
309     #ifdef Q_WS_MAEMO_5
310         emit historyShow();
311     #else
312         QPoint p = historyShowToolButton->pos();
313         p.setY(p.y());
314         emit historyShow(mapToGlobal(p));
315     #endif
316 }