]> git.sur5r.net Git - minitube/blob - src/searchlineedit.cpp
Initial import
[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 ClearButton::ClearButton(QWidget *parent)
47   : QAbstractButton(parent)
48 {
49     setCursor(Qt::ArrowCursor);
50     setToolTip(tr("Clear"));
51     setVisible(false);
52     setFocusPolicy(Qt::NoFocus);
53 }
54
55 void ClearButton::paintEvent(QPaintEvent *event)
56 {
57     Q_UNUSED(event);
58     QPainter painter(this);
59     int height = this->height();
60
61     painter.setRenderHint(QPainter::Antialiasing, true);
62     QColor color = palette().color(QPalette::Mid);
63     painter.setBrush(isDown()
64                      ? palette().color(QPalette::Dark)
65                      : palette().color(QPalette::Mid));
66     painter.setPen(painter.brush().color());
67     int size = width();
68     int offset = size / 3.5;
69     int radius = size - offset * 2;
70     painter.drawEllipse(offset, offset, radius, radius);
71
72     painter.setPen(QPen(palette().color(QPalette::Base),2));
73     int border = offset * 1.6;
74     painter.drawLine(border, border, width() - border, height - border);
75     painter.drawLine(border, height - border, width() - border, border);
76 }
77
78 void ClearButton::textChanged(const QString &text)
79 {
80     setVisible(!text.isEmpty());
81 }
82
83 /*
84     Search icon on the left hand side of the search widget
85     When a menu is set a down arrow appears
86  */
87 class SearchButton : public QAbstractButton {
88 public:
89     SearchButton(QWidget *parent = 0);
90     void paintEvent(QPaintEvent *event);
91     QMenu *m_menu;
92
93 protected:
94     void mousePressEvent(QMouseEvent *event);
95 };
96
97 SearchButton::SearchButton(QWidget *parent)
98   : QAbstractButton(parent),
99     m_menu(0)
100 {
101     setObjectName(QLatin1String("SearchButton"));
102     setCursor(Qt::ArrowCursor);
103     setFocusPolicy(Qt::NoFocus);
104 }
105
106 void SearchButton::mousePressEvent(QMouseEvent *event)
107 {
108     if (m_menu && event->button() == Qt::LeftButton) {
109         QWidget *p = parentWidget();
110         if (p) {
111             QPoint r = p->mapToGlobal(QPoint(0, p->height()));
112             m_menu->exec(QPoint(r.x() + height() / 2, r.y()));
113         }
114         event->accept();
115     }
116     QAbstractButton::mousePressEvent(event);
117 }
118
119 void SearchButton::paintEvent(QPaintEvent *event)
120 {
121     Q_UNUSED(event);
122     QPainterPath myPath;
123
124     int radius = (height() / 5) * 2;
125     QRect circle(height() / 5.5, height() / 3.5, radius, radius);
126     myPath.addEllipse(circle);
127
128     myPath.arcMoveTo(circle, 300);
129     QPointF c = myPath.currentPosition();
130     int diff = height() / 6;
131     myPath.lineTo(qMin(width() - 2, (int)c.x() + diff), c.y() + diff);
132
133     QPainter painter(this);
134     painter.setRenderHint(QPainter::Antialiasing, true);
135     painter.setPen(QPen(Qt::darkGray, 2));
136     painter.drawPath(myPath);
137
138     if (m_menu) {
139         QPainterPath dropPath;
140         dropPath.arcMoveTo(circle, 320);
141         QPointF c = dropPath.currentPosition();
142         c = QPointF(c.x() + 3.5, c.y() + 0.5);
143         dropPath.moveTo(c);
144         dropPath.lineTo(c.x() + 4, c.y());
145         dropPath.lineTo(c.x() + 2, c.y() + 2);
146         dropPath.closeSubpath();
147         painter.setPen(Qt::darkGray);
148         painter.setBrush(Qt::darkGray);
149         painter.setRenderHint(QPainter::Antialiasing, false);
150         painter.drawPath(dropPath);
151     }
152     painter.end();
153 }
154
155 /*
156     SearchLineEdit is an enhanced QLineEdit
157     - A Search icon on the left with optional menu
158     - When there is no text and doesn't have focus an "inactive text" is displayed
159     - When there is text a clear button is displayed on the right hand side
160  */
161 SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent),
162     m_searchButton(new SearchButton(this))
163 {
164     connect(lineEdit(), SIGNAL(textChanged(const QString &)),
165             this, SIGNAL(textChanged(const QString &)));
166
167     connect(lineEdit(), SIGNAL(returnPressed()),
168             this, SLOT(returnPressed()));
169
170     setLeftWidget(m_searchButton);
171     m_inactiveText = tr("Search");
172
173     QSizePolicy policy = sizePolicy();
174     setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
175 }
176
177 void SearchLineEdit::paintEvent(QPaintEvent *event)
178 {
179     if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) {
180         ExLineEdit::paintEvent(event);
181         QStyleOptionFrameV2 panel;
182         initStyleOption(&panel);
183         QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
184         QFontMetrics fm = fontMetrics();
185         int horizontalMargin = lineEdit()->x();
186         QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2,
187                        r.width() - 2 * horizontalMargin, fm.height());
188         QPainter painter(this);
189         painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
190         painter.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, m_inactiveText);
191     } else {
192         ExLineEdit::paintEvent(event);
193     }
194 }
195
196 void SearchLineEdit::resizeEvent(QResizeEvent *event)
197 {
198     updateGeometries();
199     ExLineEdit::resizeEvent(event);
200 }
201
202 void SearchLineEdit::updateGeometries()
203 {
204     int menuHeight = height();
205     int menuWidth = menuHeight + 1;
206     if (!m_searchButton->m_menu)
207         menuWidth = (menuHeight / 5) * 4;
208     m_searchButton->resize(QSize(menuWidth, menuHeight));
209 }
210
211 QString SearchLineEdit::inactiveText() const
212 {
213     return m_inactiveText;
214 }
215
216 void SearchLineEdit::setInactiveText(const QString &text)
217 {
218     m_inactiveText = text;
219 }
220
221 void SearchLineEdit::setMenu(QMenu *menu)
222 {
223     if (m_searchButton->m_menu)
224         m_searchButton->m_menu->deleteLater();
225     m_searchButton->m_menu = menu;
226     updateGeometries();
227 }
228
229 QMenu *SearchLineEdit::menu() const
230 {
231     if (!m_searchButton->m_menu) {
232         m_searchButton->m_menu = new QMenu(m_searchButton);
233         if (isVisible())
234             (const_cast<SearchLineEdit*>(this))->updateGeometries();
235     }
236     return m_searchButton->m_menu;
237 }
238
239 void SearchLineEdit::returnPressed()
240 {
241     emit search(lineEdit()->text());
242 }