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