Added creation of inactive and rotated icons from standard hildon methods. Added...
[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
29
30 SearchBarWidget::SearchBarWidget(Backbone* backbone, QWidget *parent) :
31     QWidget(parent) {
32
33     this->backbone = backbone;
34
35     initializeUI();
36
37     setMaximumHeight(150);
38
39     _isSearching = false;
40
41     connect(searchPushButton, SIGNAL(clicked()),
42             this, SLOT(searchPushButtonClicked()));
43
44     connect(searchWordLineEdit, SIGNAL(returnPressed()),
45             this, SLOT(searchPushButtonClicked()));
46
47     connect(historyNextToolButton, SIGNAL(clicked()),
48             this, SLOT(historyNextToolButtonClicked()));
49
50     connect(historyPrevToolButton, SIGNAL(clicked()),
51             this, SLOT(historyPrevToolButtonClicked()));
52
53     connect(historyShowToolButton, SIGNAL(clicked()),
54             this, SLOT(historyShowToolButtonClicked()));
55
56     connect(clearSearchWordToolButton, SIGNAL(clicked()),
57             this, SLOT(clearSearchWordToolButtonClicked()));
58
59
60     //connects request to backbone
61     connect(this, SIGNAL(searchForTranslations(QString)),
62             backbone, SLOT(search(QString)));
63
64     connect(this, SIGNAL(stopSearching()),
65             backbone, SLOT(stopSearching()));
66
67     connect(backbone, SIGNAL(ready()),
68             this, SLOT(setIdle()));
69
70     connect(backbone, SIGNAL(htmlReady()),
71             this, SLOT(setIdle()));
72
73     searchWordLineEdit->setFocus();
74 }
75
76 SearchBarWidget::~SearchBarWidget() {
77
78 }
79
80 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
81     QPixmap p = oryginal.pixmap(64);
82
83     if(rotation != 0) {
84         QMatrix m;
85         m.rotate(rotation);
86
87         p = p.transformed(m);
88     }
89
90     QIcon newIcon;
91     newIcon.addPixmap(p);
92
93     QPainter painter(&p);
94     painter.fillRect(p.rect(), QColor(0,0,0,192));
95
96     newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
97
98     return newIcon;
99 }
100
101
102 void SearchBarWidget::initializeUI() {
103     horizontalLayout = new QHBoxLayout();
104     verticalLayout = new QVBoxLayout();
105
106
107     searchPushButton = new QPushButton(tr("Search"));
108     searchPushButton->setMinimumWidth(150);
109
110
111     searchWordLineEdit = new QLineEdit();
112     searchWordLineEdit->setMinimumWidth(300);
113     //create layout for lineEdit to have clear button on it
114     QHBoxLayout* lineEditLayout = new QHBoxLayout;
115     searchWordLineEdit->setLayout(lineEditLayout);
116
117
118     clearSearchWordToolButton = new QToolButton();
119     clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
120     //tool buttons will have size 2 times smaller
121     clearSearchWordToolButton->setMaximumSize(
122             clearSearchWordToolButton->sizeHint().width()/2,
123             clearSearchWordToolButton->sizeHint().height()/2);
124
125
126     historyNextToolButton = new QToolButton();
127     historyNextToolButton->setIcon(
128             generateIcon(QIcon::fromTheme("general_forward")));
129
130     historyPrevToolButton = new QToolButton();
131     historyPrevToolButton->setIcon(
132             generateIcon(QIcon::fromTheme("general_back")));
133
134     historyShowToolButton = new QToolButton();
135     historyShowToolButton->setIcon(
136             generateIcon(QIcon::fromTheme("general_back"), 90));
137
138
139
140     searchingProgressBar = new QProgressBar();
141     //progress bar have minimum and maximum values set to 0, which will effect
142     //with "I'm alive" bar
143     searchingProgressBar->setMinimum(0);
144     searchingProgressBar->setMaximum(0);
145     searchingProgressBar->hide();
146     searchingProgressBar->setMaximumHeight(50);
147
148
149     setLayout(verticalLayout);
150
151     verticalLayout->addWidget(searchingProgressBar);
152
153     //adding widgets to layout
154     horizontalLayout->addWidget(searchWordLineEdit);
155     horizontalLayout->addWidget(searchPushButton);
156     horizontalLayout->addWidget(historyPrevToolButton);
157     horizontalLayout->addWidget(historyShowToolButton);
158     horizontalLayout->addWidget(historyNextToolButton);
159
160     //adding clear toolButton to textEdit with right alignment
161     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
162                               Qt::AlignRight | Qt::AlignVCenter);
163
164     verticalLayout->addLayout(horizontalLayout);
165
166 }
167
168
169 void SearchBarWidget::searchPushButtonClicked() {
170     if(_isSearching) {
171         setIdle();
172         emit stopSearching();
173     }
174     else {
175         search(searchWordLineEdit->text());
176     }
177 }
178
179
180 void SearchBarWidget::search(QString word) {
181     if(!_isSearching && !word.isEmpty()) {
182         searchWordLineEdit->setText(word);
183         setBusy();
184         emit searchForTranslations(word);
185     }
186 }
187
188 void SearchBarWidget::setEnabled(bool enabled) {
189     searchWordLineEdit->setEnabled(enabled);
190     historyNextToolButton->setEnabled(enabled);
191     historyPrevToolButton->setEnabled(enabled);
192     historyShowToolButton->setEnabled(enabled);
193 }
194
195 void SearchBarWidget::setBusy() {
196     if(_isSearching) return;
197     searchingProgressBar->show();
198     searchPushButton->setText(tr("Stop"));
199     setEnabled(false);
200     _isSearching = true;
201     emit busy();
202 }
203
204 void SearchBarWidget::setIdle() {
205     if(!_isSearching) return;
206     searchingProgressBar->hide();
207     searchPushButton->setText(tr("Search"));
208     setEnabled(true);
209     _isSearching = false;
210     emit idle();
211 }
212
213 void SearchBarWidget::historyNextToolButtonClicked() {
214
215 }
216
217 void SearchBarWidget::historyPrevToolButtonClicked() {
218
219 }
220
221 void SearchBarWidget::historyShowToolButtonClicked() {
222
223 }
224
225 void SearchBarWidget::clearSearchWordToolButtonClicked() {
226     searchWordLineEdit->clear();
227 }
228
229 void SearchBarWidget::showHistoryListDialog() {
230
231 }
232
233 bool SearchBarWidget::isSearching() const {
234     return _isSearching;
235 }