1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 /*! \file SearchBarWidget.cpp
23 \brief Displays search bar
25 \author Mateusz Półrola <mateusz.polrola@comarch.pl>
29 #include "SearchBarWidget.h"
31 #include "../../include/DictDialog.h"
32 #include "HistoryListDialog.h"
35 SearchBarWidget::SearchBarWidget(QWidget *parent) : QWidget(parent) {
38 this->setMaximumHeight(50);
41 progressBar = new QDeclarativeView();
42 progressBar->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/ProgressBar.qml"));
43 progressBar->setResizeMode(QDeclarativeView::SizeRootObjectToView);
44 progressBar->setAlignment(Qt::AlignCenter);
47 view= new QDeclarativeView();
48 view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/SearchBarWidget.qml"));
49 view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
50 view->setAlignment(Qt::AlignCenter);
53 mainLayout = new QVBoxLayout;
54 mainLayout->addWidget(progressBar);
55 mainLayout->addWidget(view);
56 setLayout(mainLayout);
58 QGraphicsObject *rootObject = view->rootObject();
59 QGraphicsObject *rootObject2 = progressBar->rootObject();
61 connect(rootObject, SIGNAL(searchButtonClicked(QString)),
62 this, SLOT(searchButtonClicked(QString)));
63 connect(rootObject, SIGNAL(historyNextToolButtonClicked()),
64 this, SIGNAL(historyNext()));
65 connect(rootObject, SIGNAL(historyPrevToolButtonClicked()),
66 this, SIGNAL(historyPrev()));
67 connect(rootObject, SIGNAL(historyShowToolButtonClicked()),
68 this, SLOT(showHistoryButtonClicked()));
70 connect(this, SIGNAL(progresSetMax(QVariant)),
71 rootObject2, SLOT(setMax(QVariant)));
72 connect(this, SIGNAL(progresSetMin(QVariant)),
73 rootObject2, SLOT(setMin(QVariant)));
74 connect(this, SIGNAL(progresSetValue(QVariant)),
75 rootObject2, SLOT(setValue(QVariant)));
76 connect(this, SIGNAL(progresSetValue2(QVariant)),
77 rootObject2, SLOT(setValue2(QVariant)));
79 connect(this, SIGNAL(setEnableHistoryNext(QVariant)),
80 rootObject, SLOT(setEnableHistoryNext(QVariant)));
81 connect(this, SIGNAL(setEnableHistoryShow(QVariant)),
82 rootObject, SLOT(setEnableHistoryShow(QVariant)));
83 connect(this, SIGNAL(setEnableHistoryPrev(QVariant)),
84 rootObject, SLOT(setEnableHistoryPrev(QVariant)));
85 connect(this, SIGNAL(setButtonText(QVariant)),
86 rootObject, SLOT(setButtonText(QVariant)));
87 connect(this, SIGNAL(setLineEditText(QVariant)),
88 rootObject, SLOT(setLineEditText(QVariant)));
89 connect(this, SIGNAL(setLineEditEnables(QVariant)),
90 rootObject, SLOT(setEnableLineEdit(QVariant)));
92 connect(busyTimer, SIGNAL(timeout()),
93 this, SLOT(updateBusyTimer()));
95 emit setEnableHistoryNext(false);
96 emit setEnableHistoryShow(false);
97 emit setEnableHistoryPrev(false);
99 completerModel = new QStringListModel(this);
100 connect(&delayTimer, SIGNAL(timeout()),
101 this, SLOT(delaySearchTimeout()));
107 connect(searchPushButton, SIGNAL(clicked()),
108 this, SLOT(searchPushButtonClicked()));
109 connect(searchWordLineEdit, SIGNAL(returnPressed()),
110 this, SLOT(searchPushButtonClicked()));
111 connect(historyNextToolButton, SIGNAL(clicked()),
112 this, SIGNAL(historyNext()));
113 connect(historyPrevToolButton, SIGNAL(clicked()),
114 this, SIGNAL(historyPrev()));
115 connect(historyShowToolButton, SIGNAL(clicked()),
116 this, SLOT(showHistoryButtonClicked()));
117 connect(clearSearchWordToolButton, SIGNAL(clicked()),
118 this, SLOT(clearSearchWordToolButtonClicked()));
119 connect(&delayTimer, SIGNAL(timeout()),
120 this, SLOT(delaySearchTimeout()));
123 searchWordLineEdit->setFocus();
128 updateHistoryButtons(false,false,false);
131 SearchBarWidget::~SearchBarWidget() {
135 QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
137 QPixmap p = original.pixmap(64);
143 p = p.transformed(m);
147 newIcon.addPixmap(p);
151 QImage img = p.toImage();
153 for(int i=0; i < img.width(); i++) {
154 for(int j=0; j < img.height(); j++) {
155 QColor c = img.pixel(i,j);
156 if(c != QColor(0,0,0,255)) {
158 c.setGreen(c.green()/2);
159 c.setBlue(c.blue()/2);
160 img.setPixel(i, j, c.rgb());
164 p = p.fromImage(img);
166 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
172 void SearchBarWidget::setFocus() {
177 searchWordLineEdit->setFocus();
181 void SearchBarWidget::initializeUI() {
184 setMaximumHeight(150);
186 horizontalLayout = new QHBoxLayout;
187 verticalLayout = new QVBoxLayout;
189 searchPushButton = new QPushButton(tr("Search"));
190 searchPushButton->setMinimumWidth(125);
192 searchWordLineEdit = new QLineEdit;
193 searchWordLineEdit->setMinimumWidth(250);
195 completerModel = new QStringListModel(this);
197 lineEditCompleter = new QCompleter(searchWordLineEdit);
198 lineEditCompleter->setModel(completerModel);
199 lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
200 lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
201 searchWordLineEdit->setCompleter(lineEditCompleter);
204 searchWordLineEdit->setMinimumHeight(
205 searchWordLineEdit->sizeHint().height()*3/2);
208 //create layout for lineEdit to have clear button on it
209 QHBoxLayout* lineEditLayout = new QHBoxLayout;
210 searchWordLineEdit->setLayout(lineEditLayout);
213 clearSearchWordToolButton = new QToolButton;
215 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
216 clearSearchWordToolButton->setMaximumSize(
217 clearSearchWordToolButton->sizeHint().height()/2,
218 clearSearchWordToolButton->sizeHint().height()/2);
219 lineEditLayout->setContentsMargins(0,0,15,0);
221 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
222 clearSearchWordToolButton->setMinimumSize(
223 searchWordLineEdit->sizeHint().height()*1.2,
224 searchWordLineEdit->sizeHint().height()*1.2);
225 lineEditLayout->setContentsMargins(0,0,5,0);
229 historyNextToolButton = new QToolButton;
231 historyNextToolButton->setIcon(
232 generateIcon(QIcon::fromTheme("general_forward")));
234 historyNextToolButton->setIcon(
235 generateIcon(QIcon::fromTheme("go-next")));
240 historyPrevToolButton = new QToolButton;
242 historyPrevToolButton->setIcon(
243 generateIcon(QIcon::fromTheme("general_back")));
245 historyPrevToolButton->setIcon(
246 generateIcon(QIcon::fromTheme("go-previous")));
251 historyShowToolButton = new QToolButton;
253 historyShowToolButton->setIcon(
254 generateIcon(QIcon::fromTheme("general_back"), 90));
256 historyShowToolButton->setIcon(
257 generateIcon(QIcon::fromTheme("go-up")));
260 searchingProgressBar = new QProgressBar;
261 //progress bar has minimum and maximum values set to 0, which will effect
262 //with "I'm alive" bar
263 searchingProgressBar->setMinimum(0);
264 searchingProgressBar->setMaximum(0);
266 searchingProgressBar->setMaximumHeight(50);
268 searchingProgressBar->hide();
270 setLayout(verticalLayout);
272 verticalLayout->addWidget(searchingProgressBar);
274 //adding widgets to layout
275 horizontalLayout->addWidget(searchWordLineEdit);
276 horizontalLayout->addWidget(searchPushButton);
277 horizontalLayout->addWidget(historyPrevToolButton);
278 horizontalLayout->addWidget(historyShowToolButton);
279 horizontalLayout->addWidget(historyNextToolButton);
281 //adding clear toolButton to textEdit with right alignment
282 lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
284 verticalLayout->addLayout(horizontalLayout);
288 void SearchBarWidget::searchButtonClicked(QString text) {
291 Q_EMIT stopSearching();
296 void SearchBarWidget::searchPushButtonClicked() {
300 Q_EMIT stopSearching();
303 search(searchWordLineEdit->text());
308 void SearchBarWidget::search(QString word) {
311 if(!busy && !word.isEmpty()) {
312 completerModel->insertRow(completerModel->rowCount());
313 QModelIndex index=completerModel->index(completerModel->rowCount() -1);
314 completerModel->setData(index, word);
317 emit setLineEditText(word);
319 searchWordLineEdit->setText(word);
321 Q_EMIT searchForTranslations(word);
325 void SearchBarWidget::searchDelay(QString word) {
327 if(!busy && !word.isEmpty()) {
329 emit setLineEditText(word);
331 searchWordLineEdit->setText(word);
333 if(delayTimer.isActive())
336 delayTimer.start(500);
341 void SearchBarWidget::delaySearchTimeout() {
345 Q_EMIT searchForTranslations(delayString);
349 void SearchBarWidget::setEnabled(bool enabled) {
352 emit setLineEditEnables(enabled);
355 emit setEnableHistoryNext(false);
356 emit setEnableHistoryShow(false);
357 emit setEnableHistoryPrev(false);
360 searchWordLineEdit->setEnabled(enabled);
362 historyPrevToolButton->setEnabled(false);
363 historyNextToolButton->setEnabled(false);
364 historyShowToolButton->setEnabled(false);
370 void SearchBarWidget::setBusy() {
375 busyTimer->start(50);
376 emit setButtonText(tr("Stop"));
377 this->setMaximumHeight(88);
379 emit progresSetMax(100);
380 emit progresSetMin(0);
381 emit progresSetValue(-1);
382 emit progresSetValue2(100);
385 searchingProgressBar->show();
386 searchPushButton->setText(tr("Stop"));
393 void SearchBarWidget::updateBusyTimer(){
395 if(progressMax==true){
396 emit progresSetValue2(0);
400 emit progresSetValue2(100);
403 busyTimer->start(50);
406 void SearchBarWidget::setIdle() {
412 emit progresSetValue2(0);
413 this->setMaximumHeight(50);
414 emit setButtonText(tr("Search"));
416 searchingProgressBar->hide();
417 searchPushButton->setText(tr("Search"));
421 Q_EMIT refreshHistoryButtons();
425 void SearchBarWidget::clearSearchWordToolButtonClicked() {
428 searchWordLineEdit->clear();
434 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
438 emit setEnableHistoryNext(next);
439 emit setEnableHistoryShow(list);
440 emit setEnableHistoryPrev(prev);
442 historyPrevToolButton->setEnabled(prev);
443 historyNextToolButton->setEnabled(next);
444 historyShowToolButton->setEnabled(list);
449 void SearchBarWidget::showHistoryButtonClicked() {
452 QPoint p=view->pos(); // = historyShowToolButton->pos();
454 p.setX(p.x()+view->width());