xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/allwinner/browser/searchlineedit.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL21$
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.
16 **
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.
25 **
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.
29 **
30 ** $QT_END_LICENSE$
31 **
32 ****************************************************************************/
33 
34 #include "searchlineedit.h"
35 
36 #include <QtGui/QPainter>
37 #include <QtGui/QMouseEvent>
38 #include <QtWidgets/QMenu>
39 #include <QtWidgets/QStyle>
40 #include <QtWidgets/QStyleOptionFrameV2>
41 
ClearButton(QWidget * parent)42 ClearButton::ClearButton(QWidget *parent)
43   : QAbstractButton(parent)
44 {
45 #ifndef QT_NO_CURSOR
46     setCursor(Qt::ArrowCursor);
47 #endif // QT_NO_CURSOR
48     setToolTip(tr("Clear"));
49     setVisible(false);
50     setFocusPolicy(Qt::NoFocus);
51 }
52 
paintEvent(QPaintEvent * event)53 void ClearButton::paintEvent(QPaintEvent *event)
54 {
55     Q_UNUSED(event);
56     QPainter painter(this);
57     int height = this->height();
58 
59     painter.setRenderHint(QPainter::Antialiasing, true);
60     painter.setBrush(isDown()
61                      ? palette().color(QPalette::Dark)
62                      : palette().color(QPalette::Mid));
63     painter.setPen(painter.brush().color());
64     int size = width();
65     int offset = size / 5;
66     int radius = size - offset * 2;
67     painter.drawEllipse(offset, offset, radius, radius);
68 
69     painter.setPen(palette().color(QPalette::Base));
70     int border = offset * 2;
71     painter.drawLine(border, border, width() - border, height - border);
72     painter.drawLine(border, height - border, width() - border, border);
73 }
74 
textChanged(const QString & text)75 void ClearButton::textChanged(const QString &text)
76 {
77     setVisible(!text.isEmpty());
78 }
79 
80 /*
81     Search icon on the left hand side of the search widget
82     When a menu is set a down arrow appears
83  */
84 class SearchButton : public QAbstractButton {
85 public:
86     SearchButton(QWidget *parent = 0);
87     void paintEvent(QPaintEvent *event);
88     QMenu *m_menu;
89 
90 protected:
91     void mousePressEvent(QMouseEvent *event);
92 };
93 
SearchButton(QWidget * parent)94 SearchButton::SearchButton(QWidget *parent)
95   : QAbstractButton(parent),
96     m_menu(0)
97 {
98     setObjectName(QLatin1String("SearchButton"));
99 #ifndef QT_NO_CURSOR
100     setCursor(Qt::ArrowCursor);
101 #endif //QT_NO_CURSOR
102     setFocusPolicy(Qt::NoFocus);
103 }
104 
mousePressEvent(QMouseEvent * event)105 void SearchButton::mousePressEvent(QMouseEvent *event)
106 {
107     if (m_menu && event->button() == Qt::LeftButton) {
108         QWidget *p = parentWidget();
109         if (p) {
110             QPoint r = p->mapToGlobal(QPoint(0, p->height()));
111             m_menu->exec(QPoint(r.x() + height() / 2, r.y()));
112         }
113         event->accept();
114     }
115     QAbstractButton::mousePressEvent(event);
116 }
117 
paintEvent(QPaintEvent * event)118 void SearchButton::paintEvent(QPaintEvent *event)
119 {
120     Q_UNUSED(event);
121     QPainterPath myPath;
122 
123     int radius = (height() / 5) * 2;
124     QRect circle(height() / 3 - 1, height() / 4, radius, radius);
125     myPath.addEllipse(circle);
126 
127     myPath.arcMoveTo(circle, 300);
128     QPointF c = myPath.currentPosition();
129     int diff = height() / 7;
130     myPath.lineTo(qMin(width() - 2, (int)c.x() + diff), c.y() + diff);
131 
132     QPainter painter(this);
133     painter.setRenderHint(QPainter::Antialiasing, true);
134     painter.setPen(QPen(Qt::darkGray, 2));
135     painter.drawPath(myPath);
136 
137     if (m_menu) {
138         QPainterPath dropPath;
139         dropPath.arcMoveTo(circle, 320);
140         QPointF c = dropPath.currentPosition();
141         c = QPointF(c.x() + 3.5, c.y() + 0.5);
142         dropPath.moveTo(c);
143         dropPath.lineTo(c.x() + 4, c.y());
144         dropPath.lineTo(c.x() + 2, c.y() + 2);
145         dropPath.closeSubpath();
146         painter.setPen(Qt::darkGray);
147         painter.setBrush(Qt::darkGray);
148         painter.setRenderHint(QPainter::Antialiasing, false);
149         painter.drawPath(dropPath);
150     }
151     painter.end();
152 }
153 
154 /*
155     SearchLineEdit is an enhanced QLineEdit
156     - A Search icon on the left with optional menu
157     - When there is no text and doesn't have focus an "inactive text" is displayed
158     - When there is text a clear button is displayed on the right hand side
159  */
SearchLineEdit(QWidget * parent)160 SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent),
161     m_searchButton(new SearchButton(this))
162 {
163     connect(lineEdit(), SIGNAL(textChanged(QString)),
164             this, SIGNAL(textChanged(QString)));
165     setLeftWidget(m_searchButton);
166     m_inactiveText = tr("Search");
167 
168     QSizePolicy policy = sizePolicy();
169     setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
170 }
171 
paintEvent(QPaintEvent * event)172 void SearchLineEdit::paintEvent(QPaintEvent *event)
173 {
174     if (lineEdit()->text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) {
175         ExLineEdit::paintEvent(event);
176         QStyleOptionFrameV2 panel;
177         initStyleOption(&panel);
178         QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
179         QFontMetrics fm = fontMetrics();
180         int horizontalMargin = lineEdit()->x();
181         QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2,
182                        r.width() - 2 * horizontalMargin, fm.height());
183         QPainter painter(this);
184         painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
185         painter.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, m_inactiveText);
186     } else {
187         ExLineEdit::paintEvent(event);
188     }
189 }
190 
resizeEvent(QResizeEvent * event)191 void SearchLineEdit::resizeEvent(QResizeEvent *event)
192 {
193     updateGeometries();
194     ExLineEdit::resizeEvent(event);
195 }
196 
updateGeometries()197 void SearchLineEdit::updateGeometries()
198 {
199     int menuHeight = height();
200     int menuWidth = menuHeight + 1;
201     if (!m_searchButton->m_menu)
202         menuWidth = (menuHeight / 5) * 4;
203     m_searchButton->resize(QSize(menuWidth, menuHeight));
204 }
205 
inactiveText() const206 QString SearchLineEdit::inactiveText() const
207 {
208     return m_inactiveText;
209 }
210 
setInactiveText(const QString & text)211 void SearchLineEdit::setInactiveText(const QString &text)
212 {
213     m_inactiveText = text;
214 }
215 
setMenu(QMenu * menu)216 void SearchLineEdit::setMenu(QMenu *menu)
217 {
218     if (m_searchButton->m_menu)
219         m_searchButton->m_menu->deleteLater();
220     m_searchButton->m_menu = menu;
221     updateGeometries();
222 }
223 
menu() const224 QMenu *SearchLineEdit::menu() const
225 {
226     if (!m_searchButton->m_menu) {
227         m_searchButton->m_menu = new QMenu(m_searchButton);
228         if (isVisible())
229             (const_cast<SearchLineEdit*>(this))->updateGeometries();
230     }
231     return m_searchButton->m_menu;
232 }
233 
234