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 #include "googlesuggest.h"
48 ClearButton::ClearButton(QWidget *parent)
49 : QAbstractButton(parent)
51 setCursor(Qt::ArrowCursor);
52 setToolTip(tr("Clear"));
54 setFocusPolicy(Qt::NoFocus);
57 void ClearButton::paintEvent(QPaintEvent *event)
60 QPainter painter(this);
61 int height = this->height();
63 painter.setRenderHint(QPainter::Antialiasing, true);
64 QColor color = palette().color(QPalette::Mid);
65 painter.setBrush(isDown()
66 ? palette().color(QPalette::Dark)
67 : palette().color(QPalette::Mid));
68 painter.setPen(painter.brush().color());
70 int offset = size / 3.5;
71 int radius = size - offset * 2;
72 painter.drawEllipse(offset, offset, radius, radius);
74 painter.setPen(QPen(palette().color(QPalette::Base),2));
75 int border = offset * 1.6;
76 painter.drawLine(border, border, width() - border, height - border);
77 painter.drawLine(border, height - border, width() - border, border);
80 void ClearButton::textChanged(const QString &text)
82 setVisible(!text.isEmpty());
86 Search icon on the left hand side of the search widget
87 When a menu is set a down arrow appears
89 class SearchButton : public QAbstractButton {
91 SearchButton(QWidget *parent = 0);
92 void paintEvent(QPaintEvent *event);
96 void mousePressEvent(QMouseEvent *event);
99 SearchButton::SearchButton(QWidget *parent)
100 : QAbstractButton(parent),
103 setObjectName(QLatin1String("SearchButton"));
104 setCursor(Qt::ArrowCursor);
105 setFocusPolicy(Qt::NoFocus);
108 void SearchButton::mousePressEvent(QMouseEvent *event)
110 if (m_menu && event->button() == Qt::LeftButton) {
111 QWidget *p = parentWidget();
113 QPoint r = p->mapToGlobal(QPoint(0, p->height()));
114 m_menu->exec(QPoint(r.x() + height() / 2, r.y()));
118 QAbstractButton::mousePressEvent(event);
121 void SearchButton::paintEvent(QPaintEvent *event)
126 int radius = (height() / 5) * 2;
127 QRect circle(height() / 5.5, height() / 3.5, radius, radius);
128 myPath.addEllipse(circle);
130 myPath.arcMoveTo(circle, 300);
131 QPointF c = myPath.currentPosition();
132 int diff = height() / 6;
133 myPath.lineTo(qMin(width() - 2, (int)c.x() + diff), c.y() + diff);
135 QPainter painter(this);
136 painter.setRenderHint(QPainter::Antialiasing, true);
137 painter.setPen(QPen(Qt::darkGray, 2));
138 painter.drawPath(myPath);
141 QPainterPath dropPath;
142 dropPath.arcMoveTo(circle, 320);
143 QPointF c = dropPath.currentPosition();
144 c = QPointF(c.x() + 3.5, c.y() + 0.5);
146 dropPath.lineTo(c.x() + 4, c.y());
147 dropPath.lineTo(c.x() + 2, c.y() + 2);
148 dropPath.closeSubpath();
149 painter.setPen(Qt::darkGray);
150 painter.setBrush(Qt::darkGray);
151 painter.setRenderHint(QPainter::Antialiasing, false);
152 painter.drawPath(dropPath);
158 SearchLineEdit is an enhanced QLineEdit
159 - A Search icon on the left with optional menu
160 - When there is no text and doesn't have focus an "inactive text" is displayed
161 - When there is text a clear button is displayed on the right hand side
163 SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent),
164 m_searchButton(new SearchButton(this))
166 connect(lineEdit(), SIGNAL(textChanged(const QString &)),
167 this, SIGNAL(textChanged(const QString &)));
169 connect(lineEdit(), SIGNAL(returnPressed()),
170 this, SLOT(returnPressed()));
172 setLeftWidget(m_searchButton);
173 m_inactiveText = tr("Search");
175 QSizePolicy policy = sizePolicy();
176 setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
179 completion = new GSuggestCompletion(m_lineEdit);
182 void SearchLineEdit::paintEvent(QPaintEvent *event)
184 if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) {
185 ExLineEdit::paintEvent(event);
186 QStyleOptionFrameV2 panel;
187 initStyleOption(&panel);
188 QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
189 QFontMetrics fm = fontMetrics();
190 int horizontalMargin = lineEdit()->x();
191 QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2,
192 r.width() - 2 * horizontalMargin, fm.height());
193 QPainter painter(this);
194 painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
195 painter.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, m_inactiveText);
197 ExLineEdit::paintEvent(event);
201 void SearchLineEdit::resizeEvent(QResizeEvent *event)
204 ExLineEdit::resizeEvent(event);
207 void SearchLineEdit::updateGeometries()
209 int menuHeight = height();
210 int menuWidth = menuHeight + 1;
211 if (!m_searchButton->m_menu)
212 menuWidth = (menuHeight / 5) * 4;
213 m_searchButton->resize(QSize(menuWidth, menuHeight));
216 QString SearchLineEdit::inactiveText() const
218 return m_inactiveText;
221 void SearchLineEdit::setInactiveText(const QString &text)
223 m_inactiveText = text;
226 void SearchLineEdit::setMenu(QMenu *menu)
228 if (m_searchButton->m_menu)
229 m_searchButton->m_menu->deleteLater();
230 m_searchButton->m_menu = menu;
234 QMenu *SearchLineEdit::menu() const
236 if (!m_searchButton->m_menu) {
237 m_searchButton->m_menu = new QMenu(m_searchButton);
239 (const_cast<SearchLineEdit*>(this))->updateGeometries();
241 return m_searchButton->m_menu;
244 void SearchLineEdit::returnPressed()
246 completion->preventSuggest();
247 emit search(lineEdit()->text());