KDevelop4/Manual/Plugins: Code Generation

Jump to: navigation, search

Contents

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 /

Flexibility

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.

Dialog-information.png
 
Information
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.

Ktip.png
 
Tip
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 }}
  3.  
  4. class {{ MyCppClassContext.name }} : {% for %}{% endfor %}
  5. {
  6. }
  7.  
  8. #endif // {{ MyCppClassContext.name }}

See also

The KDevelop4/Plugins:_Code_Generation page on Techbase.


This page was last modified on 17 August 2012, at 20:11. This page has been accessed 6,775 times. Content is available under Creative Commons License SA 3.0 and the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal