comparison demos/browser/searchlineedit.d @ 72:b149ef2cb18b

more porting
author mandel
date Wed, 20 May 2009 22:44:31 +0000
parents 71b382c10ef6
children 7bfd46c330dc
comparison
equal deleted inserted replaced
71:9071511f7a5b 72:b149ef2cb18b
37 ** contact the sales department at qt-sales@nokia.com. 37 ** contact the sales department at qt-sales@nokia.com.
38 ** $QT_END_LICENSE$ 38 ** $QT_END_LICENSE$
39 ** 39 **
40 ****************************************************************************/ 40 ****************************************************************************/
41 41
42 module searchlineedit;
43
42 import urllineedit; 44 import urllineedit;
43 45
44 import QtGui.QLineEdit; 46 import qt.gui.QLineEdit;
45 import QtGui.QAbstractButton; 47 import qt.gui.QAbstractButton;
46 48 import qt.gui.QPainter;
47 import searchlineedit; 49 import qt.gui.QMouseEvent;
48 50 import qt.gui.QMenu;
49 import QtGui.QPainter; 51 import qt.gui.QStyle;
50 import QtGui.QMouseEvent; 52 import qt.gui.QStyleOptionFrameV2;
51 import QtGui.QMenu;
52 import QtGui.QStyle;
53 import QtGui.QStyleOptionFrameV2;
54 53
55 /* 54 /*
56 QT_BEGIN_NAMESPACE 55 QT_BEGIN_NAMESPACE
57 class QMenu; 56 class QMenu;
58 QT_END_NAMESPACE 57 QT_END_NAMESPACE
65 Hidden by default 64 Hidden by default
66 "A circle with an X in it" 65 "A circle with an X in it"
67 */ 66 */
68 class ClearButton : public QAbstractButton 67 class ClearButton : public QAbstractButton
69 { 68 {
70 Q_OBJECT
71
72 public: 69 public:
73 this(QWidget *parent = null) 70
74 { 71 this(QWidget parent = null)
75 super(parent); 72 {
76 setCursor(Qt.ArrowCursor); 73 super(parent);
77 setToolTip(tr("Clear")); 74 setCursor(Qt.ArrowCursor);
78 setVisible(false); 75 setToolTip(tr("Clear"));
79 setFocusPolicy(Qt.NoFocus); 76 setVisible(false);
80 } 77 setFocusPolicy(Qt.NoFocus);
81 void paintEvent(QPaintEvent *event) 78 }
82 { 79
83 Q_UNUSED(event); 80 void paintEvent(QPaintEvent *event)
84 QPainter painter(this); 81 {
85 int height = this.height(); 82 //Q_UNUSED(event);
86 83 QPainter painter(this);
87 painter.setRenderHint(QPainter::Antialiasing, true); 84 int height = this.height();
88 QColor color = palette().color(QPalette.Mid); 85
89 painter.setBrush(isDown() 86 painter.setRenderHint(QPainter.Antialiasing, true);
90 ? palette().color(QPalette.Dark) 87 QColor color = palette().color(QPalette.Mid);
91 : palette().color(QPalette.Mid)); 88 painter.setBrush(isDown() ? palette().color(QPalette.Dark) : palette().color(QPalette.Mid));
92 painter.setPen(painter.brush().color()); 89 painter.setPen(painter.brush().color());
93 int size = width(); 90 int size = width();
94 int offset = size / 5; 91 int offset = size / 5;
95 int radius = size - offset * 2; 92 int radius = size - offset * 2;
96 painter.drawEllipse(offset, offset, radius, radius); 93 painter.drawEllipse(offset, offset, radius, radius);
97 94
98 painter.setPen(palette().color(QPalette.Base)); 95 painter.setPen(palette().color(QPalette.Base));
99 int border = offset * 2; 96 int border = offset * 2;
100 painter.drawLine(border, border, width() - border, height - border); 97 painter.drawLine(border, border, width() - border, height - border);
101 painter.drawLine(border, height - border, width() - border, border); 98 painter.drawLine(border, height - border, width() - border, border);
102 } 99 }
103 100
104 public slots: 101 void textChanged(QString &text)
105 void textChanged(QString &text) 102 {
106 { 103 setVisible(!text.isEmpty());
107 setVisible(!text.isEmpty()); 104 }
108 }
109
110 } 105 }
111 106
112 /* 107 /*
113 Search icon on the left hand side of the search widget 108 Search icon on the left hand side of the search widget
114 When a menu is set a down arrow appears 109 When a menu is set a down arrow appears
115 */ 110 */
116 class SearchButton : public QAbstractButton { 111 class SearchButton : public QAbstractButton {
112
117 public: 113 public:
118 SearchButton(QWidget *parent = null) 114
119 { 115 SearchButton(QWidget parent = null)
120 super(parent); 116 {
121 m_menu = 0; 117 super(parent);
122 setObjectName(QLatin1String("SearchButton")); 118 m_menu = 0;
123 setCursor(Qt.ArrowCursor); 119 setObjectName(QLatin1String("SearchButton"));
124 setFocusPolicy(Qt.NoFocus); 120 setCursor(Qt.ArrowCursor);
125 } 121 setFocusPolicy(Qt.NoFocus);
126 122 }
127 void paintEvent(QPaintEvent *event) 123
128 { 124 void paintEvent(QPaintEvent event)
129 Q_UNUSED(event); 125 {
130 QPainterPath myPath; 126 //Q_UNUSED(event);
131 127 QPainterPath myPath;
132 int radius = (height() / 5) * 2; 128
133 QRect circle(height() / 3 - 1, height() / 4, radius, radius); 129 int radius = (height() / 5) * 2;
134 myPath.addEllipse(circle); 130 QRect circle(height() / 3 - 1, height() / 4, radius, radius);
135 131 myPath.addEllipse(circle);
136 myPath.arcMoveTo(circle, 300); 132
137 QPointF c = myPath.currentPosition(); 133 myPath.arcMoveTo(circle, 300);
138 int diff = height() / 7; 134 QPointF c = myPath.currentPosition();
139 myPath.lineTo(qMin(width() - 2, (int)c.x() + diff), c.y() + diff); 135 int diff = height() / 7;
140 136 myPath.lineTo(qMin(width() - 2, (int)c.x() + diff), c.y() + diff);
141 QPainter painter(this); 137
142 painter.setRenderHint(QPainter::Antialiasing, true); 138 QPainter painter(this);
143 painter.setPen(QPen(Qt.darkGray, 2)); 139 painter.setRenderHint(QPainter.Antialiasing, true);
144 painter.drawPath(myPath); 140 painter.setPen(QPen(Qt.darkGray, 2));
145 141 painter.drawPath(myPath);
146 if (m_menu) { 142
147 QPainterPath dropPath; 143 if (m_menu) {
148 dropPath.arcMoveTo(circle, 320); 144 QPainterPath dropPath;
149 QPointF c = dropPath.currentPosition(); 145 dropPath.arcMoveTo(circle, 320);
150 c = QPointF(c.x() + 3.5, c.y() + 0.5); 146 QPointF c = dropPath.currentPosition();
151 dropPath.moveTo(c); 147 c = QPointF(c.x() + 3.5, c.y() + 0.5);
152 dropPath.lineTo(c.x() + 4, c.y()); 148 dropPath.moveTo(c);
153 dropPath.lineTo(c.x() + 2, c.y() + 2); 149 dropPath.lineTo(c.x() + 4, c.y());
154 dropPath.closeSubpath(); 150 dropPath.lineTo(c.x() + 2, c.y() + 2);
155 painter.setPen(Qt.darkGray); 151 dropPath.closeSubpath();
156 painter.setBrush(Qt.darkGray); 152 painter.setPen(Qt.darkGray);
157 painter.setRenderHint(QPainter::Antialiasing, false); 153 painter.setBrush(Qt.darkGray);
158 painter.drawPath(dropPath); 154 painter.setRenderHint(QPainter.Antialiasing, false);
159 } 155 painter.drawPath(dropPath);
160 painter.end(); 156 }
161 } 157 painter.end();
162 QMenu *m_menu; 158 }
159
160 QMenu m_menu;
163 161
164 protected: 162 protected:
165 void mousePressEvent(QMouseEvent *event) 163 void mousePressEvent(QMouseEvent event)
166 { 164 {
167 if (m_menu && event.button() == Qt.LeftButton) { 165 if (m_menu && event.button() == Qt.LeftButton) {
168 QWidget *p = parentWidget(); 166 QWidget p = parentWidget();
169 if (p) { 167 if (p) {
170 QPoint r = p.mapToGlobal(QPoint(0, p.height())); 168 QPoint r = p.mapToGlobal(QPoint(0, p.height()));
171 m_menu.exec(QPoint(r.x() + height() / 2, r.y())); 169 m_menu.exec(QPoint(r.x() + height() / 2, r.y()));
172 } 170 }
173 event.accept(); 171 event.accept();
174 } 172 }
175 QAbstractButton::mousePressEvent(event); 173 QAbstractButton.mousePressEvent(event);
176 } 174 }
177 } 175 }
178 176
179 class SearchLineEdit : public ExLineEdit 177 class SearchLineEdit : public ExLineEdit
180 { 178 {
181 Q_OBJECT 179 //Q_PROPERTY(QString inactiveText READ inactiveText WRITE setInactiveText)
182 Q_PROPERTY(QString inactiveText READ inactiveText WRITE setInactiveText) 180
183 181 mixin Signal!("textChanged", QString &text);
184 signals:
185 void textChanged(QString &text);
186 182
187 public: 183 public:
188 /* 184 /*
189 SearchLineEdit is an enhanced QLineEdit 185 SearchLineEdit is an enhanced QLineEdit
190 - A Search icon on the left with optional menu 186 - 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 187 - 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 188 - When there is text a clear button is displayed on the right hand side
193 */ 189 */
194 this(QWidget *parent = null) : ExLineEdit(parent), 190 this(QWidget parent = null) : ExLineEdit(parent)
195 m_searchButton(new SearchButton(this)) 191 {
196 { 192 m_searchButton = new SearchButton(this);
197 connect(lineEdit(), SIGNAL(textChanged(QString &)), 193 lineEdit.textChanged.connect(&this.textChanged);
198 this, SIGNAL(textChanged(QString &))); 194 setLeftWidget(m_searchButton);
199 setLeftWidget(m_searchButton); 195 m_inactiveText = tr("Search");
200 m_inactiveText = tr("Search"); 196
201 197 QSizePolicy policy = sizePolicy();
202 QSizePolicy policy = sizePolicy(); 198 setSizePolicy(QSizePolicy.Preferred, policy.verticalPolicy());
203 setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy()); 199 }
200
201 QString inactiveText()
202 {
203 return m_inactiveText;
204 }
205
206 void setInactiveText(QString &text)
207 {
208 m_inactiveText = text;
209 }
210
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 }
220
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 }
228
229 protected:
230 void resizeEvent(QResizeEvent event)
231 {
232 updateGeometries();
233 ExLineEdit.resizeEvent(event);
234 }
235
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 }
253
254 private:
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;
204 } 267 }
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 }