File: mainwindow.cpp

package info (click to toggle)
grantlee5 5.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,028 kB
  • sloc: cpp: 24,265; javascript: 6,043; python: 299; sh: 109; ruby: 24; makefile: 15
file content (186 lines) | stat: -rw-r--r-- 5,543 bytes parent folder | download | duplicates (5)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
  This file is part of the Grantlee template system.

  Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either version
  2.1 of the Licence, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "mainwindow.h"

#include <QLabel>
#include <QSplitter>
#include <QTextBrowser>

#include <QDebug>

#include "grantlee_paths.h"
#include "grantlee_templates.h"

#include "designwidget.h"

/**
  Don't escape the code generation output.

  'const QString &' should not become 'const QString &amp;'
*/
class NoEscapeOutputStream : public Grantlee::OutputStream
{
public:
  NoEscapeOutputStream() : Grantlee::OutputStream() {}

  NoEscapeOutputStream(QTextStream *stream) : OutputStream(stream) {}

  virtual QSharedPointer<Grantlee::OutputStream> clone() const
  {
    return QSharedPointer<Grantlee::OutputStream>(new NoEscapeOutputStream);
  }

  virtual QString escape(const QString &input) const { return input; }
};

MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
    : QMainWindow(parent, flags)
{
  QSplitter *splitter = new QSplitter(this);
  splitter->setStretchFactor(1, 5);

  m_designWidget = new DesignWidget(splitter);

  m_tabWidget = new QTabWidget(splitter);
  m_tabWidget->sizePolicy().setHorizontalPolicy(QSizePolicy::Expanding);

  createOutputTab("Output", "Click Generate to create output");

  setCentralWidget(splitter);

  connect(m_designWidget, SIGNAL(generateClicked(bool)),
          SLOT(generateOutput()));

  m_engine = new Grantlee::Engine(this);
  m_engine->setPluginPaths(QStringList() << GRANTLEE_PLUGIN_PATH
                                         << ":/plugins");
  m_engine->addDefaultLibrary("grantlee_scriptabletags");
  m_engine->setSmartTrimEnabled(true);

  m_loader = QSharedPointer<Grantlee::FileSystemTemplateLoader>(
      new Grantlee::FileSystemTemplateLoader);
  m_loader->setTemplateDirs(QStringList() << ":/templates");
  m_engine->addTemplateLoader(m_loader);
  m_engine->addDefaultLibrary("custom_tags");
}

MainWindow::~MainWindow() {}

void MainWindow::generateOutput()
{
  m_tabWidget->clear();

  QString outputType = m_designWidget->outputType();
  m_loader->setTheme(outputType);

  if (outputType == "cpp")
    return generateCpp();

  Grantlee::Template classTemplate
      = m_engine->loadByName("class." + outputType);

  if (classTemplate->error()) {
    createOutputTab("Class", classTemplate->errorString());
    return;
  }

  Grantlee::Context c = m_designWidget->getContext();

  QString output;
  QTextStream textStream(&output);
  NoEscapeOutputStream stream(&textStream);

  classTemplate->render(&stream, &c);
  createOutputTab("Class", classTemplate->error() ? classTemplate->errorString()
                                                  : output);
}

void MainWindow::generateCpp()
{
  Grantlee::Template headerTemplate = m_engine->loadByName("header.h");

  if (headerTemplate->error()) {
    createOutputTab("Header", headerTemplate->errorString());
    return;
  }

  Grantlee::Context c = m_designWidget->getContext();

  QString output;
  QTextStream textStream(&output);
  NoEscapeOutputStream stream(&textStream);

  headerTemplate->render(&stream, &c);
  createOutputTab("Header", headerTemplate->error()
                                ? headerTemplate->errorString()
                                : output);
  if (headerTemplate->error())
    return;

  output.clear();

  Grantlee::Template implementationTemplate
      = m_engine->loadByName("implementation.cpp");

  if (implementationTemplate->error()) {
    createOutputTab("Implementation", implementationTemplate->errorString());
    return;
  }

  implementationTemplate->render(&stream, &c);
  createOutputTab("Implementation", implementationTemplate->error()
                                        ? implementationTemplate->errorString()
                                        : output);
  if (implementationTemplate->error())
    return;
  output.clear();

  if (c.lookup("pimpl").toBool()) {
    Grantlee::Template privateHeaderTemplate
        = m_engine->loadByName("private_header.h");

    if (privateHeaderTemplate->error()) {
      createOutputTab("Private Header", privateHeaderTemplate->errorString());
      return;
    }
    c.insert("className", Grantlee::getSafeString(c.lookup("className"))
                              + QString("Private"));
    c.insert("baseClass", QVariant());
    privateHeaderTemplate->render(&stream, &c);
    createOutputTab("Private Header", privateHeaderTemplate->error()
                                          ? privateHeaderTemplate->errorString()
                                          : output);
    if (privateHeaderTemplate->error())
      return;
  }
}

void MainWindow::createOutputTab(const QString &label, const QString &content)
{
  QTextBrowser *browser = new QTextBrowser(m_tabWidget);
  QFont f;
  f.setFamily("monospace");
  browser->setCurrentFont(f);

  m_tabWidget->addTab(browser, label);

  browser->setPlainText(content);
}