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 //Created by Mateusz Półrola
25 #include "SearchBarWidget.h"
27 #include "../../includes/DictDialog.h"
28 #include "HistoryListDialog.h"
31 SearchBarWidget::SearchBarWidget(QWidget *parent) :
39 connect(searchPushButton, SIGNAL(clicked()),
40 this, SLOT(searchPushButtonClicked()));
42 connect(searchWordLineEdit, SIGNAL(returnPressed()),
43 this, SLOT(searchPushButtonClicked()));
45 connect(historyNextToolButton, SIGNAL(clicked()),
46 this, SIGNAL(historyNext()));
48 connect(historyPrevToolButton, SIGNAL(clicked()),
49 this, SIGNAL(historyPrev()));
51 connect(historyShowToolButton, SIGNAL(clicked()),
52 this, SIGNAL(historyShow()));
54 connect(clearSearchWordToolButton, SIGNAL(clicked()),
55 this, SLOT(clearSearchWordToolButtonClicked()));
58 connect(&delayTimer, SIGNAL(timeout()),
59 this, SLOT(delaySearchTimeout()));
62 searchWordLineEdit->setFocus();
64 historyPrevToolButton->setEnabled(false);
65 historyNextToolButton->setEnabled(false);
66 historyShowToolButton->setEnabled(false);
70 SearchBarWidget::~SearchBarWidget() {
74 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
75 QPixmap p = oryginal.pixmap(64);
90 painter.fillRect(p.rect(), QColor(0,0,0,192));
92 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
99 void SearchBarWidget::initializeUI() {
102 setMaximumHeight(150);
104 setMaximumHeight(100);
108 horizontalLayout = new QHBoxLayout();
109 verticalLayout = new QVBoxLayout();
112 searchPushButton = new QPushButton(tr("Search"));
113 searchPushButton->setMinimumWidth(150);
115 searchPushButton->setMinimumHeight(
116 searchPushButton->sizeHint().height()*2);
119 searchWordLineEdit = new QLineEdit();
120 searchWordLineEdit->setMinimumWidth(300);
123 searchWordLineEdit->setMinimumHeight(
124 searchWordLineEdit->sizeHint().height()*2);
128 //create layout for lineEdit to have clear button on it
129 QHBoxLayout* lineEditLayout = new QHBoxLayout;
130 searchWordLineEdit->setLayout(lineEditLayout);
133 clearSearchWordToolButton = new QToolButton();
135 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
136 //tool buttons will have size 2 times smaller
137 clearSearchWordToolButton->setMaximumSize(
138 clearSearchWordToolButton->sizeHint().height()/2,
139 clearSearchWordToolButton->sizeHint().height()/2);
141 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
142 clearSearchWordToolButton->setMinimumSize(
143 clearSearchWordToolButton->sizeHint().height(),
144 clearSearchWordToolButton->sizeHint().height());
148 historyNextToolButton = new QToolButton();
150 historyNextToolButton->setIcon(
151 generateIcon(QIcon::fromTheme("general_forward")));
153 historyNextToolButton->setIcon(
154 generateIcon(QIcon::fromTheme("go-next")));
155 historyNextToolButton->setMinimumSize(
156 clearSearchWordToolButton->sizeHint().height()*2,
157 clearSearchWordToolButton->sizeHint().height()*2);
162 historyPrevToolButton = new QToolButton();
164 historyPrevToolButton->setIcon(
165 generateIcon(QIcon::fromTheme("general_back")));
167 historyPrevToolButton->setIcon(
168 generateIcon(QIcon::fromTheme("go-previous")));
169 historyPrevToolButton->setMinimumSize(
170 clearSearchWordToolButton->sizeHint().height()*2,
171 clearSearchWordToolButton->sizeHint().height()*2);
176 historyShowToolButton = new QToolButton();
178 historyShowToolButton->setIcon(
179 generateIcon(QIcon::fromTheme("general_back"), 90));
181 historyShowToolButton->setIcon(
182 generateIcon(QIcon::fromTheme("go-up")));
183 historyShowToolButton->setMinimumSize(
184 clearSearchWordToolButton->sizeHint().height()*2,
185 clearSearchWordToolButton->sizeHint().height()*2);
189 searchingProgressBar = new QProgressBar();
190 //progress bar have minimum and maximum values set to 0, which will effect
191 //with "I'm alive" bar
192 searchingProgressBar->setMinimum(0);
193 searchingProgressBar->setMaximum(0);
194 searchingProgressBar->hide();
196 searchingProgressBar->setMaximumHeight(50);
200 setLayout(verticalLayout);
202 verticalLayout->addWidget(searchingProgressBar);
204 //adding widgets to layout
205 horizontalLayout->addWidget(searchWordLineEdit);
206 horizontalLayout->addWidget(searchPushButton);
207 horizontalLayout->addWidget(historyPrevToolButton);
208 horizontalLayout->addWidget(historyShowToolButton);
209 horizontalLayout->addWidget(historyNextToolButton);
211 //adding clear toolButton to textEdit with right alignment
212 lineEditLayout->addWidget(clearSearchWordToolButton, 0,
213 Qt::AlignRight | Qt::AlignVCenter);
215 verticalLayout->addLayout(horizontalLayout);
219 void SearchBarWidget::searchPushButtonClicked() {
221 emit stopSearching();
224 search(searchWordLineEdit->text());
229 void SearchBarWidget::search(QString word) {
230 if(!_isSearching && !word.isEmpty()) {
231 searchWordLineEdit->setText(word);
232 emit searchForTranslations(word);
236 void SearchBarWidget::searchDelay(QString word) {
237 if(!_isSearching && !word.isEmpty()) {
238 searchWordLineEdit->setText(word);
241 if(delayTimer.isActive()) {
246 delayTimer.start(500);
250 void SearchBarWidget::delaySearchTimeout() {
253 emit searchForTranslations(delayString);
257 void SearchBarWidget::setEnabled(bool enabled) {
258 searchWordLineEdit->setEnabled(enabled);
261 historyPrevToolButton->setEnabled(false);
262 historyNextToolButton->setEnabled(false);
263 historyShowToolButton->setEnabled(false);
267 void SearchBarWidget::setBusy() {
268 if(_isSearching) return;
269 searchingProgressBar->show();
270 searchPushButton->setText(tr("Stop"));
275 void SearchBarWidget::setIdle() {
276 if(!_isSearching) return;
277 searchingProgressBar->hide();
278 searchPushButton->setText(tr("Search"));
280 _isSearching = false;
281 emit refreshHistoryButtons();
285 void SearchBarWidget::clearSearchWordToolButtonClicked() {
286 searchWordLineEdit->clear();
291 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
293 historyPrevToolButton->setEnabled(prev);
294 historyNextToolButton->setEnabled(next);
295 historyShowToolButton->setEnabled(list);