From 027a3a94955127712a76f15430a1f6cc0288722e Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Tue, 5 Jan 2010 15:55:58 +1000 Subject: Basic WGT/WGZ support. Task-number: QT-524 --- tools/qmlviewer/main.cpp | 6 +-- tools/qmlviewer/qmlviewer.cpp | 100 +++++++++++++++++++++++++++++++++++++++++- tools/qmlviewer/qmlviewer.h | 9 +++- 3 files changed, 109 insertions(+), 6 deletions(-) diff --git a/tools/qmlviewer/main.cpp b/tools/qmlviewer/main.cpp index 412d3ef..d1ee733 100644 --- a/tools/qmlviewer/main.cpp +++ b/tools/qmlviewer/main.cpp @@ -326,14 +326,14 @@ int main(int argc, char ** argv) if (fullScreen && maximized) qWarning() << "Both -fullscreen and -maximized specified. Using -fullscreen."; if (!fileName.isEmpty()) { - viewer.openQml(fileName); + viewer.open(fileName); fullScreen ? viewer.showFullScreen() : maximized ? viewer.showMaximized() : viewer.show(); } else { if (!useNativeFileBrowser) - viewer.open(); + viewer.openFile(); fullScreen ? viewer.showFullScreen() : maximized ? viewer.showMaximized() : viewer.show(); if (useNativeFileBrowser) - viewer.open(); + viewer.openFile(); } viewer.raise(); diff --git a/tools/qmlviewer/qmlviewer.cpp b/tools/qmlviewer/qmlviewer.cpp index 831c680..b838f40 100644 --- a/tools/qmlviewer/qmlviewer.cpp +++ b/tools/qmlviewer/qmlviewer.cpp @@ -51,7 +51,12 @@ #include #include "deviceskin.h" +#include + #include +#include +#include +#include #include #include #include @@ -441,7 +446,7 @@ void QmlViewer::createMenu(QMenuBar *menu, QMenu *flatmenu) QAction *openAction = new QAction(tr("&Open..."), parent); openAction->setShortcut(QKeySequence("Ctrl+O")); - connect(openAction, SIGNAL(triggered()), this, SLOT(open())); + connect(openAction, SIGNAL(triggered()), this, SLOT(openFile())); fileMenu->addAction(openAction); QAction *reloadAction = new QAction(tr("&Reload"), parent); @@ -758,7 +763,98 @@ void QmlViewer::reload() openQml(currentFileOrUrl); } -void QmlViewer::open() +void QmlViewer::open(const QString& doc) +{ + if (doc.endsWith(".wgt",Qt::CaseInsensitive) + || doc.endsWith(".wgz",Qt::CaseInsensitive)) + openWgt(doc); + else + openQml(doc); +} + +void QmlViewer::openWgt(const QString& doc) +{ + // XXX This functionality could be migrated to QmlView once refined + + QUrl url(doc); + if (url.isRelative()) + url = QUrl::fromLocalFile(doc); + canvas->reset(); + QNetworkAccessManager * nam = canvas->engine()->networkAccessManager(); + wgtreply = nam->get(QNetworkRequest(url)); + connect(wgtreply,SIGNAL(finished()),this,SLOT(unpackWgt())); +} + +static void removeRecursive(const QString& dirname) +{ + QDir dir(dirname); + QFileInfoList entries(dir.entryInfoList(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot)); + for (int i = 0; i < entries.count(); ++i) + if (entries[i].isDir()) + removeRecursive(entries[i].filePath()); + else + dir.remove(entries[i].fileName()); + QDir().rmdir(dirname); +} + +void QmlViewer::unpackWgt() +{ + QByteArray all = wgtreply->readAll(); + QBuffer buf(&all); + buf.open(QIODevice::ReadOnly); + QZipReader zip(&buf); + /* + for (int i=0; iheader(QNetworkRequest::ContentTypeHeader).toString() == "application/widget" || wgtreply->url().path().endsWith(".wgt",Qt::CaseInsensitive)) { + // W3C Draft http://www.w3.org/TR/2009/CR-widgets-20091201 + QFile configfile(wgtdir+QDir::separator()+"config.xml"); + if (configfile.open(QIODevice::ReadOnly)) { + QXmlStreamReader config(&configfile); + if (config.readNextStartElement() && config.name() == "widget") { + while (config.readNextStartElement()) { + if (config.name() == "content") { + rootfile = wgtdir + QDir::separator(); + rootfile += config.attributes().value(QLatin1String("src")); + } + // XXX process other config + + config.skipCurrentElement(); + } + } + } else { + qWarning("No config.xml found - non-standard WGT file"); + } + if (rootfile.isEmpty()) { + QString def = wgtdir+QDir::separator()+"index.qml"; + if (QFile::exists(def)) + rootfile = def; + } + } else { + // Just find index.qml, preferably at the root + for (int i=0; iurl().toLocalFile(); if (useQmlFileBrowser) { diff --git a/tools/qmlviewer/qmlviewer.h b/tools/qmlviewer/qmlviewer.h index 717258c..39bedc3 100644 --- a/tools/qmlviewer/qmlviewer.h +++ b/tools/qmlviewer/qmlviewer.h @@ -56,6 +56,7 @@ class QmlGraphicsTestEngine; class QProcess; class RecordingDialog; class QmlGraphicsTester; +class QNetworkReply; class QmlViewer #if defined(Q_OS_SYMBIAN) @@ -99,8 +100,10 @@ public: public slots: void sceneResized(QSize size); + void open(const QString&); + void openWgt(const QString&); void openQml(const QString&); - void open(); + void openFile(); void reload(); void takeSnapShot(); void toggleRecording(); @@ -131,6 +134,7 @@ private slots: void setLandscape(); void startNetwork(); void toggleFullScreen(); + void unpackWgt(); private: void setupProxy(); @@ -173,6 +177,9 @@ private: ScriptOptions m_scriptOptions; QmlGraphicsTester *tester; + QNetworkReply *wgtreply; + QString wgtdir; + bool useQmlFileBrowser; }; Q_DECLARE_OPERATORS_FOR_FLAGS(QmlViewer::ScriptOptions) -- cgit v0.12