1 /****************************************************************************
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
6 ** This file is part of the demonstration applications of the Qt Toolkit.
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.
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.
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.
33 ** If you are unsure which license is appropriate for your use, please
34 ** contact the sales department at qt-sales@nokia.com.
36 ****************************************************************************/
38 #include "searchlineedit.h"
40 #include <QtGui/QPainter>
41 #include <QtGui/QMouseEvent>
42 #include <QtGui/QMenu>
43 #include <QtGui/QStyle>
44 #include <QtGui/QStyleOptionFrameV2>
46 ClearButton::ClearButton(QWidget *parent)
47 : QAbstractButton(parent)
49 setCursor(Qt::ArrowCursor);
50 setToolTip(tr("Clear"));
52 setFocusPolicy(Qt::NoFocus);
55 void ClearButton::paintEvent(QPaintEvent *event)
58 QPainter painter(this);
59 int height = this->height();
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());
68 int offset = size / 3.5;
69 int radius = size - offset * 2;
70 painter.drawEllipse(offset, offset, radius, radius);
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);
78 void ClearButton::textChanged(const QString &text)
80 setVisible(!text.isEmpty());
84 Search icon on the left hand side of the search widget
85 When a menu is set a down arrow appears
87 class SearchButton : public QAbstractButton {
89 SearchButton(QWidget *parent = 0);
90 void paintEvent(QPaintEvent *event);
94 void mousePressEvent(QMouseEvent *event);
97 SearchButton::SearchButton(QWidget *parent)
98 : QAbstractButton(parent),
101 setObjectName(QLatin1String("SearchButton"));
102 setCursor(Qt::ArrowCursor);
103 setFocusPolicy(Qt::NoFocus);
106 void SearchButton::mousePressEvent(QMouseEvent *event)
108 if (m_menu && event->button() == Qt::LeftButton) {
109 QWidget *p = parentWidget();
111 QPoint r = p->mapToGlobal(QPoint(0, p->height()));
112 m_menu->exec(QPoint(r.x() + height() / 2, r.y()));
116 QAbstractButton::mousePressEvent(event);
119 void SearchButton::paintEvent(QPaintEvent *event)
124 int radius = (height() / 5) * 2;
125 QRect circle(height() / 5.5, height() / 3.5, radius, radius);
126 myPath.addEllipse(circle);
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);
133 QPainter painter(this);
134 painter.setRenderHint(QPainter::Antialiasing, true);
135 painter.setPen(QPen(Qt::darkGray, 2));
136 painter.drawPath(myPath);
139 QPainterPath dropPath;
140 dropPath.arcMoveTo(circle, 320);
141 QPointF c = dropPath.currentPosition();
142 c = QPointF(c.x() + 3.5, c.y() + 0.5);
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);
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
161 SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent),
162 m_searchButton(new SearchButton(this))
164 connect(lineEdit(), SIGNAL(textChanged(const QString &)),
165 this, SIGNAL(textChanged(const QString &)));
167 connect(lineEdit(), SIGNAL(returnPressed()),
168 this, SLOT(returnPressed()));
170 setLeftWidget(m_searchButton);
171 m_inactiveText = tr("Search");
173 QSizePolicy policy = sizePolicy();
174 setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
177 void SearchLineEdit::paintEvent(QPaintEvent *event)
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);
192 ExLineEdit::paintEvent(event);
196 void SearchLineEdit::resizeEvent(QResizeEvent *event)
199 ExLineEdit::resizeEvent(event);
202 void SearchLineEdit::updateGeometries()
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));
211 QString SearchLineEdit::inactiveText() const
213 return m_inactiveText;
216 void SearchLineEdit::setInactiveText(const QString &text)
218 m_inactiveText = text;
221 void SearchLineEdit::setMenu(QMenu *menu)
223 if (m_searchButton->m_menu)
224 m_searchButton->m_menu->deleteLater();
225 m_searchButton->m_menu = menu;
229 QMenu *SearchLineEdit::menu() const
231 if (!m_searchButton->m_menu) {
232 m_searchButton->m_menu = new QMenu(m_searchButton);
234 (const_cast<SearchLineEdit*>(this))->updateGeometries();
236 return m_searchButton->m_menu;
239 void SearchLineEdit::returnPressed()
241 emit search(lineEdit()->text());