Fixed button positioning on line edit
[mdictionary] / trunk / src / base / 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 //Created by Mateusz Półrola
23
24
25 #include "SearchBarWidget.h"
26 #include <QDebug>
27 #include "../../includes/DictDialog.h"
28 #include "HistoryListDialog.h"
29
30
31 SearchBarWidget::SearchBarWidget(QWidget *parent) :
32     QWidget(parent) {
33
34     initializeUI();
35
36
37     _isSearching = false;
38
39     connect(searchPushButton, SIGNAL(clicked()),
40             this, SLOT(searchPushButtonClicked()));
41
42     connect(searchWordLineEdit, SIGNAL(returnPressed()),
43             this, SLOT(searchPushButtonClicked()));
44
45     connect(historyNextToolButton, SIGNAL(clicked()),
46             this, SIGNAL(historyNext()));
47
48     connect(historyPrevToolButton, SIGNAL(clicked()),
49             this, SIGNAL(historyPrev()));
50
51     connect(historyShowToolButton, SIGNAL(clicked()),
52             this, SIGNAL(historyShow()));
53
54     connect(clearSearchWordToolButton, SIGNAL(clicked()),
55             this, SLOT(clearSearchWordToolButtonClicked()));
56
57
58     connect(&delayTimer, SIGNAL(timeout()),
59             this, SLOT(delaySearchTimeout()));
60
61
62     searchWordLineEdit->setFocus();
63
64     historyPrevToolButton->setEnabled(false);
65     historyNextToolButton->setEnabled(false);
66     historyShowToolButton->setEnabled(false);
67     setEnabled(true);
68 }
69
70 SearchBarWidget::~SearchBarWidget() {
71
72 }
73
74 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
75     QPixmap p = oryginal.pixmap(64);
76
77     if(rotation != 0) {
78         QMatrix m;
79         m.rotate(rotation);
80
81         p = p.transformed(m);
82     }
83
84     QIcon newIcon;
85     newIcon.addPixmap(p);
86
87
88     #ifdef Q_WS_MAEMO_5
89         QPainter painter(&p);
90         painter.fillRect(p.rect(), QColor(0,0,0,192));
91
92         newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
93     #endif
94
95     return newIcon;
96 }
97
98
99 void SearchBarWidget::initializeUI() {
100
101     #ifdef Q_WS_MAEMO_5
102         setMaximumHeight(150);
103     #else
104         setMaximumHeight(70);
105     #endif
106
107
108     horizontalLayout = new QHBoxLayout();
109     verticalLayout = new QVBoxLayout();
110
111
112     searchPushButton = new QPushButton(tr("Search"));
113     searchPushButton->setMinimumWidth(125);
114
115
116     searchWordLineEdit = new QLineEdit();
117     searchWordLineEdit->setMinimumWidth(250);
118
119     #ifndef Q_WS_MAEMO_5
120         searchWordLineEdit->setMinimumHeight(
121                 searchWordLineEdit->sizeHint().height()*3/2);
122     #endif
123
124
125     //create layout for lineEdit to have clear button on it
126     QHBoxLayout* lineEditLayout = new QHBoxLayout;
127     searchWordLineEdit->setLayout(lineEditLayout);
128
129
130     clearSearchWordToolButton = new QToolButton();
131     #ifdef Q_WS_MAEMO_5
132         clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
133         //tool buttons will have size 2 times smaller
134         clearSearchWordToolButton->setMaximumSize(
135                 clearSearchWordToolButton->sizeHint().height()/2,
136                 clearSearchWordToolButton->sizeHint().height()/2);
137         lineEditLayout->setContentsMargins(0,0,10,0);
138     #else
139         clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
140         clearSearchWordToolButton->setMinimumSize(
141                 searchWordLineEdit->sizeHint().height()*1.2,
142                 searchWordLineEdit->sizeHint().height()*1.2);
143         lineEditLayout->setContentsMargins(0,0,5,0);
144     #endif
145
146
147     historyNextToolButton = new QToolButton();
148     #ifdef Q_WS_MAEMO_5
149         historyNextToolButton->setIcon(
150                 generateIcon(QIcon::fromTheme("general_forward")));
151     #else
152         historyNextToolButton->setIcon(
153                 generateIcon(QIcon::fromTheme("go-next")));
154     #endif
155
156
157
158     historyPrevToolButton = new QToolButton();
159     #ifdef Q_WS_MAEMO_5
160         historyPrevToolButton->setIcon(
161                 generateIcon(QIcon::fromTheme("general_back")));
162     #else
163         historyPrevToolButton->setIcon(
164                 generateIcon(QIcon::fromTheme("go-previous")));
165     #endif
166
167
168
169     historyShowToolButton = new QToolButton();
170     #ifdef Q_WS_MAEMO_5
171         historyShowToolButton->setIcon(
172                 generateIcon(QIcon::fromTheme("general_back"), 90));
173     #else
174         historyShowToolButton->setIcon(
175                 generateIcon(QIcon::fromTheme("go-up")));
176     #endif
177
178     /*fullScreenToolButton = new QToolButton();
179     #ifdef Q_WS_MAEMO_5
180         fullScreenToolButton->setIcon(
181                 generateIcon(QIcon::fromTheme("general_fullsize")));
182     #else
183         fullScreenToolButton->setIcon(
184                 generateIcon(QIcon::fromTheme("view-fullscreen")));
185         fullScreenToolButton->setMinimumSize(
186                 fullScreenToolButton->sizeHint().height()*2,
187                 fullScreenToolButton->sizeHint().height()*2);
188     #endif*/
189
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     searchingProgressBar->hide();
197     #ifdef Q_WS_MAEMO_5
198         searchingProgressBar->setMaximumHeight(50);
199     #endif
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    // horizontalLayout->addWidget(fullScreenToolButton);
213
214     //adding clear toolButton to textEdit with right alignment
215     lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
216
217
218     verticalLayout->addLayout(horizontalLayout);
219 }
220
221
222 void SearchBarWidget::searchPushButtonClicked() {
223     if(_isSearching) {
224         emit stopSearching();
225     }
226     else {
227         search(searchWordLineEdit->text());
228     }
229 }
230
231
232 void SearchBarWidget::search(QString word) {
233     if(!_isSearching && !word.isEmpty()) {
234         searchWordLineEdit->setText(word);
235         emit searchForTranslations(word);
236     }
237 }
238
239 void SearchBarWidget::searchDelay(QString word) {
240     if(!_isSearching && !word.isEmpty()) {
241         searchWordLineEdit->setText(word);
242
243
244         if(delayTimer.isActive()) {
245             delayTimer.stop();
246         }
247
248         delayString = word;
249         delayTimer.start(500);
250     }
251 }
252
253 void SearchBarWidget::delaySearchTimeout() {
254     delayTimer.stop();
255     if(!_isSearching) {
256         emit searchForTranslations(delayString);
257     }
258 }
259
260 void SearchBarWidget::setEnabled(bool enabled) {
261     searchWordLineEdit->setEnabled(enabled);
262
263     if(!enabled) {
264         historyPrevToolButton->setEnabled(false);
265         historyNextToolButton->setEnabled(false);
266         historyShowToolButton->setEnabled(false);
267     }
268 }
269
270 void SearchBarWidget::setBusy() {
271     if(_isSearching) return;
272     searchingProgressBar->show();
273     searchPushButton->setText(tr("Stop"));
274     setEnabled(false);
275     _isSearching = true;
276 }
277
278 void SearchBarWidget::setIdle() {
279     if(!_isSearching) return;
280     searchingProgressBar->hide();
281     searchPushButton->setText(tr("Search"));
282     setEnabled(true);
283     _isSearching = false;
284     emit refreshHistoryButtons();
285 }
286
287
288 void SearchBarWidget::clearSearchWordToolButtonClicked() {
289     searchWordLineEdit->clear();
290 }
291
292
293
294 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
295     if(!_isSearching) {
296         historyPrevToolButton->setEnabled(prev);
297         historyNextToolButton->setEnabled(next);
298         historyShowToolButton->setEnabled(list);
299     }
300 }