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 module urllineedit;
|
|
43
|
73
|
44 import qt.core.QUrl;
|
|
45 import qt.core.QEvent;
|
94
|
46 //import qt.core.QDebug;
|
73
|
47
|
|
48 import qt.gui.QWidget;
|
|
49 import qt.gui.QStyleOptionFrame;
|
|
50 import qt.gui.QApplication;
|
|
51 import qt.gui.QCompleter;
|
|
52 import qt.gui.QFocusEvent;
|
|
53 import qt.gui.QHBoxLayout;
|
|
54 import qt.gui.QLabel;
|
|
55 import qt.gui.QLineEdit;
|
|
56 import qt.gui.QPainter;
|
|
57 import qt.gui.QStyle;
|
|
58 import qt.gui.QStyleOptionFrameV2;
|
94
|
59 import qt.gui.QLinearGradient;
|
|
60
|
45
|
61
|
|
62 import browserapplication;
|
|
63 import searchlineedit;
|
|
64 import webview;
|
|
65
|
|
66
|
|
67 class ExLineEdit : public QWidget
|
|
68 {
|
73
|
69 public:
|
|
70
|
|
71 this(QWidget parent = null)
|
|
72 {
|
|
73 super(parent);
|
|
74
|
|
75 m_leftWidget = 0;
|
|
76 m_lineEdit = new QLineEdit(this);
|
|
77 m_clearButton = 0;
|
45
|
78
|
73
|
79 setFocusPolicy(m_lineEdit.focusPolicy());
|
|
80 setAttribute(Qt.WA_InputMethodEnabled);
|
|
81 setSizePolicy(m_lineEdit.sizePolicy());
|
|
82 setBackgroundRole(m_lineEdit.backgroundRole());
|
|
83 setMouseTracking(true);
|
|
84 setAcceptDrops(true);
|
|
85 setAttribute(Qt.WA_MacShowFocusRect, true);
|
|
86 QPalette p = m_lineEdit.palette();
|
|
87 setPalette(p);
|
45
|
88
|
73
|
89 // line edit
|
|
90 m_lineEdit.setFrame(false);
|
|
91 m_lineEdit.setFocusProxy(this);
|
|
92 m_lineEdit.setAttribute(Qt.WA_MacShowFocusRect, false);
|
|
93 QPalette clearPalette = m_lineEdit.palette();
|
|
94 clearPalette.setBrush(QPalette.Base, QBrush(Qt.transparent));
|
|
95 m_lineEdit.setPalette(clearPalette);
|
45
|
96
|
73
|
97 // clearButton
|
|
98 m_clearButton = new ClearButton(this);
|
|
99 m_clearButton.clicked.connect(&m_lineEdit.clear);
|
|
100 m_lineEdit.textChanged.connect(&m_clearButton.textChanged);
|
|
101 }
|
45
|
102
|
73
|
103 QLineEdit lineEdit() { return m_lineEdit; }
|
45
|
104
|
73
|
105 void setLeftWidget(QWidget widget)
|
|
106 {
|
|
107 m_leftWidget = widget;
|
|
108 }
|
45
|
109
|
73
|
110 QWidget leftWidget()
|
|
111 {
|
|
112 return m_leftWidget;
|
|
113 }
|
45
|
114
|
73
|
115 QSize sizeHint()
|
|
116 {
|
|
117 m_lineEdit.setFrame(true);
|
|
118 QSize size = m_lineEdit.sizeHint();
|
|
119 m_lineEdit.setFrame(false);
|
|
120 return size;
|
|
121 }
|
45
|
122
|
73
|
123 QVariant inputMethodQuery(Qt.InputMethodQuery property)
|
|
124 {
|
|
125 return m_lineEdit.inputMethodQuery(property);
|
|
126 }
|
45
|
127
|
|
128 protected:
|
73
|
129
|
|
130 void focusInEvent(QFocusEvent event)
|
|
131 {
|
|
132 m_lineEdit.event(event);
|
|
133 QWidget.focusInEvent(event);
|
|
134 }
|
|
135
|
|
136 void focusOutEvent(QFocusEvent event)
|
|
137 {
|
|
138 m_lineEdit.event(event);
|
|
139
|
|
140 if (m_lineEdit.completer()) {
|
|
141 m_lineEdit.completer().activated.connect(& m_lineEdit.setText);
|
|
142 m_lineEdit.completer().highlighted.connect(&m_lineEdit._q_completionHighlighted);
|
|
143 }
|
|
144 QWidget.focusOutEvent(event);
|
|
145 }
|
45
|
146
|
73
|
147 void keyPressEvent(QKeyEvent event)
|
|
148 {
|
|
149 m_lineEdit.event(event);
|
|
150 }
|
45
|
151
|
73
|
152 void paintEvent(QPaintEvent event)
|
|
153 {
|
85
|
154 auto p = new QPainter(this);
|
73
|
155 QStyleOptionFrameV2 panel;
|
|
156 initStyleOption(&panel);
|
|
157 style().drawPrimitive(QStyle.PE_PanelLineEdit, &panel, &p, this);
|
|
158 }
|
|
159
|
|
160 void resizeEvent(QResizeEvent event)
|
|
161 {
|
|
162 assert(m_leftWidget);
|
|
163 updateGeometries();
|
|
164 QWidget.resizeEvent(event);
|
|
165 }
|
|
166
|
|
167 void inputMethodEvent(QInputMethodEvent e)
|
|
168 {
|
|
169 m_lineEdit.event(e);
|
|
170 }
|
45
|
171
|
73
|
172 bool event(QEvent event)
|
|
173 {
|
|
174 if (event.type() == QEvent.ShortcutOverride)
|
|
175 return m_lineEdit.event(event);
|
|
176 return QWidget.event(event);
|
|
177 }
|
|
178
|
|
179 protected:
|
|
180
|
|
181 void updateGeometries()
|
|
182 {
|
|
183 QStyleOptionFrameV2 panel;
|
|
184 initStyleOption(&panel);
|
|
185 QRect rect = style().subElementRect(QStyle.SE_LineEditContents, &panel, this);
|
|
186
|
|
187 int height = rect.height();
|
|
188 int width = rect.width();
|
|
189
|
|
190 int m_leftWidgetHeight = m_leftWidget.height();
|
|
191 m_leftWidget.setGeometry(rect.x() + 2, rect.y() + (height - m_leftWidgetHeight)/2,
|
|
192 m_leftWidget.width(), m_leftWidget.height());
|
|
193
|
|
194 int clearButtonWidth = this.height();
|
|
195 m_lineEdit.setGeometry(m_leftWidget.x() + m_leftWidget.width(), 0,
|
|
196 width - clearButtonWidth - m_leftWidget.width(), this.height());
|
45
|
197
|
73
|
198 m_clearButton.setGeometry(this.width() - clearButtonWidth, 0,
|
|
199 clearButtonWidth, this.height());
|
|
200 }
|
45
|
201
|
73
|
202 void initStyleOption(QStyleOptionFrameV2 option)
|
|
203 {
|
|
204 option.initFrom(this);
|
|
205 option.rect = contentsRect();
|
|
206 option.lineWidth = style().pixelMetric(QStyle.PM_DefaultFrameWidth, option, this);
|
|
207 option.midLineWidth = 0;
|
|
208 option.state |= QStyle.State_Sunken;
|
|
209 if (m_lineEdit.isReadOnly())
|
|
210 option.state |= QStyle.State_ReadOnly;
|
|
211 version(QT_KEYPAD_NAVIGATION)
|
|
212 {
|
|
213 if (hasEditFocus())
|
|
214 option.state |= QStyle.State_HasEditFocus;
|
|
215 }
|
|
216 else
|
|
217 {
|
|
218 option.features = QStyleOptionFrameV2.None;
|
|
219 }
|
|
220 }
|
|
221
|
|
222 QWidget m_leftWidget;
|
|
223 QLineEdit m_lineEdit;
|
|
224 ClearButton m_clearButton;
|
45
|
225 }
|
|
226
|
|
227
|
|
228 class UrlIconLabel : public QLabel
|
|
229 {
|
73
|
230 public:
|
45
|
231
|
73
|
232 this(QWidget parent)
|
|
233 {
|
|
234 super(parent);
|
|
235 m_webView = 0;
|
|
236 setMinimumWidth(16);
|
|
237 setMinimumHeight(16);
|
|
238 }
|
45
|
239
|
73
|
240 WebView m_webView;
|
45
|
241
|
|
242 protected:
|
73
|
243
|
|
244 void mousePressEvent(QMouseEvent event)
|
|
245 {
|
|
246 if (event.button() == Qt.LeftButton)
|
|
247 m_dragStartPos = event.pos();
|
|
248 QLabel.mousePressEvent(event);
|
|
249 }
|
45
|
250
|
73
|
251 void mouseMoveEvent(QMouseEvent event)
|
|
252 {
|
|
253 if (event.buttons() == Qt.LeftButton
|
|
254 && (event.pos() - m_dragStartPos).manhattanLength() > QApplication.startDragDistance()
|
|
255 && m_webView) {
|
|
256 QDrag drag = new QDrag(this);
|
|
257 QMimeData mimeData = new QMimeData;
|
94
|
258 mimeData.setText(QString.fromUtf8(m_webView.getUrl().toEncoded()));
|
77
|
259 QUrl[] urls;
|
94
|
260 urls ~= m_webView.getUrl();
|
73
|
261 mimeData.setUrls(urls);
|
|
262 drag.setMimeData(mimeData);
|
|
263 drag.exec();
|
|
264 }
|
|
265 }
|
45
|
266
|
|
267 private:
|
|
268
|
73
|
269 QPoint m_dragStartPos;
|
45
|
270 }
|
|
271
|
|
272
|
|
273 class UrlLineEdit : public ExLineEdit
|
|
274 {
|
73
|
275 public:
|
45
|
276
|
73
|
277 this(QWidget parent = null)
|
|
278 {
|
|
279 super(parent);
|
|
280 m_webView = 0;
|
|
281 m_iconLabel = 0;
|
|
282 // icon
|
|
283 m_iconLabel = new UrlIconLabel(this);
|
|
284 m_iconLabel.resize(16, 16);
|
|
285 setLeftWidget(m_iconLabel);
|
|
286 m_defaultBaseColor = palette().color(QPalette.Base);
|
45
|
287
|
73
|
288 webViewIconChanged();
|
|
289 }
|
|
290
|
|
291 void setWebView(WebView webView)
|
|
292 {
|
|
293 assert(!m_webView);
|
|
294 m_webView = webView;
|
|
295 m_iconLabel.m_webView = webView;
|
|
296 webView.urlChanged.connect(&this.webViewUrlChanged);
|
|
297 webView.loadFinished.connect(&this.webViewIconChanged);
|
|
298 webView.iconChanged.connect(&this.webViewIconChanged);
|
|
299 webView.loadProgress.connect(&this.update);
|
|
300 }
|
45
|
301
|
|
302 protected:
|
73
|
303
|
|
304 void paintEvent(QPaintEvent event)
|
|
305 {
|
|
306 QPalette p = palette();
|
94
|
307 if (m_webView && m_webView.getUrl().scheme() == "https") {
|
85
|
308 auto lightYellow = new QColor(248, 248, 210);
|
73
|
309 p.setBrush(QPalette.Base, generateGradient(lightYellow));
|
|
310 } else {
|
|
311 p.setBrush(QPalette.Base, m_defaultBaseColor);
|
|
312 }
|
|
313 setPalette(p);
|
|
314 ExLineEdit.paintEvent(event);
|
45
|
315
|
80
|
316 auto painter = new QPainter(this);
|
73
|
317 QStyleOptionFrameV2 panel;
|
|
318 initStyleOption(&panel);
|
|
319 QRect backgroundRect = style().subElementRect(QStyle.SE_LineEditContents, &panel, this);
|
|
320 if (m_webView && !hasFocus()) {
|
|
321 int progress = m_webView.progress();
|
80
|
322 QColor loadingColor = new QColor(116, 192, 250);
|
73
|
323 painter.setBrush(generateGradient(loadingColor));
|
|
324 painter.setPen(Qt.transparent);
|
|
325 int mid = backgroundRect.width() / 100 * progress;
|
85
|
326 QRect progressRect = QRect(backgroundRect.x(), backgroundRect.y(), mid, backgroundRect.height());
|
73
|
327 painter.drawRect(progressRect);
|
|
328 }
|
|
329 }
|
45
|
330
|
74
|
331 void focusOutEvent(QFocusEvent event)
|
73
|
332 {
|
|
333 if (m_lineEdit.text().isEmpty() && m_webView)
|
94
|
334 m_lineEdit.setText(QString.fromUtf8(m_webView.getUrl().toEncoded()));
|
73
|
335 ExLineEdit.focusOutEvent(event);
|
|
336 }
|
45
|
337
|
|
338 private:
|
73
|
339
|
|
340 void webViewUrlChanged(QUrl url)
|
|
341 {
|
|
342 m_lineEdit.setText(QString.fromUtf8(url.toEncoded()));
|
|
343 m_lineEdit.setCursorPosition(0);
|
|
344 }
|
|
345
|
|
346 void webViewIconChanged()
|
|
347 {
|
94
|
348 QUrl url = m_webView ? m_webView.getUrl() : new QUrl();
|
73
|
349 QIcon icon = BrowserApplication.instance().icon(url);
|
85
|
350 auto pixmap = new QPixmap(icon.pixmap(16, 16));
|
73
|
351 m_iconLabel.setPixmap(pixmap);
|
|
352 }
|
|
353
|
|
354 private:
|
|
355
|
|
356 QLinearGradient generateGradient(QColor color)
|
|
357 {
|
85
|
358 auto gradient = new QLinearGradient(0, 0, 0, height());
|
73
|
359 gradient.setColorAt(0, m_defaultBaseColor);
|
|
360 gradient.setColorAt(0.15, color.lighter(120));
|
|
361 gradient.setColorAt(0.5, color);
|
|
362 gradient.setColorAt(0.85, color.lighter(120));
|
|
363 gradient.setColorAt(1, m_defaultBaseColor);
|
|
364 return gradient;
|
|
365 }
|
|
366
|
|
367 WebView m_webView;
|
|
368 UrlIconLabel m_iconLabel;
|
|
369 QColor m_defaultBaseColor;
|
45
|
370 }
|