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