]> git.sur5r.net Git - minitube/blob - src/searchlineedit.cpp
Imported Upstream version 1.5
[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     setShortcut(QKeySequence(Qt::Key_Escape));
60 }
61
62 void ClearButton::paintEvent(QPaintEvent *event)
63 {
64     Q_UNUSED(event);
65     QPainter painter(this);
66     int height = this->height();
67
68     painter.setRenderHint(QPainter::Antialiasing, true);
69     // QColor color = palette().color(QPalette::Mid);
70     painter.setBrush(isDown()
71                      ? palette().color(QPalette::Dark)
72                          : palette().color(QPalette::Mid));
73     painter.setPen(painter.brush().color());
74     int size = width();
75     int offset = size / 3.5;
76     int radius = size - offset * 2;
77     painter.drawEllipse(offset, offset, radius, radius);
78
79     painter.setPen(QPen(palette().color(QPalette::Base),2));
80     int border = offset * 1.6;
81     painter.drawLine(border, border, width() - border, height - border);
82     painter.drawLine(border, height - border, width() - border, border);
83 }
84
85 void ClearButton::textChanged(const QString &text)
86 {
87     setVisible(!text.isEmpty());
88 }
89
90 /*
91     Search icon on the left hand side of the search widget
92     When a menu is set a down arrow appears
93  */
94 class SearchButton : public QAbstractButton {
95 public:
96     SearchButton(QWidget *parent = 0);
97     void paintEvent(QPaintEvent *event);
98     QMenu *m_menu;
99
100 protected:
101     void mousePressEvent(QMouseEvent *event);
102 };
103
104 SearchButton::SearchButton(QWidget *parent)
105     : QAbstractButton(parent),
106     m_menu(0)
107 {
108     setObjectName(QLatin1String("SearchButton"));
109     setCursor(Qt::ArrowCursor);
110     setFocusPolicy(Qt::NoFocus);
111 }
112
113 void SearchButton::mousePressEvent(QMouseEvent *event)
114 {
115     if (m_menu && event->button() == Qt::LeftButton) {
116         QWidget *p = parentWidget();
117         if (p) {
118             QPoint r = p->mapToGlobal(QPoint(0, p->height()));
119             m_menu->exec(QPoint(r.x() + height() / 2, r.y()));
120         }
121         event->accept();
122     }
123     QAbstractButton::mousePressEvent(event);
124 }
125
126 void SearchButton::paintEvent(QPaintEvent *event)
127 {
128     Q_UNUSED(event);
129     QPainterPath myPath;
130
131     int radius = (height() / 5) * 2;
132     QRect circle(height() / 5.5, height() / 3.5, radius, radius);
133     myPath.addEllipse(circle);
134
135     myPath.arcMoveTo(circle, 315);
136     QPointF c = myPath.currentPosition();
137     int diff = height() / 6;
138     myPath.lineTo(qMin(width() - 2, (int)c.x() + diff), c.y() + diff);
139
140     QPainter painter(this);
141     painter.setRenderHint(QPainter::Antialiasing, true);
142     painter.setPen(QPen(palette().color(QPalette::Mid), height() / 10));
143     painter.drawPath(myPath);
144
145     if (m_menu) {
146         QPainterPath dropPath;
147         dropPath.arcMoveTo(circle, 320);
148         QPointF c = dropPath.currentPosition();
149         c = QPointF(c.x() + 3.5, c.y() + 0.5);
150         dropPath.moveTo(c);
151         dropPath.lineTo(c.x() + 4, c.y());
152         dropPath.lineTo(c.x() + 2, c.y() + 2);
153         dropPath.closeSubpath();
154         painter.setPen(Qt::darkGray);
155         painter.setBrush(Qt::darkGray);
156         painter.setRenderHint(QPainter::Antialiasing, false);
157         painter.drawPath(dropPath);
158     }
159     painter.end();
160 }
161
162 /*
163     SearchLineEdit is an enhanced QLineEdit
164     - A Search icon on the left with optional menu
165     - When there is no text and doesn't have focus an "inactive text" is displayed
166     - When there is text a clear button is displayed on the right hand side
167  */
168 SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent),
169 m_searchButton(new SearchButton(this))
170 {
171     connect(lineEdit(), SIGNAL(textChanged(const QString &)),
172             this, SIGNAL(textChanged(const QString &)));
173
174     connect(lineEdit(), SIGNAL(returnPressed()),
175             this, SLOT(returnPressed()));
176
177     setLeftWidget(m_searchButton);
178     m_inactiveText = tr("Search");
179
180     QSizePolicy policy = sizePolicy();
181     setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
182
183     // completion
184     completion = new AutoComplete(this, m_lineEdit);
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 }