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) {
42 connect(searchPushButton, SIGNAL(clicked()),
43 this, SLOT(searchPushButtonClicked()));
45 connect(searchWordLineEdit, SIGNAL(returnPressed()),
46 this, SLOT(searchPushButtonClicked()));
48 connect(historyNextToolButton, SIGNAL(clicked()),
49 this, SIGNAL(historyNext()));
51 connect(historyPrevToolButton, SIGNAL(clicked()),
52 this, SIGNAL(historyPrev()));
54 connect(historyShowToolButton, SIGNAL(clicked()),
55 this, SLOT(showHistoryButtonClicked()));
57 connect(clearSearchWordToolButton, SIGNAL(clicked()),
58 this, SLOT(clearSearchWordToolButtonClicked()));
61 connect(&delayTimer, SIGNAL(timeout()),
62 this, SLOT(delaySearchTimeout()));
65 searchWordLineEdit->setFocus();
67 historyPrevToolButton->setEnabled(false);
68 historyNextToolButton->setEnabled(false);
69 historyShowToolButton->setEnabled(false);
74 SearchBarWidget::~SearchBarWidget() {
78 QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
79 QPixmap p = original.pixmap(64);
94 QImage img = p.toImage();
96 for(int i=0; i < img.width(); i++) {
97 for(int j=0; j < img.height(); j++) {
98 QColor c = img.pixel(i,j);
99 if(c != QColor(0,0,0,255)) {
101 c.setGreen(c.green()/2);
102 c.setBlue(c.blue()/2);
103 img.setPixel(i, j, c.rgb());
107 p = p.fromImage(img);
109 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
116 void SearchBarWidget::setFocus() {
117 searchWordLineEdit->setFocus();
120 void SearchBarWidget::initializeUI() {
123 setMaximumHeight(150);
125 setMaximumHeight(100);
129 horizontalLayout = new QHBoxLayout;
130 verticalLayout = new QVBoxLayout;
133 searchPushButton = new QPushButton(tr("Search"));
134 searchPushButton->setMinimumWidth(125);
137 searchWordLineEdit = new QLineEdit;
138 searchWordLineEdit->setMinimumWidth(250);
142 completerModel = new QStringListModel(this);
145 lineEditCompleter = new QCompleter(searchWordLineEdit);
146 lineEditCompleter->setModel(completerModel);
147 lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
148 lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
149 searchWordLineEdit->setCompleter(lineEditCompleter);
153 searchWordLineEdit->setMinimumHeight(
154 searchWordLineEdit->sizeHint().height()*3/2);
158 //create layout for lineEdit to have clear button on it
159 QHBoxLayout* lineEditLayout = new QHBoxLayout;
160 searchWordLineEdit->setLayout(lineEditLayout);
163 clearSearchWordToolButton = new QToolButton;
165 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
166 clearSearchWordToolButton->setMaximumSize(
167 clearSearchWordToolButton->sizeHint().height()/2,
168 clearSearchWordToolButton->sizeHint().height()/2);
169 lineEditLayout->setContentsMargins(0,0,15,0);
171 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
172 clearSearchWordToolButton->setMinimumSize(
173 searchWordLineEdit->sizeHint().height()*1.2,
174 searchWordLineEdit->sizeHint().height()*1.2);
175 lineEditLayout->setContentsMargins(0,0,5,0);
179 historyNextToolButton = new QToolButton;
181 historyNextToolButton->setIcon(
182 generateIcon(QIcon::fromTheme("general_forward")));
184 historyNextToolButton->setIcon(
185 generateIcon(QIcon::fromTheme("go-next")));
190 historyPrevToolButton = new QToolButton;
192 historyPrevToolButton->setIcon(
193 generateIcon(QIcon::fromTheme("general_back")));
195 historyPrevToolButton->setIcon(
196 generateIcon(QIcon::fromTheme("go-previous")));
201 historyShowToolButton = new QToolButton;
203 historyShowToolButton->setIcon(
204 generateIcon(QIcon::fromTheme("general_back"), 90));
206 historyShowToolButton->setIcon(
207 generateIcon(QIcon::fromTheme("go-up")));
210 searchingProgressBar = new QProgressBar;
211 //progress bar has minimum and maximum values set to 0, which will effect
212 //with "I'm alive" bar
213 searchingProgressBar->setMinimum(0);
214 searchingProgressBar->setMaximum(0);
216 searchingProgressBar->setMaximumHeight(50);
218 searchingProgressBar->hide();
221 setLayout(verticalLayout);
223 verticalLayout->addWidget(searchingProgressBar);
225 //adding widgets to layout
226 horizontalLayout->addWidget(searchWordLineEdit);
227 horizontalLayout->addWidget(searchPushButton);
228 horizontalLayout->addWidget(historyPrevToolButton);
229 horizontalLayout->addWidget(historyShowToolButton);
230 horizontalLayout->addWidget(historyNextToolButton);
232 //adding clear toolButton to textEdit with right alignment
233 lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
236 verticalLayout->addLayout(horizontalLayout);
240 void SearchBarWidget::searchPushButtonClicked() {
242 Q_EMIT stopSearching();
245 search(searchWordLineEdit->text());
250 void SearchBarWidget::search(QString word) {
251 if(!busy && !word.isEmpty()) {
252 completerModel->insertRow(completerModel->rowCount());
254 completerModel->index(completerModel->rowCount() -1);
256 completerModel->setData(index, word);
259 searchWordLineEdit->setText(word);
260 Q_EMIT searchForTranslations(word);
264 void SearchBarWidget::searchDelay(QString word) {
265 if(!busy && !word.isEmpty()) {
266 searchWordLineEdit->setText(word);
269 if(delayTimer.isActive()) {
274 delayTimer.start(500);
278 void SearchBarWidget::delaySearchTimeout() {
281 Q_EMIT searchForTranslations(delayString);
285 void SearchBarWidget::setEnabled(bool enabled) {
286 searchWordLineEdit->setEnabled(enabled);
289 historyPrevToolButton->setEnabled(false);
290 historyNextToolButton->setEnabled(false);
291 historyShowToolButton->setEnabled(false);
295 void SearchBarWidget::setBusy() {
297 searchingProgressBar->show();
298 searchPushButton->setText(tr("Stop"));
303 void SearchBarWidget::setIdle() {
305 searchingProgressBar->hide();
306 searchPushButton->setText(tr("Search"));
309 Q_EMIT refreshHistoryButtons();
313 void SearchBarWidget::clearSearchWordToolButtonClicked() {
314 searchWordLineEdit->clear();
319 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
321 historyPrevToolButton->setEnabled(prev);
322 historyNextToolButton->setEnabled(next);
323 historyShowToolButton->setEnabled(list);
327 void SearchBarWidget::showHistoryButtonClicked() {
331 QPoint p = historyShowToolButton->pos();
333 emit historyShow(mapToGlobal(p));