grantleeprintstyle.cpp 2.64 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2013-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

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

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
18
19
20
*/

#include "grantleeprintstyle.h"
Laurent Montel's avatar
Laurent Montel committed
21
#include "kaddressbookimportexportcontactfields.h"
22
23
24
#include "printingwizard.h"
#include "printprogress.h"
#include "printstyle.h"
Laurent Montel's avatar
Laurent Montel committed
25
#include <KaddressbookGrantlee/GrantleePrint>
Laurent Montel's avatar
Laurent Montel committed
26

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
27
#include <KContacts/Addressee>
28

Laurent Montel's avatar
Laurent Montel committed
29
#include <KLocalizedString>
30
31
32

#include <QPrinter>
#include <QTextDocument>
33
34
#include <QFile>
#include <QDir>
35
36
37

using namespace KABPrinting;

Laurent Montel's avatar
Laurent Montel committed
38
39
GrantleePrintStyle::GrantleePrintStyle(const QString &themePath, PrintingWizard *parent)
    : PrintStyle(parent)
40
    , m_themePath(themePath)
41
{
Laurent Montel's avatar
Laurent Montel committed
42
    QFile previewFile(QString(themePath + QStringLiteral("/preview.png")));
43
    if (previewFile.exists()) {
Laurent Montel's avatar
Laurent Montel committed
44
        setPreview(previewFile.fileName());
45
    }
Laurent Montel's avatar
Laurent Montel committed
46
    setPreferredSortOptions(KAddressBookImportExport::KAddressBookImportExportContactFields::FormattedName, Qt::AscendingOrder);
47
48
49
50
51
52
}

GrantleePrintStyle::~GrantleePrintStyle()
{
}

Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
53
void GrantleePrintStyle::print(const KContacts::Addressee::List &contacts, PrintProgress *progress)
54
55
{
    QPrinter *printer = wizard()->printer();
Laurent Montel's avatar
Laurent Montel committed
56
    printer->setPageMargins(20, 20, 20, 20, QPrinter::DevicePixel);
57

Laurent Montel's avatar
Laurent Montel committed
58
    progress->addMessage(i18n("Setting up document"));
59

60
61
62
    KAddressBookGrantlee::GrantleePrint grantleePrint(m_themePath);
    grantleePrint.setApplicationDomain("kaddressbook");
    const QString html = grantleePrint.contactsToHtml(contacts);
63
64

    QTextDocument document;
Laurent Montel's avatar
Laurent Montel committed
65
    document.setHtml(html);
66

Laurent Montel's avatar
Laurent Montel committed
67
    progress->addMessage(i18n("Printing"));
68

Laurent Montel's avatar
Laurent Montel committed
69
    document.print(printer);
70

Laurent Montel's avatar
Laurent Montel committed
71
    progress->addMessage(i18nc("Finished printing", "Done"));
72
73
}

Laurent Montel's avatar
Laurent Montel committed
74
GrantleeStyleFactory::GrantleeStyleFactory(const QString &name, const QString &themePath, PrintingWizard *parent)
Laurent Montel's avatar
Laurent Montel committed
75
76
77
    : PrintStyleFactory(parent)
    , mThemePath(themePath)
    , mName(name)
78
79
80
81
82
{
}

PrintStyle *GrantleeStyleFactory::create() const
{
Laurent Montel's avatar
Laurent Montel committed
83
    return new GrantleePrintStyle(mThemePath, mParent);
84
85
86
87
}

QString GrantleeStyleFactory::description() const
{
Laurent Montel's avatar
Laurent Montel committed
88
    return mName;
89
}