1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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 ClearButton::ClearButton(QWidget *parent)
51 : QAbstractButton(parent)
54 setCursor(Qt::ArrowCursor);
55 #endif // QT_NO_CURSOR
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 / 5;
75 int radius = size - offset * 2;
76 painter.drawEllipse(offset, offset, radius, radius);
78 painter.setPen(palette().color(QPalette::Base));
79 int border = offset * 2;
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"));
109 setCursor(Qt::ArrowCursor);
110 #endif //QT_NO_CURSOR
111 setFocusPolicy(Qt::NoFocus);
114 void SearchButton::mousePressEvent(QMouseEvent *event)
116 if (m_menu && event->button() == Qt::LeftButton) {
117 QWidget *p = parentWidget();
119 QPoint r = p->mapToGlobal(QPoint(0, p->height()));
120 m_menu->exec(QPoint(r.x() + height() / 2, r.y()));
124 QAbstractButton::mousePressEvent(event);
127 void SearchButton::paintEvent(QPaintEvent *event)
132 int radius = (height() / 5) * 2;
133 QRect circle(height() / 3 - 1, height() / 4, radius, radius);
134 myPath.addEllipse(circle);
136 myPath.arcMoveTo(circle, 300);
137 QPointF c = myPath.currentPosition();
138 int diff = height() / 7;
139 myPath.lineTo(qMin(width() - 2, (int)c.x() + diff), c.y() + diff);
141 QPainter painter(this);
142 painter.setRenderHint(QPainter::Antialiasing, true);
143 painter.setPen(QPen(Qt::darkGray, 2));
144 painter.drawPath(myPath);
147 QPainterPath dropPath;
148 dropPath.arcMoveTo(circle, 320);
149 QPointF c = dropPath.currentPosition();
150 c = QPointF(c.x() + 3.5, c.y() + 0.5);
152 dropPath.lineTo(c.x() + 4, c.y());
153 dropPath.lineTo(c.x() + 2, c.y() + 2);
154 dropPath.closeSubpath();
155 painter.setPen(Qt::darkGray);
156 painter.setBrush(Qt::darkGray);
157 painter.setRenderHint(QPainter::Antialiasing, false);
158 painter.drawPath(dropPath);
164 SearchLineEdit is an enhanced QLineEdit
165 - A Search icon on the left with optional menu
166 - When there is no text and doesn't have focus an "inactive text" is displayed
167 - When there is text a clear button is displayed on the right hand side
169 SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent),
170 m_searchButton(new SearchButton(this))
172 connect(lineEdit(), SIGNAL(textChanged(QString)),
173 this, SIGNAL(textChanged(QString)));
174 setLeftWidget(m_searchButton);
175 m_inactiveText = tr("Search");
177 QSizePolicy policy = sizePolicy();
178 setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
181 void SearchLineEdit::paintEvent(QPaintEvent *event)
183 if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) {
184 ExLineEdit::paintEvent(event);
185 QStyleOptionFrameV2 panel;
186 initStyleOption(&panel);
187 QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
188 QFontMetrics fm = fontMetrics();
189 int horizontalMargin = lineEdit()->x();
190 QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2,
191 r.width() - 2 * horizontalMargin, fm.height());
192 QPainter painter(this);
193 painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
194 painter.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, m_inactiveText);
196 ExLineEdit::paintEvent(event);
200 void SearchLineEdit::resizeEvent(QResizeEvent *event)
203 ExLineEdit::resizeEvent(event);
206 void SearchLineEdit::updateGeometries()
208 int menuHeight = height();
209 int menuWidth = menuHeight + 1;
210 if (!m_searchButton->m_menu)
211 menuWidth = (menuHeight / 5) * 4;
212 m_searchButton->resize(QSize(menuWidth, menuHeight));
215 QString SearchLineEdit::inactiveText() const
217 return m_inactiveText;
220 void SearchLineEdit::setInactiveText(const QString &text)
222 m_inactiveText = text;
225 void SearchLineEdit::setMenu(QMenu *menu)
227 if (m_searchButton->m_menu)
228 m_searchButton->m_menu->deleteLater();
229 m_searchButton->m_menu = menu;
233 QMenu *SearchLineEdit::menu() const
235 if (!m_searchButton->m_menu) {
236 m_searchButton->m_menu = new QMenu(m_searchButton);
238 (const_cast<SearchLineEdit*>(this))->updateGeometries();
240 return m_searchButton->m_menu;