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