Added direct search of translation, changed searching for translation of words list...
[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     QIcon newIcon;
84     newIcon.addPixmap(p);
85
86     QPainter painter(&p);
87     painter.fillRect(p.rect(), QColor(0,0,0,192));
88
89     newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
90
91     return newIcon;
92 }
93
94
95 void SearchBarWidget::initializeUI() {
96     horizontalLayout = new QHBoxLayout();
97     verticalLayout = new QVBoxLayout();
98
99
100     searchPushButton = new QPushButton(tr("Search"));
101     searchPushButton->setMinimumWidth(150);
102
103
104     searchWordLineEdit = new QLineEdit();
105     searchWordLineEdit->setMinimumWidth(300);
106     //create layout for lineEdit to have clear button on it
107     QHBoxLayout* lineEditLayout = new QHBoxLayout;
108     searchWordLineEdit->setLayout(lineEditLayout);
109
110
111     clearSearchWordToolButton = new QToolButton();
112     clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
113     //tool buttons will have size 2 times smaller
114     clearSearchWordToolButton->setMaximumSize(
115             clearSearchWordToolButton->sizeHint().width()/2,
116             clearSearchWordToolButton->sizeHint().height()/2);
117
118
119     historyNextToolButton = new QToolButton();
120     historyNextToolButton->setIcon(
121             generateIcon(QIcon::fromTheme("general_forward")));
122
123     historyPrevToolButton = new QToolButton();
124     historyPrevToolButton->setIcon(
125             generateIcon(QIcon::fromTheme("general_back")));
126
127     historyShowToolButton = new QToolButton();
128     historyShowToolButton->setIcon(
129             generateIcon(QIcon::fromTheme("general_back")));
130
131
132     QPixmap p = historyShowToolButton->icon().pixmap(256);
133     qDebug()<<p.size();
134     QMatrix m;
135     m.rotate(90);
136
137     QPixmap p2 = p.transformed(m);
138     qDebug()<<p2.size();
139
140
141     QIcon temp(p2);
142     historyShowToolButton->setIcon(temp);
143
144
145     searchingProgressBar = new QProgressBar();
146     //progress bar have minimum and maximum values set to 0, which will effect
147     //with "I'm alive" bar
148     searchingProgressBar->setMinimum(0);
149     searchingProgressBar->setMaximum(0);
150     searchingProgressBar->hide();
151     searchingProgressBar->setMaximumHeight(50);
152
153
154     setLayout(verticalLayout);
155
156     verticalLayout->addWidget(searchingProgressBar);
157
158     //adding widgets to layout
159     horizontalLayout->addWidget(searchWordLineEdit);
160     horizontalLayout->addWidget(searchPushButton);
161     horizontalLayout->addWidget(historyPrevToolButton);
162     horizontalLayout->addWidget(historyShowToolButton);
163     horizontalLayout->addWidget(historyNextToolButton);
164
165     //adding clear toolButton to textEdit with right alignment
166     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
167                               Qt::AlignRight | Qt::AlignVCenter);
168
169     verticalLayout->addLayout(horizontalLayout);
170
171 }
172
173
174 void SearchBarWidget::searchPushButtonClicked() {
175     if(_isSearching) {
176         setIdle();
177         emit stopSearching();
178     }
179     else {
180         search(searchWordLineEdit->text());
181     }
182 }
183
184
185 void SearchBarWidget::search(QString word) {
186     if(!_isSearching && !word.isEmpty()) {
187         searchWordLineEdit->setText(word);
188         setBusy();
189         emit searchForTranslations(word);
190     }
191 }
192
193 void SearchBarWidget::setEnabled(bool enabled) {
194     searchWordLineEdit->setEnabled(enabled);
195     historyNextToolButton->setEnabled(enabled);
196     historyPrevToolButton->setEnabled(enabled);
197     historyShowToolButton->setEnabled(enabled);
198 }
199
200 void SearchBarWidget::setBusy() {
201     if(_isSearching) return;
202     searchingProgressBar->show();
203     searchPushButton->setText(tr("Stop"));
204     setEnabled(false);
205     _isSearching = true;
206 }
207
208 void SearchBarWidget::setIdle() {
209     if(!_isSearching) return;
210     searchingProgressBar->hide();
211     searchPushButton->setText(tr("Search"));
212     setEnabled(true);
213     _isSearching = false;
214 }
215
216 void SearchBarWidget::historyNextToolButtonClicked() {
217
218 }
219
220 void SearchBarWidget::historyPrevToolButtonClicked() {
221
222 }
223
224 void SearchBarWidget::historyShowToolButtonClicked() {
225
226 }
227
228 void SearchBarWidget::clearSearchWordToolButtonClicked() {
229     searchWordLineEdit->clear();
230 }
231
232 void SearchBarWidget::showHistoryListDialog() {
233
234 }
235
236 bool SearchBarWidget::isSearching() const {
237     return _isSearching;
238 }