diff options
author | Warwick Allison <warwick.allison@nokia.com> | 2010-01-05 05:55:58 (GMT) |
---|---|---|
committer | Warwick Allison <warwick.allison@nokia.com> | 2010-01-05 05:55:58 (GMT) |
commit | 027a3a94955127712a76f15430a1f6cc0288722e (patch) | |
tree | 20f5ff73b9dca7210e69f78c6f6a0c0ac088b415 /tools/qmlviewer | |
parent | 186cdd1c29f1c708455ec2f0771a6eb600d4a075 (diff) | |
download | Qt-027a3a94955127712a76f15430a1f6cc0288722e.zip Qt-027a3a94955127712a76f15430a1f6cc0288722e.tar.gz Qt-027a3a94955127712a76f15430a1f6cc0288722e.tar.bz2 |
Basic WGT/WGZ support.
Task-number: QT-524
Diffstat (limited to 'tools/qmlviewer')
-rw-r--r-- | tools/qmlviewer/main.cpp | 6 | ||||
-rw-r--r-- | tools/qmlviewer/qmlviewer.cpp | 100 | ||||
-rw-r--r-- | 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 <QAbstractAnimation> #include "deviceskin.h" +#include <private/qzipreader_p.h> + #include <QSettings> +#include <QXmlStreamReader> +#include <QBuffer> +#include <QNetworkReply> #include <QNetworkCookieJar> #include <QNetworkDiskCache> #include <QNetworkAccessManager> @@ -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; i<zip.count(); ++i) { + QZipReader::FileInfo info = zip.entryInfoAt(i); + qDebug() << "zip:" << info.filePath; + } + */ + wgtdir = QDir::tempPath()+QDir::separator()+QLatin1String("qmlviewer-wgt"); + removeRecursive(wgtdir); + QDir().mkpath(wgtdir); + zip.extractAll(wgtdir); + + QString rootfile; + + if (wgtreply->header(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; i<zip.count(); ++i) { + QZipReader::FileInfo info = zip.entryInfoAt(i); + if (info.filePath.compare(QLatin1String("index.qml"),Qt::CaseInsensitive)==0) + rootfile = wgtdir+QDir::separator()+info.filePath; + if (rootfile.isEmpty() && info.filePath.endsWith("/index.qml",Qt::CaseInsensitive)) + rootfile = wgtdir+QDir::separator()+info.filePath; + } + } + + openQml(rootfile); +} + +void QmlViewer::openFile() { QString cur = canvas->url().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) |