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 this->setMaximumHeight(50);
39 view= new QDeclarativeView();
40 view->setSource(QUrl("src/mdictionary/qml/SearchBarWidget.qml"));
41 view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
42 view->setAlignment(Qt::AlignCenter);
45 mainLayout = new QVBoxLayout;
46 mainLayout->addWidget(view);
47 setLayout(mainLayout);
49 QGraphicsObject *rootObject = view->rootObject();
51 connect(rootObject, SIGNAL(searchButtonClicked(QString)),
52 this, SLOT(searchButtonClicked(QString)));
53 connect(rootObject, SIGNAL(historyNextToolButtonClicked()),
54 this, SIGNAL(historyNext()));
55 connect(rootObject, SIGNAL(historyPrevToolButtonClicked()),
56 this, SIGNAL(historyPrev()));
57 connect(rootObject, SIGNAL(historyShowToolButtonClicked()),
58 this, SIGNAL(historyShow()));
60 connect(this, SIGNAL(setEnableHistoryNext(QVariant)),
61 rootObject, SLOT(setEnableHistoryNext(QVariant)));
62 connect(this, SIGNAL(setEnableHistoryShow(QVariant)),
63 rootObject, SLOT(setEnableHistoryShow(QVariant)));
64 connect(this, SIGNAL(setEnableHistoryPrev(QVariant)),
65 rootObject, SLOT(setEnableHistoryPrev(QVariant)));
66 connect(this, SIGNAL(setButtonText(QVariant)),
67 rootObject, SLOT(setButtonText(QVariant)));
68 connect(this, SIGNAL(setLineEditText(QVariant)),
69 rootObject, SLOT(setLineEditText(QVariant)));
70 connect(this, SIGNAL(setLineEditEnables(QVariant)),
71 rootObject, SLOT(setEnableLineEdit(QVariant)));
73 emit setEnableHistoryNext(false);
74 emit setEnableHistoryShow(false);
75 emit setEnableHistoryPrev(false);
77 completerModel = new QStringListModel(this);
78 connect(&delayTimer, SIGNAL(timeout()),
79 this, SLOT(delaySearchTimeout()));
82 connect(searchPushButton, SIGNAL(clicked()),
83 this, SLOT(searchPushButtonClicked()));
84 connect(searchWordLineEdit, SIGNAL(returnPressed()),
85 this, SLOT(searchPushButtonClicked()));
86 connect(historyNextToolButton, SIGNAL(clicked()),
87 this, SIGNAL(historyNext()));
88 connect(historyPrevToolButton, SIGNAL(clicked()),
89 this, SIGNAL(historyPrev()));
90 connect(historyShowToolButton, SIGNAL(clicked()),
91 this, SLOT(showHistoryButtonClicked()));
92 connect(clearSearchWordToolButton, SIGNAL(clicked()),
93 this, SLOT(clearSearchWordToolButtonClicked()));
94 connect(&delayTimer, SIGNAL(timeout()),
95 this, SLOT(delaySearchTimeout()));
98 searchWordLineEdit->setFocus();
103 updateHistoryButtons(false,false,false);
106 SearchBarWidget::~SearchBarWidget() {
110 QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
111 QPixmap p = original.pixmap(64);
117 p = p.transformed(m);
121 newIcon.addPixmap(p);
125 QImage img = p.toImage();
127 for(int i=0; i < img.width(); i++) {
128 for(int j=0; j < img.height(); j++) {
129 QColor c = img.pixel(i,j);
130 if(c != QColor(0,0,0,255)) {
132 c.setGreen(c.green()/2);
133 c.setBlue(c.blue()/2);
134 img.setPixel(i, j, c.rgb());
138 p = p.fromImage(img);
140 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
146 void SearchBarWidget::setFocus() {
148 searchWordLineEdit->setFocus();
152 void SearchBarWidget::initializeUI() {
154 setMaximumHeight(150);
156 horizontalLayout = new QHBoxLayout;
157 verticalLayout = new QVBoxLayout;
159 searchPushButton = new QPushButton(tr("Search"));
160 searchPushButton->setMinimumWidth(125);
162 searchWordLineEdit = new QLineEdit;
163 searchWordLineEdit->setMinimumWidth(250);
165 completerModel = new QStringListModel(this);
167 lineEditCompleter = new QCompleter(searchWordLineEdit);
168 lineEditCompleter->setModel(completerModel);
169 lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
170 lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
171 searchWordLineEdit->setCompleter(lineEditCompleter);
174 searchWordLineEdit->setMinimumHeight(
175 searchWordLineEdit->sizeHint().height()*3/2);
178 //create layout for lineEdit to have clear button on it
179 QHBoxLayout* lineEditLayout = new QHBoxLayout;
180 searchWordLineEdit->setLayout(lineEditLayout);
183 clearSearchWordToolButton = new QToolButton;
185 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
186 clearSearchWordToolButton->setMaximumSize(
187 clearSearchWordToolButton->sizeHint().height()/2,
188 clearSearchWordToolButton->sizeHint().height()/2);
189 lineEditLayout->setContentsMargins(0,0,15,0);
191 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
192 clearSearchWordToolButton->setMinimumSize(
193 searchWordLineEdit->sizeHint().height()*1.2,
194 searchWordLineEdit->sizeHint().height()*1.2);
195 lineEditLayout->setContentsMargins(0,0,5,0);
199 historyNextToolButton = new QToolButton;
201 historyNextToolButton->setIcon(
202 generateIcon(QIcon::fromTheme("general_forward")));
204 historyNextToolButton->setIcon(
205 generateIcon(QIcon::fromTheme("go-next")));
210 historyPrevToolButton = new QToolButton;
212 historyPrevToolButton->setIcon(
213 generateIcon(QIcon::fromTheme("general_back")));
215 historyPrevToolButton->setIcon(
216 generateIcon(QIcon::fromTheme("go-previous")));
221 historyShowToolButton = new QToolButton;
223 historyShowToolButton->setIcon(
224 generateIcon(QIcon::fromTheme("general_back"), 90));
226 historyShowToolButton->setIcon(
227 generateIcon(QIcon::fromTheme("go-up")));
230 searchingProgressBar = new QProgressBar;
231 //progress bar has minimum and maximum values set to 0, which will effect
232 //with "I'm alive" bar
233 searchingProgressBar->setMinimum(0);
234 searchingProgressBar->setMaximum(0);
236 searchingProgressBar->setMaximumHeight(50);
238 searchingProgressBar->hide();
240 setLayout(verticalLayout);
242 verticalLayout->addWidget(searchingProgressBar);
244 //adding widgets to layout
245 horizontalLayout->addWidget(searchWordLineEdit);
246 horizontalLayout->addWidget(searchPushButton);
247 horizontalLayout->addWidget(historyPrevToolButton);
248 horizontalLayout->addWidget(historyShowToolButton);
249 horizontalLayout->addWidget(historyNextToolButton);
251 //adding clear toolButton to textEdit with right alignment
252 lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
254 verticalLayout->addLayout(horizontalLayout);
258 void SearchBarWidget::searchButtonClicked(QString text) {
260 Q_EMIT stopSearching();
265 void SearchBarWidget::searchPushButtonClicked() {
268 Q_EMIT stopSearching();
271 search(searchWordLineEdit->text());
276 void SearchBarWidget::search(QString word) {
278 if(!busy && !word.isEmpty()) {
279 completerModel->insertRow(completerModel->rowCount());
280 QModelIndex index=completerModel->index(completerModel->rowCount() -1);
281 completerModel->setData(index, word);
284 emit setLineEditText(word);
286 searchWordLineEdit->setText(word);
288 Q_EMIT searchForTranslations(word);
292 void SearchBarWidget::searchDelay(QString word) {
293 if(!busy && !word.isEmpty()) {
295 emit setLineEditText(word);
297 searchWordLineEdit->setText(word);
299 if(delayTimer.isActive())
302 delayTimer.start(500);
307 void SearchBarWidget::delaySearchTimeout() {
310 Q_EMIT searchForTranslations(delayString);
314 void SearchBarWidget::setEnabled(bool enabled) {
316 emit setLineEditEnables(enabled);
318 emit setEnableHistoryNext(false);
319 emit setEnableHistoryShow(false);
320 emit setEnableHistoryPrev(false);
323 searchWordLineEdit->setEnabled(enabled);
325 historyPrevToolButton->setEnabled(false);
326 historyNextToolButton->setEnabled(false);
327 historyShowToolButton->setEnabled(false);
332 void SearchBarWidget::setBusy() {
336 emit setButtonText(tr("Stop"));
337 // searchingProgressBar->show();
339 searchingProgressBar->show();
340 searchPushButton->setText(tr("Stop"));
347 void SearchBarWidget::setIdle() {
350 // searchingProgressBar->hide();
351 emit setButtonText(tr("Search"));
353 searchingProgressBar->hide();
354 searchPushButton->setText(tr("Search"));
358 Q_EMIT refreshHistoryButtons();
362 void SearchBarWidget::clearSearchWordToolButtonClicked() {
364 searchWordLineEdit->clear();
370 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
373 emit setEnableHistoryNext(next);
374 emit setEnableHistoryShow(list);
375 emit setEnableHistoryPrev(prev);
377 historyPrevToolButton->setEnabled(prev);
378 historyNextToolButton->setEnabled(next);
379 historyShowToolButton->setEnabled(list);
384 void SearchBarWidget::showHistoryButtonClicked() {
386 QPoint p=view->pos(); // = historyShowToolButton->pos();
388 emit historyShow(mapToGlobal(p));