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 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 #include "SearchBarWidget.h"
28 #include "../../include/DictDialog.h"
29 #include "HistoryListDialog.h"
32 SearchBarWidget::SearchBarWidget(QWidget *parent) :
40 connect(searchPushButton, SIGNAL(clicked()),
41 this, SLOT(searchPushButtonClicked()));
43 connect(searchWordLineEdit, SIGNAL(returnPressed()),
44 this, SLOT(searchPushButtonClicked()));
46 connect(historyNextToolButton, SIGNAL(clicked()),
47 this, SIGNAL(historyNext()));
49 connect(historyPrevToolButton, SIGNAL(clicked()),
50 this, SIGNAL(historyPrev()));
52 connect(historyShowToolButton, SIGNAL(clicked()),
53 this, SLOT(showHistoryButtonClicked()));
55 connect(clearSearchWordToolButton, SIGNAL(clicked()),
56 this, SLOT(clearSearchWordToolButtonClicked()));
59 connect(&delayTimer, SIGNAL(timeout()),
60 this, SLOT(delaySearchTimeout()));
63 searchWordLineEdit->setFocus();
65 historyPrevToolButton->setEnabled(false);
66 historyNextToolButton->setEnabled(false);
67 historyShowToolButton->setEnabled(false);
72 SearchBarWidget::~SearchBarWidget() {
76 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
77 QPixmap p = oryginal.pixmap(64);
92 QImage img = p.toImage();
94 for(int i=0; i < img.width(); i++) {
95 for(int j=0; j < img.height(); j++) {
96 QColor c = img.pixel(i,j);
97 if(c != QColor(0,0,0,255)) {
99 c.setGreen(c.green()/2);
100 c.setBlue(c.blue()/2);
101 img.setPixel(i, j, c.rgb());
105 p = p.fromImage(img);
107 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
114 void SearchBarWidget::setFocus() {
115 searchWordLineEdit->setFocus();
118 void SearchBarWidget::initializeUI() {
121 setMaximumHeight(150);
123 setMaximumHeight(100);
127 horizontalLayout = new QHBoxLayout;
128 verticalLayout = new QVBoxLayout;
131 searchPushButton = new QPushButton(tr("Search"));
132 searchPushButton->setMinimumWidth(125);
135 searchWordLineEdit = new QLineEdit;
136 searchWordLineEdit->setMinimumWidth(250);
140 completerModel = new QStringListModel(this);
143 lineEditCompleter = new QCompleter(searchWordLineEdit);
144 lineEditCompleter->setModel(completerModel);
145 lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
146 lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
147 searchWordLineEdit->setCompleter(lineEditCompleter);
151 searchWordLineEdit->setMinimumHeight(
152 searchWordLineEdit->sizeHint().height()*3/2);
156 //create layout for lineEdit to have clear button on it
157 QHBoxLayout* lineEditLayout = new QHBoxLayout;
158 searchWordLineEdit->setLayout(lineEditLayout);
161 clearSearchWordToolButton = new QToolButton;
163 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
164 clearSearchWordToolButton->setMaximumSize(
165 clearSearchWordToolButton->sizeHint().height()/2,
166 clearSearchWordToolButton->sizeHint().height()/2);
167 lineEditLayout->setContentsMargins(0,0,15,0);
169 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
170 clearSearchWordToolButton->setMinimumSize(
171 searchWordLineEdit->sizeHint().height()*1.2,
172 searchWordLineEdit->sizeHint().height()*1.2);
173 lineEditLayout->setContentsMargins(0,0,5,0);
177 historyNextToolButton = new QToolButton;
179 historyNextToolButton->setIcon(
180 generateIcon(QIcon::fromTheme("general_forward")));
182 historyNextToolButton->setIcon(
183 generateIcon(QIcon::fromTheme("go-next")));
188 historyPrevToolButton = new QToolButton;
190 historyPrevToolButton->setIcon(
191 generateIcon(QIcon::fromTheme("general_back")));
193 historyPrevToolButton->setIcon(
194 generateIcon(QIcon::fromTheme("go-previous")));
199 historyShowToolButton = new QToolButton;
201 historyShowToolButton->setIcon(
202 generateIcon(QIcon::fromTheme("general_back"), 90));
204 historyShowToolButton->setIcon(
205 generateIcon(QIcon::fromTheme("go-up")));
208 searchingProgressBar = new QProgressBar;
209 //progress bar has minimum and maximum values set to 0, which will effect
210 //with "I'm alive" bar
211 searchingProgressBar->setMinimum(0);
212 searchingProgressBar->setMaximum(0);
214 searchingProgressBar->setMaximumHeight(50);
216 searchingProgressBar->hide();
219 setLayout(verticalLayout);
221 verticalLayout->addWidget(searchingProgressBar);
223 //adding widgets to layout
224 horizontalLayout->addWidget(searchWordLineEdit);
225 horizontalLayout->addWidget(searchPushButton);
226 horizontalLayout->addWidget(historyPrevToolButton);
227 horizontalLayout->addWidget(historyShowToolButton);
228 horizontalLayout->addWidget(historyNextToolButton);
230 //adding clear toolButton to textEdit with right alignment
231 lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
234 verticalLayout->addLayout(horizontalLayout);
238 void SearchBarWidget::searchPushButtonClicked() {
240 Q_EMIT stopSearching();
243 search(searchWordLineEdit->text());
248 void SearchBarWidget::search(QString word) {
249 if(!busy && !word.isEmpty()) {
250 completerModel->insertRow(completerModel->rowCount());
252 completerModel->index(completerModel->rowCount() -1);
254 completerModel->setData(index, word);
257 searchWordLineEdit->setText(word);
258 Q_EMIT searchForTranslations(word);
262 void SearchBarWidget::searchDelay(QString word) {
263 if(!busy && !word.isEmpty()) {
264 searchWordLineEdit->setText(word);
267 if(delayTimer.isActive()) {
272 delayTimer.start(500);
276 void SearchBarWidget::delaySearchTimeout() {
279 Q_EMIT searchForTranslations(delayString);
283 void SearchBarWidget::setEnabled(bool enabled) {
284 searchWordLineEdit->setEnabled(enabled);
287 historyPrevToolButton->setEnabled(false);
288 historyNextToolButton->setEnabled(false);
289 historyShowToolButton->setEnabled(false);
293 void SearchBarWidget::setBusy() {
295 searchingProgressBar->show();
296 searchPushButton->setText(tr("Stop"));
301 void SearchBarWidget::setIdle() {
303 searchingProgressBar->hide();
304 searchPushButton->setText(tr("Search"));
307 Q_EMIT refreshHistoryButtons();
311 void SearchBarWidget::clearSearchWordToolButtonClicked() {
312 searchWordLineEdit->clear();
317 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
319 historyPrevToolButton->setEnabled(prev);
320 historyNextToolButton->setEnabled(next);
321 historyShowToolButton->setEnabled(list);
325 void SearchBarWidget::showHistoryButtonClicked() {
329 QPoint p = historyShowToolButton->pos();
331 emit historyShow(mapToGlobal(p));