Fixed bug with disabling history buttons while searching
[mdictionary] / trunk / src / base / gui / SearchBarWidget.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
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.
9
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.
14
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/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //Created by Mateusz Półrola
23
24
25 #include "SearchBarWidget.h"
26 #include <QDebug>
27 #include "../../includes/DictDialog.h"
28 #include "HistoryListDialog.h"
29
30
31 SearchBarWidget::SearchBarWidget(Backbone* backbone, QWidget *parent) :
32     QWidget(parent) {
33
34     this->backbone = backbone;
35
36     history = backbone->history();
37
38     initializeUI();
39
40     setMaximumHeight(150);
41
42     _isSearching = false;
43
44     connect(searchPushButton, SIGNAL(clicked()),
45             this, SLOT(searchPushButtonClicked()));
46
47     connect(searchWordLineEdit, SIGNAL(returnPressed()),
48             this, SLOT(searchPushButtonClicked()));
49
50     connect(historyNextToolButton, SIGNAL(clicked()),
51             this, SLOT(historyNextToolButtonClicked()));
52
53     connect(historyPrevToolButton, SIGNAL(clicked()),
54             this, SLOT(historyPrevToolButtonClicked()));
55
56     connect(historyShowToolButton, SIGNAL(clicked()),
57             this, SLOT(historyShowToolButtonClicked()));
58
59     connect(clearSearchWordToolButton, SIGNAL(clicked()),
60             this, SLOT(clearSearchWordToolButtonClicked()));
61
62
63     //connects request to backbone
64     connect(this, SIGNAL(searchForTranslations(QString)),
65             backbone, SLOT(search(QString)));
66
67     connect(this, SIGNAL(stopSearching()),
68             backbone, SLOT(stopSearching()));
69
70     connect(backbone, SIGNAL(ready()),
71             this, SLOT(setIdle()));
72
73     connect(backbone, SIGNAL(htmlReady()),
74             this, SLOT(setIdle()));
75
76
77     connect(history, SIGNAL(historyChanged(bool,bool,bool)),
78             this, SLOT(updateHistoryButtons(bool,bool,bool)));
79
80     searchWordLineEdit->setFocus();
81
82     setEnabled(true);
83 }
84
85 SearchBarWidget::~SearchBarWidget() {
86
87 }
88
89 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
90     QPixmap p = oryginal.pixmap(64);
91
92     if(rotation != 0) {
93         QMatrix m;
94         m.rotate(rotation);
95
96         p = p.transformed(m);
97     }
98
99     QIcon newIcon;
100     newIcon.addPixmap(p);
101
102     QPainter painter(&p);
103     painter.fillRect(p.rect(), QColor(0,0,0,192));
104
105     newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
106
107     return newIcon;
108 }
109
110
111 void SearchBarWidget::initializeUI() {
112     horizontalLayout = new QHBoxLayout();
113     verticalLayout = new QVBoxLayout();
114
115
116     searchPushButton = new QPushButton(tr("Search"));
117     searchPushButton->setMinimumWidth(150);
118
119
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);
125
126
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);
133
134
135     historyNextToolButton = new QToolButton();
136     historyNextToolButton->setIcon(
137             generateIcon(QIcon::fromTheme("general_forward")));
138
139     historyPrevToolButton = new QToolButton();
140     historyPrevToolButton->setIcon(
141             generateIcon(QIcon::fromTheme("general_back")));
142
143     historyShowToolButton = new QToolButton();
144     historyShowToolButton->setIcon(
145             generateIcon(QIcon::fromTheme("general_back"), 90));
146
147
148
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);
156
157
158     setLayout(verticalLayout);
159
160     verticalLayout->addWidget(searchingProgressBar);
161
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);
168
169     //adding clear toolButton to textEdit with right alignment
170     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
171                               Qt::AlignRight | Qt::AlignVCenter);
172
173     verticalLayout->addLayout(horizontalLayout);
174 }
175
176
177 void SearchBarWidget::searchPushButtonClicked() {
178     if(_isSearching) {
179         setIdle();
180         emit stopSearching();
181     }
182     else {
183         search(searchWordLineEdit->text());
184     }
185 }
186
187
188 void SearchBarWidget::search(QString word) {
189     if(!_isSearching && !word.isEmpty()) {
190         searchWordLineEdit->setText(word);
191         setBusy();
192         history->add(word);
193         emit searchForTranslations(word);
194     }
195 }
196
197 void SearchBarWidget::setEnabled(bool enabled) {
198     searchWordLineEdit->setEnabled(enabled);
199
200     if(enabled) {
201         historyNextToolButton->setEnabled(history->nextAvailable());
202         historyPrevToolButton->setEnabled(history->prevAvailable());
203         historyShowToolButton->setEnabled(history->listAvailable());
204     }
205     else {
206         historyNextToolButton->setEnabled(false);
207         historyPrevToolButton->setEnabled(false);
208         historyShowToolButton->setEnabled(false);
209     }
210 }
211
212 void SearchBarWidget::setBusy() {
213     if(_isSearching) return;
214     searchingProgressBar->show();
215     searchPushButton->setText(tr("Stop"));
216     setEnabled(false);
217     _isSearching = true;
218     emit busy();
219 }
220
221 void SearchBarWidget::setIdle() {
222     if(!_isSearching) return;
223     searchingProgressBar->hide();
224     searchPushButton->setText(tr("Search"));
225     setEnabled(true);
226     _isSearching = false;
227     emit idle();
228 }
229
230 void SearchBarWidget::historyNextToolButtonClicked() {
231     QString next = history->next();
232     if(!next.isEmpty()) {
233         searchWordLineEdit->setText(next);
234     }
235 }
236
237 void SearchBarWidget::historyPrevToolButtonClicked() {
238     QString prev = history->previous();
239     if(!prev.isEmpty()) {
240         searchWordLineEdit->setText(prev);
241     }
242 }
243
244 void SearchBarWidget::historyShowToolButtonClicked() {
245     HistoryListDialog listDialog(history, this);
246     listDialog.exec();
247 }
248
249 void SearchBarWidget::clearSearchWordToolButtonClicked() {
250     searchWordLineEdit->clear();
251 }
252
253
254 bool SearchBarWidget::isSearching() const {
255     return _isSearching;
256 }
257
258 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
259     if(!isSearching()) {
260         historyPrevToolButton->setEnabled(prev);
261         historyNextToolButton->setEnabled(next);
262         historyShowToolButton->setEnabled(list);
263     }
264 }