[Qt-qml] env variable expansion in import, useful for styles?

2010-04-26 Thread Matthias Ettrich
Hi,

attached is a small patch which adds environment variable expansion to the 
import statement. Rationale: this might be useful to implement different 
(native) component styles.

You could make a module:

com.mycompany.qml-ui-components

which contains a C++ plugin and a bunch of files, e.g. Button.qml

The Button.qml itself reads only:

---Button.qml---
import $STYLE
Button{}


and you have subdirectories like:

 plain/Button.qml
 fancy/Button.qml
 crazy/Button.qml

The C++ plugin is responsible for setting the correct default style (using 
::qputenv()) on the current platform, if not set otherwise.

Currently you can only achive this when playing with the module imports path, 
the patch would make this easier.

Reasonable extension?

Matthias
diff --git a/src/declarative/qml/qdeclarativecompositetypemanager.cpp b/src/declarative/qml/qdeclarativecompositetypemanager.cpp
index 0eb7e1b..084cf1d 100644
--- a/src/declarative/qml/qdeclarativecompositetypemanager.cpp
+++ b/src/declarative/qml/qdeclarativecompositetypemanager.cpp
@@ -560,6 +560,26 @@ int QDeclarativeCompositeTypeManager::resolveTypes(QDeclarativeCompositeTypeData
 if (imp.type == QDeclarativeScriptParser::Import::Script)
 continue;
 
+if (imp.type == QDeclarativeScriptParser::Import::File) {
+// expand import variables
+int i = 0;
+forever {
+i = imp.uri.indexOf(QLatin1Char('$'), i);
+if (i  0)
+break;
+if (i  0  imp.uri.at(i-1) == QLatin1Char('\\'))
+continue;
+int len = 1;
+if (i + len  imp.uri.size()  imp.uri.at(i + len).isLetter()) {
+++len;
+while (i + len  imp.uri.size()  imp.uri.at(i + len).isLetterOrNumber())
+++len;
+}
+QByteArray var = ::qgetenv(imp.uri.mid(i+1, len-1).toLatin1());
+imp.uri.replace(i, len, QString::fromLatin1(var));
+}
+}
+
 if (imp.type == QDeclarativeScriptParser::Import::File  imp.qualifier.isEmpty()) {
 QString importUrl = unit-imports.baseUrl().resolved(QUrl(imp.uri + QLatin1String(/qmldir))).toString();
 for (int ii = 0; ii  unit-resources.count(); ++ii) {
___
Qt-qml mailing list
Qt-qml@trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-qml


Re: [Qt-qml] env variable expansion in import, useful for styles?

2010-04-26 Thread warwick.allison
 ---Button.qml---
 import $STYLE
 Button{}
 

Environment variables are notoriously cumbersome to set - especially hard if 
you want to set it for already-running processes!

Note that you can achieve styling in multiple ways already, without this, if 
somewhat more cumbersome ways. For example, using Loader based on a single 
string gotten via SQL or XmlListModel.
 
Personally, I hate the particular kind of styling this allows. A UI should be 
designed as a beautiful working whole, not a pile of abstractions that have to 
work regardless of what junk someone else writes later.

Also note that Theming was explicitly discarded as a use case for the 
requirement (see QT-558). By me though, so that's no stronger argument against 
;-)

--
Warwick
Ps. if you must, you'll need ${STYLE:-mydefaultstyle}.

___
Qt-qml mailing list
Qt-qml@trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-qml