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) {
37 completerModel = new QStringList;
38 lineEditCompleter = new QCompleter(this);
39 lineEditCompleter->setModel(new QStringListModel(*completerModel));
40 lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
41 lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
44 this->setMaximumHeight(50);
47 progressBar = new QDeclarativeView();
48 progressBar->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/ProgressBar.qml"));
49 progressBar->setResizeMode(QDeclarativeView::SizeRootObjectToView);
50 progressBar->setAlignment(Qt::AlignCenter);
53 view= new QDeclarativeView();
54 ctxt = view->rootContext();
56 view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/SearchBarWidget.qml"));
57 view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
58 view->setAlignment(Qt::AlignCenter);
61 mainLayout = new QVBoxLayout;
62 mainLayout->addWidget(progressBar);
63 mainLayout->addWidget(view);
64 setLayout(mainLayout);
66 QGraphicsObject *rootObject = view->rootObject();
67 QGraphicsObject *rootObject2 = progressBar->rootObject();
69 connect(rootObject, SIGNAL(searchButtonClicked(QString)),
70 this, SLOT(searchButtonClicked(QString)));
71 connect(rootObject, SIGNAL(historyNextToolButtonClicked()),
72 this, SIGNAL(historyNext()));
73 connect(rootObject, SIGNAL(historyPrevToolButtonClicked()),
74 this, SIGNAL(historyPrev()));
75 connect(rootObject, SIGNAL(historyShowToolButtonClicked()),
76 this, SLOT(showHistoryButtonClicked()));
77 connect(rootObject, SIGNAL(textChange(QString)),
78 this, SLOT(textChange(QString)));
79 connect(rootObject, SIGNAL(nextCompleter()),
80 this, SLOT(nextCompleter()));
81 connect(rootObject, SIGNAL(prevCompleter()),
82 this, SLOT(prevCompleter()));
84 connect(this, SIGNAL(progresSetMax(QVariant)),
85 rootObject2, SLOT(setMax(QVariant)));
86 connect(this, SIGNAL(progresSetMin(QVariant)),
87 rootObject2, SLOT(setMin(QVariant)));
88 connect(this, SIGNAL(progresSetValue(QVariant)),
89 rootObject2, SLOT(setValue(QVariant)));
90 connect(this, SIGNAL(progresSetValue2(QVariant)),
91 rootObject2, SLOT(setValue2(QVariant)));
93 connect(this, SIGNAL(setEnableHistoryNext(QVariant)),
94 rootObject, SLOT(setEnableHistoryNext(QVariant)));
95 connect(this, SIGNAL(setEnableHistoryShow(QVariant)),
96 rootObject, SLOT(setEnableHistoryShow(QVariant)));
97 connect(this, SIGNAL(setEnableHistoryPrev(QVariant)),
98 rootObject, SLOT(setEnableHistoryPrev(QVariant)));
99 connect(this, SIGNAL(setButtonText(QVariant)),
100 rootObject, SLOT(setButtonText(QVariant)));
101 connect(this, SIGNAL(setLineEditText(QVariant)),
102 rootObject, SLOT(setLineEditText(QVariant)));
103 connect(this, SIGNAL(setLineEditEnables(QVariant)),
104 rootObject, SLOT(setEnableLineEdit(QVariant)));
106 connect(this, SIGNAL(setCompleterText(QVariant)),
107 rootObject, SLOT(setCompleterText(QVariant)));
109 connect(busyTimer, SIGNAL(timeout()),
110 this, SLOT(updateBusyTimer()));
112 emit setEnableHistoryNext(false);
113 emit setEnableHistoryShow(false);
114 emit setEnableHistoryPrev(false);
116 connect(&delayTimer, SIGNAL(timeout()),
117 this, SLOT(delaySearchTimeout()));
123 connect(searchPushButton, SIGNAL(clicked()),
124 this, SLOT(searchPushButtonClicked()));
125 connect(searchWordLineEdit, SIGNAL(returnPressed()),
126 this, SLOT(searchPushButtonClicked()));
127 connect(historyNextToolButton, SIGNAL(clicked()),
128 this, SIGNAL(historyNext()));
129 connect(historyPrevToolButton, SIGNAL(clicked()),
130 this, SIGNAL(historyPrev()));
131 connect(historyShowToolButton, SIGNAL(clicked()),
132 this, SLOT(showHistoryButtonClicked()));
133 connect(clearSearchWordToolButton, SIGNAL(clicked()),
134 this, SLOT(clearSearchWordToolButtonClicked()));
135 connect(&delayTimer, SIGNAL(timeout()),
136 this, SLOT(delaySearchTimeout()));
138 searchWordLineEdit->setFocus();
143 updateHistoryButtons(false,false,false);
146 SearchBarWidget::~SearchBarWidget() {
150 void SearchBarWidget::textChange(QString text){
155 completerActualList.clear();
157 line.setCompleter(lineEditCompleter);
158 line.completer()->setCompletionPrefix(text);
159 for (int i = 0; lineEditCompleter->setCurrentRow(i); i++)
160 completerActualList.append(lineEditCompleter->currentCompletion());
162 completerActualList.sort();
163 if(completerActualList.contains(preferedCompliter)){
164 tempString = preferedCompliter;
165 toSend = tempString.remove(0,text.size());
166 emit setCompleterText(toSend);
168 else if(completerActualList.size()>0 && text.size()>0){
169 toSend = completerActualList.at(0);
170 preferedCompliter= toSend;
171 toSend=toSend.remove(0,text.size());
173 emit setCompleterText(toSend);
174 else if(completerActualList.size()>1){
175 toSend = completerActualList.at(1);
176 preferedCompliter= toSend;
177 toSend=toSend.remove(0,text.size());
178 emit setCompleterText(toSend);
182 preferedCompliter="";
183 emit setCompleterText(toSend);
187 void SearchBarWidget::prevCompleter(){
189 if(!preferedCompliter.isEmpty()){
190 int index = completerActualList.indexOf(preferedCompliter);
191 qDebug()<<"index"<<index<<"size"<<completerActualList.size()<<"+1";
192 if(index!=-1 && completerActualList.size()>index+1){
193 preferedCompliter = completerActualList.at(index+1);
194 tempString=preferedCompliter;
195 QString toSend = tempString.remove(0,actualString.size());
196 emit setCompleterText(toSend);
201 void SearchBarWidget::nextCompleter(){
203 if(!preferedCompliter.isEmpty()){
204 int index = completerActualList.indexOf(preferedCompliter);
206 preferedCompliter = completerActualList.at(index-1);
207 tempString=preferedCompliter;
208 QString toSend = tempString.remove(0,actualString.size());
209 emit setCompleterText(toSend);
215 QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
217 QPixmap p = original.pixmap(64);
223 p = p.transformed(m);
227 newIcon.addPixmap(p);
231 QImage img = p.toImage();
233 for(int i=0; i < img.width(); i++) {
234 for(int j=0; j < img.height(); j++) {
235 QColor c = img.pixel(i,j);
236 if(c != QColor(0,0,0,255)) {
238 c.setGreen(c.green()/2);
239 c.setBlue(c.blue()/2);
240 img.setPixel(i, j, c.rgb());
244 p = p.fromImage(img);
246 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
252 void SearchBarWidget::setFocus() {
257 searchWordLineEdit->setFocus();
261 void SearchBarWidget::initializeUI() {
264 setMaximumHeight(150);
266 horizontalLayout = new QHBoxLayout;
267 verticalLayout = new QVBoxLayout;
269 searchPushButton = new QPushButton(tr("Search"));
270 searchPushButton->setMinimumWidth(125);
272 searchWordLineEdit = new QLineEdit;
273 searchWordLineEdit->setMinimumWidth(250);
275 searchWordLineEdit->setCompleter(lineEditCompleter);
278 searchWordLineEdit->setMinimumHeight(
279 searchWordLineEdit->sizeHint().height()*3/2);
282 //create layout for lineEdit to have clear button on it
283 QHBoxLayout* lineEditLayout = new QHBoxLayout;
284 searchWordLineEdit->setLayout(lineEditLayout);
287 clearSearchWordToolButton = new QToolButton;
289 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
290 clearSearchWordToolButton->setMaximumSize(
291 clearSearchWordToolButton->sizeHint().height()/2,
292 clearSearchWordToolButton->sizeHint().height()/2);
293 lineEditLayout->setContentsMargins(0,0,15,0);
295 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
296 clearSearchWordToolButton->setMinimumSize(
297 searchWordLineEdit->sizeHint().height()*1.2,
298 searchWordLineEdit->sizeHint().height()*1.2);
299 lineEditLayout->setContentsMargins(0,0,5,0);
303 historyNextToolButton = new QToolButton;
305 historyNextToolButton->setIcon(
306 generateIcon(QIcon::fromTheme("general_forward")));
308 historyNextToolButton->setIcon(
309 generateIcon(QIcon::fromTheme("go-next")));
314 historyPrevToolButton = new QToolButton;
316 historyPrevToolButton->setIcon(
317 generateIcon(QIcon::fromTheme("general_back")));
319 historyPrevToolButton->setIcon(
320 generateIcon(QIcon::fromTheme("go-previous")));
325 historyShowToolButton = new QToolButton;
327 historyShowToolButton->setIcon(
328 generateIcon(QIcon::fromTheme("general_back"), 90));
330 historyShowToolButton->setIcon(
331 generateIcon(QIcon::fromTheme("go-up")));
334 searchingProgressBar = new QProgressBar;
335 //progress bar has minimum and maximum values set to 0, which will effect
336 //with "I'm alive" bar
337 searchingProgressBar->setMinimum(0);
338 searchingProgressBar->setMaximum(0);
340 searchingProgressBar->setMaximumHeight(50);
342 searchingProgressBar->hide();
344 setLayout(verticalLayout);
346 verticalLayout->addWidget(searchingProgressBar);
348 //adding widgets to layout
349 horizontalLayout->addWidget(searchWordLineEdit);
350 horizontalLayout->addWidget(searchPushButton);
351 horizontalLayout->addWidget(historyPrevToolButton);
352 horizontalLayout->addWidget(historyShowToolButton);
353 horizontalLayout->addWidget(historyNextToolButton);
355 //adding clear toolButton to textEdit with right alignment
356 lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
358 verticalLayout->addLayout(horizontalLayout);
362 void SearchBarWidget::searchButtonClicked(QString text) {
365 Q_EMIT stopSearching();
370 void SearchBarWidget::searchPushButtonClicked() {
374 Q_EMIT stopSearching();
377 search(searchWordLineEdit->text());
382 void SearchBarWidget::search(QString word) {
383 if(!busy && !word.isEmpty()) {
384 while(word.lastIndexOf(" ")==word.size()-1 && word.size()>0)
385 word=word.remove(word.size()-1,1);
386 if(!completerModel->contains(word))
387 completerModel->append(word);
388 QAbstractItemModel *temp=lineEditCompleter->model();
389 lineEditCompleter->setModel(new QStringListModel(*completerModel));
392 emit setLineEditText(word);
394 searchWordLineEdit->setText(word);
396 Q_EMIT searchForTranslations(word);
400 void SearchBarWidget::searchDelay(QString word) {
402 if(!busy && !word.isEmpty()) {
404 emit setLineEditText(word);
406 searchWordLineEdit->setText(word);
408 if(delayTimer.isActive())
411 delayTimer.start(500);
416 void SearchBarWidget::delaySearchTimeout() {
420 Q_EMIT searchForTranslations(delayString);
424 void SearchBarWidget::setEnabled(bool enabled) {
427 emit setLineEditEnables(enabled);
430 emit setEnableHistoryNext(false);
431 emit setEnableHistoryShow(false);
432 emit setEnableHistoryPrev(false);
435 searchWordLineEdit->setEnabled(enabled);
437 historyPrevToolButton->setEnabled(false);
438 historyNextToolButton->setEnabled(false);
439 historyShowToolButton->setEnabled(false);
445 void SearchBarWidget::setBusy() {
450 busyTimer->start(50);
451 emit setButtonText(tr("Stop"));
452 this->setMaximumHeight(88);
454 emit progresSetMax(100);
455 emit progresSetMin(0);
456 emit progresSetValue(-1);
457 emit progresSetValue2(100);
460 searchingProgressBar->show();
461 searchPushButton->setText(tr("Stop"));
468 void SearchBarWidget::updateBusyTimer(){
470 if(progressMax==true){
471 emit progresSetValue2(0);
475 emit progresSetValue2(100);
478 busyTimer->start(50);
481 void SearchBarWidget::setIdle() {
487 emit progresSetValue2(0);
488 this->setMaximumHeight(50);
489 emit setButtonText(tr("Search"));
491 searchingProgressBar->hide();
492 searchPushButton->setText(tr("Search"));
496 Q_EMIT refreshHistoryButtons();
500 void SearchBarWidget::clearSearchWordToolButtonClicked() {
503 searchWordLineEdit->clear();
509 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
513 emit setEnableHistoryNext(next);
514 emit setEnableHistoryShow(list);
515 emit setEnableHistoryPrev(prev);
517 historyPrevToolButton->setEnabled(prev);
518 historyNextToolButton->setEnabled(next);
519 historyShowToolButton->setEnabled(list);
524 void SearchBarWidget::showHistoryButtonClicked() {
527 QPoint p=view->pos(); // = historyShowToolButton->pos();
529 p.setX(p.x()+view->width());