[Release] Webkit2-efl-123997_0.11.78
[framework/web/webkit-efl.git] / Tools / MiniBrowser / qt / BrowserWindow.cpp
1 /*
2  * Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
3  * Copyright (C) 2010 University of Szeged
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28
29 #include "BrowserWindow.h"
30
31 #include "private/qquickwebpage_p.h"
32 #include "private/qquickwebview_p.h"
33 #include "utils.h"
34
35 #include <QQmlEngine>
36 #include <QDir>
37 #include <QPointF>
38
39 BrowserWindow::BrowserWindow(WindowOptions* options)
40     : m_windowOptions(options)
41 {
42     setWindowTitle("MiniBrowser");
43     setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
44     setResizeMode(QQuickView::SizeRootObjectToView);
45
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());
50
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())
58         showFullScreen();
59     else {
60         if (options->startMaximized())
61             setWindowState(Qt::WindowMaximized);
62         else
63             resize(options->requestedWindowSize());
64         show();
65     }
66
67     QQmlContext* context = rootContext();
68     context->setContextProperty("Window", this);
69
70     if (!options->userAgent().isNull())
71         webViewExperimental()->setUserAgent(options->userAgent());
72
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
75                  << 1
76                  << 1.11 << 1.22 << 1.33 << 1.5 << 1.7 << 2 << 2.4 << 3;
77
78     m_currentZoomLevel = m_zoomLevels.indexOf(1);
79 }
80
81 QQuickWebView* BrowserWindow::webView() const
82 {
83     return rootObject()->property("webview").value<QQuickWebView*>();
84 }
85
86 QQuickWebViewExperimental* BrowserWindow::webViewExperimental() const
87 {
88     return webView()->property("experimental").value<QQuickWebViewExperimental*>();
89 }
90
91 void BrowserWindow::load(const QString& url)
92 {
93     QUrl completedUrl = Utils::urlFromUserInput(url);
94     QMetaObject::invokeMethod(rootObject(), "load", Qt::DirectConnection, Q_ARG(QVariant, completedUrl));
95 }
96
97 void BrowserWindow::reload()
98 {
99     QMetaObject::invokeMethod(rootObject(), "reload", Qt::DirectConnection);
100 }
101
102 void BrowserWindow::focusAddressBar()
103 {
104     QMetaObject::invokeMethod(rootObject(), "focusAddressBar", Qt::DirectConnection);
105 }
106
107 BrowserWindow* BrowserWindow::newWindow(const QString& url)
108 {
109     BrowserWindow* window = new BrowserWindow(m_windowOptions);
110     window->load(url);
111     return window;
112 }
113
114 void BrowserWindow::updateVisualMockTouchPoints(const QList<QTouchEvent::TouchPoint>& touchPoints)
115 {
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);
119
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());
127         }
128
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));
135     }
136 }
137
138 void BrowserWindow::screenshot()
139 {
140 }
141
142 BrowserWindow::~BrowserWindow()
143 {
144 }
145
146 void BrowserWindow::onTitleChanged(QString title)
147 {
148     if (title.isEmpty())
149         title = QLatin1String("MiniBrowser");
150     setWindowTitle(title);
151 }
152
153 void BrowserWindow::zoomIn()
154 {
155     if (m_currentZoomLevel < m_zoomLevels.size() - 1)
156         ++m_currentZoomLevel;
157     webView()->setZoomFactor(m_zoomLevels[m_currentZoomLevel]);
158 }
159
160 void BrowserWindow::zoomOut()
161 {
162     if (m_currentZoomLevel > 0)
163         --m_currentZoomLevel;
164     webView()->setZoomFactor(m_zoomLevels[m_currentZoomLevel]);
165 }
166
167 void BrowserWindow::keyPressEvent(QKeyEvent* event)
168 {
169
170     if (event->modifiers() & Qt::ControlModifier) {
171         bool handled;
172         if ((handled = event->key() == Qt::Key_Plus))
173             zoomIn();
174         else if ((handled = event->key() == Qt::Key_Minus))
175             zoomOut();
176
177         if (handled) {
178             event->accept();
179             return;
180         }
181     }
182     QQuickView::keyPressEvent(event);
183 }
184
185 void BrowserWindow::wheelEvent(QWheelEvent* event)
186 {
187     if (event->modifiers() & Qt::ControlModifier && event->orientation() == Qt::Vertical) {
188         if (event->delta() > 0)
189             zoomIn();
190         else
191             zoomOut();
192
193         event->accept();
194         return;
195     }
196     QQuickView::wheelEvent(event);
197 }