2 * Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
3 * Copyright (C) 2010 University of Szeged
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "BrowserWindow.h"
31 #include "private/qquickwebpage_p.h"
32 #include "private/qquickwebview_p.h"
39 BrowserWindow::BrowserWindow(WindowOptions* options)
40 : m_windowOptions(options)
42 setWindowTitle("MiniBrowser");
43 setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
44 setResizeMode(QQuickView::SizeRootObjectToView);
46 // This allows starting MiniBrowser from the build directory without previously defining QML_IMPORT_PATH.
47 QDir qmlImportDir = QDir(QCoreApplication::applicationDirPath());
48 qmlImportDir.cd("../imports");
49 engine()->addImportPath(qmlImportDir.canonicalPath());
51 Utils* utils = new Utils(this);
52 engine()->rootContext()->setContextProperty("utils", utils);
53 engine()->rootContext()->setContextProperty("options", options);
54 setSource(QUrl("qrc:///qml/BrowserWindow.qml"));
55 connect(rootObject(), SIGNAL(pageTitleChanged(QString)), this, SLOT(onTitleChanged(QString)));
56 connect(rootObject(), SIGNAL(newWindow(QString)), this, SLOT(newWindow(QString)));
57 if (options->startFullScreen())
60 if (options->startMaximized())
61 setWindowState(Qt::WindowMaximized);
63 resize(options->requestedWindowSize());
67 QQmlContext* context = rootContext();
68 context->setContextProperty("Window", this);
70 if (!options->userAgent().isNull())
71 webViewExperimental()->setUserAgent(options->userAgent());
73 // Zoom values are chosen to be like in Mozilla Firefox 3.
74 m_zoomLevels << 0.3 << 0.5 << 0.67 << 0.8 << 0.9
76 << 1.11 << 1.22 << 1.33 << 1.5 << 1.7 << 2 << 2.4 << 3;
78 m_currentZoomLevel = m_zoomLevels.indexOf(1);
81 QQuickWebView* BrowserWindow::webView() const
83 return rootObject()->property("webview").value<QQuickWebView*>();
86 QQuickWebViewExperimental* BrowserWindow::webViewExperimental() const
88 return webView()->property("experimental").value<QQuickWebViewExperimental*>();
91 void BrowserWindow::load(const QString& url)
93 QUrl completedUrl = Utils::urlFromUserInput(url);
94 QMetaObject::invokeMethod(rootObject(), "load", Qt::DirectConnection, Q_ARG(QVariant, completedUrl));
97 void BrowserWindow::reload()
99 QMetaObject::invokeMethod(rootObject(), "reload", Qt::DirectConnection);
102 void BrowserWindow::focusAddressBar()
104 QMetaObject::invokeMethod(rootObject(), "focusAddressBar", Qt::DirectConnection);
107 BrowserWindow* BrowserWindow::newWindow(const QString& url)
109 BrowserWindow* window = new BrowserWindow(m_windowOptions);
114 void BrowserWindow::updateVisualMockTouchPoints(const QList<QTouchEvent::TouchPoint>& touchPoints)
116 foreach (const QTouchEvent::TouchPoint& touchPoint, touchPoints) {
117 QString mockTouchPointIdentifier = QString("mockTouchPoint%1").arg(touchPoint.id());
118 QQuickItem* mockTouchPointItem = rootObject()->findChild<QQuickItem*>(mockTouchPointIdentifier, Qt::FindDirectChildrenOnly);
120 if (!mockTouchPointItem) {
121 QQmlComponent touchMockPointComponent(engine(), QUrl("qrc:///qml/MockTouchPoint.qml"));
122 mockTouchPointItem = qobject_cast<QQuickItem*>(touchMockPointComponent.create());
123 mockTouchPointItem->setObjectName(mockTouchPointIdentifier);
124 mockTouchPointItem->setProperty("pointId", QVariant(touchPoint.id()));
125 mockTouchPointItem->setParent(rootObject());
126 mockTouchPointItem->setParentItem(rootObject());
129 QRectF touchRect = touchPoint.rect();
130 mockTouchPointItem->setX(touchRect.center().x());
131 mockTouchPointItem->setY(touchRect.center().y());
132 mockTouchPointItem->setWidth(touchRect.width());
133 mockTouchPointItem->setHeight(touchRect.height());
134 mockTouchPointItem->setProperty("pressed", QVariant(touchPoint.state() != Qt::TouchPointReleased));
138 void BrowserWindow::screenshot()
142 BrowserWindow::~BrowserWindow()
146 void BrowserWindow::onTitleChanged(QString title)
149 title = QLatin1String("MiniBrowser");
150 setWindowTitle(title);
153 void BrowserWindow::zoomIn()
155 if (m_currentZoomLevel < m_zoomLevels.size() - 1)
156 ++m_currentZoomLevel;
157 webView()->setZoomFactor(m_zoomLevels[m_currentZoomLevel]);
160 void BrowserWindow::zoomOut()
162 if (m_currentZoomLevel > 0)
163 --m_currentZoomLevel;
164 webView()->setZoomFactor(m_zoomLevels[m_currentZoomLevel]);
167 void BrowserWindow::keyPressEvent(QKeyEvent* event)
170 if (event->modifiers() & Qt::ControlModifier) {
172 if ((handled = event->key() == Qt::Key_Plus))
174 else if ((handled = event->key() == Qt::Key_Minus))
182 QQuickView::keyPressEvent(event);
185 void BrowserWindow::wheelEvent(QWheelEvent* event)
187 if (event->modifiers() & Qt::ControlModifier && event->orientation() == Qt::Vertical) {
188 if (event->delta() > 0)
196 QQuickView::wheelEvent(event);