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) :
36 setMaximumHeight(150);
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, SIGNAL(historyShow()));
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);
71 SearchBarWidget::~SearchBarWidget() {
75 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
76 QPixmap p = oryginal.pixmap(64);
91 painter.fillRect(p.rect(), QColor(0,0,0,192));
93 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
100 void SearchBarWidget::initializeUI() {
101 horizontalLayout = new QHBoxLayout();
102 verticalLayout = new QVBoxLayout();
105 searchPushButton = new QPushButton(tr("Search"));
106 searchPushButton->setMinimumWidth(150);
108 searchPushButton->setMinimumHeight(
109 searchPushButton->sizeHint().height()*2);
112 searchWordLineEdit = new QLineEdit();
113 searchWordLineEdit->setMinimumWidth(300);
116 searchWordLineEdit->setMinimumHeight(
117 searchWordLineEdit->sizeHint().height()*2);
122 //create layout for lineEdit to have clear button on it
123 QHBoxLayout* lineEditLayout = new QHBoxLayout;
124 searchWordLineEdit->setLayout(lineEditLayout);
127 clearSearchWordToolButton = new QToolButton();
130 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
131 //tool buttons will have size 2 times smaller
132 clearSearchWordToolButton->setMaximumSize(
133 clearSearchWordToolButton->sizeHint().height()/2,
134 clearSearchWordToolButton->sizeHint().height()/2);
136 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
137 clearSearchWordToolButton->setMinimumSize(
138 clearSearchWordToolButton->sizeHint().height(),
139 clearSearchWordToolButton->sizeHint().height());
143 historyNextToolButton = new QToolButton();
145 historyNextToolButton->setIcon(
146 generateIcon(QIcon::fromTheme("general_forward")));
148 historyNextToolButton->setIcon(
149 generateIcon(QIcon::fromTheme("go-next")));
150 historyNextToolButton->setMinimumSize(
151 clearSearchWordToolButton->sizeHint().height()*2,
152 clearSearchWordToolButton->sizeHint().height()*2);
155 historyPrevToolButton = new QToolButton();
157 historyPrevToolButton->setIcon(
158 generateIcon(QIcon::fromTheme("general_back")));
160 historyPrevToolButton->setIcon(
161 generateIcon(QIcon::fromTheme("go-previous")));
162 historyPrevToolButton->setMinimumSize(
163 clearSearchWordToolButton->sizeHint().height()*2,
164 clearSearchWordToolButton->sizeHint().height()*2);
167 historyShowToolButton = new QToolButton();
169 historyShowToolButton->setIcon(
170 generateIcon(QIcon::fromTheme("general_back"), 90));
172 historyShowToolButton->setIcon(
173 generateIcon(QIcon::fromTheme("go-up")));
174 historyShowToolButton->setMinimumSize(
175 clearSearchWordToolButton->sizeHint().height()*2,
176 clearSearchWordToolButton->sizeHint().height()*2);
180 searchingProgressBar = new QProgressBar();
181 //progress bar have minimum and maximum values set to 0, which will effect
182 //with "I'm alive" bar
183 searchingProgressBar->setMinimum(0);
184 searchingProgressBar->setMaximum(0);
185 searchingProgressBar->hide();
186 searchingProgressBar->setMaximumHeight(50);
189 setLayout(verticalLayout);
191 verticalLayout->addWidget(searchingProgressBar);
193 //adding widgets to layout
194 horizontalLayout->addWidget(searchWordLineEdit);
195 horizontalLayout->addWidget(searchPushButton);
196 horizontalLayout->addWidget(historyPrevToolButton);
197 horizontalLayout->addWidget(historyShowToolButton);
198 horizontalLayout->addWidget(historyNextToolButton);
200 //adding clear toolButton to textEdit with right alignment
201 lineEditLayout->addWidget(clearSearchWordToolButton, 0,
202 Qt::AlignRight | Qt::AlignVCenter);
204 verticalLayout->addLayout(horizontalLayout);
208 void SearchBarWidget::searchPushButtonClicked() {
210 emit stopSearching();
213 search(searchWordLineEdit->text());
218 void SearchBarWidget::search(QString word) {
219 if(!_isSearching && !word.isEmpty()) {
220 searchWordLineEdit->setText(word);
221 emit searchForTranslations(word);
225 void SearchBarWidget::searchDelay(QString word) {
226 if(!_isSearching && !word.isEmpty()) {
227 searchWordLineEdit->setText(word);
230 if(delayTimer.isActive()) {
235 delayTimer.start(500);
239 void SearchBarWidget::delaySearchTimeout() {
242 emit searchForTranslations(delayString);
246 void SearchBarWidget::setEnabled(bool enabled) {
247 searchWordLineEdit->setEnabled(enabled);
250 historyPrevToolButton->setEnabled(false);
251 historyNextToolButton->setEnabled(false);
252 historyShowToolButton->setEnabled(false);
256 void SearchBarWidget::setBusy() {
257 if(_isSearching) return;
258 searchingProgressBar->show();
259 searchPushButton->setText(tr("Stop"));
264 void SearchBarWidget::setIdle() {
265 if(!_isSearching) return;
266 searchingProgressBar->hide();
267 searchPushButton->setText(tr("Search"));
269 _isSearching = false;
270 emit refreshHistoryButtons();
274 void SearchBarWidget::clearSearchWordToolButtonClicked() {
275 searchWordLineEdit->clear();
280 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
282 historyPrevToolButton->setEnabled(prev);
283 historyNextToolButton->setEnabled(next);
284 historyShowToolButton->setEnabled(list);