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 "autocomplete.h"
52 ClearButton::ClearButton(QWidget *parent)
53 : QAbstractButton(parent)
55 setCursor(Qt::ArrowCursor);
56 setToolTip(tr("Clear"));
58 setFocusPolicy(Qt::NoFocus);
59 setShortcut(QKeySequence(Qt::Key_Escape));
62 void ClearButton::paintEvent(QPaintEvent *event)
65 QPainter painter(this);
66 int height = this->height();
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());
75 int offset = size / 3.5;
76 int radius = size - offset * 2;
77 painter.drawEllipse(offset, offset, radius, radius);
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);
85 void ClearButton::textChanged(const QString &text)
87 setVisible(!text.isEmpty());
91 Search icon on the left hand side of the search widget
92 When a menu is set a down arrow appears
94 class SearchButton : public QAbstractButton {
96 SearchButton(QWidget *parent = 0);
97 void paintEvent(QPaintEvent *event);
101 void mousePressEvent(QMouseEvent *event);
104 SearchButton::SearchButton(QWidget *parent)
105 : QAbstractButton(parent),
108 setObjectName(QLatin1String("SearchButton"));
109 setCursor(Qt::ArrowCursor);
110 setFocusPolicy(Qt::NoFocus);
113 void SearchButton::mousePressEvent(QMouseEvent *event)
115 if (m_menu && event->button() == Qt::LeftButton) {
116 QWidget *p = parentWidget();
118 QPoint r = p->mapToGlobal(QPoint(0, p->height()));
119 m_menu->exec(QPoint(r.x() + height() / 2, r.y()));
123 QAbstractButton::mousePressEvent(event);
126 void SearchButton::paintEvent(QPaintEvent *event)
131 int radius = (height() / 5) * 2;
132 QRect circle(height() / 5.5, height() / 3.5, radius, radius);
133 myPath.addEllipse(circle);
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);
140 QPainter painter(this);
141 painter.setRenderHint(QPainter::Antialiasing, true);
142 painter.setPen(QPen(palette().color(QPalette::Mid), height() / 10));
143 painter.drawPath(myPath);
146 QPainterPath dropPath;
147 dropPath.arcMoveTo(circle, 320);
148 QPointF c = dropPath.currentPosition();
149 c = QPointF(c.x() + 3.5, c.y() + 0.5);
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);
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
168 SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent),
169 m_searchButton(new SearchButton(this))
171 connect(lineEdit(), SIGNAL(textChanged(const QString &)),
172 this, SIGNAL(textChanged(const QString &)));
174 connect(lineEdit(), SIGNAL(returnPressed()),
175 this, SLOT(returnPressed()));
177 setLeftWidget(m_searchButton);
178 m_inactiveText = tr("Search");
180 QSizePolicy policy = sizePolicy();
181 setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
184 completion = new AutoComplete(this, m_lineEdit);
188 void SearchLineEdit::paintEvent(QPaintEvent *event)
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);
203 ExLineEdit::paintEvent(event);
207 void SearchLineEdit::resizeEvent(QResizeEvent *event)
210 ExLineEdit::resizeEvent(event);
213 void SearchLineEdit::updateGeometries()
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));
222 QString SearchLineEdit::inactiveText() const
224 return m_inactiveText;
227 void SearchLineEdit::setInactiveText(const QString &text)
229 m_inactiveText = text;
232 void SearchLineEdit::setMenu(QMenu *menu)
234 if (m_searchButton->m_menu)
235 m_searchButton->m_menu->deleteLater();
236 m_searchButton->m_menu = menu;
240 QMenu *SearchLineEdit::menu() const
242 if (!m_searchButton->m_menu) {
243 m_searchButton->m_menu = new QMenu(m_searchButton);
245 (const_cast<SearchLineEdit*>(this))->updateGeometries();
247 return m_searchButton->m_menu;
250 void SearchLineEdit::returnPressed()
252 if (!lineEdit()->text().isEmpty()) {
253 completion->preventSuggest();
254 emit search(lineEdit()->text());
258 void SearchLineEdit::enableSuggest() {
259 completion->enableSuggest();
262 void SearchLineEdit::preventSuggest() {
263 completion->preventSuggest();
266 void SearchLineEdit::focusInEvent(QFocusEvent *event) {
267 ExLineEdit::focusInEvent(event);