KonsolePrintManager.cpp 2.23 KB
Newer Older
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
/*
    Copyright 2020-2020 by Gustavo Carneiro <gcarneiroa@hotmail.com>
    Copyright 2012-2020 by Kurt Hindenburg <kurt.hindenburg@gmail.com>
    Copyright 2020-2020 by Tomaz Canabrava <tcanabrava@kde.org>

    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; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

// Own
#include "KonsolePrintManager.h"

// Konsole
#include "PrintOptions.h"

// Qt
#include <QWidget>
#include <QPrinter>
#include <QPainter>
#include <QPointer>
#include <QPrintDialog>

// KDE
#include <KConfigGroup>
#include <KSharedConfig>

using namespace Konsole;

void KonsolePrintManager::printRequest(pPrintContent pContent, QWidget *parent)
{
Gustavo Carneiro's avatar
Gustavo Carneiro committed
43
44
45
46
    if (!pContent) {
        return;
    }

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
    QPrinter printer;

    QPointer<QPrintDialog> dialog = new QPrintDialog(&printer, parent);
    auto options = new PrintOptions();

    dialog->setOptionTabs({options});
    dialog->setWindowTitle(i18n("Print Shell"));
    QObject::connect(dialog,
            QOverload<>::of(&QPrintDialog::accepted),
            options,
            &Konsole::PrintOptions::saveSettings);
    if (dialog->exec() != QDialog::Accepted) {
        return;
    }

    QPainter painter;
    painter.begin(&printer);

    KConfigGroup configGroup(KSharedConfig::openConfig(), "PrintOptions");

    if (configGroup.readEntry("ScaleOutput", true)) {
        double scale = qMin(printer.pageRect().width() / static_cast<double>(parent->width()),
                            printer.pageRect().height() / static_cast<double>(parent->height()));
        painter.scale(scale, scale);
    }
    
    pContent(painter, configGroup.readEntry("PrinterFriendly", true));
}