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