Difference between revisions of "KDevelop5/Manual/Plugins: Code Generation"

Jump to: navigation, search
(Created page with "== How To == {{Construction}} === Files organization === The Code Generation plugin files are located into the [http://techbase.kde.org/KDE_System_Administration/KDE_Filesy...")
(No difference)

Latest revision as of 08:38, 1 October 2017

How To

Under construction.png
Under Construction
This is a new page, currently under construction!

Files organization

The Code Generation plugin files are located into the KDE user data directory of KDevelop.

Each resource has its own folder :

  • codegeneration/
    • templates/
    • contexts/
    • generators /


As Code Generation plugin is very flexible, you do not need to create templates, contexts and generators for simple tasks.

You can edit a template to modify generated source code using the same contexts. If provided data are not enough, modify the context and use your new variables in your template or simply add existing contexts.

Creating your context

The first step is to create a new widget with Qt Designer.

Do not add usual dialog buttons ("Finish", "Cancel", etc.). Your widget will be inserted into a standard wizard.

Creating your context as a simple Widget

Then add a new dynamic property called contextName. Set up the value with the name of your context.

Try to find an unique context name to avoid conflicts.

Seting up the name of your context

It's time to define your data. Add many fields into your form. Use layouts, labels and others setup tools to make your own dialog. They will do not produce any data.

Making your own context

Take care of the fields names. They will be used to generate variables in the template.

Defining variables names

Like previously mentionned, each kind of form field will produce a different data type:

  • name will be a string containing the name of the class
  • baseClasses will be a list of string containing the names of base classes
  • all implements* fields will be boolean

All variables names wil be prefixed by the context name : MyCppClassContext.name, MyCppClassContext.baseClasses, etc.

Now, you can save your context in an UI file named <contextName>.ui:

  • codegeneration/
    • templates/
    • contexts/
      • MyCppClassContext.ui
    • generators/

Using data in your template

Create a simple text file <templateName>.tpl:

  • codegeneration/
    • templates/
      • MyCppClassTemplate.tpl
    • contexts/
      • MyCppClassContext.ui
    • generators/

Use data you have defined into your context :

1 #ifndef {{ MyCppClassContext.name }}
2 #define {{ MyCppClassContext.name }}
4 class {{ MyCppClassContext.name }} : {% for %}{% endfor %}
5 {
6 }
8 #endif // {{ MyCppClassContext.name }}

See also

The KDevelop5/Plugins:_Code_Generation page on Techbase.

This page was last modified on 1 October 2017, at 08:38. Content is available under Creative Commons License SA 4.0 unless otherwise noted.