]> git.sur5r.net Git - minitube/blob - src/searchlineedit.cpp
Imported Upstream version 1.7
[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     connect(completion, SIGNAL(suggestionAccepted(const QString &)), SIGNAL(suggestionAccepted(const QString &)));
185
186 }
187
188 void SearchLineEdit::paintEvent(QPaintEvent *event)
189 {
190     if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) {
191         ExLineEdit::paintEvent(event);
192         QStyleOptionFrameV2 panel;
193         initStyleOption(&panel);
194         QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
195         QFontMetrics fm = fontMetrics();
196         int horizontalMargin = lineEdit()->x();
197         QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2,
198                        r.width() - 2 * horizontalMargin, fm.height());
199         QPainter painter(this);
200         painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
201         painter.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, m_inactiveText);
202     } else {
203         ExLineEdit::paintEvent(event);
204     }
205 }
206
207 void SearchLineEdit::resizeEvent(QResizeEvent *event)
208 {
209     updateGeometries();
210     ExLineEdit::resizeEvent(event);
211 }
212
213 void SearchLineEdit::updateGeometries()
214 {
215     int menuHeight = height();
216     int menuWidth = menuHeight + 1;
217     if (!m_searchButton->m_menu)
218         menuWidth = (menuHeight / 5) * 4;
219     m_searchButton->resize(QSize(menuWidth, menuHeight));
220 }
221
222 QString SearchLineEdit::inactiveText() const
223 {
224     return m_inactiveText;
225 }
226
227 void SearchLineEdit::setInactiveText(const QString &text)
228 {
229     m_inactiveText = text;
230 }
231
232 void SearchLineEdit::setMenu(QMenu *menu)
233 {
234     if (m_searchButton->m_menu)
235         m_searchButton->m_menu->deleteLater();
236     m_searchButton->m_menu = menu;
237     updateGeometries();
238 }
239
240 QMenu *SearchLineEdit::menu() const
241 {
242     if (!m_searchButton->m_menu) {
243         m_searchButton->m_menu = new QMenu(m_searchButton);
244         if (isVisible())
245             (const_cast<SearchLineEdit*>(this))->updateGeometries();
246     }
247     return m_searchButton->m_menu;
248 }
249
250 void SearchLineEdit::returnPressed()
251 {
252     if (!lineEdit()->text().isEmpty()) {
253         completion->preventSuggest();
254         emit search(lineEdit()->text());
255     }
256 }
257
258 void SearchLineEdit::enableSuggest() {
259     completion->enableSuggest();
260 }
261
262 void SearchLineEdit::preventSuggest() {
263     completion->preventSuggest();
264 }
265
266 void SearchLineEdit::focusInEvent(QFocusEvent *event) {
267     ExLineEdit::focusInEvent(event);
268     enableSuggest();
269 }