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 "../../common/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 painter.fillRect(p.rect(), QColor(0,0,0,192));
94 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
101 void SearchBarWidget::initializeUI() {
104 setMaximumHeight(150);
106 setMaximumHeight(100);
110 horizontalLayout = new QHBoxLayout;
111 verticalLayout = new QVBoxLayout;
114 searchPushButton = new QPushButton(tr("Search"));
115 searchPushButton->setMinimumWidth(125);
118 searchWordLineEdit = new QLineEdit;
119 searchWordLineEdit->setMinimumWidth(250);
123 completerModel = new QStringListModel(this);
126 lineEditCompleter = new QCompleter(searchWordLineEdit);
127 lineEditCompleter->setModel(completerModel);
128 lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
129 lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
130 searchWordLineEdit->setCompleter(lineEditCompleter);
134 searchWordLineEdit->setMinimumHeight(
135 searchWordLineEdit->sizeHint().height()*3/2);
139 //create layout for lineEdit to have clear button on it
140 QHBoxLayout* lineEditLayout = new QHBoxLayout;
141 searchWordLineEdit->setLayout(lineEditLayout);
144 clearSearchWordToolButton = new QToolButton;
146 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
147 clearSearchWordToolButton->setMaximumSize(
148 clearSearchWordToolButton->sizeHint().height()/2,
149 clearSearchWordToolButton->sizeHint().height()/2);
150 lineEditLayout->setContentsMargins(0,0,10,0);
152 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
153 clearSearchWordToolButton->setMinimumSize(
154 searchWordLineEdit->sizeHint().height()*1.2,
155 searchWordLineEdit->sizeHint().height()*1.2);
156 lineEditLayout->setContentsMargins(0,0,5,0);
160 historyNextToolButton = new QToolButton;
162 historyNextToolButton->setIcon(
163 generateIcon(QIcon::fromTheme("general_forward")));
165 historyNextToolButton->setIcon(
166 generateIcon(QIcon::fromTheme("go-next")));
171 historyPrevToolButton = new QToolButton;
173 historyPrevToolButton->setIcon(
174 generateIcon(QIcon::fromTheme("general_back")));
176 historyPrevToolButton->setIcon(
177 generateIcon(QIcon::fromTheme("go-previous")));
182 historyShowToolButton = new QToolButton;
184 historyShowToolButton->setIcon(
185 generateIcon(QIcon::fromTheme("general_back"), 90));
187 historyShowToolButton->setIcon(
188 generateIcon(QIcon::fromTheme("go-up")));
191 searchingProgressBar = new QProgressBar;
192 //progress bar have minimum and maximum values set to 0, which will effect
193 //with "I'm alive" bar
194 searchingProgressBar->setMinimum(0);
195 searchingProgressBar->setMaximum(0);
197 searchingProgressBar->setMaximumHeight(50);
199 searchingProgressBar->hide();
202 setLayout(verticalLayout);
204 verticalLayout->addWidget(searchingProgressBar);
206 //adding widgets to layout
207 horizontalLayout->addWidget(searchWordLineEdit);
208 horizontalLayout->addWidget(searchPushButton);
209 horizontalLayout->addWidget(historyPrevToolButton);
210 horizontalLayout->addWidget(historyShowToolButton);
211 horizontalLayout->addWidget(historyNextToolButton);
213 //adding clear toolButton to textEdit with right alignment
214 lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
217 verticalLayout->addLayout(horizontalLayout);
221 void SearchBarWidget::searchPushButtonClicked() {
223 Q_EMIT stopSearching();
226 search(searchWordLineEdit->text());
231 void SearchBarWidget::search(QString word) {
232 if(!busy && !word.isEmpty()) {
233 completerModel->insertRow(completerModel->rowCount());
235 completerModel->index(completerModel->rowCount() -1);
237 completerModel->setData(index, word);
240 searchWordLineEdit->setText(word);
241 Q_EMIT searchForTranslations(word);
245 void SearchBarWidget::searchDelay(QString word) {
246 if(!busy && !word.isEmpty()) {
247 searchWordLineEdit->setText(word);
250 if(delayTimer.isActive()) {
255 delayTimer.start(500);
259 void SearchBarWidget::delaySearchTimeout() {
262 Q_EMIT searchForTranslations(delayString);
266 void SearchBarWidget::setEnabled(bool enabled) {
267 searchWordLineEdit->setEnabled(enabled);
270 historyPrevToolButton->setEnabled(false);
271 historyNextToolButton->setEnabled(false);
272 historyShowToolButton->setEnabled(false);
276 void SearchBarWidget::setBusy() {
278 searchingProgressBar->show();
279 searchPushButton->setText(tr("Stop"));
284 void SearchBarWidget::setIdle() {
286 searchingProgressBar->hide();
287 searchPushButton->setText(tr("Search"));
290 Q_EMIT refreshHistoryButtons();
294 void SearchBarWidget::clearSearchWordToolButtonClicked() {
295 searchWordLineEdit->clear();
300 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
302 historyPrevToolButton->setEnabled(prev);
303 historyNextToolButton->setEnabled(next);
304 historyShowToolButton->setEnabled(list);
308 void SearchBarWidget::showHistoryButtonClicked() {
312 QPoint p = historyShowToolButton->pos();
314 emit historyShow(mapToGlobal(p));