summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWarwick Allison <warwick.allison@nokia.com>2010-01-08 04:32:41 (GMT)
committerWarwick Allison <warwick.allison@nokia.com>2010-01-08 04:32:41 (GMT)
commit9151c4838ac7428107246abb9c99f296ab395c3d (patch)
treec677bd3fbe24ab3cdbde2b7a5e9cd608a1c8e27d
parent42ab4b839af7d02258bc40389f11ec615cf42f0c (diff)
downloadQt-9151c4838ac7428107246abb9c99f296ab395c3d.zip
Qt-9151c4838ac7428107246abb9c99f296ab395c3d.tar.gz
Qt-9151c4838ac7428107246abb9c99f296ab395c3d.tar.bz2
Allow QML types defined in both C++ and QML files to be in the same module.
-rw-r--r--doc/src/declarative/modules.qdoc5
-rw-r--r--examples/declarative/plugins/README8
-rw-r--r--examples/declarative/plugins/files/Clock.qml50
-rw-r--r--examples/declarative/plugins/files/center.pngbin0 -> 765 bytes
-rw-r--r--examples/declarative/plugins/files/clock.pngbin0 -> 20653 bytes
-rw-r--r--examples/declarative/plugins/files/hour.pngbin0 -> 625 bytes
-rw-r--r--examples/declarative/plugins/files/minute.pngbin0 -> 625 bytes
-rw-r--r--examples/declarative/plugins/plugin.cpp82
-rw-r--r--examples/declarative/plugins/plugins.pro4
-rw-r--r--examples/declarative/plugins/plugins.qml22
-rw-r--r--src/declarative/qml/qmlengine.cpp99
-rw-r--r--src/declarative/qml/qmlmetatype.cpp1
12 files changed, 193 insertions, 78 deletions
diff --git a/doc/src/declarative/modules.qdoc b/doc/src/declarative/modules.qdoc
index ec36fe7..368595f 100644
--- a/doc/src/declarative/modules.qdoc
+++ b/doc/src/declarative/modules.qdoc
@@ -63,7 +63,8 @@ This makes available all types in Qt that were available in Qt 4.6, regardless o
actual version of Qt executing the QML. So even if Qt 4.7 adds a type that would conflict
with a type you defined while using 4.6, that type is not imported, so there is no conflict.
-Types defined by plugins are made using QmlModulePlugin.
+Types defined by plugins are made using QmlModulePlugin. Installed plugins and QML files
+can both contribute types to the same module.
\section1 Importing Types Defined in QML
@@ -71,6 +72,7 @@ Types defined by plugins are made using QmlModulePlugin.
When importing types \link components defined using QML\endlink, the syntax depends
on whether or not the types are installed on the system.
+
\section2 Installed QML Files
To import types defined in QML files that are installed on the system running the
@@ -83,6 +85,7 @@ import com.nokia.Example 1.0
Files imported in this way are found on the paths added by QmlEngine::addImportPath(),
which by default only inludes \c $QTDIR/qml, so the above would make available those types
defined in \c $QTDIR/qml/com/nokia/Example which are specified as being in version 1.0.
+Installed plugins and QML files can both contribute types to the same module.
The specification of types to versions is given by a special file, \c qmldir which must
exist in the module directory. The syntax is described below.
diff --git a/examples/declarative/plugins/README b/examples/declarative/plugins/README
index e67b1de..621f570 100644
--- a/examples/declarative/plugins/README
+++ b/examples/declarative/plugins/README
@@ -1,6 +1,8 @@
-This example show a C++ plugin that provides additional modules of QML types
-(in this case, a single module with a single extra type).
-to run:
+This example shows a module "com.nokia.TimeExample" that is implelement
+by a C++ plugin (providing the "Time" type), and by QML files (providing the
+"Clock" type).
+
+To run:
make install
qmlviewer plugins.qml
diff --git a/examples/declarative/plugins/files/Clock.qml b/examples/declarative/plugins/files/Clock.qml
new file mode 100644
index 0000000..01ec686
--- /dev/null
+++ b/examples/declarative/plugins/files/Clock.qml
@@ -0,0 +1,50 @@
+import Qt 4.6
+
+Item {
+ id: clock
+ width: 200; height: 200
+
+ property alias city: cityLabel.text
+ property var hours
+ property var minutes
+ property var shift : 0
+
+ Image { id: background; source: "clock.png" }
+
+ Image {
+ x: 92.5; y: 27
+ source: "hour.png"
+ smooth: true
+ transform: Rotation {
+ id: hourRotation
+ origin.x: 7.5; origin.y: 73; angle: 0
+ angle: SpringFollow {
+ spring: 2; damping: 0.2; modulus: 360
+ source: (clock.hours * 30) + (clock.minutes * 0.5)
+ }
+ }
+ }
+
+ Image {
+ x: 93.5; y: 17
+ source: "minute.png"
+ smooth: true
+ transform: Rotation {
+ id: minuteRotation
+ origin.x: 6.5; origin.y: 83; angle: 0
+ angle: SpringFollow {
+ spring: 2; damping: 0.2; modulus: 360
+ source: clock.minutes * 6
+ }
+ }
+ }
+
+ Image {
+ anchors.centerIn: background; source: "center.png"
+ }
+
+ Text {
+ id: cityLabel; font.bold: true; font.pixelSize: 14; y:200; color: "white"
+ anchors.horizontalCenter: parent.horizontalCenter
+ }
+}
diff --git a/examples/declarative/plugins/files/center.png b/examples/declarative/plugins/files/center.png
new file mode 100644
index 0000000..7fbd802
--- /dev/null
+++ b/examples/declarative/plugins/files/center.png
Binary files differ
diff --git a/examples/declarative/plugins/files/clock.png b/examples/declarative/plugins/files/clock.png
new file mode 100644
index 0000000..462edac
--- /dev/null
+++ b/examples/declarative/plugins/files/clock.png
Binary files differ
diff --git a/examples/declarative/plugins/files/hour.png b/examples/declarative/plugins/files/hour.png
new file mode 100644
index 0000000..f8061a1
--- /dev/null
+++ b/examples/declarative/plugins/files/hour.png
Binary files differ
diff --git a/examples/declarative/plugins/files/minute.png b/examples/declarative/plugins/files/minute.png
new file mode 100644
index 0000000..1297ec7
--- /dev/null
+++ b/examples/declarative/plugins/files/minute.png
Binary files differ
diff --git a/examples/declarative/plugins/plugin.cpp b/examples/declarative/plugins/plugin.cpp
index ac44e7e..9688caf 100644
--- a/examples/declarative/plugins/plugin.cpp
+++ b/examples/declarative/plugins/plugin.cpp
@@ -44,22 +44,35 @@
#include <qdebug.h>
#include <qdatetime.h>
#include <qbasictimer.h>
+#include <qapplication.h>
-class Time : public QObject
+// Implements a "Time" class with hour and minute properties
+// that change on-the-minute yet efficiently sleep the rest
+// of the time.
+
+class MinuteTimer : public QObject
{
Q_OBJECT
- Q_PROPERTY(int hour READ hour NOTIFY timeChanged)
- Q_PROPERTY(int minute READ minute NOTIFY timeChanged)
-
public:
- Time(QObject *parent=0) : QObject(parent)
+ MinuteTimer(QObject *parent) : QObject(parent)
+ {
+ }
+
+ void start()
{
- timerEvent(0);
- timer.start(30000,this);
+ if (!timer.isActive()) {
+ time = QTime::currentTime();
+ timer.start(60000-time.second()*1000, this);
+ }
}
- int minute() const { return t.minute(); }
- int hour() const { return t.hour(); }
+ void stop()
+ {
+ timer.stop();
+ }
+
+ int hour() const { return time.hour(); }
+ int minute() const { return time.minute(); }
signals:
void timeChanged();
@@ -67,18 +80,67 @@ signals:
protected:
void timerEvent(QTimerEvent *)
{
- t = QTime::currentTime();
+ QTime now = QTime::currentTime();
+ if (now.second() == 59 && now.minute() == time.minute() && now.hour() == time.hour()) {
+ // just missed time tick over, force it, wait extra 0.5 seconds
+ time.addSecs(60);
+ timer.start(60500, this);
+ } else {
+ time = now;
+ timer.start(60000-time.second()*1000, this);
+ }
emit timeChanged();
}
private:
+ QTime time;
QBasicTimer timer;
+};
+
+class Time : public QObject
+{
+ Q_OBJECT
+ Q_PROPERTY(int hour READ hour NOTIFY timeChanged)
+ Q_PROPERTY(int minute READ minute NOTIFY timeChanged)
+
+public:
+ Time(QObject *parent=0) : QObject(parent)
+ {
+ if (++instances == 1) {
+ if (!timer)
+ timer = new MinuteTimer(qApp);
+ connect(timer, SIGNAL(timeChanged()), this, SIGNAL(timeChanged()));
+ timer->start();
+ }
+ }
+
+ ~Time()
+ {
+ if (--instances == 0) {
+ timer->stop();
+ }
+ }
+
+ int minute() const { return timer->minute(); }
+ int hour() const { return timer->hour(); }
+
+signals:
+ void timeChanged();
+
+private:
QTime t;
+ static MinuteTimer *timer;
+ static int instances;
};
+int Time::instances=0;
+MinuteTimer *Time::timer=0;
+
+
QML_DECLARE_TYPE(Time);
QML_DEFINE_TYPE(com.nokia.TimeExample,1,0,Time,Time);
+
class QExampleQmlPlugin : public QmlModulePlugin
{
Q_OBJECT
diff --git a/examples/declarative/plugins/plugins.pro b/examples/declarative/plugins/plugins.pro
index a1a94f9..4109eba 100644
--- a/examples/declarative/plugins/plugins.pro
+++ b/examples/declarative/plugins/plugins.pro
@@ -5,7 +5,9 @@ CONFIG += qt plugin declarative
SOURCES += plugin.cpp
target.path += $$[QT_INSTALL_PLUGINS]/qmlmodules
-INSTALLS += target
+sources.files += files/Clock.qml files/qmldir files/background.png files/center.png files/clock-night.png files/clock.png files/hour.png files/minute.png
+sources.path += $$[QT_INSTALL_DATA]/qml/com/nokia/TimeExample
+INSTALLS += target sources
VERSION=1.0.0
diff --git a/examples/declarative/plugins/plugins.qml b/examples/declarative/plugins/plugins.qml
index df6b5d4..97c1b69 100644
--- a/examples/declarative/plugins/plugins.qml
+++ b/examples/declarative/plugins/plugins.qml
@@ -1,23 +1,11 @@
-import Qt 4.6
import com.nokia.TimeExample 1.0
-Item {
+Clock { // this class is defined in QML (files/Clock.qml)
- Time { id: time }
-
- width: 200
- height: 100
-
- Rectangle {
- anchors.fill: parent
- color: "blue"
+ Time { // this class is defined in C++ (plugins.cpp)
+ id: time
}
- Text {
- text: time.hour + ":" + (time.minute < 10 ? "0" : "") + time.minute
- anchors.centerIn: parent
- color: "white"
- font.pixelSize: 42
- font.bold: true
- }
+ hours: time.hour
+ minutes: time.minute
}
diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp
index c44f6b6..e7d0743 100644
--- a/src/declarative/qml/qmlengine.cpp
+++ b/src/declarative/qml/qmlengine.cpp
@@ -79,6 +79,7 @@
#include <QDebug>
#include <QMetaObject>
#include <QStack>
+#include <QtCore/qlibraryinfo.h>
#include <QtCore/qthreadstorage.h>
#include <QtCore/qthread.h>
#include <QtCore/qcoreapplication.h>
@@ -123,6 +124,7 @@ QmlEnginePrivate::QmlEnginePrivate(QmlEngine *e)
inBeginCreate(false), networkAccessManager(0), typeManager(e), uniqueId(1)
{
globalClass = new QmlGlobalScriptClass(&scriptEngine);
+ fileImportPath.append(QLibraryInfo::location(QLibraryInfo::DataPath)+QDir::separator()+QLatin1String("qml"));
}
QUrl QmlScriptEngine::resolvedUrl(QScriptContext *context, const QUrl& url)
@@ -1083,11 +1085,12 @@ static QString toLocalFileOrQrc(const QUrl& url)
/////////////////////////////////////////////////////////////
struct QmlEnginePrivate::ImportedNamespace {
+ QStringList uris;
QStringList urls;
QList<int> majversions;
QList<int> minversions;
QList<bool> isLibrary;
- QList<bool> isBuiltin;
+ QList<bool> isBuiltin; // Types provided by C++ code (including plugins)
QList<QString> qmlDirContent;
bool find(const QByteArray& type, int *vmajor, int *vminor, QmlType** type_return, QUrl* url_return) const
@@ -1097,57 +1100,60 @@ struct QmlEnginePrivate::ImportedNamespace {
int vmin = minversions.at(i);
if (isBuiltin.at(i)) {
- QByteArray qt = urls.at(i).toUtf8();
+ QByteArray qt = uris.at(i).toUtf8();
qt += '/';
qt += type;
+ if (qmlImportTrace())
+ qDebug() << "Look in" << qt;
QmlType *t = QmlMetaType::qmlType(qt,vmaj,vmin);
if (vmajor) *vmajor = vmaj;
if (vminor) *vminor = vmin;
if (t) {
+ if (qmlImportTrace())
+ qDebug() << "Found" << qt;
if (type_return)
*type_return = t;
return true;
}
- } else {
- QUrl url = QUrl(urls.at(i) + QLatin1Char('/') + QString::fromUtf8(type) + QLatin1String(".qml"));
- QString qmldircontent = qmlDirContent.at(i);
- if (vmaj || vmin || !qmldircontent.isEmpty()) {
- // Check version file - XXX cache these in QmlEngine!
- if (qmldircontent.isEmpty()) {
- QFile qmldir(toLocalFileOrQrc(QUrl(urls.at(i)+QLatin1String("/qmldir"))));
- if (qmldir.open(QIODevice::ReadOnly)) {
- qmldircontent = QString::fromUtf8(qmldir.readAll());
- }
+ }
+ QUrl url = QUrl(urls.at(i) + QLatin1Char('/') + QString::fromUtf8(type) + QLatin1String(".qml"));
+ QString qmldircontent = qmlDirContent.at(i);
+ if (vmaj || vmin || !qmldircontent.isEmpty()) {
+ // Check version file - XXX cache these in QmlEngine!
+ if (qmldircontent.isEmpty()) {
+ QFile qmldir(toLocalFileOrQrc(QUrl(urls.at(i)+QLatin1String("/qmldir"))));
+ if (qmldir.open(QIODevice::ReadOnly)) {
+ qmldircontent = QString::fromUtf8(qmldir.readAll());
}
- QString typespace = QString::fromUtf8(type)+QLatin1Char(' ');
- QStringList lines = qmldircontent.split(QLatin1Char('\n'));
- foreach (QString line, lines) {
- if (line.isEmpty() || line.at(0) == QLatin1Char('#'))
- continue;
- if (line.startsWith(typespace)) {
- int space1 = line.indexOf(QLatin1Char(' '));
- int space2 = space1 >=0 ? line.indexOf(QLatin1Char(' '),space1+1) : -1;
- QString mapversions = line.mid(space1+1,space2<0?line.length()-space1-1:space2-space1-1);
- int dot = mapversions.indexOf(QLatin1Char('.'));
- int mapvmaj = mapversions.left(dot).toInt();
- if (mapvmaj<=vmaj) {
- if (mapvmaj<vmaj || vmin >= mapversions.mid(dot+1).toInt()) {
- QStringRef mapfile = space2<0 ? QStringRef() : line.midRef(space2+1,line.length()-space2-1);
- if (url_return)
- *url_return = url.resolved(mapfile.toString());
- return true;
- }
+ }
+ QString typespace = QString::fromUtf8(type)+QLatin1Char(' ');
+ QStringList lines = qmldircontent.split(QLatin1Char('\n'));
+ foreach (QString line, lines) {
+ if (line.isEmpty() || line.at(0) == QLatin1Char('#'))
+ continue;
+ if (line.startsWith(typespace)) {
+ int space1 = line.indexOf(QLatin1Char(' '));
+ int space2 = space1 >=0 ? line.indexOf(QLatin1Char(' '),space1+1) : -1;
+ QString mapversions = line.mid(space1+1,space2<0?line.length()-space1-1:space2-space1-1);
+ int dot = mapversions.indexOf(QLatin1Char('.'));
+ int mapvmaj = mapversions.left(dot).toInt();
+ if (mapvmaj<=vmaj) {
+ if (mapvmaj<vmaj || vmin >= mapversions.mid(dot+1).toInt()) {
+ QStringRef mapfile = space2<0 ? QStringRef() : line.midRef(space2+1,line.length()-space2-1);
+ if (url_return)
+ *url_return = url.resolved(mapfile.toString());
+ return true;
}
}
}
- } else {
- // XXX search non-files too! (eg. zip files, see QT-524)
- QFileInfo f(toLocalFileOrQrc(url));
- if (f.exists()) {
- if (url_return)
- *url_return = url;
- return true;
- }
+ }
+ } else {
+ // XXX search non-files too! (eg. zip files, see QT-524)
+ QFileInfo f(toLocalFileOrQrc(url));
+ if (f.exists()) {
+ if (url_return)
+ *url_return = url;
+ return true;
}
}
}
@@ -1195,19 +1201,19 @@ public:
}
}
if (!found) {
- if (uri != QLatin1String("Qt")) { // skip well-known, it's not in a plugin
- QFactoryLoader *l = loader();
- QmlModuleFactoryInterface *factory =
- qobject_cast<QmlModuleFactoryInterface*>(l->instance(uri));
- // return value not used currently
- }
-
// XXX assume it is a built-in type qualifier
isbuiltin = true;
+ } else {
+ QFactoryLoader *l = loader();
+ QmlModuleFactoryInterface *factory =
+ qobject_cast<QmlModuleFactoryInterface*>(l->instance(uri));
+ if (factory)
+ isbuiltin = true;
}
} else {
url = base.resolved(QUrl(url)).toString();
}
+ s->uris.prepend(uri);
s->urls.prepend(url);
s->majversions.prepend(vmaj);
s->minversions.prepend(vmin);
@@ -1389,6 +1395,9 @@ QUrl QmlEnginePrivate::Imports::baseUrl() const
in \c /opt/MyApp/lib/qml/com/mycompany/Feature/ for the components
provided by that module (and in the case of versioned imports,
for the \c qmldir file definiting the type version mapping.
+
+ By default, only the "qml" subdirectory of QLibraryInfo::location(QLibraryInfo::DataPath)
+ is included on the import path.
*/
void QmlEngine::addImportPath(const QString& path)
{
diff --git a/src/declarative/qml/qmlmetatype.cpp b/src/declarative/qml/qmlmetatype.cpp
index fbb3952..0b79a21 100644
--- a/src/declarative/qml/qmlmetatype.cpp
+++ b/src/declarative/qml/qmlmetatype.cpp
@@ -462,7 +462,6 @@ int QmlMetaType::registerType(const QmlPrivate::MetaTypeIds &id, QmlPrivate::Fun
if (uri)
name += '/';
name += cname;
- name.replace('.','/');
QmlType *type = new QmlType(id.typeId, id.listId, id.qmlListId,
func, name, version_maj, version_min, mo, attach, attachMo, pStatus,