Merge branch 'gui'
[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(QWidget *parent) :
32     QWidget(parent) {
33
34     initializeUI();
35
36     setMaximumHeight(150);
37
38     _isSearching = false;
39
40     connect(searchPushButton, SIGNAL(clicked()),
41             this, SLOT(searchPushButtonClicked()));
42
43     connect(searchWordLineEdit, SIGNAL(returnPressed()),
44             this, SLOT(searchPushButtonClicked()));
45
46     connect(historyNextToolButton, SIGNAL(clicked()),
47             this, SIGNAL(historyNext()));
48
49     connect(historyPrevToolButton, SIGNAL(clicked()),
50             this, SIGNAL(historyPrev()));
51
52     connect(historyShowToolButton, SIGNAL(clicked()),
53             this, SIGNAL(historyShow()));
54
55     connect(clearSearchWordToolButton, SIGNAL(clicked()),
56             this, SLOT(clearSearchWordToolButtonClicked()));
57
58
59     searchWordLineEdit->setFocus();
60
61     historyPrevToolButton->setEnabled(false);
62     historyNextToolButton->setEnabled(false);
63     historyShowToolButton->setEnabled(false);
64     setEnabled(true);
65 }
66
67 SearchBarWidget::~SearchBarWidget() {
68
69 }
70
71 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
72     QPixmap p = oryginal.pixmap(64);
73
74     if(rotation != 0) {
75         QMatrix m;
76         m.rotate(rotation);
77
78         p = p.transformed(m);
79     }
80
81     QIcon newIcon;
82     newIcon.addPixmap(p);
83
84     QPainter painter(&p);
85     painter.fillRect(p.rect(), QColor(0,0,0,192));
86
87     newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
88
89     return newIcon;
90 }
91
92
93 void SearchBarWidget::initializeUI() {
94     horizontalLayout = new QHBoxLayout();
95     verticalLayout = new QVBoxLayout();
96
97
98     searchPushButton = new QPushButton(tr("Search"));
99     searchPushButton->setMinimumWidth(150);
100
101
102     searchWordLineEdit = new QLineEdit();
103     searchWordLineEdit->setMinimumWidth(300);
104
105
106
107     //create layout for lineEdit to have clear button on it
108     QHBoxLayout* lineEditLayout = new QHBoxLayout;
109     searchWordLineEdit->setLayout(lineEditLayout);
110
111
112     clearSearchWordToolButton = new QToolButton();
113     clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
114     //tool buttons will have size 2 times smaller
115     clearSearchWordToolButton->setMaximumSize(
116             clearSearchWordToolButton->sizeHint().width()/2,
117             clearSearchWordToolButton->sizeHint().height()/2);
118
119
120     historyNextToolButton = new QToolButton();
121     historyNextToolButton->setIcon(
122             generateIcon(QIcon::fromTheme("general_forward")));
123
124     historyPrevToolButton = new QToolButton();
125     historyPrevToolButton->setIcon(
126             generateIcon(QIcon::fromTheme("general_back")));
127
128     historyShowToolButton = new QToolButton();
129     historyShowToolButton->setIcon(
130             generateIcon(QIcon::fromTheme("general_back"), 90));
131
132
133
134     searchingProgressBar = new QProgressBar();
135     //progress bar have minimum and maximum values set to 0, which will effect
136     //with "I'm alive" bar
137     searchingProgressBar->setMinimum(0);
138     searchingProgressBar->setMaximum(0);
139     searchingProgressBar->hide();
140     searchingProgressBar->setMaximumHeight(50);
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 void SearchBarWidget::searchPushButtonClicked() {
163     if(_isSearching) {
164         emit stopSearching();
165     }
166     else {
167         search(searchWordLineEdit->text());
168     }
169 }
170
171
172 void SearchBarWidget::search(QString word) {
173     if(!_isSearching && !word.isEmpty()) {
174         searchWordLineEdit->setText(word);
175         emit searchForTranslations(word);
176     }
177 }
178
179 void SearchBarWidget::setEnabled(bool enabled) {
180     searchWordLineEdit->setEnabled(enabled);
181
182     if(!enabled) {
183         historyPrevToolButton->setEnabled(false);
184         historyNextToolButton->setEnabled(false);
185         historyShowToolButton->setEnabled(false);
186     }
187 }
188
189 void SearchBarWidget::setBusy() {
190     if(_isSearching) return;
191     searchingProgressBar->show();
192     searchPushButton->setText(tr("Stop"));
193     setEnabled(false);
194     _isSearching = true;
195 }
196
197 void SearchBarWidget::setIdle() {
198     if(!_isSearching) return;
199     searchingProgressBar->hide();
200     searchPushButton->setText(tr("Search"));
201     setEnabled(true);
202     _isSearching = false;
203     emit refreshHistoryButtons();
204 }
205
206
207 void SearchBarWidget::clearSearchWordToolButtonClicked() {
208     searchWordLineEdit->clear();
209 }
210
211
212
213 void SearchBarWidget::updateHistoryButtons(bool prev, bool next, bool list) {
214     if(!_isSearching) {
215         historyPrevToolButton->setEnabled(prev);
216         historyNextToolButton->setEnabled(next);
217         historyShowToolButton->setEnabled(list);
218     }
219 }