gui signals and slots connected with backbone
[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
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     connect(this, SIGNAL(searchForTranslations(QString)),
58             backbone, SLOT(search(QString)));
59
60     connect(this, SIGNAL(stopSearching()),
61             backbone, SLOT(stopSearching()));
62
63     connect(backbone, SIGNAL(ready()),
64             this, SLOT(setEnabled(bool)));
65
66 }
67
68 SearchBarWidget::~SearchBarWidget() {
69
70 }
71
72
73 void SearchBarWidget::initializeUI() {
74     horizontalLayout = new QHBoxLayout();
75     verticalLayout = new QVBoxLayout();
76
77
78     searchPushButton = new QPushButton(tr("Search"));
79     searchPushButton->setMinimumWidth(200);
80
81
82     searchWordLineEdit = new QLineEdit();
83     searchWordLineEdit->setMinimumWidth(350);
84     //create layout for lineEdit to have clear button on it
85     QHBoxLayout* lineEditLayout = new QHBoxLayout;
86     searchWordLineEdit->setLayout(lineEditLayout);
87
88
89     clearSearchWordToolButton = new QToolButton();
90     clearSearchWordToolButton->setIcon(QIcon("sowa.svg"));
91     //tool buttons will have size 2 times smaller
92     clearSearchWordToolButton->setMaximumSize(
93             clearSearchWordToolButton->sizeHint().width()/2,
94             clearSearchWordToolButton->sizeHint().height()/2);
95
96
97     historyNextToolButton = new QToolButton();
98     historyNextToolButton->setIcon(QIcon("sowa.svg"));
99     historyNextToolButton->setMaximumSize(
100             historyNextToolButton->sizeHint().width()/2,
101             historyNextToolButton->sizeHint().height()/2);
102
103
104     historyPrevToolButton = new QToolButton();
105     historyPrevToolButton->setIcon(QIcon("sowa.svg"));
106     historyPrevToolButton->setMaximumSize(
107             historyPrevToolButton->sizeHint().width()/2,
108             historyPrevToolButton->sizeHint().height()/2);
109
110
111     historyShowToolButton = new QToolButton();
112     historyShowToolButton->setIcon(QIcon("sowa.svg"));
113     historyShowToolButton->setMaximumSize(
114             historyShowToolButton->sizeHint().width()/2,
115             historyShowToolButton->sizeHint().height()/2);
116
117
118     searchingProgressBar = new QProgressBar();
119     searchingProgressBar->setMinimum(0);
120     searchingProgressBar->setMaximum(0);
121     searchingProgressBar->hide();
122     searchingProgressBar->setMaximumHeight(50);
123
124
125
126     setLayout(verticalLayout);
127
128     verticalLayout->addWidget(searchingProgressBar);
129
130     //adding widgets to layout
131     horizontalLayout->addWidget(searchWordLineEdit);
132     horizontalLayout->addWidget(searchPushButton);
133     horizontalLayout->addWidget(historyPrevToolButton);
134     horizontalLayout->addWidget(historyShowToolButton);
135     horizontalLayout->addWidget(historyNextToolButton);
136
137     //adding clear toolButton to textEdit with right alignment
138     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
139                               Qt::AlignRight | Qt::AlignVCenter);
140
141     verticalLayout->addLayout(horizontalLayout);
142 }
143
144
145 void SearchBarWidget::searchPushButtonClicked() {
146     if(_isSearching) {
147         emit stopSearching();
148         searchingProgressBar->hide();
149         searchPushButton->setText(tr("Search"));
150         setEnabled(true);
151         _isSearching = false;
152     }
153     else {
154         emit searchForTranslations(searchWordLineEdit->text());
155         searchingProgressBar->show();
156         searchPushButton->setText(tr("Stop"));
157         setEnabled(false);
158         _isSearching = true;
159     }
160 }
161
162 void SearchBarWidget::setEnabled(bool enabled) {
163     searchWordLineEdit->setEnabled(enabled);
164     historyNextToolButton->setEnabled(enabled);
165     historyPrevToolButton->setEnabled(enabled);
166     historyShowToolButton->setEnabled(enabled);
167 }
168
169 void SearchBarWidget::historyNextToolButtonClicked() {
170
171 }
172
173 void SearchBarWidget::historyPrevToolButtonClicked() {
174
175 }
176
177 void SearchBarWidget::historyShowToolButtonClicked() {
178
179 }
180
181 void SearchBarWidget::clearSearchWordToolButtonClicked() {
182     searchWordLineEdit->clear();
183 }
184
185 void SearchBarWidget::showHistoryListDialog() {
186
187 }
188
189 bool SearchBarWidget::isSearching() const {
190     return _isSearching;
191 }