2011-01-26 16:36:18 +01:00
|
|
|
/*
|
|
|
|
|
This file was generated by the Html5 Application wizard of Qt Creator.
|
|
|
|
|
Html5ApplicationViewer is a convenience class containing mobile device specific
|
|
|
|
|
code such as screen orientation handling.
|
|
|
|
|
It is recommended not to modify this file, since newer versions of Qt Creator
|
|
|
|
|
may offer an updated version of it.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "html5applicationviewer.h"
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
|
#include <QtCore/QDir>
|
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
|
#include <QtGui/QVBoxLayout>
|
|
|
|
|
#include <QtGui/QGraphicsView>
|
|
|
|
|
#include <QtGui/QGraphicsScene>
|
|
|
|
|
#include <QtGui/QGraphicsLinearLayout>
|
|
|
|
|
#include <QtWebKit/QGraphicsWebView>
|
|
|
|
|
#include <QtWebKit/QWebFrame>
|
|
|
|
|
|
2011-03-18 17:14:53 +01:00
|
|
|
#ifdef TOUCH_OPTIMIZED_NAVIGATION
|
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
|
#include <QtGui/QGraphicsSceneMouseEvent>
|
|
|
|
|
#include <QtWebKit/QWebElement>
|
2011-04-15 23:27:45 -04:00
|
|
|
#include "navigationcontroller.h"
|
|
|
|
|
#include "webtouchevent.h"
|
|
|
|
|
#include "webtouchnavigation.h"
|
|
|
|
|
#include "webtouchphysics.h"
|
|
|
|
|
#include "webtouchphysicsinterface.h"
|
|
|
|
|
#include "webtouchscroller.h"
|
2011-03-18 17:14:53 +01:00
|
|
|
#endif // TOUCH_OPTIMIZED_NAVIGATION
|
|
|
|
|
|
2011-01-26 16:36:18 +01:00
|
|
|
class Html5ApplicationViewerPrivate : public QGraphicsView
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
|
|
|
|
Html5ApplicationViewerPrivate(QWidget *parent = 0);
|
|
|
|
|
|
|
|
|
|
void resizeEvent(QResizeEvent *event);
|
|
|
|
|
static QString adjustPath(const QString &path);
|
|
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
void quit();
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
void addToJavaScript();
|
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
void quitRequested();
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
QGraphicsWebView *m_webView;
|
2011-03-18 17:14:53 +01:00
|
|
|
#ifdef TOUCH_OPTIMIZED_NAVIGATION
|
|
|
|
|
NavigationController *m_controller;
|
|
|
|
|
#endif // TOUCH_OPTIMIZED_NAVIGATION
|
2011-01-26 16:36:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Html5ApplicationViewerPrivate::Html5ApplicationViewerPrivate(QWidget *parent)
|
|
|
|
|
: QGraphicsView(parent)
|
|
|
|
|
{
|
|
|
|
|
QGraphicsScene *scene = new QGraphicsScene;
|
|
|
|
|
setScene(scene);
|
|
|
|
|
setFrameShape(QFrame::NoFrame);
|
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
|
|
|
|
|
m_webView = new QGraphicsWebView;
|
2011-03-18 17:14:53 +01:00
|
|
|
m_webView->setAcceptTouchEvents(true);
|
|
|
|
|
m_webView->setAcceptHoverEvents(false);
|
|
|
|
|
setAttribute(Qt::WA_AcceptTouchEvents, true);
|
2011-01-26 16:36:18 +01:00
|
|
|
scene->addItem(m_webView);
|
2011-03-18 17:14:53 +01:00
|
|
|
scene->setActiveWindow(m_webView);
|
|
|
|
|
#ifdef TOUCH_OPTIMIZED_NAVIGATION
|
|
|
|
|
m_controller = new NavigationController(parent, m_webView);
|
|
|
|
|
#endif // TOUCH_OPTIMIZED_NAVIGATION
|
2011-01-26 16:36:18 +01:00
|
|
|
connect(m_webView->page()->mainFrame(),
|
|
|
|
|
SIGNAL(javaScriptWindowObjectCleared()), SLOT(addToJavaScript()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Html5ApplicationViewerPrivate::resizeEvent(QResizeEvent *event)
|
|
|
|
|
{
|
|
|
|
|
m_webView->resize(event->size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Html5ApplicationViewerPrivate::adjustPath(const QString &path)
|
|
|
|
|
{
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
|
if (!QDir::isAbsolutePath(path))
|
|
|
|
|
return QCoreApplication::applicationDirPath()
|
|
|
|
|
+ QLatin1String("/../Resources/") + path;
|
|
|
|
|
#else
|
2011-02-09 16:37:25 +01:00
|
|
|
const QString pathInInstallDir = QCoreApplication::applicationDirPath()
|
|
|
|
|
+ QLatin1String("/../") + path;
|
|
|
|
|
if (pathInInstallDir.contains(QLatin1String("opt"))
|
|
|
|
|
&& pathInInstallDir.contains(QLatin1String("bin"))
|
|
|
|
|
&& QFileInfo(pathInInstallDir).exists()) {
|
|
|
|
|
return pathInInstallDir;
|
|
|
|
|
}
|
2011-01-26 16:36:18 +01:00
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Html5ApplicationViewerPrivate::quit()
|
|
|
|
|
{
|
|
|
|
|
emit quitRequested();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Html5ApplicationViewerPrivate::addToJavaScript()
|
|
|
|
|
{
|
|
|
|
|
m_webView->page()->mainFrame()->addToJavaScriptWindowObject("Qt", this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Html5ApplicationViewer::Html5ApplicationViewer(QWidget *parent)
|
|
|
|
|
: QWidget(parent)
|
|
|
|
|
, m_d(new Html5ApplicationViewerPrivate(this))
|
|
|
|
|
{
|
|
|
|
|
connect(m_d, SIGNAL(quitRequested()), SLOT(close()));
|
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout;
|
|
|
|
|
layout->addWidget(m_d);
|
|
|
|
|
layout->setMargin(0);
|
|
|
|
|
setLayout(layout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Html5ApplicationViewer::~Html5ApplicationViewer()
|
|
|
|
|
{
|
|
|
|
|
delete m_d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Html5ApplicationViewer::loadFile(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
m_d->m_webView->setUrl(QUrl(Html5ApplicationViewerPrivate::adjustPath(fileName)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Html5ApplicationViewer::loadUrl(const QUrl &url)
|
|
|
|
|
{
|
|
|
|
|
m_d->m_webView->setUrl(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Html5ApplicationViewer::setOrientation(ScreenOrientation orientation)
|
|
|
|
|
{
|
2011-02-02 16:25:33 +01:00
|
|
|
#if defined(Q_OS_SYMBIAN)
|
|
|
|
|
// If the version of Qt on the device is < 4.7.2, that attribute won't work
|
2011-01-26 16:36:18 +01:00
|
|
|
if (orientation != ScreenOrientationAuto) {
|
2012-01-31 12:35:02 +01:00
|
|
|
const QStringList v = QString::fromLatin1(qVersion()).split(QLatin1Char('.'));
|
2011-02-02 16:25:33 +01:00
|
|
|
if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
|
|
|
|
|
qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-01-26 16:36:18 +01:00
|
|
|
}
|
2011-02-02 16:25:33 +01:00
|
|
|
#endif // Q_OS_SYMBIAN
|
|
|
|
|
|
2011-01-26 16:36:18 +01:00
|
|
|
Qt::WidgetAttribute attribute;
|
|
|
|
|
switch (orientation) {
|
2011-02-02 16:25:33 +01:00
|
|
|
#if QT_VERSION < 0x040702
|
|
|
|
|
// Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
|
2011-01-26 16:36:18 +01:00
|
|
|
case ScreenOrientationLockPortrait:
|
2011-02-02 16:25:33 +01:00
|
|
|
attribute = static_cast<Qt::WidgetAttribute>(128);
|
2011-01-26 16:36:18 +01:00
|
|
|
break;
|
|
|
|
|
case ScreenOrientationLockLandscape:
|
2011-02-02 16:25:33 +01:00
|
|
|
attribute = static_cast<Qt::WidgetAttribute>(129);
|
2011-01-26 16:36:18 +01:00
|
|
|
break;
|
2011-02-02 16:25:33 +01:00
|
|
|
default:
|
2011-01-26 16:36:18 +01:00
|
|
|
case ScreenOrientationAuto:
|
2011-02-02 16:25:33 +01:00
|
|
|
attribute = static_cast<Qt::WidgetAttribute>(130);
|
|
|
|
|
break;
|
|
|
|
|
#else // QT_VERSION < 0x040702
|
|
|
|
|
case ScreenOrientationLockPortrait:
|
|
|
|
|
attribute = Qt::WA_LockPortraitOrientation;
|
|
|
|
|
break;
|
|
|
|
|
case ScreenOrientationLockLandscape:
|
|
|
|
|
attribute = Qt::WA_LockLandscapeOrientation;
|
|
|
|
|
break;
|
2011-01-26 16:36:18 +01:00
|
|
|
default:
|
2011-02-02 16:25:33 +01:00
|
|
|
case ScreenOrientationAuto:
|
|
|
|
|
attribute = Qt::WA_AutoOrientation;
|
2011-01-26 16:36:18 +01:00
|
|
|
break;
|
2011-02-02 16:25:33 +01:00
|
|
|
#endif // QT_VERSION < 0x040702
|
|
|
|
|
};
|
2011-01-26 16:36:18 +01:00
|
|
|
setAttribute(attribute, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Html5ApplicationViewer::showExpanded()
|
|
|
|
|
{
|
2011-07-12 11:47:42 +02:00
|
|
|
#if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
|
2011-01-26 16:36:18 +01:00
|
|
|
showFullScreen();
|
|
|
|
|
#elif defined(Q_WS_MAEMO_5)
|
|
|
|
|
showMaximized();
|
|
|
|
|
#else
|
|
|
|
|
show();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QGraphicsWebView *Html5ApplicationViewer::webView() const
|
|
|
|
|
{
|
|
|
|
|
return m_d->m_webView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#include "html5applicationviewer.moc"
|