| 1 | [](https://travis-ci.org/agateau/qpropgen) |
| 2 | |
| 3 | # qpropgen |
| 4 | |
| 5 | A tool to generate QML-friendly QObject-based C++ classes from class definition |
| 6 | files. |
| 7 | |
| 8 | Install it with: |
| 9 | |
| 10 | pip install qpropgen |
| 11 | |
| 12 | ## Quick Intro |
| 13 | |
| 14 | Declaring properties in a QObject class requires writing a lot of boilerplate |
| 15 | code. qpropgen goal is to write this boilerplate for you. |
| 16 | |
| 17 | Suppose we want to create a `Person` class, with `firstName`, `lastName` and |
| 18 | `birthDate` properties. |
| 19 | |
| 20 | First we create a class definition file named `person.yaml` with the following |
| 21 | content: |
| 22 | |
| 23 | ```yaml |
| 24 | class: Person |
| 25 | properties: |
| 26 | - name: firstName |
| 27 | type: QString |
| 28 | - name: lastName |
| 29 | type: QString |
| 30 | - name: birthDate |
| 31 | type: QDateTime |
| 32 | ``` |
| 33 | |
| 34 | Next, we generate its header and implementation with `qpropgen person.yaml`. |
| 35 | This produces two files: `person.h` and `person.cpp` (The filenames are based |
| 36 | on the filename of the class definition). |
| 37 | |
| 38 | Note: in practice, you probably want to inherit from the generated classes to |
| 39 | implement other aspects of the class to create and/or to override getters and |
| 40 | setters. |
| 41 | |
| 42 | ## Syntax of class definition files |
| 43 | |
| 44 | A class definition file **must** contain the following fields: |
| 45 | |
| 46 | - `class`: the name of the class to generate. |
| 47 | |
| 48 | - `properties`: the list of its properties (see below). |
| 49 | |
| 50 | It **may** also contain the following fields: |
| 51 | |
| 52 | - `includes`: a list of files to include in the header. |
| 53 | |
| 54 | - `baseClass`: name of the class to inherit from. Defaults to `QObject`. |
| 55 | |
| 56 | - `defaults`: default values for some property attributes (see below). |
| 57 | |
| 58 | ### The `properties` field |
| 59 | |
| 60 | `properties` is an array of property definitions. |
| 61 | |
| 62 | A property definition **must** contain the following fields: |
| 63 | |
| 64 | - `type` |
| 65 | - `name` |
| 66 | |
| 67 | It **may** contain the following fields: |
| 68 | |
| 69 | - `access`: Can be `private` or `protected`. Defines the access modifier for |
| 70 | the generated member variables. Defaults to `private`. |
| 71 | |
| 72 | - `mutability`: One of `constant`, `readonly`, `readwrite`. Defaults to |
| 73 | `readwrite`. |
| 74 | |
| 75 | - `argType`: The type of the setter argument. If not set qpropgen uses const |
| 76 | references for types which are not pointers and not known scalars (int, bool, |
| 77 | qreal). |
| 78 | |
| 79 | - `varName`: Name of the variable backing the property. Defaults to `m<Name>`, |
| 80 | so the variable of the `foo` property will be `mFoo`. |
| 81 | |
| 82 | - `setterName`: Name of the setter. Defaults to `set<Name>`, so the setter of |
| 83 | the `foo` property will be `setFoo`. |
| 84 | |
| 85 | - `impl`: One of `plain` (getter and setter), `virtual` (virtual getter and |
| 86 | setter) or `pure` (virtual pure getter and setter). |
| 87 | |
| 88 | - `value`: The default value of the property. |
| 89 | |
| 90 | ### The `defaults` field |
| 91 | |
| 92 | Adding a field to the `defaults` object lets you define default values for all |
| 93 | properties. |
| 94 | |
| 95 | For example you can define that all properties are of type `qreal` by default |
| 96 | with: |
| 97 | |
| 98 | ```yaml |
| 99 | defaults: |
| 100 | type: qreal |
| 101 | ``` |
| 102 | |
| 103 | Of course fields which require a unique value, like `name`, should not have a |
| 104 | default. |
| 105 | |
| 106 | ## Build system integration |
| 107 | |
| 108 | The `cmake/qpropgen.cmake` can be included in your project to integrate |
| 109 | qpropgen. It takes care of finding the `qpropgen` executable and provides a |
| 110 | `qpropgen()` CMake function. |
| 111 | |
| 112 | This CMake function lets you define .yaml files to process. For example: |
| 113 | |
| 114 | |
| 115 | ```cmake |
| 116 | set(prj_SRCS main.cpp) |
| 117 | qpropgen(prj_QPROPGEN foo.yaml bar.yaml) |
| 118 | add_executable(prj ${prj_SRCS} ${prj_QPROPGEN}) |
| 119 | ``` |
| 120 | |
| 121 | ## Examples |
| 122 | |
| 123 | The `examples/` directory contains examples of the various settings. The |
| 124 | produced executable does nothing, but you can look in the build directory at |
| 125 | the .h and .cpp files produced by qpropgen during the build. |
| 126 | |
| 127 | ## Tests |
| 128 | |
| 129 | The project currently lacks real unit tests, so the examples serve as tests: |
| 130 | the `./tests.sh` script can be run to build the examples. |
| 131 | |
| 132 | ## Trivia |
| 133 | |
| 134 | I started this project when I was working on the [SFXR-Qt][] sound generator (a |
| 135 | QtQuick port of [SFXR][]), and was finding it too tedious to declare all the |
| 136 | properties necessary to represent sounds :) |
| 137 | |
| 138 | [SFXR-Qt]: https://github.com/agateau/sfxr-qt |
| 139 | [SFXR]: http://www.drpetter.se/project_sfxr.html |