]> git.sur5r.net Git - minitube/blob - src/searchlineedit.cpp
Imported Upstream version 1.4.1
[minitube] / src / searchlineedit.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Nokia Corporation (qt-info@nokia.com)
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial Usage
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Nokia.
14 **
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file.  Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 **
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
27 **
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file.  Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 **
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at http://www.qtsoftware.com/contact.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "searchlineedit.h"
43
44 #include <QtGui/QPainter>
45 #include <QtGui/QMouseEvent>
46 #include <QtGui/QMenu>
47 #include <QtGui/QStyle>
48 #include <QtGui/QStyleOptionFrameV2>
49
50 #include "autocomplete.h"
51
52 ClearButton::ClearButton(QWidget *parent)
53     : QAbstractButton(parent)
54 {
55     setCursor(Qt::ArrowCursor);
56     setToolTip(tr("Clear"));
57     setVisible(false);
58     setFocusPolicy(Qt::NoFocus);
59 }
60
61 void ClearButton::paintEvent(QPaintEvent *event)
62 {
63     Q_UNUSED(event);
64     QPainter painter(this);
65     int height = this->height();
66
67     painter.setRenderHint(QPainter::Antialiasing, true);
68     QColor color = palette().color(QPalette::Mid);
69     painter.setBrush(isDown()
70                      ? palette().color(QPalette::Dark)
71                          : palette().color(QPalette::Mid));
72     painter.setPen(painter.brush().color());
73     int size = width();
74     int offset = size / 3.5;
75     int radius = size - offset * 2;
76     painter.drawEllipse(offset, offset, radius, radius);
77
78     painter.setPen(QPen(palette().color(QPalette::Base),2));
79     int border = offset * 1.6;
80     painter.drawLine(border, border, width() - border, height - border);
81     painter.drawLine(border, height - border, width() - border, border);
82 }
83
84 void ClearButton::textChanged(const QString &text)
85 {
86     setVisible(!text.isEmpty());
87 }
88
89 /*
90     Search icon on the left hand side of the search widget
91     When a menu is set a down arrow appears
92  */
93 class SearchButton : public QAbstractButton {
94 public:
95     SearchButton(QWidget *parent = 0);
96     void paintEvent(QPaintEvent *event);
97     QMenu *m_menu;
98
99 protected:
100     void mousePressEvent(QMouseEvent *event);
101 };
102
103 SearchButton::SearchButton(QWidget *parent)
104     : QAbstractButton(parent),
105     m_menu(0)
106 {
107     setObjectName(QLatin1String("SearchButton"));
108     setCursor(Qt::ArrowCursor);
109     setFocusPolicy(Qt::NoFocus);
110 }
111
112 void SearchButton::mousePressEvent(QMouseEvent *event)
113 {
114     if (m_menu && event->button() == Qt::LeftButton) {
115         QWidget *p = parentWidget();
116         if (p) {
117             QPoint r = p->mapToGlobal(QPoint(0, p->height()));
118             m_menu->exec(QPoint(r.x() + height() / 2, r.y()));
119         }
120         event->accept();
121     }
122     QAbstractButton::mousePressEvent(event);
123 }
124
125 void SearchButton::paintEvent(QPaintEvent *event)
126 {
127     Q_UNUSED(event);
128     QPainterPath myPath;
129
130     int radius = (height() / 5) * 2;
131     QRect circle(height() / 5.5, height() / 3.5, radius, radius);
132     myPath.addEllipse(circle);
133
134     myPath.arcMoveTo(circle, 315);
135     QPointF c = myPath.currentPosition();
136     int diff = height() / 6;
137     myPath.lineTo(qMin(width() - 2, (int)c.x() + diff), c.y() + diff);
138
139     QPainter painter(this);
140     painter.setRenderHint(QPainter::Antialiasing, true);
141     painter.setPen(QPen(palette().color(QPalette::Mid), height() / 10));
142     painter.drawPath(myPath);
143
144     if (m_menu) {
145         QPainterPath dropPath;
146         dropPath.arcMoveTo(circle, 320);
147         QPointF c = dropPath.currentPosition();
148         c = QPointF(c.x() + 3.5, c.y() + 0.5);
149         dropPath.moveTo(c);
150         dropPath.lineTo(c.x() + 4, c.y());
151         dropPath.lineTo(c.x() + 2, c.y() + 2);
152         dropPath.closeSubpath();
153         painter.setPen(Qt::darkGray);
154         painter.setBrush(Qt::darkGray);
155         painter.setRenderHint(QPainter::Antialiasing, false);
156         painter.drawPath(dropPath);
157     }
158     painter.end();
159 }
160
161 /*
162     SearchLineEdit is an enhanced QLineEdit
163     - A Search icon on the left with optional menu
164     - When there is no text and doesn't have focus an "inactive text" is displayed
165     - When there is text a clear button is displayed on the right hand side
166  */
167 SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent),
168 m_searchButton(new SearchButton(this))
169 {
170     connect(lineEdit(), SIGNAL(textChanged(const QString &)),
171             this, SIGNAL(textChanged(const QString &)));
172
173     connect(lineEdit(), SIGNAL(returnPressed()),
174             this, SLOT(returnPressed()));
175
176     setLeftWidget(m_searchButton);
177     m_inactiveText = tr("Search");
178
179     QSizePolicy policy = sizePolicy();
180     setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
181
182     // completion
183     completion = new AutoComplete(this, m_lineEdit);
184
185 }
186
187 void SearchLineEdit::paintEvent(QPaintEvent *event)
188 {
189     if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) {
190         ExLineEdit::paintEvent(event);
191         QStyleOptionFrameV2 panel;
192         initStyleOption(&panel);
193         QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
194         QFontMetrics fm = fontMetrics();
195         int horizontalMargin = lineEdit()->x();
196         QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2,
197                        r.width() - 2 * horizontalMargin, fm.height());
198         QPainter painter(this);
199         painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
200         painter.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, m_inactiveText);
201     } else {
202         ExLineEdit::paintEvent(event);
203     }
204 }
205
206 void SearchLineEdit::resizeEvent(QResizeEvent *event)
207 {
208     updateGeometries();
209     ExLineEdit::resizeEvent(event);
210 }
211
212 void SearchLineEdit::updateGeometries()
213 {
214     int menuHeight = height();
215     int menuWidth = menuHeight + 1;
216     if (!m_searchButton->m_menu)
217         menuWidth = (menuHeight / 5) * 4;
218     m_searchButton->resize(QSize(menuWidth, menuHeight));
219 }
220
221 QString SearchLineEdit::inactiveText() const
222 {
223     return m_inactiveText;
224 }
225
226 void SearchLineEdit::setInactiveText(const QString &text)
227 {
228     m_inactiveText = text;
229 }
230
231 void SearchLineEdit::setMenu(QMenu *menu)
232 {
233     if (m_searchButton->m_menu)
234         m_searchButton->m_menu->deleteLater();
235     m_searchButton->m_menu = menu;
236     updateGeometries();
237 }
238
239 QMenu *SearchLineEdit::menu() const
240 {
241     if (!m_searchButton->m_menu) {
242         m_searchButton->m_menu = new QMenu(m_searchButton);
243         if (isVisible())
244             (const_cast<SearchLineEdit*>(this))->updateGeometries();
245     }
246     return m_searchButton->m_menu;
247 }
248
249 void SearchLineEdit::returnPressed()
250 {
251     if (!lineEdit()->text().isEmpty()) {
252         completion->preventSuggest();
253         emit search(lineEdit()->text());
254     }
255 }
256
257 void SearchLineEdit::enableSuggest() {
258     completion->enableSuggest();
259 }
260
261 void SearchLineEdit::preventSuggest() {
262     completion->preventSuggest();
263 }
264
265 void SearchLineEdit::focusInEvent(QFocusEvent *event) {
266     ExLineEdit::focusInEvent(event);
267     enableSuggest();
268 }