1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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.
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.
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
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.
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at http://www.qtsoftware.com/contact.
40 ****************************************************************************/
42 #include "searchlineedit.h"
44 #include <QtGui/QPainter>
45 #include <QtGui/QMouseEvent>
46 #include <QtGui/QMenu>
47 #include <QtGui/QStyle>
48 #include <QtGui/QStyleOptionFrameV2>
50 #include "googlesuggest.h"
52 ClearButton::ClearButton(QWidget *parent)
53 : QAbstractButton(parent)
55 setCursor(Qt::ArrowCursor);
56 setToolTip(tr("Clear"));
58 setFocusPolicy(Qt::NoFocus);
61 void ClearButton::paintEvent(QPaintEvent *event)
64 QPainter painter(this);
65 int height = this->height();
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());
74 int offset = size / 3.5;
75 int radius = size - offset * 2;
76 painter.drawEllipse(offset, offset, radius, radius);
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);
84 void ClearButton::textChanged(const QString &text)
86 setVisible(!text.isEmpty());
90 Search icon on the left hand side of the search widget
91 When a menu is set a down arrow appears
93 class SearchButton : public QAbstractButton {
95 SearchButton(QWidget *parent = 0);
96 void paintEvent(QPaintEvent *event);
100 void mousePressEvent(QMouseEvent *event);
103 SearchButton::SearchButton(QWidget *parent)
104 : QAbstractButton(parent),
107 setObjectName(QLatin1String("SearchButton"));
108 setCursor(Qt::ArrowCursor);
109 setFocusPolicy(Qt::NoFocus);
112 void SearchButton::mousePressEvent(QMouseEvent *event)
114 if (m_menu && event->button() == Qt::LeftButton) {
115 QWidget *p = parentWidget();
117 QPoint r = p->mapToGlobal(QPoint(0, p->height()));
118 m_menu->exec(QPoint(r.x() + height() / 2, r.y()));
122 QAbstractButton::mousePressEvent(event);
125 void SearchButton::paintEvent(QPaintEvent *event)
130 int radius = (height() / 5) * 2;
131 QRect circle(height() / 5.5, height() / 3.5, radius, radius);
132 myPath.addEllipse(circle);
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);
139 QPainter painter(this);
140 painter.setRenderHint(QPainter::Antialiasing, true);
141 painter.setPen(QPen(palette().color(QPalette::Mid), height() / 10));
142 painter.drawPath(myPath);
145 QPainterPath dropPath;
146 dropPath.arcMoveTo(circle, 320);
147 QPointF c = dropPath.currentPosition();
148 c = QPointF(c.x() + 3.5, c.y() + 0.5);
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);
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
167 SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent),
168 m_searchButton(new SearchButton(this))
170 connect(lineEdit(), SIGNAL(textChanged(const QString &)),
171 this, SIGNAL(textChanged(const QString &)));
173 connect(lineEdit(), SIGNAL(returnPressed()),
174 this, SLOT(returnPressed()));
176 setLeftWidget(m_searchButton);
177 m_inactiveText = tr("Search");
179 QSizePolicy policy = sizePolicy();
180 setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
183 completion = new GSuggestCompletion(this, m_lineEdit);
187 void SearchLineEdit::paintEvent(QPaintEvent *event)
189 if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) {
190 ExLineEdit::paintEvent(event);
191 QStyleOptionFrameV2 panel;
192 initStyleOption(&panel);
193 QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
194 QFontMetrics fm = fontMetrics();
195 int horizontalMargin = lineEdit()->x();
196 QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2,
197 r.width() - 2 * horizontalMargin, fm.height());
198 QPainter painter(this);
199 painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
200 painter.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, m_inactiveText);
202 ExLineEdit::paintEvent(event);
206 void SearchLineEdit::resizeEvent(QResizeEvent *event)
209 ExLineEdit::resizeEvent(event);
212 void SearchLineEdit::updateGeometries()
214 int menuHeight = height();
215 int menuWidth = menuHeight + 1;
216 if (!m_searchButton->m_menu)
217 menuWidth = (menuHeight / 5) * 4;
218 m_searchButton->resize(QSize(menuWidth, menuHeight));
221 QString SearchLineEdit::inactiveText() const
223 return m_inactiveText;
226 void SearchLineEdit::setInactiveText(const QString &text)
228 m_inactiveText = text;
231 void SearchLineEdit::setMenu(QMenu *menu)
233 if (m_searchButton->m_menu)
234 m_searchButton->m_menu->deleteLater();
235 m_searchButton->m_menu = menu;
239 QMenu *SearchLineEdit::menu() const
241 if (!m_searchButton->m_menu) {
242 m_searchButton->m_menu = new QMenu(m_searchButton);
244 (const_cast<SearchLineEdit*>(this))->updateGeometries();
246 return m_searchButton->m_menu;
249 void SearchLineEdit::returnPressed()
251 if (!lineEdit()->text().isEmpty()) {
252 completion->preventSuggest();
253 emit search(lineEdit()->text());
257 void SearchLineEdit::enableSuggest() {
258 completion->enableSuggest();
261 void SearchLineEdit::preventSuggest() {
262 completion->preventSuggest();
265 void SearchLineEdit::focusInEvent(QFocusEvent *event) {
266 ExLineEdit::focusInEvent(event);