Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 99f25ce9 authored by Laurent Montel's avatar Laurent Montel 😁

Split grantlee print code to be able to create unittest

parent f5ad0806
......@@ -28,6 +28,7 @@ set(libknotesprivate_print_lib_SRCS
print/knoteprintselectthemecombobox.cpp
print/knoteprinter.cpp
print/knoteprintobject.cpp
print/knotegrantleeprint.cpp
)
set(libknotesprivate_lib_SRCS
......
......@@ -504,7 +504,7 @@ void KNote::print(bool preview)
delete dlg;
}
if (!printingTheme.isEmpty()) {
KNotePrinter printer;
KNotePrinter printer(this);
QList<KNotePrintObject*> lst;
lst.append(new KNotePrintObject(mItem));
printer.setDefaultFont( mDisplayAttribute->font() );
......
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "knotegrantleeprint.h"
#include "knoteprintobject.h"
#include <KLocalizedString>
#include <grantlee/context.h>
#include <grantlee/engine.h>
KNoteGrantleePrint::KNoteGrantleePrint(QObject *parent)
: QObject(parent)
{
mEngine = new Grantlee::Engine;
}
KNoteGrantleePrint::KNoteGrantleePrint(const QString &themePath, QObject *parent)
: QObject(parent)
{
mEngine = new Grantlee::Engine;
mTemplateLoader = Grantlee::FileSystemTemplateLoader::Ptr( new Grantlee::FileSystemTemplateLoader );
mTemplateLoader->setTemplateDirs(QStringList() << themePath);
mEngine->addTemplateLoader(mTemplateLoader);
mSelfcontainedTemplate = mEngine->loadByName(QLatin1String("theme.html"));
if (mSelfcontainedTemplate->error()) {
mErrorMessage = mSelfcontainedTemplate->errorString() + QLatin1String("<br>");
}
}
KNoteGrantleePrint::~KNoteGrantleePrint()
{
mEngine->deleteLater();
mEngine = 0;
}
QString KNoteGrantleePrint::errorMessage() const
{
return mErrorMessage;
}
void KNoteGrantleePrint::setContent(const QString &content)
{
mSelfcontainedTemplate = mEngine->newTemplate(content, QLatin1String("content"));
if (mSelfcontainedTemplate->error()) {
mErrorMessage = mSelfcontainedTemplate->errorString() + QLatin1String("<br>");
}
}
QString KNoteGrantleePrint::notesToHtml(const QList<KNotePrintObject *> lst)
{
QVariantList notes;
Q_FOREACH(KNotePrintObject *n, lst)
notes << QVariant::fromValue(static_cast<QObject*>(n));
Grantlee::Context c;
c.insert(QLatin1String("notes"), notes);
c.insert(QLatin1String("alarm_i18n"), i18n("Alarm:"));
c.insert(QLatin1String("note_is_locked_i18n"), i18n("Note is locked"));
const QString htmlText = mSelfcontainedTemplate->render(&c);
return htmlText;
}
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program 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
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef KNOTEGRANTLEEPRINT_H
#define KNOTEGRANTLEEPRINT_H
#include <QObject>
#include <grantlee/templateloader.h>
namespace Grantlee {
class Engine;
}
class KNotePrintObject;
class KNoteGrantleePrint : public QObject
{
Q_OBJECT
public:
explicit KNoteGrantleePrint(QObject *parent=0);
KNoteGrantleePrint(const QString &themePath, QObject *parent=0);
~KNoteGrantleePrint();
QString errorMessage() const;
QString notesToHtml(const QList<KNotePrintObject *> lst);
void setContent(const QString &content);
private:
Grantlee::Template mSelfcontainedTemplate;
QString mErrorMessage;
Grantlee::Engine *mEngine;
Grantlee::FileSystemTemplateLoader::Ptr mTemplateLoader;
};
#endif // KNOTEGRANTLEEPRINT_H
......@@ -17,6 +17,7 @@
#include "knoteprinter.h"
#include "print/knoteprintobject.h"
#include "knotegrantleeprint.h"
#include <QPainter>
#include <QTextDocument>
......@@ -37,16 +38,14 @@
#include <grantlee/templateloader.h>
KNotePrinter::KNotePrinter()
: mEngine(new Grantlee::Engine)
KNotePrinter::KNotePrinter(QObject *parent)
: QObject(parent),
mGrantleePrint(0)
{
mTemplateLoader = Grantlee::FileSystemTemplateLoader::Ptr( new Grantlee::FileSystemTemplateLoader );
}
KNotePrinter::~KNotePrinter()
{
mEngine->deleteLater();
mEngine = 0;
}
void KNotePrinter::setDefaultFont( const QFont &font )
......@@ -125,25 +124,9 @@ void KNotePrinter::print(QPrinter &printer, const QString &htmlText)
void KNotePrinter::printNotes(const QList<KNotePrintObject *> lst, const QString &themePath, bool preview)
{
mTemplateLoader->setTemplateDirs( QStringList() << themePath );
mEngine->addTemplateLoader( mTemplateLoader );
mSelfcontainedTemplate = mEngine->loadByName( QLatin1String("theme.html") );
QString mErrorMessage;
if ( mSelfcontainedTemplate->error() ) {
mErrorMessage += mSelfcontainedTemplate->errorString() + QLatin1String("<br>");
}
if (mErrorMessage.isEmpty()) {
QVariantList notes;
Q_FOREACH(KNotePrintObject *n, lst)
notes << QVariant::fromValue(static_cast<QObject*>(n));
Grantlee::Context c;
c.insert(QLatin1String("notes"), notes);
c.insert(QLatin1String("alarm_i18n"), i18n("Alarm:"));
c.insert(QLatin1String("note_is_locked_i18n"), i18n("Note is locked"));
const QString htmlText = mSelfcontainedTemplate->render(&c);
mGrantleePrint = new KNoteGrantleePrint(themePath, this);
if (mGrantleePrint->errorMessage().isEmpty()) {
const QString htmlText = mGrantleePrint->notesToHtml(lst);
const QString dialogCaption = i18np( "Print Note", "Print %1 notes",
lst.count() );
if (preview)
......
......@@ -30,10 +30,12 @@ class Engine;
class QPrinter;
class KNotePrintObject;
class KNOTES_EXPORT KNotePrinter
class KNoteGrantleePrint;
class KNOTES_EXPORT KNotePrinter : public QObject
{
Q_OBJECT
public:
KNotePrinter();
explicit KNotePrinter(QObject *parent=0);
~KNotePrinter();
void setDefaultFont( const QFont &font );
......@@ -46,9 +48,7 @@ private:
void doPrintPreview(const QString &htmlText);
QFont m_defaultFont;
Grantlee::Engine *mEngine;
Grantlee::FileSystemTemplateLoader::Ptr mTemplateLoader;
Grantlee::Template mSelfcontainedTemplate;
KNoteGrantleePrint *mGrantleePrint;
};
#endif // KNOTEPRINTER
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment