KDE theme colors API for QML

Денис Купляков dener.kup at gmail.com
Wed Aug 14 20:49:23 BST 2013


Hey all!

I want to implement API to access KDE theme colors from QML to use in
games or other applications. For example it will allow us to implement
KGamePopupItem substitution for QML.

Now developer need to make own custom c++ writen class to access
colors through Q_PROPERTY using. But we can't provide hundreds of
props for every KDE color.

So I decided to make universal API, that will use enums that are
declared at KColorScheme class to access colors.

I have started investigating what we can use for this:

What we have:
1) Q_ENUMS macro that allows to use enumeration in MOS (metaobject
system), can only be used at QObject derived class as other macros. We
can also use it like this:

Class A : QObject {
  Q_OBJECT
public:
  enum TestEnum {
  FOO,
  BAR
  }
}

Class B: QObject {
  Q_OBJECT
  Q_ENUMS(A::TestEnum)
  PROPERTY(A::TestEnum someprop READ ... WRITE ... )
}

It allows to register enum from other QObject derived class at MOS,
but if we get instance of B at QML we have no way to use enum. Also
because of QObject we can't use it because KColorScheme is not QObject
derived.

OK, still if we write

Class A : QObject {
  Q_OBJECT
  Q_ENUMS(TestEnum)
public:
  enum TestEnum {
  FOO,
  BAR
  }
}

we can assign FOO and BAR only to properties of A or other class, we
can't pass it as arguments (so we even can't do smth like
console.log(A.FOO) from QML)

2) Q_INVOKABLE macro that allows to use function in MOS, can only be
used at QObject derived class, at its arguments seems to be only MOS
classes and primitive classes (for example: int, QString, QObject *)

3) Q_DECLARE_METATYPE macro it allows to register class at MOS and use
as functions arguments. Here [1] and here [2] we see that people say
it is applicable for enums.

So there is a lot of limitations, and I have tried two approaches:
FIRST) we are creating uncreatable (I usually say static class) (if
smth go wrong can be creatable)  ColorScheme (with
qmlRegisterUncreatableType function) that will have functions like:

Q_INVOKABLE QColor background(KColorScheme::
ColorSet,
KColorScheme::BackgroundRole, QPalette::ColorGroup)

SECOND) we are creating ColorToken class that will have propeties like:
colorSet, backgroundRole, colorGroup (it is arguments READ WRITE access)
backgroundColor (it is what we need it has only READ access)

Now I can't do normally nor first neither second, because I can't
access KColorScheme enums as it is not QObject derived... And making
copy of all of them will be hardly to maintain I think. Previously I
was thinking that such thing will make a trick:

class ColorScheme: public QObject, public KColorScheme {
 Q_OBEJECT
 Q_ENUMS(....)
}

but was disappointed looking at moc file, it don't see enums.

Also even making KColorScheme Q_OBJECT derived don't gives ability to
use enums as arguments for functions. So we have only SECOND approach.

We simply doing all our stuff by directly changing
KColorScheme class: adding Q_OBJECT and Q_ENUMS statements to it, than
going by SECOND approach. But its also bad because of binary
compatibility of libraries, as was discussed at another maillinglist
[3].

So I have found about Q_GADGET [4], can we use it to expose
KColorScheme enums instead of Q_OBJECT. What are you think about all
of this?

[1] http://qt-project.org/forums/viewreply/39427/
[2] http://qt-project.org/forums/viewthread/10308/
[3] http://lists.kde.org/?t=137573624500003&r=1&w=2
[4] http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqt4/qobject.html
search for Q_GADGET

Best regards,
Denis Kuplyakov




More information about the kde-core-devel mailing list