Example #1
0
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;
  }
}
Example #2
0
QString TemplateEngine::render(const string& file) {
  Grantlee::Template t = m_engine->loadByName(QString::fromStdString(file));

  if (!t) {
    qDebug() << "Unable to load template"
        << QString("Error loading template: %1").arg(QString::fromStdString(file));
    return QString();
  }

  if (t->error()) {
    qDebug() << "Unable to load template"
        << QString("Error loading template: %1").arg(t->errorString());
    return QString();
  }

  QString content = t->render(&c);

  if (t->error()) {
    qDebug() << "Unable render template"
        << QString("Error rendering template: %1").arg(t->errorString());
    return QString();
  }
  content.replace("\n\n", "\n");

  LogDebug << content.toStdString();

  return content;
}
Example #3
0
void BookWindow::renderBooks() const
{
    int rows = model->rowCount();
    QVariantHash mapping;
    QVariantList bookList;
    for (int row = 0; row < rows; ++row)
    {
      QString title = model->index(row, 1).data().toString();
      QString author = model->index(row, 2).data().toString();
      QString genre = model->index(row, 3).data().toString();
      int rating = model->index(row, 5).data().toInt();
      QObject *book = new BookWrapper(author, title, genre, rating, this);
      QVariant var = QVariant::fromValue(book);
      bookList.append(var);
    }
    mapping.insert("books", bookList);

    QString themeName = ui.exportTheme->currentText();

    Grantlee::Context c(mapping);

    Grantlee::Template t = m_engine->loadByName( themeName + ".html" );
    if (!t)
    {
      QMessageBox::critical(this, "Unable to load template",
                QString( "Error loading template: %1" ).arg( themeName + ".html" ) );
      return;
    }

    if ( t->error() )
    {
      QMessageBox::critical(this, "Unable to load template",
                QString( "Error loading template: %1" ).arg( t->errorString() ) );
      return;
    }

    bool ok;
    QString text = QInputDialog::getText(this, tr("Export Location"),
                                         tr("file name:"), QLineEdit::Normal,
                                         QDir::home().absolutePath() + "/book_export.html", &ok);
    if (!ok || text.isEmpty())
      return;

    QFile file( text );
    if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
      return;

    QString content = t->render(&c);

    if ( t->error() )
    {
      QMessageBox::critical(this, "Unable render template",
                QString( "Error rendering template: %1" ).arg( t->errorString() ) );
      return;
    }

    file.write(content.toLocal8Bit());
    file.close();

}
Example #4
0
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);
}
QString TemplateLayout::generate()
{
	int progress = 0;
	int totalWork = getTotalWork(PrintOptions);
	QString templateName;

	QString htmlContent;
	m_engine = new Grantlee::Engine(this);

	QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader =
		QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
	m_templateLoader->setTemplateDirs(QStringList() << getSubsurfaceDataPath("printing_templates"));
	m_engine->addTemplateLoader(m_templateLoader);

	Grantlee::registerMetaType<Dive>();
	Grantlee::registerMetaType<template_options>();
	Grantlee::registerMetaType<print_options>();

	QVariantHash mapping;
	QVariantList diveList;

	struct dive *dive;
	int i;
	for_each_dive (i, dive) {
		//TODO check for exporting selected dives only
		if (!dive->selected && PrintOptions->print_selected)
			continue;
		Dive d(dive);
		diveList.append(QVariant::fromValue(d));
		progress++;
		emit progressUpdated(progress * 100.0 / totalWork);
	}
	mapping.insert("dives", diveList);
	mapping.insert("template_options", QVariant::fromValue(*templateOptions));
	mapping.insert("print_options", QVariant::fromValue(*PrintOptions));

	Grantlee::Context c(mapping);

	if (PrintOptions->p_template == print_options::ONE_DIVE) {
		templateName = "one_dive.html";
	} else if (PrintOptions->p_template == print_options::TWO_DIVE) {
		templateName = "two_dives.html";
	} else if (PrintOptions->p_template == print_options::CUSTOM) {
		templateName = "custom.html";
	}
	Grantlee::Template t = m_engine->loadByName(templateName);
	if (!t || t->error()) {
		qDebug() << "Can't load template";
		return htmlContent;
	}

	htmlContent = t->render(&c);

	if (t->error()) {
		qDebug() << "Can't render template";
		return htmlContent;
	}
	return htmlContent;
}
void PublicServerSystem::Web::View::render(const QString &templateName, QTextStream &stream, Grantlee::Engine *templateEngine, Grantlee::Context *requestContext)
{
   Grantlee::OutputStream out(&stream);

   Grantlee::Template tem = templateEngine->loadByName(templateName);

   if ( tem->error() != Grantlee::NoError ) {
       qDebug() << tem->errorString();
   }

   tem->render(&out, requestContext);
}
Example #7
0
QString ApplicationPGPKeyFormatter::render(const PgpKeyMessagePart &mp) const
{
    GrantleeTheme::Engine engine;
    engine.localizer()->setApplicationDomain(QByteArrayLiteral("messageviewer_application_gnupgwks_plugin"));
    engine.addTemplateLoader(QSharedPointer<GrantleeTheme::QtResourceTemplateLoader>::create());

    Grantlee::Template tpl = engine.loadByName(QStringLiteral(":/pgpkeymessagepart.html"));
    Grantlee::Context ctx;
    ctx.setLocalizer(engine.localizer());

    QObject block;

    block.setProperty("showKeyDetails", mp.source()->showSignatureDetails());
    block.setProperty("error", mp.error());
    block.setProperty("importUrl", mp.part()->makeLink(QStringLiteral("pgpkey?action=import")));
    const auto key = mp.key();
    if (key.isNull()) {
        block.setProperty("uid", mp.userID());
        block.setProperty("fingerprint", mp.fingerprint());
        block.setProperty("created", mp.keyDate().toString(Qt::SystemLocaleDate));
    } else {
        const auto uid = key.userID(0);
        block.setProperty("hasKey", true);
        if (uid.email() && *uid.email() && uid.name() && *uid.name()) {
            block.setProperty("uid", QStringLiteral("%1 <%2>").arg(QString::fromUtf8(uid.name()),
                              QString::fromUtf8(uid.email())));
        } else if (uid.name() && *uid.name()) {
            block.setProperty("uid", QString::fromUtf8(uid.name()));
        } else if (uid.email() && *uid.email()) {
            block.setProperty("uid", QString::fromUtf8(uid.email()));
        } else {
            block.setProperty("uid", i18n("Unknown identity"));
        }
        block.setProperty("created", QDateTime::fromTime_t(key.subkey(0).creationTime()).toString(Qt::SystemLocaleDate));
        block.setProperty("fingerprint", QString::fromLatin1(key.primaryFingerprint()));
        block.setProperty("keyUrl", QStringLiteral("kmail:showCertificate#GpgME ### gpgme ### %1").arg(QString::fromLatin1(key.keyID())));
    }

    QObject style;
    const auto bgColor = QApplication::palette().color(QPalette::Base);
    if (bgColor.value() < 128) { // HSV value (brightness): 0 = darkest, 255 = brightest
        style.setProperty("borderColor", QColor(Qt::white).name());
        style.setProperty("frameTextColor", QColor(Qt::black).name());
    } else {
        style.setProperty("borderColor", QColor(Qt::black).name());
        style.setProperty("frameTextColor", QColor(Qt::white).name());
    }

    ctx.insert(QStringLiteral("block"), &block);
    ctx.insert(QStringLiteral("style"), &style);
    return tpl->render(&ctx);
}
Example #8
0
QString TemplateLayout::generate()
{
	int progress = 0;
	int totalWork = getTotalWork(PrintOptions);

	QString htmlContent;
	delete m_engine;
	m_engine = new Grantlee::Engine(this);

	QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader =
		QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
	m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser());
	m_engine->addTemplateLoader(m_templateLoader);

	Grantlee::registerMetaType<template_options>();
	Grantlee::registerMetaType<print_options>();

	QVariantList diveList;

	struct dive *dive;
	int i;
	for_each_dive (i, dive) {
		//TODO check for exporting selected dives only
		if (!dive->selected && PrintOptions->print_selected)
			continue;
		DiveObjectHelper *d = new DiveObjectHelper(dive);
		diveList.append(QVariant::fromValue(d));
		progress++;
		emit progressUpdated(progress * 100.0 / totalWork);
	}
	Grantlee::Context c;
	c.insert("dives", diveList);
	c.insert("template_options", QVariant::fromValue(*templateOptions));
	c.insert("print_options", QVariant::fromValue(*PrintOptions));

	Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template);
	if (!t || t->error()) {
		qDebug() << "Can't load template";
		return htmlContent;
	}

	htmlContent = t->render(&c);

	if (t->error()) {
		qDebug() << "Can't render template";
	}
	return htmlContent;
}
Example #9
0
QString TemplateLayout::generate()
{
	int progress = 0;
	int totalWork = getTotalWork(PrintOptions);

	QString htmlContent;
	delete m_engine;
	m_engine = new Grantlee::Engine(this);
	Grantlee::registerMetaType<template_options>();
	Grantlee::registerMetaType<print_options>();

	QVariantList diveList;

	struct dive *dive;
	int i;
	for_each_dive (i, dive) {
		//TODO check for exporting selected dives only
		if (!dive->selected && PrintOptions->print_selected)
			continue;
		DiveObjectHelper *d = new DiveObjectHelper(dive);
		diveList.append(QVariant::fromValue(d));
		progress++;
		emit progressUpdated(lrint(progress * 100.0 / totalWork));
	}
	Grantlee::Context c;
	c.insert("dives", diveList);
	c.insert("template_options", QVariant::fromValue(*templateOptions));
	c.insert("print_options", QVariant::fromValue(*PrintOptions));

	/* don't use the Grantlee loader API */
	QString templateContents = readTemplate(PrintOptions->p_template);
	QString preprocessed = preprocessTemplate(templateContents);

	/* create the template from QString; is this thing allocating memory? */
	Grantlee::Template t = m_engine->newTemplate(preprocessed, PrintOptions->p_template);
	if (!t || t->error()) {
		qDebug() << "Can't load template";
		return htmlContent;
	}

	htmlContent = t->render(&c);

	if (t->error()) {
		qDebug() << "Can't render template";
	}
	return htmlContent;
}
Example #10
0
QString TemplateLayout::generateStatistics()
{
	QString htmlContent;
	delete m_engine;
	m_engine = new Grantlee::Engine(this);

	QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader =
		QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
	m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser() + QDir::separator() + QString("statistics"));
	m_engine->addTemplateLoader(m_templateLoader);

	Grantlee::registerMetaType<YearInfo>();
	Grantlee::registerMetaType<template_options>();
	Grantlee::registerMetaType<print_options>();

	QVariantList years;

	int i = 0;
	while (stats_yearly != NULL && stats_yearly[i].period) {
		YearInfo year(stats_yearly[i]);
		years.append(QVariant::fromValue(year));
		i++;
	}

	Grantlee::Context c;
	c.insert("years", years);
	c.insert("template_options", QVariant::fromValue(*templateOptions));
	c.insert("print_options", QVariant::fromValue(*PrintOptions));

	Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template);
	if (!t || t->error()) {
		qDebug() << "Can't load template";
		return htmlContent;
	}

	htmlContent = t->render(&c);

	if (t->error()) {
		qDebug() << "Can't render template";
		return htmlContent;
	}

	emit progressUpdated(100);
	return htmlContent;
}
Example #11
0
static void renderTemplate( Grantlee::Engine *engine, const QString &templateName )
{

  Grantlee::Template t = engine->loadByName( templateName );

  if ( t->error() ) {
    qDebug() << t->errorString();
    Q_ASSERT( !"Failed to get template" );
  }

  Grantlee::Context c;

  QString result = t->render( &c );

  if ( t->error() ) {
    qDebug() << t->errorString();
    Q_ASSERT( !"Failed to render template" );
  }

  qDebug() << result;
}