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) :
43 connect(searchPushButton, SIGNAL(clicked()),
44 this, SLOT(searchPushButtonClicked()));
46 connect(searchWordLineEdit, SIGNAL(returnPressed()),
47 this, SLOT(searchPushButtonClicked()));
49 connect(historyNextToolButton, SIGNAL(clicked()),
50 this, SIGNAL(historyNext()));
52 connect(historyPrevToolButton, SIGNAL(clicked()),
53 this, SIGNAL(historyPrev()));
55 connect(historyShowToolButton, SIGNAL(clicked()),
56 this, SLOT(showHistoryButtonClicked()));
58 connect(clearSearchWordToolButton, SIGNAL(clicked()),
59 this, SLOT(clearSearchWordToolButtonClicked()));
62 connect(&delayTimer, SIGNAL(timeout()),
63 this, SLOT(delaySearchTimeout()));
66 searchWordLineEdit->setFocus();
68 historyPrevToolButton->setEnabled(false);
69 historyNextToolButton->setEnabled(false);
70 historyShowToolButton->setEnabled(false);
75 SearchBarWidget::~SearchBarWidget() {
79 QIcon SearchBarWidget::generateIcon(QIcon original, qreal rotation) {
80 QPixmap p = original.pixmap(64);
95 QImage img = p.toImage();
97 for(int i=0; i < img.width(); i++) {
98 for(int j=0; j < img.height(); j++) {
99 QColor c = img.pixel(i,j);
100 if(c != QColor(0,0,0,255)) {
102 c.setGreen(c.green()/2);
103 c.setBlue(c.blue()/2);
104 img.setPixel(i, j, c.rgb());
108 p = p.fromImage(img);
110 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
117 void SearchBarWidget::setFocus() {
118 searchWordLineEdit->setFocus();
121 void SearchBarWidget::initializeUI() {
124 setMaximumHeight(150);
126 setMaximumHeight(100);
130 horizontalLayout = new QHBoxLayout;
131 verticalLayout = new QVBoxLayout;
134 searchPushButton = new QPushButton(tr("Search"));
135 searchPushButton->setMinimumWidth(125);
138 searchWordLineEdit = new QLineEdit;
139 searchWordLineEdit->setMinimumWidth(250);
143 completerModel = new QStringListModel(this);
146 lineEditCompleter = new QCompleter(searchWordLineEdit);
147 lineEditCompleter->setModel(completerModel);
148 lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
149 lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
150 searchWordLineEdit->setCompleter(lineEditCompleter);
154 searchWordLineEdit->setMinimumHeight(
155 searchWordLineEdit->sizeHint().height()*3/2);
159 //create layout for lineEdit to have clear button on it
160 QHBoxLayout* lineEditLayout = new QHBoxLayout;
161 searchWordLineEdit->setLayout(lineEditLayout);
164 clearSearchWordToolButton = new QToolButton;
166 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
167 clearSearchWordToolButton->setMaximumSize(
168 clearSearchWordToolButton->sizeHint().height()/2,
169 clearSearchWordToolButton->sizeHint().height()/2);
170 lineEditLayout->setContentsMargins(0,0,15,0);
172 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
173 clearSearchWordToolButton->setMinimumSize(
174 searchWordLineEdit->sizeHint().height()*1.2,
175 searchWordLineEdit->sizeHint().height()*1.2);
176 lineEditLayout->setContentsMargins(0,0,5,0);
180 historyNextToolButton = new QToolButton;
182 historyNextToolButton->setIcon(
183 generateIcon(QIcon::fromTheme("general_forward")));
185 historyNextToolButton->setIcon(
186 generateIcon(QIcon::fromTheme("go-next")));
191 historyPrevToolButton = new QToolButton;
193 historyPrevToolButton->setIcon(
194 generateIcon(QIcon::fromTheme("general_back")));
196 historyPrevToolButton->setIcon(
197 generateIcon(QIcon::fromTheme("go-previous")));
202 historyShowToolButton = new QToolButton;
204 historyShowToolButton->setIcon(
205 generateIcon(QIcon::fromTheme("general_back"), 90));
207 historyShowToolButton->setIcon(
208 generateIcon(QIcon::fromTheme("go-up")));
211 searchingProgressBar = new QProgressBar;
212 //progress bar has minimum and maximum values set to 0, which will effect
213 //with "I'm alive" bar
214 searchingProgressBar->setMinimum(0);
215 searchingProgressBar->setMaximum(0);
217 searchingProgressBar->setMaximumHeight(50);
219 searchingProgressBar->hide();
222 setLayout(verticalLayout);
224 verticalLayout->addWidget(searchingProgressBar);
226 //adding widgets to layout
227 horizontalLayout->addWidget(searchWordLineEdit);
228 horizontalLayout->addWidget(searchPushButton);
229 horizontalLayout->addWidget(historyPrevToolButton);
230 horizontalLayout->addWidget(historyShowToolButton);
231 horizontalLayout->addWidget(historyNextToolButton);
233 //adding clear toolButton to textEdit with right alignment
234 lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
237 verticalLayout->addLayout(horizontalLayout);
241 void SearchBarWidget::searchPushButtonClicked() {
243 Q_EMIT stopSearching();
246 search(searchWordLineEdit->text());
251 void SearchBarWidget::search(QString word) {
252 if(!busy && !word.isEmpty()) {
253 completerModel->insertRow(completerModel->rowCount());
255 completerModel->index(completerModel->rowCount() -1);
257 completerModel->setData(index, word);
260 searchWordLineEdit->setText(word);
261 Q_EMIT searchForTranslations(word);
265 void SearchBarWidget::searchDelay(QString word) {
266 if(!busy && !word.isEmpty()) {
267 searchWordLineEdit->setText(word);
270 if(delayTimer.isActive()) {
275 delayTimer.start(500);
279 void SearchBarWidget::delaySearchTimeout() {
282 Q_EMIT searchForTranslations(delayString);
286 void SearchBarWidget::setEnabled(bool enabled) {
287 searchWordLineEdit->setEnabled(enabled);
290 historyPrevToolButton->setEnabled(false);
291 historyNextToolButton->setEnabled(false);
292 historyShowToolButton->setEnabled(false);
296 void SearchBarWidget::setBusy() {
298 searchingProgressBar->show();
299 searchPushButton->setText(tr("Stop"));
304 void SearchBarWidget::setIdle() {
306 searchingProgressBar->hide();
307 searchPushButton->setText(tr("Search"));
310 Q_EMIT refreshHistoryButtons();
314 void SearchBarWidget::clearSearchWordToolButtonClicked() {
315 searchWordLineEdit->clear();
320 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
322 historyPrevToolButton->setEnabled(prev);
323 historyNextToolButton->setEnabled(next);
324 historyShowToolButton->setEnabled(list);
328 void SearchBarWidget::showHistoryButtonClicked() {
332 QPoint p = historyShowToolButton->pos();
334 emit historyShow(mapToGlobal(p));