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);
91 p.setMask(p.createHeuristicMask());
92 /* #ifdef Q_WS_MAEMO_5
94 QColor c = palette().background().color();
96 painter.fillRect(p.rect(), c);
98 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
105 void SearchBarWidget::initializeUI() {
108 setMaximumHeight(150);
110 setMaximumHeight(100);
114 horizontalLayout = new QHBoxLayout;
115 verticalLayout = new QVBoxLayout;
118 searchPushButton = new QPushButton(tr("Search"));
119 searchPushButton->setMinimumWidth(125);
122 searchWordLineEdit = new QLineEdit;
123 searchWordLineEdit->setMinimumWidth(250);
127 completerModel = new QStringListModel(this);
130 lineEditCompleter = new QCompleter(searchWordLineEdit);
131 lineEditCompleter->setModel(completerModel);
132 lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
133 lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
134 searchWordLineEdit->setCompleter(lineEditCompleter);
138 searchWordLineEdit->setMinimumHeight(
139 searchWordLineEdit->sizeHint().height()*3/2);
143 //create layout for lineEdit to have clear button on it
144 QHBoxLayout* lineEditLayout = new QHBoxLayout;
145 searchWordLineEdit->setLayout(lineEditLayout);
148 clearSearchWordToolButton = new QToolButton;
150 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
151 clearSearchWordToolButton->setMaximumSize(
152 clearSearchWordToolButton->sizeHint().height()/2,
153 clearSearchWordToolButton->sizeHint().height()/2);
154 lineEditLayout->setContentsMargins(0,0,10,0);
156 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
157 clearSearchWordToolButton->setMinimumSize(
158 searchWordLineEdit->sizeHint().height()*1.2,
159 searchWordLineEdit->sizeHint().height()*1.2);
160 lineEditLayout->setContentsMargins(0,0,5,0);
164 historyNextToolButton = new QToolButton;
166 historyNextToolButton->setIcon(
167 generateIcon(QIcon::fromTheme("general_forward")));
169 historyNextToolButton->setIcon(
170 generateIcon(QIcon::fromTheme("go-next")));
175 historyPrevToolButton = new QToolButton;
177 historyPrevToolButton->setIcon(
178 generateIcon(QIcon::fromTheme("general_back")));
180 historyPrevToolButton->setIcon(
181 generateIcon(QIcon::fromTheme("go-previous")));
186 historyShowToolButton = new QToolButton;
188 historyShowToolButton->setIcon(
189 generateIcon(QIcon::fromTheme("general_back"), 90));
191 historyShowToolButton->setIcon(
192 generateIcon(QIcon::fromTheme("go-up")));
195 searchingProgressBar = new QProgressBar;
196 //progress bar has minimum and maximum values set to 0, which will effect
197 //with "I'm alive" bar
198 searchingProgressBar->setMinimum(0);
199 searchingProgressBar->setMaximum(0);
201 searchingProgressBar->setMaximumHeight(50);
203 searchingProgressBar->hide();
206 setLayout(verticalLayout);
208 verticalLayout->addWidget(searchingProgressBar);
210 //adding widgets to layout
211 horizontalLayout->addWidget(searchWordLineEdit);
212 horizontalLayout->addWidget(searchPushButton);
213 horizontalLayout->addWidget(historyPrevToolButton);
214 horizontalLayout->addWidget(historyShowToolButton);
215 horizontalLayout->addWidget(historyNextToolButton);
217 //adding clear toolButton to textEdit with right alignment
218 lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
221 verticalLayout->addLayout(horizontalLayout);
225 void SearchBarWidget::searchPushButtonClicked() {
227 Q_EMIT stopSearching();
230 search(searchWordLineEdit->text());
235 void SearchBarWidget::search(QString word) {
236 if(!busy && !word.isEmpty()) {
237 completerModel->insertRow(completerModel->rowCount());
239 completerModel->index(completerModel->rowCount() -1);
241 completerModel->setData(index, word);
244 searchWordLineEdit->setText(word);
245 Q_EMIT searchForTranslations(word);
249 void SearchBarWidget::searchDelay(QString word) {
250 if(!busy && !word.isEmpty()) {
251 searchWordLineEdit->setText(word);
254 if(delayTimer.isActive()) {
259 delayTimer.start(500);
263 void SearchBarWidget::delaySearchTimeout() {
266 Q_EMIT searchForTranslations(delayString);
270 void SearchBarWidget::setEnabled(bool enabled) {
271 searchWordLineEdit->setEnabled(enabled);
274 historyPrevToolButton->setEnabled(false);
275 historyNextToolButton->setEnabled(false);
276 historyShowToolButton->setEnabled(false);
280 void SearchBarWidget::setBusy() {
282 searchingProgressBar->show();
283 searchPushButton->setText(tr("Stop"));
288 void SearchBarWidget::setIdle() {
290 searchingProgressBar->hide();
291 searchPushButton->setText(tr("Search"));
294 Q_EMIT refreshHistoryButtons();
298 void SearchBarWidget::clearSearchWordToolButtonClicked() {
299 searchWordLineEdit->clear();
304 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
306 historyPrevToolButton->setEnabled(prev);
307 historyNextToolButton->setEnabled(next);
308 historyShowToolButton->setEnabled(list);
312 void SearchBarWidget::showHistoryButtonClicked() {
316 QPoint p = historyShowToolButton->pos();
318 emit historyShow(mapToGlobal(p));