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(125);
116 searchWordLineEdit = new QLineEdit;
117 searchWordLineEdit->setMinimumWidth(250);
121 completerModel = new QStringListModel;
124 lineEditCompleter = new QCompleter(searchWordLineEdit);
125 lineEditCompleter->setModel(completerModel);
126 lineEditCompleter->setCaseSensitivity(Qt::CaseInsensitive);
127 lineEditCompleter->setCompletionMode(QCompleter::InlineCompletion);
128 searchWordLineEdit->setCompleter(lineEditCompleter);
132 searchWordLineEdit->setMinimumHeight(
133 searchWordLineEdit->sizeHint().height()*3/2);
137 //create layout for lineEdit to have clear button on it
138 QHBoxLayout* lineEditLayout = new QHBoxLayout;
139 searchWordLineEdit->setLayout(lineEditLayout);
142 clearSearchWordToolButton = new QToolButton;
144 clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
145 //tool buttons will have size 2 times smaller
146 clearSearchWordToolButton->setMaximumSize(
147 clearSearchWordToolButton->sizeHint().height()/2,
148 clearSearchWordToolButton->sizeHint().height()/2);
149 lineEditLayout->setContentsMargins(0,0,10,0);
151 clearSearchWordToolButton->setIcon(QIcon::fromTheme("edit-clear"));
152 clearSearchWordToolButton->setMinimumSize(
153 searchWordLineEdit->sizeHint().height()*1.2,
154 searchWordLineEdit->sizeHint().height()*1.2);
155 lineEditLayout->setContentsMargins(0,0,5,0);
159 historyNextToolButton = new QToolButton;
161 historyNextToolButton->setIcon(
162 generateIcon(QIcon::fromTheme("general_forward")));
164 historyNextToolButton->setIcon(
165 generateIcon(QIcon::fromTheme("go-next")));
170 historyPrevToolButton = new QToolButton;
172 historyPrevToolButton->setIcon(
173 generateIcon(QIcon::fromTheme("general_back")));
175 historyPrevToolButton->setIcon(
176 generateIcon(QIcon::fromTheme("go-previous")));
181 historyShowToolButton = new QToolButton;
183 historyShowToolButton->setIcon(
184 generateIcon(QIcon::fromTheme("general_back"), 90));
186 historyShowToolButton->setIcon(
187 generateIcon(QIcon::fromTheme("go-up")));
190 searchingProgressBar = new QProgressBar;
191 //progress bar have minimum and maximum values set to 0, which will effect
192 //with "I'm alive" bar
193 searchingProgressBar->setMinimum(0);
194 searchingProgressBar->setMaximum(0);
196 searchingProgressBar->setMaximumHeight(50);
198 searchingProgressBar->hide();
201 setLayout(verticalLayout);
203 verticalLayout->addWidget(searchingProgressBar);
205 //adding widgets to layout
206 horizontalLayout->addWidget(searchWordLineEdit);
207 horizontalLayout->addWidget(searchPushButton);
208 horizontalLayout->addWidget(historyPrevToolButton);
209 horizontalLayout->addWidget(historyShowToolButton);
210 horizontalLayout->addWidget(historyNextToolButton);
212 //adding clear toolButton to textEdit with right alignment
213 lineEditLayout->addWidget(clearSearchWordToolButton, 0, Qt::AlignRight);
216 verticalLayout->addLayout(horizontalLayout);
220 void SearchBarWidget::searchPushButtonClicked() {
222 Q_EMIT stopSearching();
225 search(searchWordLineEdit->text());
230 void SearchBarWidget::search(QString word) {
231 if(!_isSearching && !word.isEmpty()) {
232 completerModel->insertRow(completerModel->rowCount());
234 completerModel->index(completerModel->rowCount() -1);
236 completerModel->setData(index, word);
239 searchWordLineEdit->setText(word);
240 Q_EMIT searchForTranslations(word);
244 void SearchBarWidget::searchDelay(QString word) {
245 if(!_isSearching && !word.isEmpty()) {
246 searchWordLineEdit->setText(word);
249 if(delayTimer.isActive()) {
254 delayTimer.start(500);
258 void SearchBarWidget::delaySearchTimeout() {
261 Q_EMIT searchForTranslations(delayString);
265 void SearchBarWidget::setEnabled(bool enabled) {
266 searchWordLineEdit->setEnabled(enabled);
269 historyPrevToolButton->setEnabled(false);
270 historyNextToolButton->setEnabled(false);
271 historyShowToolButton->setEnabled(false);
275 void SearchBarWidget::setBusy() {
276 if(_isSearching) return;
277 searchingProgressBar->show();
278 searchPushButton->setText(tr("Stop"));
283 void SearchBarWidget::setIdle() {
284 if(!_isSearching) return;
285 searchingProgressBar->hide();
286 searchPushButton->setText(tr("Search"));
288 _isSearching = false;
289 Q_EMIT refreshHistoryButtons();
293 void SearchBarWidget::clearSearchWordToolButtonClicked() {
294 searchWordLineEdit->clear();
299 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
301 historyPrevToolButton->setEnabled(prev);
302 historyNextToolButton->setEnabled(next);
303 historyShowToolButton->setEnabled(list);