comparison demos/browser/urllineedit.d @ 45:71b382c10ef6

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