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);
89 painter.fillRect(p.rect(), QColor(0,0,0,192));
91 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
97 void SearchBarWidget::initializeUI() {
98 horizontalLayout = new QHBoxLayout();
99 verticalLayout = new QVBoxLayout();
102 searchPushButton = new QPushButton(tr("Search"));
103 searchPushButton->setMinimumWidth(150);
106 searchWordLineEdit = new QLineEdit();
107 searchWordLineEdit->setMinimumWidth(300);
111 //create layout for lineEdit to have clear button on it
112 QHBoxLayout* lineEditLayout = new QHBoxLayout;
113 searchWordLineEdit->setLayout(lineEditLayout);
116 clearSearchWordToolButton = new QToolButton();
117 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
118 //tool buttons will have size 2 times smaller
119 clearSearchWordToolButton->setMaximumSize(
120 clearSearchWordToolButton->sizeHint().width()/2,
121 clearSearchWordToolButton->sizeHint().height()/2);
124 historyNextToolButton = new QToolButton();
125 historyNextToolButton->setIcon(
126 generateIcon(QIcon::fromTheme("general_forward")));
128 historyPrevToolButton = new QToolButton();
129 historyPrevToolButton->setIcon(
130 generateIcon(QIcon::fromTheme("general_back")));
132 historyShowToolButton = new QToolButton();
133 historyShowToolButton->setIcon(
134 generateIcon(QIcon::fromTheme("general_back"), 90));
138 searchingProgressBar = new QProgressBar();
139 //progress bar have minimum and maximum values set to 0, which will effect
140 //with "I'm alive" bar
141 searchingProgressBar->setMinimum(0);
142 searchingProgressBar->setMaximum(0);
143 searchingProgressBar->hide();
144 searchingProgressBar->setMaximumHeight(50);
147 setLayout(verticalLayout);
149 verticalLayout->addWidget(searchingProgressBar);
151 //adding widgets to layout
152 horizontalLayout->addWidget(searchWordLineEdit);
153 horizontalLayout->addWidget(searchPushButton);
154 horizontalLayout->addWidget(historyPrevToolButton);
155 horizontalLayout->addWidget(historyShowToolButton);
156 horizontalLayout->addWidget(historyNextToolButton);
158 //adding clear toolButton to textEdit with right alignment
159 lineEditLayout->addWidget(clearSearchWordToolButton, 0,
160 Qt::AlignRight | Qt::AlignVCenter);
162 verticalLayout->addLayout(horizontalLayout);
166 void SearchBarWidget::searchPushButtonClicked() {
168 emit stopSearching();
171 search(searchWordLineEdit->text());
176 void SearchBarWidget::search(QString word) {
177 if(!_isSearching && !word.isEmpty()) {
178 searchWordLineEdit->setText(word);
179 emit searchForTranslations(word);
183 void SearchBarWidget::searchDelay(QString word) {
184 if(!_isSearching && !word.isEmpty()) {
185 searchWordLineEdit->setText(word);
188 if(delayTimer.isActive()) {
193 delayTimer.start(500);
197 void SearchBarWidget::delaySearchTimeout() {
200 emit searchForTranslations(delayString);
204 void SearchBarWidget::setEnabled(bool enabled) {
205 searchWordLineEdit->setEnabled(enabled);
208 historyPrevToolButton->setEnabled(false);
209 historyNextToolButton->setEnabled(false);
210 historyShowToolButton->setEnabled(false);
214 void SearchBarWidget::setBusy() {
215 if(_isSearching) return;
216 searchingProgressBar->show();
217 searchPushButton->setText(tr("Stop"));
222 void SearchBarWidget::setIdle() {
223 if(!_isSearching) return;
224 searchingProgressBar->hide();
225 searchPushButton->setText(tr("Search"));
227 _isSearching = false;
228 emit refreshHistoryButtons();
232 void SearchBarWidget::clearSearchWordToolButtonClicked() {
233 searchWordLineEdit->clear();
238 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
240 historyPrevToolButton->setEnabled(prev);
241 historyNextToolButton->setEnabled(next);
242 historyShowToolButton->setEnabled(list);