Add missing xdxf caching dialog source files
[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     setMaximumHeight(150);
37
38     _isSearching = 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, SIGNAL(historyShow()));
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     setEnabled(true);
69 }
70
71 SearchBarWidget::~SearchBarWidget() {
72
73 }
74
75 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
76     QPixmap p = oryginal.pixmap(64);
77
78     if(rotation != 0) {
79         QMatrix m;
80         m.rotate(rotation);
81
82         p = p.transformed(m);
83     }
84
85
86     QIcon newIcon;
87     newIcon.addPixmap(p);
88
89     #ifdef Q_WS_MAEMO_5
90         QPainter painter(&p);
91         painter.fillRect(p.rect(), QColor(0,0,0,192));
92
93         newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
94     #endif
95
96     return newIcon;
97 }
98
99
100 void SearchBarWidget::initializeUI() {
101     horizontalLayout = new QHBoxLayout();
102     verticalLayout = new QVBoxLayout();
103
104
105     searchPushButton = new QPushButton(tr("Search"));
106     searchPushButton->setMinimumWidth(150);
107     #ifndef Q_WS_MAEMO_5
108         searchPushButton->setMinimumHeight(
109                 searchPushButton->sizeHint().height()*2);
110     #endif
111
112     searchWordLineEdit = new QLineEdit();
113     searchWordLineEdit->setMinimumWidth(300);
114
115     #ifndef Q_WS_MAEMO_5
116         searchWordLineEdit->setMinimumHeight(
117                 searchWordLineEdit->sizeHint().height()*2);
118     #endif
119
120
121
122     //create layout for lineEdit to have clear button on it
123     QHBoxLayout* lineEditLayout = new QHBoxLayout;
124     searchWordLineEdit->setLayout(lineEditLayout);
125
126
127     clearSearchWordToolButton = new QToolButton();
128
129     #ifdef Q_WS_MAEMO_5
130         clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
131         //tool buttons will have size 2 times smaller
132         clearSearchWordToolButton->setMaximumSize(
133                 clearSearchWordToolButton->sizeHint().height()/2,
134                 clearSearchWordToolButton->sizeHint().height()/2);
135     #else
136         clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
137         clearSearchWordToolButton->setMinimumSize(
138                 clearSearchWordToolButton->sizeHint().height(),
139                 clearSearchWordToolButton->sizeHint().height());
140     #endif
141
142
143     historyNextToolButton = new QToolButton();
144     #ifdef Q_WS_MAEMO_5
145         historyNextToolButton->setIcon(
146                 generateIcon(QIcon::fromTheme("general_forward")));
147     #else
148         historyNextToolButton->setIcon(
149                 generateIcon(QIcon::fromTheme("go-next")));
150         historyNextToolButton->setMinimumSize(
151                 clearSearchWordToolButton->sizeHint().height()*2,
152                 clearSearchWordToolButton->sizeHint().height()*2);
153     #endif
154
155     historyPrevToolButton = new QToolButton();
156     #ifdef Q_WS_MAEMO_5
157         historyPrevToolButton->setIcon(
158                 generateIcon(QIcon::fromTheme("general_back")));
159     #else
160         historyPrevToolButton->setIcon(
161                 generateIcon(QIcon::fromTheme("go-previous")));
162         historyPrevToolButton->setMinimumSize(
163                 clearSearchWordToolButton->sizeHint().height()*2,
164                 clearSearchWordToolButton->sizeHint().height()*2);
165     #endif
166
167     historyShowToolButton = new QToolButton();
168     #ifdef Q_WS_MAEMO_5
169         historyShowToolButton->setIcon(
170                 generateIcon(QIcon::fromTheme("general_back"), 90));
171     #else
172         historyShowToolButton->setIcon(
173                 generateIcon(QIcon::fromTheme("go-up")));
174         historyShowToolButton->setMinimumSize(
175                 clearSearchWordToolButton->sizeHint().height()*2,
176                 clearSearchWordToolButton->sizeHint().height()*2);
177     #endif
178
179
180     searchingProgressBar = new QProgressBar();
181     //progress bar have minimum and maximum values set to 0, which will effect
182     //with "I'm alive" bar
183     searchingProgressBar->setMinimum(0);
184     searchingProgressBar->setMaximum(0);
185     searchingProgressBar->hide();
186     searchingProgressBar->setMaximumHeight(50);
187
188
189     setLayout(verticalLayout);
190
191     verticalLayout->addWidget(searchingProgressBar);
192
193     //adding widgets to layout
194     horizontalLayout->addWidget(searchWordLineEdit);
195     horizontalLayout->addWidget(searchPushButton);
196     horizontalLayout->addWidget(historyPrevToolButton);
197     horizontalLayout->addWidget(historyShowToolButton);
198     horizontalLayout->addWidget(historyNextToolButton);
199
200     //adding clear toolButton to textEdit with right alignment
201     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
202                               Qt::AlignRight | Qt::AlignVCenter);
203
204     verticalLayout->addLayout(horizontalLayout);
205 }
206
207
208 void SearchBarWidget::searchPushButtonClicked() {
209     if(_isSearching) {
210         emit stopSearching();
211     }
212     else {
213         search(searchWordLineEdit->text());
214     }
215 }
216
217
218 void SearchBarWidget::search(QString word) {
219     if(!_isSearching && !word.isEmpty()) {
220         searchWordLineEdit->setText(word);
221         emit searchForTranslations(word);
222     }
223 }
224
225 void SearchBarWidget::searchDelay(QString word) {
226     if(!_isSearching && !word.isEmpty()) {
227         searchWordLineEdit->setText(word);
228
229
230         if(delayTimer.isActive()) {
231             delayTimer.stop();
232         }
233
234         delayString = word;
235         delayTimer.start(500);
236     }
237 }
238
239 void SearchBarWidget::delaySearchTimeout() {
240     delayTimer.stop();
241     if(!_isSearching) {
242         emit searchForTranslations(delayString);
243     }
244 }
245
246 void SearchBarWidget::setEnabled(bool enabled) {
247     searchWordLineEdit->setEnabled(enabled);
248
249     if(!enabled) {
250         historyPrevToolButton->setEnabled(false);
251         historyNextToolButton->setEnabled(false);
252         historyShowToolButton->setEnabled(false);
253     }
254 }
255
256 void SearchBarWidget::setBusy() {
257     if(_isSearching) return;
258     searchingProgressBar->show();
259     searchPushButton->setText(tr("Stop"));
260     setEnabled(false);
261     _isSearching = true;
262 }
263
264 void SearchBarWidget::setIdle() {
265     if(!_isSearching) return;
266     searchingProgressBar->hide();
267     searchPushButton->setText(tr("Search"));
268     setEnabled(true);
269     _isSearching = false;
270     emit refreshHistoryButtons();
271 }
272
273
274 void SearchBarWidget::clearSearchWordToolButtonClicked() {
275     searchWordLineEdit->clear();
276 }
277
278
279
280 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
281     if(!_isSearching) {
282         historyPrevToolButton->setEnabled(prev);
283         historyNextToolButton->setEnabled(next);
284         historyShowToolButton->setEnabled(list);
285     }
286 }