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