Added context menu to TranslationWidget
[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     //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("gtk-delete"));
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("gtk-go-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("gtk-go-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("gtk-go-up"));
114     historyShowToolButton->setMaximumSize(
115             historyShowToolButton->sizeHint().width()/2,
116             historyShowToolButton->sizeHint().height()/2);
117
118
119     searchingProgressBar = new QProgressBar();
120     //progress bar have minimum and maximum values set to 0, which will effect
121     //with "I'm alive" bar
122     searchingProgressBar->setMinimum(0);
123     searchingProgressBar->setMaximum(0);
124     searchingProgressBar->hide();
125     searchingProgressBar->setMaximumHeight(50);
126
127
128
129     setLayout(verticalLayout);
130
131     verticalLayout->addWidget(searchingProgressBar);
132
133     //adding widgets to layout
134     horizontalLayout->addWidget(searchWordLineEdit);
135     horizontalLayout->addWidget(searchPushButton);
136     horizontalLayout->addWidget(historyPrevToolButton);
137     horizontalLayout->addWidget(historyShowToolButton);
138     horizontalLayout->addWidget(historyNextToolButton);
139
140     //adding clear toolButton to textEdit with right alignment
141     lineEditLayout->addWidget(clearSearchWordToolButton, 0,
142                               Qt::AlignRight | Qt::AlignVCenter);
143
144     verticalLayout->addLayout(horizontalLayout);
145 }
146
147
148 void SearchBarWidget::searchPushButtonClicked() {
149     if(_isSearching) {
150         searchingProgressBar->hide();
151         searchPushButton->setText(tr("Search"));
152         setEnabled(true);
153         _isSearching = false;
154         emit stopSearching();
155     }
156     else {
157         searchingProgressBar->show();
158         searchPushButton->setText(tr("Stop"));
159         setEnabled(false);
160         _isSearching = true;
161         emit searchForTranslations(searchWordLineEdit->text());
162     }
163 }
164
165 void SearchBarWidget::setEnabled(bool enabled) {
166     searchWordLineEdit->setEnabled(enabled);
167     historyNextToolButton->setEnabled(enabled);
168     historyPrevToolButton->setEnabled(enabled);
169     historyShowToolButton->setEnabled(enabled);
170 }
171
172
173 void SearchBarWidget::searchFinished() {
174         searchingProgressBar->hide();
175         searchPushButton->setText(tr("Search"));
176         setEnabled(true);
177         _isSearching = false;
178 }
179
180 void SearchBarWidget::historyNextToolButtonClicked() {
181
182 }
183
184 void SearchBarWidget::historyPrevToolButtonClicked() {
185
186 }
187
188 void SearchBarWidget::historyShowToolButtonClicked() {
189
190 }
191
192 void SearchBarWidget::clearSearchWordToolButtonClicked() {
193     searchWordLineEdit->clear();
194 }
195
196 void SearchBarWidget::showHistoryListDialog() {
197
198 }
199
200 bool SearchBarWidget::isSearching() const {
201     return _isSearching;
202 }