BrowserWindow.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "BrowserWindow.h"
  29. #include "private/qquickwebpage_p.h"
  30. #include "private/qquickwebview_p.h"
  31. #include "utils.h"
  32. #include <QQmlEngine>
  33. #include <QDir>
  34. #include <QPointF>
  35. BrowserWindow::BrowserWindow(WindowOptions* options)
  36. : m_windowOptions(options)
  37. {
  38. setTitle("MiniBrowser");
  39. setFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
  40. setResizeMode(QQuickView::SizeRootObjectToView);
  41. // This allows starting MiniBrowser from the build directory without previously defining QML_IMPORT_PATH.
  42. QDir qmlImportDir = QDir(QCoreApplication::applicationDirPath());
  43. qmlImportDir.cd("../imports");
  44. engine()->addImportPath(qmlImportDir.canonicalPath());
  45. Utils* utils = new Utils(this);
  46. engine()->rootContext()->setContextProperty("utils", utils);
  47. engine()->rootContext()->setContextProperty("options", options);
  48. setSource(QUrl("qrc:///qml/BrowserWindow.qml"));
  49. connect(rootObject(), SIGNAL(pageTitleChanged(QString)), this, SLOT(onTitleChanged(QString)));
  50. connect(rootObject(), SIGNAL(newWindow(QString)), this, SLOT(newWindow(QString)));
  51. if (options->startFullScreen())
  52. showFullScreen();
  53. else {
  54. if (options->startMaximized())
  55. setWindowState(Qt::WindowMaximized);
  56. else
  57. resize(options->requestedWindowSize());
  58. show();
  59. }
  60. QQmlContext* context = rootContext();
  61. context->setContextProperty("Window", this);
  62. if (!options->userAgent().isNull())
  63. webViewExperimental()->setUserAgent(options->userAgent());
  64. // Zoom values are chosen to be like in Mozilla Firefox 3.
  65. m_zoomLevels << 0.3 << 0.5 << 0.67 << 0.8 << 0.9
  66. << 1
  67. << 1.11 << 1.22 << 1.33 << 1.5 << 1.7 << 2 << 2.4 << 3;
  68. m_currentZoomLevel = m_zoomLevels.indexOf(1);
  69. }
  70. QQuickWebView* BrowserWindow::webView() const
  71. {
  72. return rootObject()->property("webview").value<QQuickWebView*>();
  73. }
  74. QQuickWebViewExperimental* BrowserWindow::webViewExperimental() const
  75. {
  76. return webView()->property("experimental").value<QQuickWebViewExperimental*>();
  77. }
  78. void BrowserWindow::load(const QString& url)
  79. {
  80. QUrl completedUrl = Utils::urlFromUserInput(url);
  81. QMetaObject::invokeMethod(rootObject(), "load", Qt::DirectConnection, Q_ARG(QVariant, completedUrl));
  82. }
  83. void BrowserWindow::reload()
  84. {
  85. QMetaObject::invokeMethod(rootObject(), "reload", Qt::DirectConnection);
  86. }
  87. void BrowserWindow::focusAddressBar()
  88. {
  89. QMetaObject::invokeMethod(rootObject(), "focusAddressBar", Qt::DirectConnection);
  90. }
  91. void BrowserWindow::toggleFind()
  92. {
  93. QMetaObject::invokeMethod(rootObject(), "toggleFind", Qt::DirectConnection);
  94. }
  95. BrowserWindow* BrowserWindow::newWindow(const QString& url)
  96. {
  97. BrowserWindow* window = new BrowserWindow(m_windowOptions);
  98. window->load(url);
  99. return window;
  100. }
  101. void BrowserWindow::updateVisualMockTouchPoints(const QList<QTouchEvent::TouchPoint>& touchPoints)
  102. {
  103. if (touchPoints.isEmpty()) {
  104. // Hide all touch indicator items.
  105. foreach (QQuickItem* item, m_activeMockComponents.values())
  106. item->setProperty("pressed", false);
  107. return;
  108. }
  109. foreach (const QTouchEvent::TouchPoint& touchPoint, touchPoints) {
  110. QQuickItem* mockTouchPointItem = m_activeMockComponents.value(touchPoint.id());
  111. if (!mockTouchPointItem) {
  112. QQmlComponent touchMockPointComponent(engine(), QUrl("qrc:///qml/MockTouchPoint.qml"));
  113. mockTouchPointItem = qobject_cast<QQuickItem*>(touchMockPointComponent.create());
  114. Q_ASSERT(mockTouchPointItem);
  115. m_activeMockComponents.insert(touchPoint.id(), mockTouchPointItem);
  116. mockTouchPointItem->setProperty("pointId", QVariant(touchPoint.id()));
  117. mockTouchPointItem->setParent(rootObject());
  118. mockTouchPointItem->setParentItem(rootObject());
  119. }
  120. QRectF touchRect = touchPoint.rect();
  121. mockTouchPointItem->setX(touchRect.center().x());
  122. mockTouchPointItem->setY(touchRect.center().y());
  123. mockTouchPointItem->setWidth(touchRect.width());
  124. mockTouchPointItem->setHeight(touchRect.height());
  125. mockTouchPointItem->setProperty("pressed", QVariant(touchPoint.state() != Qt::TouchPointReleased));
  126. }
  127. }
  128. void BrowserWindow::screenshot()
  129. {
  130. }
  131. BrowserWindow::~BrowserWindow()
  132. {
  133. }
  134. void BrowserWindow::onTitleChanged(QString title)
  135. {
  136. if (title.isEmpty())
  137. title = QLatin1String("MiniBrowser");
  138. setTitle(title);
  139. }
  140. void BrowserWindow::zoomIn()
  141. {
  142. if (m_currentZoomLevel < m_zoomLevels.size() - 1)
  143. ++m_currentZoomLevel;
  144. webView()->setZoomFactor(m_zoomLevels[m_currentZoomLevel]);
  145. }
  146. void BrowserWindow::zoomOut()
  147. {
  148. if (m_currentZoomLevel > 0)
  149. --m_currentZoomLevel;
  150. webView()->setZoomFactor(m_zoomLevels[m_currentZoomLevel]);
  151. }
  152. void BrowserWindow::keyPressEvent(QKeyEvent* event)
  153. {
  154. if (event->modifiers() & Qt::ControlModifier) {
  155. bool handled;
  156. if ((handled = event->key() == Qt::Key_Plus))
  157. zoomIn();
  158. else if ((handled = event->key() == Qt::Key_Minus))
  159. zoomOut();
  160. if (handled) {
  161. event->accept();
  162. return;
  163. }
  164. }
  165. QQuickView::keyPressEvent(event);
  166. }
  167. void BrowserWindow::wheelEvent(QWheelEvent* event)
  168. {
  169. if (event->modifiers() & Qt::ControlModifier && event->orientation() == Qt::Vertical) {
  170. if (event->delta() > 0)
  171. zoomIn();
  172. else
  173. zoomOut();
  174. event->accept();
  175. return;
  176. }
  177. QQuickView::wheelEvent(event);
  178. }