File indexing completed on 2024-05-05 16:45:37
0001 /* 0002 SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "templatepreview.h" 0008 0009 #include <language/codegen/templaterenderer.h> 0010 #include <language/codegen/codedescription.h> 0011 0012 #include <QDir> 0013 #include <QVBoxLayout> 0014 0015 #include <KTextEditor/Editor> 0016 #include <KTextEditor/View> 0017 #include <KTextEditor/ConfigInterface> 0018 #include <KTextEditor/Document> 0019 0020 #include <KLocalizedString> 0021 #include <KMacroExpander> 0022 0023 using namespace KDevelop; 0024 0025 TemplatePreviewRenderer::TemplatePreviewRenderer() 0026 { 0027 QVariantHash vars; 0028 vars[QStringLiteral("name")] = QStringLiteral("Example"); 0029 vars[QStringLiteral("license")] = QStringLiteral("This file is licensed under the ExampleLicense 3.0"); 0030 // TODO: More variables, preferably the ones from TemplateClassGenerator 0031 0032 VariableDescriptionList publicMembers; 0033 VariableDescriptionList protectedMembers; 0034 VariableDescriptionList privateMembers; 0035 publicMembers << VariableDescription(QStringLiteral("int"), QStringLiteral("number")); 0036 protectedMembers << VariableDescription(QStringLiteral("string"), QStringLiteral("name")); 0037 privateMembers << VariableDescription(QStringLiteral("float"), QStringLiteral("variable")); 0038 vars[QStringLiteral("members")] = CodeDescription::toVariantList(publicMembers + protectedMembers + privateMembers); 0039 vars[QStringLiteral("public_members")] = CodeDescription::toVariantList(publicMembers); 0040 vars[QStringLiteral("protected_members")] = CodeDescription::toVariantList(protectedMembers); 0041 vars[QStringLiteral("private_members")] = CodeDescription::toVariantList(privateMembers); 0042 0043 FunctionDescriptionList publicFunctions; 0044 FunctionDescriptionList protectedFunctions; 0045 FunctionDescriptionList privateFunctions; 0046 0047 FunctionDescription complexFunction(QStringLiteral("doBar"), VariableDescriptionList(), VariableDescriptionList()); 0048 complexFunction.arguments << VariableDescription(QStringLiteral("bool"), QStringLiteral("really")); 0049 complexFunction.arguments << VariableDescription(QStringLiteral("int"), QStringLiteral("howMuch")); 0050 complexFunction.returnArguments << VariableDescription(QStringLiteral("double"), QString()); 0051 0052 publicFunctions << FunctionDescription(QStringLiteral("doFoo"), VariableDescriptionList(), VariableDescriptionList()); 0053 publicFunctions << complexFunction; 0054 protectedFunctions << FunctionDescription(QStringLiteral("onUpdate"), VariableDescriptionList(), VariableDescriptionList()); 0055 0056 vars[QStringLiteral("functions")] = CodeDescription::toVariantList(publicFunctions + protectedFunctions + privateFunctions); 0057 vars[QStringLiteral("public_functions")] = CodeDescription::toVariantList(publicFunctions); 0058 vars[QStringLiteral("protected_functions")] = CodeDescription::toVariantList(protectedFunctions); 0059 vars[QStringLiteral("private_functions")] = CodeDescription::toVariantList(privateFunctions); 0060 0061 vars[QStringLiteral("testCases")] = QStringList { 0062 QStringLiteral("doFoo"), 0063 QStringLiteral("doBar"), 0064 QStringLiteral("doMore") 0065 }; 0066 0067 addVariables(vars); 0068 } 0069 0070 TemplatePreviewRenderer::~TemplatePreviewRenderer() 0071 { 0072 0073 } 0074 0075 TemplatePreview::TemplatePreview(QWidget* parent) 0076 : QWidget(parent) 0077 { 0078 m_variables[QStringLiteral("APPNAME")] = QStringLiteral("Example"); 0079 m_variables[QStringLiteral("APPNAMELC")] = QStringLiteral("example"); 0080 m_variables[QStringLiteral("APPNAMEUC")] = QStringLiteral("EXAMPLE"); 0081 m_variables[QStringLiteral("APPNAMEID")] = QStringLiteral("Example"); 0082 0083 m_variables[QStringLiteral("PROJECTDIR")] = QDir::homePath() + QLatin1String("/projects/ExampleProjectDir"); 0084 m_variables[QStringLiteral("PROJECTDIRNAME")] = QStringLiteral("ExampleProjectDir"); 0085 m_variables[QStringLiteral("VERSIONCONTROLPLUGIN")] = QStringLiteral("kdevgit"); 0086 0087 KTextEditor::Document* doc = KTextEditor::Editor::instance()->createDocument(this); 0088 m_preview.reset(doc); 0089 m_preview->setReadWrite(false); 0090 0091 auto* layout = new QVBoxLayout; 0092 layout->setContentsMargins(0, 0, 0, 0); 0093 setLayout(layout); 0094 m_view = m_preview->createView(this); 0095 m_view->setStatusBarEnabled(false); 0096 if (KTextEditor::ConfigInterface* config = qobject_cast<KTextEditor::ConfigInterface*>(m_view)) { 0097 config->setConfigValue(QStringLiteral("icon-bar"), false); 0098 config->setConfigValue(QStringLiteral("folding-bar"), false); 0099 config->setConfigValue(QStringLiteral("line-numbers"), false); 0100 config->setConfigValue(QStringLiteral("dynamic-word-wrap"), true); 0101 } 0102 layout->addWidget(m_view); 0103 } 0104 0105 TemplatePreview::~TemplatePreview() 0106 { 0107 0108 } 0109 0110 QString TemplatePreview::setText(const QString& text, bool isProject, TemplateRenderer::EmptyLinesPolicy policy) 0111 { 0112 QString rendered; 0113 QString errorString; 0114 0115 if (!text.isEmpty()) { 0116 if (isProject) { 0117 rendered = KMacroExpander::expandMacros(text, m_variables); 0118 } else { 0119 TemplatePreviewRenderer renderer; 0120 renderer.setEmptyLinesPolicy(policy); 0121 rendered = renderer.render(text); 0122 errorString = renderer.errorString(); 0123 } 0124 } 0125 0126 m_preview->setReadWrite(true); 0127 m_preview->setText(rendered); 0128 m_view->setCursorPosition(KTextEditor::Cursor(0, 0)); 0129 m_preview->setReadWrite(false); 0130 0131 return errorString; 0132 } 0133 0134 KTextEditor::Document* TemplatePreview::document() const 0135 { 0136 return m_preview.data(); 0137 } 0138 0139 #include "moc_templatepreview.cpp"