Added dictionaries settings dialog
[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(historyNextToolButton, SIGNAL(clicked()),
45             this, SLOT(historyNextToolButtonClicked()));
46
47     connect(historyPrevToolButton, SIGNAL(clicked()),
48             this, SLOT(historyPrevToolButtonClicked()));
49
50     connect(historyShowToolButton, SIGNAL(clicked()),
51             this, SLOT(historyShowToolButtonClicked()));
52
53     connect(clearSearchWordToolButton, SIGNAL(clicked()),
54             this, SLOT(clearSearchWordToolButtonClicked()));
55
56
57     //connects request to backbone
58     connect(this, SIGNAL(searchForTranslations(QString)),
59             backbone, SLOT(search(QString)));
60
61     connect(this, SIGNAL(stopSearching()),
62             backbone, SLOT(stopSearching()));
63
64     connect(backbone, SIGNAL(ready()),
65             this, SLOT(searchFinished()));
66
67 }
68
69 SearchBarWidget::~SearchBarWidget() {
70
71 }
72
73
74 void SearchBarWidget::initializeUI() {
75     horizontalLayout = new QHBoxLayout();
76     verticalLayout = new QVBoxLayout();
77
78
79     searchPushButton = new QPushButton(tr("Search"));
80     searchPushButton->setMinimumWidth(200);
81
82
83     searchWordLineEdit = new QLineEdit();
84     searchWordLineEdit->setMinimumWidth(350);
85     //create layout for lineEdit to have clear button on it
86     QHBoxLayout* lineEditLayout = new QHBoxLayout;
87     searchWordLineEdit->setLayout(lineEditLayout);
88
89
90     clearSearchWordToolButton = new QToolButton();
91     clearSearchWordToolButton->setIcon(QIcon::fromTheme("general_stop"));
92     //tool buttons will have size 2 times smaller
93     clearSearchWordToolButton->setMaximumSize(
94             clearSearchWordToolButton->sizeHint().width()/2,
95             clearSearchWordToolButton->sizeHint().height()/2);
96
97
98     historyNextToolButton = new QToolButton();
99     historyNextToolButton->setIcon(QIcon::fromTheme("general_forward"));
100     historyNextToolButton->setMaximumSize(
101             historyNextToolButton->sizeHint().width()/2,
102             historyNextToolButton->sizeHint().height()/2);
103
104
105     historyPrevToolButton = new QToolButton();
106     historyPrevToolButton->setIcon(QIcon::fromTheme("general_back"));
107     historyPrevToolButton->setMaximumSize(
108             historyPrevToolButton->sizeHint().width()/2,
109             historyPrevToolButton->sizeHint().height()/2);
110
111
112     historyShowToolButton = new QToolButton();
113     historyShowToolButton->setIcon(QIcon::fromTheme("general_back"));
114     historyShowToolButton->setMaximumSize(
115             historyShowToolButton->sizeHint().width()/2,
116             historyShowToolButton->sizeHint().height()/2);
117
118     QPixmap p = historyShowToolButton->icon().pixmap(256);
119     QMatrix m;
120     m.rotate(90);
121
122
123     QIcon temp;
124     temp.addPixmap(p.transformed(m));
125     historyShowToolButton->setIcon(temp);
126
127
128     searchingProgressBar = new QProgressBar();
129     //progress bar have minimum and maximum values set to 0, which will effect
130     //with "I'm alive" bar
131     searchingProgressBar->setMinimum(0);
132     searchingProgressBar->setMaximum(0);
133     searchingProgressBar->hide();
134     searchingProgressBar->setMaximumHeight(50);
135
136
137
138     setLayout(verticalLayout);
139
140     verticalLayout->addWidget(searchingProgressBar);
141
142     //adding widgets to layout
143     horizontalLayout->addWidget(searchWordLineEdit);
144     horizontalLayout->addWidget(searchPushButton);
145     horizontalLayout->addWidget(historyPrevToolButton);
146     horizontalLayout->addWidget(historyShowToolButton);
147     horizontalLayout->addWidget(historyNextToolButton);
148
149     //adding clear toolButton to textEdit with right alignment
150     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
151                               Qt::AlignRight | Qt::AlignVCenter);
152
153     verticalLayout->addLayout(horizontalLayout);
154 }
155
156
157 void SearchBarWidget::searchPushButtonClicked() {
158     if(_isSearching) {
159         searchingProgressBar->hide();
160         searchPushButton->setText(tr("Search"));
161         setEnabled(true);
162         _isSearching = false;
163         emit stopSearching();
164     }
165     else {
166         searchingProgressBar->show();
167         searchPushButton->setText(tr("Stop"));
168         setEnabled(false);
169         _isSearching = true;
170         emit searchForTranslations(searchWordLineEdit->text());
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
182 void SearchBarWidget::searchFinished() {
183         searchingProgressBar->hide();
184         searchPushButton->setText(tr("Search"));
185         setEnabled(true);
186         _isSearching = false;
187 }
188
189 void SearchBarWidget::historyNextToolButtonClicked() {
190
191     CommonDictInterface*p = backbone->getPlugins()[0];
192     p->dictDialog()->addNewDictionary(this);
193 }
194
195 void SearchBarWidget::historyPrevToolButtonClicked() {
196
197 }
198
199 void SearchBarWidget::historyShowToolButtonClicked() {
200
201 }
202
203 void SearchBarWidget::clearSearchWordToolButtonClicked() {
204     searchWordLineEdit->clear();
205 }
206
207 void SearchBarWidget::showHistoryListDialog() {
208
209 }
210
211 bool SearchBarWidget::isSearching() const {
212     return _isSearching;
213 }