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