Refactorization of 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     setEnabled(true);
62 }
63
64 SearchBarWidget::~SearchBarWidget() {
65
66 }
67
68 QIcon SearchBarWidget::generateIcon(QIcon oryginal, qreal rotation) {
69     QPixmap p = oryginal.pixmap(64);
70
71     if(rotation != 0) {
72         QMatrix m;
73         m.rotate(rotation);
74
75         p = p.transformed(m);
76     }
77
78     QIcon newIcon;
79     newIcon.addPixmap(p);
80
81     QPainter painter(&p);
82     painter.fillRect(p.rect(), QColor(0,0,0,192));
83
84     newIcon.addPixmap(p, QIcon::Disabled, QIcon::Off);
85
86     return newIcon;
87 }
88
89
90 void SearchBarWidget::initializeUI() {
91     horizontalLayout = new QHBoxLayout();
92     verticalLayout = new QVBoxLayout();
93
94
95     searchPushButton = new QPushButton(tr("Search"));
96     searchPushButton->setMinimumWidth(150);
97
98
99     searchWordLineEdit = new QLineEdit();
100     searchWordLineEdit->setMinimumWidth(300);
101
102
103
104     //create layout for lineEdit to have clear button on it
105     QHBoxLayout* lineEditLayout = new QHBoxLayout;
106     searchWordLineEdit->setLayout(lineEditLayout);
107
108
109     clearSearchWordToolButton = new QToolButton();
110     clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
111     //tool buttons will have size 2 times smaller
112     clearSearchWordToolButton->setMaximumSize(
113             clearSearchWordToolButton->sizeHint().width()/2,
114             clearSearchWordToolButton->sizeHint().height()/2);
115
116
117     historyNextToolButton = new QToolButton();
118     historyNextToolButton->setIcon(
119             generateIcon(QIcon::fromTheme("general_forward")));
120
121     historyPrevToolButton = new QToolButton();
122     historyPrevToolButton->setIcon(
123             generateIcon(QIcon::fromTheme("general_back")));
124
125     historyShowToolButton = new QToolButton();
126     historyShowToolButton->setIcon(
127             generateIcon(QIcon::fromTheme("general_back"), 90));
128
129
130
131     searchingProgressBar = new QProgressBar();
132     //progress bar have minimum and maximum values set to 0, which will effect
133     //with "I'm alive" bar
134     searchingProgressBar->setMinimum(0);
135     searchingProgressBar->setMaximum(0);
136     searchingProgressBar->hide();
137     searchingProgressBar->setMaximumHeight(50);
138
139
140     setLayout(verticalLayout);
141
142     verticalLayout->addWidget(searchingProgressBar);
143
144     //adding widgets to layout
145     horizontalLayout->addWidget(searchWordLineEdit);
146     horizontalLayout->addWidget(searchPushButton);
147     horizontalLayout->addWidget(historyPrevToolButton);
148     horizontalLayout->addWidget(historyShowToolButton);
149     horizontalLayout->addWidget(historyNextToolButton);
150
151     //adding clear toolButton to textEdit with right alignment
152     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
153                               Qt::AlignRight | Qt::AlignVCenter);
154
155     verticalLayout->addLayout(horizontalLayout);
156 }
157
158
159 void SearchBarWidget::searchPushButtonClicked() {
160     if(_isSearching) {
161         emit stopSearching();
162     }
163     else {
164         search(searchWordLineEdit->text());
165     }
166 }
167
168
169 void SearchBarWidget::search(QString word) {
170     if(!_isSearching && !word.isEmpty()) {
171         searchWordLineEdit->setText(word);
172         emit searchForTranslations(word);
173     }
174 }
175
176 void SearchBarWidget::setEnabled(bool enabled) {
177     searchWordLineEdit->setEnabled(enabled);
178
179     if(!enabled) {
180         historyPrevToolButton->setEnabled(false);
181         historyNextToolButton->setEnabled(false);
182         historyShowToolButton->setEnabled(false);
183     }
184 }
185
186 void SearchBarWidget::setBusy() {
187     if(_isSearching) return;
188     searchingProgressBar->show();
189     searchPushButton->setText(tr("Stop"));
190     setEnabled(false);
191     _isSearching = true;
192 }
193
194 void SearchBarWidget::setIdle() {
195     if(!_isSearching) return;
196     searchingProgressBar->hide();
197     searchPushButton->setText(tr("Search"));
198     setEnabled(true);
199     _isSearching = false;
200     emit refreshHistoryButtons();
201 }
202
203
204 void SearchBarWidget::clearSearchWordToolButtonClicked() {
205     searchWordLineEdit->clear();
206 }
207
208
209 bool SearchBarWidget::isSearching() const {
210     return _isSearching;
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 }