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(Backbone* backbone, QWidget *parent) :
34 this->backbone = backbone;
36 history = backbone->history();
40 setMaximumHeight(150);
44 connect(searchPushButton, SIGNAL(clicked()),
45 this, SLOT(searchPushButtonClicked()));
47 connect(searchWordLineEdit, SIGNAL(returnPressed()),
48 this, SLOT(searchPushButtonClicked()));
50 connect(historyNextToolButton, SIGNAL(clicked()),
51 this, SLOT(historyNextToolButtonClicked()));
53 connect(historyPrevToolButton, SIGNAL(clicked()),
54 this, SLOT(historyPrevToolButtonClicked()));
56 connect(historyShowToolButton, SIGNAL(clicked()),
57 this, SLOT(historyShowToolButtonClicked()));
59 connect(clearSearchWordToolButton, SIGNAL(clicked()),
60 this, SLOT(clearSearchWordToolButtonClicked()));
63 //connects request to backbone
64 connect(this, SIGNAL(searchForTranslations(QString)),
65 backbone, SLOT(search(QString)));
67 connect(this, SIGNAL(stopSearching()),
68 backbone, SLOT(stopSearching()));
70 connect(backbone, SIGNAL(ready()),
71 this, SLOT(setIdle()));
73 connect(backbone, SIGNAL(htmlReady()),
74 this, SLOT(setIdle()));
77 connect(history, SIGNAL(historyChanged(bool,bool,bool)),
78 this, SLOT(updateHistoryButtons(bool,bool,bool)));
80 searchWordLineEdit->setFocus();
85 SearchBarWidget::~SearchBarWidget() {
89 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
90 QPixmap p = oryginal.pixmap(64);
100 newIcon.addPixmap(p);
102 QPainter painter(&p);
103 painter.fillRect(p.rect(), QColor(0,0,0,192));
105 newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
111 void SearchBarWidget::initializeUI() {
112 horizontalLayout = new QHBoxLayout();
113 verticalLayout = new QVBoxLayout();
116 searchPushButton = new QPushButton(tr("Search"));
117 searchPushButton->setMinimumWidth(150);
120 searchWordLineEdit = new QLineEdit();
121 searchWordLineEdit->setMinimumWidth(300);
122 //create layout for lineEdit to have clear button on it
123 QHBoxLayout* lineEditLayout = new QHBoxLayout;
124 searchWordLineEdit->setLayout(lineEditLayout);
127 clearSearchWordToolButton = new QToolButton();
128 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
129 //tool buttons will have size 2 times smaller
130 clearSearchWordToolButton->setMaximumSize(
131 clearSearchWordToolButton->sizeHint().width()/2,
132 clearSearchWordToolButton->sizeHint().height()/2);
135 historyNextToolButton = new QToolButton();
136 historyNextToolButton->setIcon(
137 generateIcon(QIcon::fromTheme("general_forward")));
139 historyPrevToolButton = new QToolButton();
140 historyPrevToolButton->setIcon(
141 generateIcon(QIcon::fromTheme("general_back")));
143 historyShowToolButton = new QToolButton();
144 historyShowToolButton->setIcon(
145 generateIcon(QIcon::fromTheme("general_back"), 90));
149 searchingProgressBar = new QProgressBar();
150 //progress bar have minimum and maximum values set to 0, which will effect
151 //with "I'm alive" bar
152 searchingProgressBar->setMinimum(0);
153 searchingProgressBar->setMaximum(0);
154 searchingProgressBar->hide();
155 searchingProgressBar->setMaximumHeight(50);
158 setLayout(verticalLayout);
160 verticalLayout->addWidget(searchingProgressBar);
162 //adding widgets to layout
163 horizontalLayout->addWidget(searchWordLineEdit);
164 horizontalLayout->addWidget(searchPushButton);
165 horizontalLayout->addWidget(historyPrevToolButton);
166 horizontalLayout->addWidget(historyShowToolButton);
167 horizontalLayout->addWidget(historyNextToolButton);
169 //adding clear toolButton to textEdit with right alignment
170 lineEditLayout->addWidget(clearSearchWordToolButton, 0,
171 Qt::AlignRight | Qt::AlignVCenter);
173 verticalLayout->addLayout(horizontalLayout);
177 void SearchBarWidget::searchPushButtonClicked() {
180 emit stopSearching();
183 search(searchWordLineEdit->text());
188 void SearchBarWidget::search(QString word) {
189 if(!_isSearching && !word.isEmpty()) {
190 searchWordLineEdit->setText(word);
193 emit searchForTranslations(word);
197 void SearchBarWidget::setEnabled(bool enabled) {
198 searchWordLineEdit->setEnabled(enabled);
201 historyNextToolButton->setEnabled(history->nextAvailable());
202 historyPrevToolButton->setEnabled(history->prevAvailable());
203 historyShowToolButton->setEnabled(history->listAvailable());
206 historyNextToolButton->setEnabled(false);
207 historyPrevToolButton->setEnabled(false);
208 historyShowToolButton->setEnabled(false);
212 void SearchBarWidget::setBusy() {
213 if(_isSearching) return;
214 searchingProgressBar->show();
215 searchPushButton->setText(tr("Stop"));
221 void SearchBarWidget::setIdle() {
222 if(!_isSearching) return;
223 searchingProgressBar->hide();
224 searchPushButton->setText(tr("Search"));
226 _isSearching = false;
230 void SearchBarWidget::historyNextToolButtonClicked() {
231 QString next = history->next();
232 if(!next.isEmpty()) {
233 searchWordLineEdit->setText(next);
237 void SearchBarWidget::historyPrevToolButtonClicked() {
238 QString prev = history->previous();
239 if(!prev.isEmpty()) {
240 searchWordLineEdit->setText(prev);
244 void SearchBarWidget::historyShowToolButtonClicked() {
245 HistoryListDialog listDialog(history, this);
249 void SearchBarWidget::clearSearchWordToolButtonClicked() {
250 searchWordLineEdit->clear();
254 bool SearchBarWidget::isSearching() const {
258 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
260 historyPrevToolButton->setEnabled(prev);
261 historyNextToolButton->setEnabled(next);
262 historyShowToolButton->setEnabled(list);