45
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
5 **
|
|
6 ** This file is part of the demonstration applications of the Qt Toolkit.
|
|
7 **
|
|
8 ** $QT_BEGIN_LICENSE:LGPL$
|
|
9 ** Commercial Usage
|
|
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.
|
|
14 **
|
|
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.
|
|
22 **
|
|
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
|
|
26 ** package.
|
|
27 **
|
|
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.
|
|
35 **
|
|
36 ** If you are unsure which license is appropriate for your use, please
|
|
37 ** contact the sales department at qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41
|
|
42 import urllineedit;
|
|
43
|
|
44 import QtGui.QLineEdit;
|
|
45 import QtGui.QAbstractButton;
|
|
46
|
|
47 import searchlineedit;
|
|
48
|
|
49 import QtGui.QPainter;
|
|
50 import QtGui.QMouseEvent;
|
|
51 import QtGui.QMenu;
|
|
52 import QtGui.QStyle;
|
|
53 import QtGui.QStyleOptionFrameV2;
|
|
54
|
|
55 /*
|
|
56 QT_BEGIN_NAMESPACE
|
|
57 class QMenu;
|
|
58 QT_END_NAMESPACE
|
|
59
|
|
60 class SearchButton;
|
|
61 */
|
|
62
|
|
63 /*
|
|
64 Clear button on the right hand side of the search widget.
|
|
65 Hidden by default
|
|
66 "A circle with an X in it"
|
|
67 */
|
|
68 class ClearButton : public QAbstractButton
|
|
69 {
|
|
70 Q_OBJECT
|
|
71
|
|
72 public:
|
|
73 this(QWidget *parent = null)
|
|
74 {
|
|
75 super(parent);
|
|
76 setCursor(Qt.ArrowCursor);
|
|
77 setToolTip(tr("Clear"));
|
|
78 setVisible(false);
|
|
79 setFocusPolicy(Qt.NoFocus);
|
|
80 }
|
|
81 void paintEvent(QPaintEvent *event)
|
|
82 {
|
|
83 Q_UNUSED(event);
|
|
84 QPainter painter(this);
|
|
85 int height = this.height();
|
|
86
|
|
87 painter.setRenderHint(QPainter::Antialiasing, true);
|
|
88 QColor color = palette().color(QPalette.Mid);
|
|
89 painter.setBrush(isDown()
|
|
90 ? palette().color(QPalette.Dark)
|
|
91 : palette().color(QPalette.Mid));
|
|
92 painter.setPen(painter.brush().color());
|
|
93 int size = width();
|
|
94 int offset = size / 5;
|
|
95 int radius = size - offset * 2;
|
|
96 painter.drawEllipse(offset, offset, radius, radius);
|
|
97
|
|
98 painter.setPen(palette().color(QPalette.Base));
|
|
99 int border = offset * 2;
|
|
100 painter.drawLine(border, border, width() - border, height - border);
|
|
101 painter.drawLine(border, height - border, width() - border, border);
|
|
102 }
|
|
103
|
|
104 public slots:
|
|
105 void textChanged(QString &text)
|
|
106 {
|
|
107 setVisible(!text.isEmpty());
|
|
108 }
|
|
109
|
|
110 }
|
|
111
|
|
112 /*
|
|
113 Search icon on the left hand side of the search widget
|
|
114 When a menu is set a down arrow appears
|
|
115 */
|
|
116 class SearchButton : public QAbstractButton {
|
|
117 public:
|
|
118 SearchButton(QWidget *parent = null)
|
|
119 {
|
|
120 super(parent);
|
|
121 m_menu = 0;
|
|
122 setObjectName(QLatin1String("SearchButton"));
|
|
123 setCursor(Qt.ArrowCursor);
|
|
124 setFocusPolicy(Qt.NoFocus);
|
|
125 }
|
|
126
|
|
127 void paintEvent(QPaintEvent *event)
|
|
128 {
|
|
129 Q_UNUSED(event);
|
|
130 QPainterPath myPath;
|
|
131
|
|
132 int radius = (height() / 5) * 2;
|
|
133 QRect circle(height() / 3 - 1, height() / 4, radius, radius);
|
|
134 myPath.addEllipse(circle);
|
|
135
|
|
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);
|
|
140
|
|
141 QPainter painter(this);
|
|
142 painter.setRenderHint(QPainter::Antialiasing, true);
|
|
143 painter.setPen(QPen(Qt.darkGray, 2));
|
|
144 painter.drawPath(myPath);
|
|
145
|
|
146 if (m_menu) {
|
|
147 QPainterPath dropPath;
|
|
148 dropPath.arcMoveTo(circle, 320);
|
|
149 QPointF c = dropPath.currentPosition();
|
|
150 c = QPointF(c.x() + 3.5, c.y() + 0.5);
|
|
151 dropPath.moveTo(c);
|
|
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);
|
|
159 }
|
|
160 painter.end();
|
|
161 }
|
|
162 QMenu *m_menu;
|
|
163
|
|
164 protected:
|
|
165 void mousePressEvent(QMouseEvent *event)
|
|
166 {
|
|
167 if (m_menu && event.button() == Qt.LeftButton) {
|
|
168 QWidget *p = parentWidget();
|
|
169 if (p) {
|
|
170 QPoint r = p.mapToGlobal(QPoint(0, p.height()));
|
|
171 m_menu.exec(QPoint(r.x() + height() / 2, r.y()));
|
|
172 }
|
|
173 event.accept();
|
|
174 }
|
|
175 QAbstractButton::mousePressEvent(event);
|
|
176 }
|
|
177 }
|
|
178
|
|
179 class SearchLineEdit : public ExLineEdit
|
|
180 {
|
|
181 Q_OBJECT
|
|
182 Q_PROPERTY(QString inactiveText READ inactiveText WRITE setInactiveText)
|
|
183
|
|
184 signals:
|
|
185 void textChanged(QString &text);
|
|
186
|
|
187 public:
|
|
188 /*
|
|
189 SearchLineEdit is an enhanced QLineEdit
|
|
190 - A Search icon on the left with optional menu
|
|
191 - When there is no text and doesn't have focus an "inactive text" is displayed
|
|
192 - When there is text a clear button is displayed on the right hand side
|
|
193 */
|
|
194 this(QWidget *parent = null) : ExLineEdit(parent),
|
|
195 m_searchButton(new SearchButton(this))
|
|
196 {
|
|
197 connect(lineEdit(), SIGNAL(textChanged(QString &)),
|
|
198 this, SIGNAL(textChanged(QString &)));
|
|
199 setLeftWidget(m_searchButton);
|
|
200 m_inactiveText = tr("Search");
|
|
201
|
|
202 QSizePolicy policy = sizePolicy();
|
|
203 setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
|
|
204 }
|
|
205
|
|
206 QString inactiveText()
|
|
207 {
|
|
208 return m_inactiveText;
|
|
209 }
|
|
210
|
|
211 void setInactiveText(QString &text)
|
|
212 {
|
|
213 m_inactiveText = text;
|
|
214 }
|
|
215
|
|
216 QMenu *menu()
|
|
217 {
|
|
218 if (!m_searchButton.m_menu) {
|
|
219 m_searchButton.m_menu = new QMenu(m_searchButton);
|
|
220 if (isVisible())
|
|
221 (const_cast<SearchLineEdit*>(this)).updateGeometries();
|
|
222 }
|
|
223 return m_searchButton.m_menu;
|
|
224 }
|
|
225
|
|
226 void setMenu(QMenu *menu)
|
|
227 {
|
|
228 if (m_searchButton.m_menu)
|
|
229 m_searchButton.m_menu.deleteLater();
|
|
230 m_searchButton.m_menu = menu;
|
|
231 updateGeometries();
|
|
232 }
|
|
233
|
|
234 protected:
|
|
235 void resizeEvent(QResizeEvent *event)
|
|
236 {
|
|
237 updateGeometries();
|
|
238 ExLineEdit.resizeEvent(event);
|
|
239 }
|
|
240
|
|
241 void paintEvent(QPaintEvent *event)
|
|
242 {
|
|
243 if (lineEdit().text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) {
|
|
244 ExLineEdit.paintEvent(event);
|
|
245 QStyleOptionFrameV2 panel;
|
|
246 initStyleOption(&panel);
|
|
247 QRect r = style().subElementRect(QStyle::SE_LineEditContents, &panel, this);
|
|
248 QFontMetrics fm = fontMetrics();
|
|
249 int horizontalMargin = lineEdit().x();
|
|
250 QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2,
|
|
251 r.width() - 2 * horizontalMargin, fm.height());
|
|
252 QPainter painter(this);
|
|
253 painter.setPen(palette().brush(QPalette.Disabled, QPalette.Text).color());
|
|
254 painter.drawText(lineRect, Qt.AlignLeft|Qt.AlignVCenter, m_inactiveText);
|
|
255 } else {
|
|
256 ExLineEdit.paintEvent(event);
|
|
257 }
|
|
258 }
|
|
259
|
|
260 private:
|
|
261 void updateGeometries()
|
|
262 {
|
|
263 int menuHeight = height();
|
|
264 int menuWidth = menuHeight + 1;
|
|
265 if (!m_searchButton.m_menu)
|
|
266 menuWidth = (menuHeight / 5) * 4;
|
|
267 m_searchButton.resize(QSize(menuWidth, menuHeight));
|
|
268 }
|
|
269
|
|
270 SearchButton *m_searchButton;
|
|
271 QString m_inactiveText;
|
|
272 }
|