KDevelop5/Manual/Code generation with templates/da: Difference between revisions

From KDE UserBase Wiki
(Importing a new version from external source)
(Importing a new version from external source)
Line 22: Line 22:
[[Image:kdevelop-template-members.png|thumb|500px|center]]
[[Image:kdevelop-template-members.png|thumb|500px|center]]


In the following pages, you can choose a license for you new class, set any custom options required by the selected template, and configure output locations for all the generated files. By clicking <menuchoice>Finish</menuchoice>, you complete the assistant and create the new class. The generated files will be opened in the editor, so you can start adding code right away.  
I de følgende vinduer kan du vælge licensen for din nye klasse, angive særlige oplysninger, som den valgte skabelon skal bruge og angive, hvor de genererede filer skal placeres. Du afslutter guiden ved at klikke <menuchoice>Afslut</menuchoice>, hvorefter den nye klasse genereres. De genererede filer vil blive åbnet i editoren, sådan at du kan begynde at arbejde med dem med det samme.  


After creating a new C++ class, you will be given an option of adding the class to a project target. Choose a target from the dialog page, or dismiss the page and add the files to a target manually.  
After creating a new C++ class, you will be given an option of adding the class to a project target. Choose a target from the dialog page, or dismiss the page and add the files to a target manually.  

Revision as of 08:03, 16 September 2017

Other languages:

Under Construction

This is a new page, currently under construction!


Kodegenering med skabeloner

KDevelop bruger skabeloner til at generere kildekodefiler og for at undgå at skrive kode som gentages.

Opret en ny klasse

Den mest almindelige anvendelse af kodegenerering er formentlig at skrive en ny klasse. For at oprettet en ny klasse i et eksisterende projekt skal du højreklikke på projektmappen og vælge Opret fra skabelon. Den samme dialog kan startes fra menuen ved at vælge Fil -> Ny fra skabelon, men at bruge en projektmappe har den fordel, at basis-URL'en bliver sat for outputfilen. Vælg Klasse i afsnittet kategori og det sprog og den skabelon der ønskes i de andre to felter. Efter at du har valgt en klasseskabelon skal du angive detaljer om den nye klasse.

Først skal du angive en identifikator for klassen. Dette kan være en simpelt navn (som Bus) eller en komplet identifikator men navnerum (som Transportation::Bus). I det sidste tilfælde vil KDevelop fortolke identifikatoren og separere navnerummet fra det egentlige navn. I det samme vindue kan du tilføje den nye klasses basisklasser. Du bemærker måske, at nogle skabeloner selv vælger en basisklasse, men du kan frit fjerne den og du kan tilføje andre basisklasser. Du bør skrive hele nedarvningserklæringen her, hvilket afhænger af sproget, fx public QObject for C++, extends SomeClass for PHP og blot klassens navn for Python.

I det næste vindue kan du vælge bland de virtuelle metoder fra alle nedarvede klasser samt nogle standard konstruktorer, destruktorer og operatorer. Markér afkrydsningsfeltet vedsiden af en metodesignatur for at implementere denne metode i den nye klasse.

Når du klikker på Næste kommer du til et vindue, hvor du kan tilføje medlemmer til klassen. Afhængigt af den valgte skabelon vil de optræde som medlemsvariable i den nye klasse eller skabelonen vil oprette settere og gettere for dem. I sprog som C++, hvor variable skal erklæres skal du angive både type og navn for medlemmet som fx int number eller QString name. I andre sprog behøver du ikke at angive typen, men det er god praksis at have den med alligevel, for den valgte skabelon kan stadig drage nytte af det.

I de følgende vinduer kan du vælge licensen for din nye klasse, angive særlige oplysninger, som den valgte skabelon skal bruge og angive, hvor de genererede filer skal placeres. Du afslutter guiden ved at klikke Afslut, hvorefter den nye klasse genereres. De genererede filer vil blive åbnet i editoren, sådan at du kan begynde at arbejde med dem med det samme.

After creating a new C++ class, you will be given an option of adding the class to a project target. Choose a target from the dialog page, or dismiss the page and add the files to a target manually.

If you chose the Qt Object template, checked some of the default methods, and added two member variables, the output should look like on the following picture.

You can see that data members are converted into Qt properties, with accessor functions and the Q_PROPERTY macros. Arguments to setter functions are even passed by const-reference, where appropriate. Additionally, a private class is declared, and a private pointer created with Q_DECLARE_PRIVATE. All this is done by the template, choosing a different template in the first step could completely change the output.

Creating a new unit test

Even though most testing frameworks require each test to also be a class, KDevelop includes a method to simplify the creation of unit tests. To create a new test, right click on a project folder and choose Create from Template. In the template selection page, choose Test as the category, then choose your programming language and template and click Next.

You will be prompted for the test name and a list of test cases. For the test cases, you only have to specify a list of names. Some unit testing frameworks, such as PyUnit and PHPUnit, require that test cases start with a special prefix. In KDevelop, the template is responsible for adding the prefix, so you do not have to prefix the test cases here. After clicking Next, specify the license and output locations for the generated files, and the test will be created.

Unit tests created this way will not be added to any target automatically. If you are using CTest or some other testing framework, make sure to add the new files to a target. KDevelop provides a Unit Tests toolview that integrates with CTest.

Other files

While classes and unit tests receive special attention when generating code from templates, the same method can be used for any kind of source code files. For example, one could use a template for a CMake Find module or a .desktop file. This can be done by choosing Create from Template, and selecting the wanted category and template. If the selected category is neither Class nor Test, you will only have the option of choosing the license, any custom options specified by the template, and the output file locations. As with classes and tests, finishing the assistant will generate the files and open them in the editor.

Managing templates

From the File -> New from Template assistant, you can also download additional file templates by clicking the Get more Templates... button. This opens a Get Hot New Stuff dialog, where you can install additional templates, as well as update or remove them. There is also a configuration module for template, which can be reached by clicking Settings -> Configure KDevelop -> Templates. From there, you can manage both file templates (explained above) and project templates (used for creating new projects).

Of course, if none of the available template suit your project, you can always create new ones. The easiest way is probably to copy and modify an existing template, while a short tutorial and a longer specification document are there to help you. To copy an installed template, open the template manager by clicking Settings -> Configure KDevelop... -> Templates, select the template you wish to copy, then click the Extract Template button. Select a destination folder, then click OK, and the contents of the template will be extracted into the selected folder. Now you can edit the template by opening the extracted files and modifying them. After you are done, you can import your new template into KDevelop by opening the template manager, activating the appropriate tab (either Project Templates or File Templates) and clicking Load Template. Open the template description file, which is the one with the suffix either .kdevtemplate or .desktop. KDevelop will compress the files into a template archive and import the template.

Note

When copying an existing template, make sure you rename it before importing it again. Otherwise, you will either overwrite the old template, or will end up with two templates with identical names. To rename a template, rename the description file to something unique (but keep the suffix), and change the Name entry in the description file.


If you want to write a template from scratch, you can start with a sample C++ class template by creating a new project and selecting the C++ Class Template project in category KDevelop.