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