File indexing completed on 2024-12-22 03:46:49

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 
0008 */
0009 
0010 #include "mainwindow.h"
0011 
0012 #include <QLabel>
0013 #include <QSplitter>
0014 #include <QTextBrowser>
0015 
0016 #include <QDebug>
0017 
0018 #include <KTextTemplate/Context>
0019 #include <KTextTemplate/Engine>
0020 #include <KTextTemplate/FileSystemTemplateLoader>
0021 #include <KTextTemplate/Util>
0022 
0023 #include "ktexttemplate_paths.h"
0024 
0025 #include "designwidget.h"
0026 
0027 /**
0028   Don't escape the code generation output.
0029 
0030   'const QString &' should not become 'const QString &amp;'
0031 */
0032 class NoEscapeOutputStream : public KTextTemplate::OutputStream
0033 {
0034 public:
0035     NoEscapeOutputStream()
0036         : KTextTemplate::OutputStream()
0037     {
0038     }
0039 
0040     NoEscapeOutputStream(QTextStream *stream)
0041         : OutputStream(stream)
0042     {
0043     }
0044 
0045     virtual QSharedPointer<KTextTemplate::OutputStream> clone() const
0046     {
0047         return QSharedPointer<KTextTemplate::OutputStream>(new NoEscapeOutputStream);
0048     }
0049 
0050     virtual QString escape(const QString &input) const
0051     {
0052         return input;
0053     }
0054 };
0055 
0056 MainWindow::MainWindow(QWidget *parent)
0057     : QMainWindow(parent)
0058 {
0059     QSplitter *splitter = new QSplitter(this);
0060     splitter->setStretchFactor(1, 5);
0061 
0062     m_designWidget = new DesignWidget(splitter);
0063 
0064     m_tabWidget = new QTabWidget(splitter);
0065     m_tabWidget->sizePolicy().setHorizontalPolicy(QSizePolicy::Expanding);
0066 
0067     createOutputTab("Output", "Click Generate to create output");
0068 
0069     setCentralWidget(splitter);
0070 
0071     connect(m_designWidget, SIGNAL(generateClicked(bool)), SLOT(generateOutput()));
0072 
0073     m_engine = new KTextTemplate::Engine(this);
0074     m_engine->setPluginPaths(QStringList() << KTEXTTEMPLATE_PLUGIN_PATH << ":/plugins");
0075     m_engine->addDefaultLibrary("ktexttemplate_scriptabletags");
0076     m_engine->setSmartTrimEnabled(true);
0077 
0078     m_loader = QSharedPointer<KTextTemplate::FileSystemTemplateLoader>(new KTextTemplate::FileSystemTemplateLoader);
0079     m_loader->setTemplateDirs(QStringList() << ":/templates");
0080     m_engine->addTemplateLoader(m_loader);
0081     m_engine->addDefaultLibrary("custom_tags");
0082 }
0083 
0084 MainWindow::~MainWindow()
0085 {
0086 }
0087 
0088 void MainWindow::generateOutput()
0089 {
0090     m_tabWidget->clear();
0091 
0092     QString outputType = m_designWidget->outputType();
0093     m_loader->setTheme(outputType);
0094 
0095     if (outputType == "cpp")
0096         return generateCpp();
0097 
0098     KTextTemplate::Template classTemplate = m_engine->loadByName("class." + outputType);
0099 
0100     if (classTemplate->error()) {
0101         createOutputTab("Class", classTemplate->errorString());
0102         return;
0103     }
0104 
0105     KTextTemplate::Context c = m_designWidget->getContext();
0106 
0107     QString output;
0108     QTextStream textStream(&output);
0109     NoEscapeOutputStream stream(&textStream);
0110 
0111     classTemplate->render(&stream, &c);
0112     createOutputTab("Class", classTemplate->error() ? classTemplate->errorString() : output);
0113 }
0114 
0115 void MainWindow::generateCpp()
0116 {
0117     KTextTemplate::Template headerTemplate = m_engine->loadByName("header.h");
0118 
0119     if (headerTemplate->error()) {
0120         createOutputTab("Header", headerTemplate->errorString());
0121         return;
0122     }
0123 
0124     KTextTemplate::Context c = m_designWidget->getContext();
0125 
0126     QString output;
0127     QTextStream textStream(&output);
0128     NoEscapeOutputStream stream(&textStream);
0129 
0130     headerTemplate->render(&stream, &c);
0131     createOutputTab("Header", headerTemplate->error() ? headerTemplate->errorString() : output);
0132     if (headerTemplate->error())
0133         return;
0134 
0135     output.clear();
0136 
0137     KTextTemplate::Template implementationTemplate = m_engine->loadByName("implementation.cpp");
0138 
0139     if (implementationTemplate->error()) {
0140         createOutputTab("Implementation", implementationTemplate->errorString());
0141         return;
0142     }
0143 
0144     implementationTemplate->render(&stream, &c);
0145     createOutputTab("Implementation", implementationTemplate->error() ? implementationTemplate->errorString() : output);
0146     if (implementationTemplate->error())
0147         return;
0148     output.clear();
0149 
0150     if (c.lookup("pimpl").toBool()) {
0151         KTextTemplate::Template privateHeaderTemplate = m_engine->loadByName("private_header.h");
0152 
0153         if (privateHeaderTemplate->error()) {
0154             createOutputTab("Private Header", privateHeaderTemplate->errorString());
0155             return;
0156         }
0157         c.insert("className", KTextTemplate::getSafeString(c.lookup("className")) + QString("Private"));
0158         c.insert("baseClass", QVariant());
0159         privateHeaderTemplate->render(&stream, &c);
0160         createOutputTab("Private Header", privateHeaderTemplate->error() ? privateHeaderTemplate->errorString() : output);
0161         if (privateHeaderTemplate->error())
0162             return;
0163     }
0164 }
0165 
0166 void MainWindow::createOutputTab(const QString &label, const QString &content)
0167 {
0168     QTextBrowser *browser = new QTextBrowser(m_tabWidget);
0169     QFont f;
0170     f.setFamily("monospace");
0171     browser->setCurrentFont(f);
0172 
0173     m_tabWidget->addTab(browser, label);
0174 
0175     browser->setPlainText(content);
0176 }
0177 
0178 #include "moc_mainwindow.cpp"