File: templatepreview.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (136 lines) | stat: -rw-r--r-- 5,360 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
    SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#include "templatepreview.h"

#include <language/codegen/templaterenderer.h>
#include <language/codegen/codedescription.h>

#include <QDir>
#include <QVBoxLayout>

#include <KTextEditor/Editor>
#include <KTextEditor/View>
#include <KTextEditor/Document>

#include <KLocalizedString>
#include <KMacroExpander>

using namespace KDevelop;

TemplatePreviewRenderer::TemplatePreviewRenderer()
{
    QVariantHash vars;
    vars[QStringLiteral("name")] = QStringLiteral("Example");
    vars[QStringLiteral("license")] = QStringLiteral("This file is licensed under the ExampleLicense 3.0");
    // TODO: More variables, preferably the ones from TemplateClassGenerator

    VariableDescriptionList publicMembers;
    VariableDescriptionList protectedMembers;
    VariableDescriptionList privateMembers;
    publicMembers    << VariableDescription(QStringLiteral("int"),    QStringLiteral("number"));
    protectedMembers << VariableDescription(QStringLiteral("string"), QStringLiteral("name"));
    privateMembers   << VariableDescription(QStringLiteral("float"),  QStringLiteral("variable"));
    vars[QStringLiteral("members")] = CodeDescription::toVariantList(publicMembers + protectedMembers + privateMembers);
    vars[QStringLiteral("public_members")]    = CodeDescription::toVariantList(publicMembers);
    vars[QStringLiteral("protected_members")] = CodeDescription::toVariantList(protectedMembers);
    vars[QStringLiteral("private_members")]   = CodeDescription::toVariantList(privateMembers);

    FunctionDescriptionList publicFunctions;
    FunctionDescriptionList protectedFunctions;
    FunctionDescriptionList privateFunctions;

    FunctionDescription complexFunction(QStringLiteral("doBar"), VariableDescriptionList(), VariableDescriptionList());
    complexFunction.arguments << VariableDescription(QStringLiteral("bool"), QStringLiteral("really"));
    complexFunction.arguments << VariableDescription(QStringLiteral("int"), QStringLiteral("howMuch"));
    complexFunction.returnArguments << VariableDescription(QStringLiteral("double"), QString());

    publicFunctions << FunctionDescription(QStringLiteral("doFoo"), VariableDescriptionList(), VariableDescriptionList());
    publicFunctions << complexFunction;
    protectedFunctions << FunctionDescription(QStringLiteral("onUpdate"), VariableDescriptionList(), VariableDescriptionList());

    vars[QStringLiteral("functions")] = CodeDescription::toVariantList(publicFunctions + protectedFunctions + privateFunctions);
    vars[QStringLiteral("public_functions")]    = CodeDescription::toVariantList(publicFunctions);
    vars[QStringLiteral("protected_functions")] = CodeDescription::toVariantList(protectedFunctions);
    vars[QStringLiteral("private_functions")]   = CodeDescription::toVariantList(privateFunctions);

    vars[QStringLiteral("testCases")]  = QStringList {
        QStringLiteral("doFoo"),
        QStringLiteral("doBar"),
        QStringLiteral("doMore")
    };

    addVariables(vars);
}

TemplatePreviewRenderer::~TemplatePreviewRenderer()
{

}

TemplatePreview::TemplatePreview(QWidget* parent)
    : QWidget(parent)
{
    m_variables[QStringLiteral("APPNAME")] = QStringLiteral("Example");
    m_variables[QStringLiteral("APPNAMELC")] = QStringLiteral("example");
    m_variables[QStringLiteral("APPNAMEUC")] = QStringLiteral("EXAMPLE");
    m_variables[QStringLiteral("APPNAMEID")] = QStringLiteral("Example");

    m_variables[QStringLiteral("PROJECTDIR")] = QDir::homePath() + QLatin1String("/projects/ExampleProjectDir");
    m_variables[QStringLiteral("PROJECTDIRNAME")] = QStringLiteral("ExampleProjectDir");
    m_variables[QStringLiteral("VERSIONCONTROLPLUGIN")] = QStringLiteral("kdevgit");

    KTextEditor::Document* doc = KTextEditor::Editor::instance()->createDocument(this);
    m_preview.reset(doc);
    m_preview->setReadWrite(false);

    auto* layout = new QVBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);
    m_view = m_preview->createView(this);
    m_view->setStatusBarEnabled(false);
    m_view->setConfigValue(QStringLiteral("icon-bar"), false);
    m_view->setConfigValue(QStringLiteral("folding-bar"), false);
    m_view->setConfigValue(QStringLiteral("line-numbers"), false);
    m_view->setConfigValue(QStringLiteral("dynamic-word-wrap"), true);
    layout->addWidget(m_view);
}

TemplatePreview::~TemplatePreview()
{

}

QString TemplatePreview::setText(const QString& text, bool isProject, TemplateRenderer::EmptyLinesPolicy policy)
{
    QString rendered;
    QString errorString;

    if (!text.isEmpty()) {
        if (isProject) {
            rendered = KMacroExpander::expandMacros(text, m_variables);
        } else {
            TemplatePreviewRenderer renderer;
            renderer.setEmptyLinesPolicy(policy);
            rendered = renderer.render(text);
            errorString = renderer.errorString();
        }
    }

    m_preview->setReadWrite(true);
    m_preview->setText(rendered);
    m_view->setCursorPosition(KTextEditor::Cursor(0, 0));
    m_preview->setReadWrite(false);

    return errorString;
}

KTextEditor::Document* TemplatePreview::document() const
{
    return m_preview.data();
}

#include "moc_templatepreview.cpp"