File indexing completed on 2024-04-28 05:50:54
0001 /* 0002 SPDX-FileCopyrightText: 2020-2020 Gustavo Carneiro <gcarneiroa@hotmail.com> 0003 SPDX-FileCopyrightText: 2012-2020 Kurt Hindenburg <kurt.hindenburg@gmail.com> 0004 SPDX-FileCopyrightText: 2020-2020 Tomaz Canabrava <tcanabrava@kde.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 // Own 0010 #include "KonsolePrintManager.h" 0011 0012 // Konsole 0013 #include "PrintOptions.h" 0014 0015 // Qt 0016 #include <QFont> 0017 #include <QPainter> 0018 #include <QPoint> 0019 #include <QPointer> 0020 #include <QPrintDialog> 0021 #include <QPrinter> 0022 #include <QRect> 0023 #include <QWidget> 0024 0025 // KDE 0026 #include <KConfigGroup> 0027 #include <KSharedConfig> 0028 0029 using namespace Konsole; 0030 0031 KonsolePrintManager::KonsolePrintManager(pDrawBackground drawBackground, pDrawContents drawContents, pColorGet colorGet) 0032 { 0033 _drawBackground = drawBackground; 0034 _drawContents = drawContents; 0035 _backgroundColor = colorGet; 0036 } 0037 0038 void KonsolePrintManager::printRequest(pPrintContent pContent, QWidget *parent) 0039 { 0040 if (!pContent) { 0041 return; 0042 } 0043 0044 QPrinter printer; 0045 0046 QPointer<QPrintDialog> dialog = new QPrintDialog(&printer, parent); 0047 auto options = new PrintOptions(); 0048 0049 dialog->setOptionTabs({options}); 0050 dialog->setWindowTitle(i18n("Print Shell")); 0051 // Make MSVC happy by using ultra verbose static_cast :( 0052 QObject::connect(dialog, static_cast<void (QPrintDialog::*)()>(&QPrintDialog::accepted), options, &Konsole::PrintOptions::saveSettings); 0053 if (dialog->exec() != QDialog::Accepted) { 0054 return; 0055 } 0056 0057 QPainter painter; 0058 painter.begin(&printer); 0059 0060 KConfigGroup configGroup(KSharedConfig::openConfig(), QStringLiteral("PrintOptions")); 0061 0062 if (configGroup.readEntry("ScaleOutput", true)) { 0063 QRect page_rect = printer.pageLayout().paintRectPixels(printer.resolution()); 0064 double scale = qMin(page_rect.width() / static_cast<double>(parent->width()), page_rect.height() / static_cast<double>(parent->height())); 0065 painter.scale(scale, scale); 0066 } 0067 0068 pContent(painter, configGroup.readEntry("PrinterFriendly", true)); 0069 } 0070 0071 void KonsolePrintManager::printContent(QPainter &painter, bool friendly, QPoint columnsLines, pVTFontGet vtFontGet, pVTFontSet vtFontSet) 0072 { 0073 // Reinitialize the font with the printers paint device so the font 0074 // measurement calculations will be done correctly 0075 QFont savedFont = vtFontGet(); 0076 QFont font(savedFont, painter.device()); 0077 painter.setFont(font); 0078 vtFontSet(font); 0079 0080 QRect rect(0, 0, columnsLines.y(), columnsLines.x()); 0081 0082 if (!friendly) { 0083 _drawBackground(painter, rect, _backgroundColor(), true); 0084 } 0085 _drawContents(painter, rect, friendly); 0086 vtFontSet(savedFont); 0087 }