431 lines
13 KiB
C++
Executable File
431 lines
13 KiB
C++
Executable File
|
|
|
|
|
|
#include "printing.h"
|
|
#include "printing_base.h"
|
|
|
|
#include <LtsSession/lts_session.h>
|
|
#include <LtsSession/lts_session_data.h>
|
|
#include <LtsWidgets/lts_base_layout_widget.h>
|
|
|
|
#include <KDReports/KDReportsReport>
|
|
#include <QString>
|
|
#include <QTableView>
|
|
#include <QTreeView>
|
|
#include <QListView>
|
|
#include <QAbstractItemModel>
|
|
#include <QImage>
|
|
#include <QTextDocument>
|
|
#include <QTextCursor>
|
|
|
|
#include <QPrinter>
|
|
#include <QPainter>
|
|
#include <QPrintDialog>
|
|
#include <QTableWidget>
|
|
#include <QLabel>
|
|
#include <QTextTable>
|
|
#include <QDebug>
|
|
#include <QTextEdit>
|
|
|
|
namespace LtsPrinting
|
|
{
|
|
|
|
|
|
|
|
|
|
bool itemsHaveNoChildren(const QTreeView *view)
|
|
{
|
|
const QAbstractItemModel *itemModel = view->model();
|
|
int rowCount = itemModel->rowCount();
|
|
for (int row = 0; row < rowCount; ++row)
|
|
{
|
|
if (itemModel->hasChildren(itemModel->index(row, 0)))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
void setNameOfExerciseAndOperator(LtsSession::LtsSession *session, LtsPrinting::Base &nglPrinter)
|
|
{
|
|
if ( session->exerciseId() != 0 )
|
|
nglPrinter.setCurrentExerciseName(QObject::tr("Exercise: %1 %2").arg(session->exerciseData()->exName,
|
|
session->exerciseData()->phName));
|
|
else
|
|
nglPrinter.setCurrentExerciseName(QObject::tr("Exercise: Administration") );
|
|
|
|
LtsSessionData::LtsSessionData *sessionData = LtsSessionData::LtsSessionData::getInstance();
|
|
nglPrinter.setOperatorName(sessionData->getOperatorName());
|
|
}
|
|
|
|
|
|
void printView(LtsSession::LtsSession *session,
|
|
QWidget *dialogParent,
|
|
const QAbstractItemView *view,
|
|
const QString& headingHtml)
|
|
{
|
|
const QTableView *tableView = dynamic_cast<const QTableView *>(view);
|
|
const QTreeView *treeView = dynamic_cast<const QTreeView *>(view);
|
|
QSet<int> hiddenCols;
|
|
|
|
int switchNum = 0;
|
|
if (tableView != nullptr)
|
|
{
|
|
switchNum = 1;
|
|
hiddenCols = LtsPrinting::Base::getHiddenCols(tableView);
|
|
}
|
|
else if (treeView != nullptr)
|
|
{
|
|
switchNum = 2;
|
|
hiddenCols = LtsPrinting::Base::getHiddenCols(treeView);
|
|
if (itemsHaveNoChildren(treeView)) switchNum = 1;
|
|
}
|
|
else if (dynamic_cast<const QListView *>(view) != nullptr)
|
|
switchNum = 3;
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
|
|
QTextDocument doc;
|
|
QTextCursor cursor(&doc);
|
|
|
|
doc.setMetaInformation(QTextDocument::DocumentTitle, dialogParent->windowTitle());
|
|
LtsPrinting::Base nglPrinter(doc, dialogParent,
|
|
QObject::tr("Print %1").arg(dialogParent->windowTitle()),
|
|
dialogParent->windowTitle(),
|
|
QPrinter::A4, QPrinter::Landscape);
|
|
if (!nglPrinter.printer())
|
|
return;
|
|
|
|
setNameOfExerciseAndOperator(session, nglPrinter);
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
QString htmlStr;
|
|
|
|
switch (switchNum)
|
|
{
|
|
case 1:
|
|
nglPrinter.printTableData(htmlStr, view, QString(), hiddenCols);
|
|
break;
|
|
case 2:
|
|
nglPrinter.printTreeData(htmlStr, view, QString(), hiddenCols);
|
|
break;
|
|
case 3:
|
|
nglPrinter.printListData(htmlStr, view, QString());
|
|
break;
|
|
}
|
|
cursor.insertHtml(headingHtml);
|
|
cursor.insertHtml(htmlStr);
|
|
nglPrinter.print(&doc);
|
|
|
|
}
|
|
|
|
|
|
void printView(LtsSession::LtsSession *session,
|
|
QWidget *dialogParent,
|
|
QAbstractItemView *view,
|
|
const QString& headingHtml)
|
|
{
|
|
const QAbstractItemView *itemView = static_cast<const QAbstractItemView *>(view);
|
|
printView(session, dialogParent, itemView, headingHtml);
|
|
}
|
|
|
|
|
|
void printImage(LtsSession::LtsSession *session, QWidget *dialogParent,
|
|
const QImage &image, const QString &imageTitle, const QString &description,
|
|
const QPrinter::PageSize &pageSize, const QPrinter::Orientation &pageOrientation)
|
|
{
|
|
QTextDocument doc;
|
|
QTextCursor cursor(&doc);
|
|
|
|
QString windowTitle = dialogParent->windowTitle();
|
|
doc.setMetaInformation(QTextDocument::DocumentTitle, dialogParent->windowTitle());
|
|
LtsPrinting::Base nglPrinter(doc, dialogParent,
|
|
QObject::tr("Print %1").arg(windowTitle),
|
|
windowTitle,
|
|
pageSize, pageOrientation);
|
|
if (!nglPrinter.printer())
|
|
return;
|
|
|
|
setNameOfExerciseAndOperator(session, nglPrinter);
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
nglPrinter.printDocImage(doc, image, imageTitle, description);
|
|
nglPrinter.print(&doc);
|
|
}
|
|
|
|
|
|
void printImages(LtsSession::LtsSession *session, QWidget *dialogParent,
|
|
const QString &docName, QVector<QImage> &images,
|
|
QVector<QString> &imageTitles, QVector<QString> &descriptions,
|
|
const QPrinter::PageSize &pageSize, const QPrinter::Orientation &pageOrientation)
|
|
|
|
{
|
|
QTextDocument doc;
|
|
QTextCursor cursor(&doc);
|
|
|
|
doc.setMetaInformation(QTextDocument::DocumentTitle, docName);
|
|
LtsPrinting::Base nglPrinter(doc, dialogParent,
|
|
QObject::tr("Print %1").arg(docName),
|
|
docName, pageSize, pageOrientation);
|
|
if (!nglPrinter.printer())
|
|
return;
|
|
|
|
setNameOfExerciseAndOperator(session, nglPrinter);
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
for ( int i = 0; i < images.size(); i++)
|
|
{
|
|
QString imageTitle, imageDescription;
|
|
if (imageTitles.size() > i && imageTitles[i].size() > 0)
|
|
imageTitle = imageTitles[i];
|
|
if (descriptions.size() > i && descriptions[i].size() > 0)
|
|
imageDescription = descriptions[i];
|
|
nglPrinter.printDocImage(doc, images[i], imageTitle, imageDescription);
|
|
nglPrinter.printSinglePage(&doc);
|
|
doc.clear();
|
|
|
|
if (i != images.size() -1)
|
|
nglPrinter.newPage();
|
|
|
|
}
|
|
|
|
nglPrinter.printDone();
|
|
|
|
|
|
}
|
|
|
|
|
|
void printPDFImages(QWidget *dialogParent, const QString &docName, const QString &printPath, QVector<QImage> &images)
|
|
|
|
{
|
|
QPrinter printer;
|
|
|
|
printer.setColorMode(QPrinter::ColorMode::Color);
|
|
printer.setOrientation(QPrinter::Orientation::Portrait);
|
|
printer.setPageSize(QPrinter::PageSize::A4);
|
|
QString fn = docName;
|
|
fn = fn.remove(QLatin1Char(' '));
|
|
fn = fn.remove(QLatin1String(".."));
|
|
fn = fn.remove(QLatin1Char('/'));
|
|
fn = printPath + fn;
|
|
if (!fn.endsWith(QLatin1String(".pdf"), Qt::CaseInsensitive))
|
|
fn += QLatin1String(".pdf");
|
|
#ifndef WIN32
|
|
printer.setOutputFileName(fn);
|
|
#endif
|
|
printer.setOutputFormat(QPrinter::NativeFormat);
|
|
|
|
QPrintDialog *dialog = new QPrintDialog(&printer, dialogParent);
|
|
dialog->setWindowTitle(QObject::tr("Print %1").arg(docName));
|
|
if (dialog->exec() != QDialog::Accepted)
|
|
return;
|
|
|
|
QPainter painter;
|
|
painter.begin(&printer);
|
|
int lastItem = images.size() - 1;
|
|
for (int i = 0; i < images.size(); ++i)
|
|
{
|
|
QRect rect = painter.viewport();
|
|
QSize size = images.at(i).size();
|
|
size.scale(rect.size(), Qt::KeepAspectRatio);
|
|
painter.setViewport(rect.x(), rect.y(),
|
|
size.width(), size.height());
|
|
painter.setWindow(images.at(i).rect());
|
|
painter.drawImage(0, 0, images.at(i));
|
|
if (i != lastItem)
|
|
printer.newPage();
|
|
}
|
|
painter.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void printReport( LtsSession::LtsSession *session, QWidget *dialogParent, KDReports::Report& report )
|
|
{
|
|
QTextDocument doc;
|
|
QTextCursor cursor(&doc);
|
|
|
|
doc.setMetaInformation(QTextDocument::DocumentTitle, dialogParent->windowTitle());
|
|
LtsPrinting::Base nglPrinter(doc, dialogParent,
|
|
QObject::tr("Print %1").arg(dialogParent->windowTitle()),
|
|
dialogParent->windowTitle(),
|
|
QPrinter::A3, QPrinter::Landscape);
|
|
if (!nglPrinter.printer())
|
|
return;
|
|
|
|
setNameOfExerciseAndOperator( session, nglPrinter );
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
report.setWidthForEndlessPrinter(0);
|
|
report.setPageSize(QPrinter::A3);
|
|
report.setOrientation(QPrinter::Landscape);
|
|
|
|
LtsPrinting::Base::printReportData(doc, report);
|
|
nglPrinter.print(&doc);
|
|
}
|
|
|
|
|
|
void printWidget(LtsSession::LtsSession *session, QWidget *dialogParent, QWidget *widget, const QString &title)
|
|
{
|
|
QTextDocument doc;
|
|
QTextCursor cursor(&doc);
|
|
QImage image(widget->size(), QImage::Format_ARGB32);
|
|
widget->render(&image);
|
|
|
|
QString windowTitle = dialogParent->windowTitle();
|
|
doc.setMetaInformation(QTextDocument::DocumentTitle, dialogParent->windowTitle());
|
|
LtsPrinting::Base nglPrinter(doc, dialogParent,
|
|
QObject::tr("Print %1").arg(windowTitle),
|
|
windowTitle,
|
|
QPrinter::A3, QPrinter::Landscape);
|
|
if (!nglPrinter.printer())
|
|
return;
|
|
|
|
setNameOfExerciseAndOperator(session, nglPrinter);
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
nglPrinter.printDocImage(doc, image, title);
|
|
nglPrinter.print(&doc);
|
|
}
|
|
|
|
void printMessage(LtsSession::LtsSession *session,
|
|
QWidget *dialogParent,
|
|
const QString &subject, const QString &subjectContent,
|
|
const QString &from, const QString &fromContent,
|
|
const QString &to, const QString &toContent,
|
|
const QString &msgTextContent,
|
|
const QString &time, const QString &timeContent)
|
|
{
|
|
QTextDocument doc;
|
|
QString windowTitle = subject;
|
|
|
|
doc.setMetaInformation(QTextDocument::DocumentTitle, subject);
|
|
LtsPrinting::Base nglPrinter(doc, dialogParent,
|
|
QObject::tr("Print %1").arg(windowTitle),
|
|
windowTitle,
|
|
QPrinter::A4, QPrinter::Portrait);
|
|
|
|
QString str = QString(QStringLiteral("<H2><strong>%1</strong> %2 </H2>")).arg(subject).arg(subjectContent);
|
|
str.append( QString(QStringLiteral("<strong>%1</strong> %2<br>")).arg(time).arg(timeContent));
|
|
str.append( QString(QStringLiteral("<strong>%1</strong> %2<br>")).arg(from).arg(fromContent));
|
|
str.append( QString(QStringLiteral("<strong>%1</strong> %2<hr>")).arg(to).arg(toContent));
|
|
str.append( QString(QStringLiteral("%1")).arg(msgTextContent));
|
|
|
|
doc.setHtml(str);
|
|
if (!nglPrinter.printer())
|
|
return;
|
|
|
|
setNameOfExerciseAndOperator(session, nglPrinter);
|
|
|
|
nglPrinter.print(&doc);
|
|
}
|
|
|
|
|
|
void printStoryboard(LtsSession::LtsSession *session, QWidget *dialogParent, QAbstractItemView *view, const QString &headingHtml)
|
|
{
|
|
QTableWidget *tbl = dynamic_cast< QTableWidget *>(view);
|
|
QTextDocument doc;
|
|
QTextCursor cursor(&doc);
|
|
|
|
QTextBlockFormat tbfFormat;
|
|
tbfFormat.setPageBreakPolicy(QTextFormat::PageBreakFlag::PageBreak_Auto);
|
|
|
|
QString windowTitle = dialogParent->windowTitle();
|
|
doc.setMetaInformation(QTextDocument::DocumentTitle, dialogParent->windowTitle());
|
|
|
|
LtsPrinting::Base nglPrinter(doc, dialogParent,
|
|
QObject::tr("Print %1").arg(windowTitle),
|
|
windowTitle,
|
|
QPrinter::A4, QPrinter::Portrait);
|
|
|
|
if (!nglPrinter.printer())
|
|
return;
|
|
|
|
const int rows = tbl->rowCount();
|
|
cursor.insertHtml(headingHtml);
|
|
|
|
double currentHeight = 0;
|
|
double previousPagesHeight = 0;
|
|
QString htmlStr;
|
|
for (int rowCounter = 0; rowCounter < rows; rowCounter++)
|
|
{
|
|
|
|
htmlStr.append(QStringLiteral("<table>"));
|
|
QTextEdit *labelForFirstCell = (QTextEdit*)tbl->cellWidget(rowCounter, 0);
|
|
QString stringFirstColumn = labelForFirstCell->toHtml();
|
|
|
|
QTextEdit *labelSecondColumn = (QTextEdit*)tbl->cellWidget(rowCounter, 1);
|
|
QString stringSecondColumn = labelSecondColumn->toHtml();
|
|
|
|
QTextEdit *labelThirdColumn = (QTextEdit*)tbl->cellWidget(rowCounter, 2);
|
|
QString stringThirdColumn = labelThirdColumn->toHtml();
|
|
|
|
for (int rowInCellCounter = 0; rowInCellCounter < stringSecondColumn.split(QStringLiteral("<br>")).size(); ++rowInCellCounter)
|
|
{
|
|
htmlStr.append(QStringLiteral("<tr>"));
|
|
|
|
if (rowInCellCounter < stringFirstColumn.split(QStringLiteral("<br>")).size())
|
|
{
|
|
htmlStr.append(QStringLiteral("<td width = \"25%\">"));
|
|
htmlStr.append(stringFirstColumn.split(QStringLiteral("<br>"))[rowInCellCounter]);
|
|
htmlStr.append(QStringLiteral("</td>"));
|
|
}
|
|
else
|
|
{
|
|
htmlStr.append(QStringLiteral("<td width = \"25%\"> "));
|
|
htmlStr.append(QStringLiteral("</td>"));
|
|
}
|
|
QString currentTextSecond = stringSecondColumn.split(QStringLiteral("<br>"))[rowInCellCounter];
|
|
htmlStr.append(QStringLiteral("<td width = \"37%\">"));
|
|
htmlStr.append(currentTextSecond);
|
|
htmlStr.append(QStringLiteral("</td>"));
|
|
|
|
QString currentTextThird = stringThirdColumn.split(QStringLiteral("<br>"))[rowInCellCounter];
|
|
htmlStr.append(QStringLiteral("<td width = \"37%\">"));
|
|
htmlStr.append(currentTextThird);
|
|
htmlStr.append(QStringLiteral("</td>"));
|
|
htmlStr.append(QStringLiteral("</tr>"));
|
|
}
|
|
QTextDocument tempDoc;
|
|
tempDoc.setPageSize(doc.pageSize());
|
|
tempDoc.setHtml(htmlStr);
|
|
currentHeight = doc.size().height() - previousPagesHeight;
|
|
|
|
if (currentHeight + tempDoc.size().height() > nglPrinter.pageSizeInMM().height())
|
|
{
|
|
tbfFormat.setPageBreakPolicy(QTextFormat::PageBreakFlag::PageBreak_AlwaysBefore);
|
|
cursor.insertBlock(tbfFormat);
|
|
previousPagesHeight = doc.size().height();
|
|
currentHeight = doc.size().height() - previousPagesHeight;
|
|
}
|
|
else
|
|
{
|
|
tbfFormat.setPageBreakPolicy(QTextFormat::PageBreakFlag::PageBreak_Auto);
|
|
currentHeight = doc.size().height() - previousPagesHeight;
|
|
}
|
|
cursor.insertHtml(htmlStr);
|
|
htmlStr.clear();
|
|
}
|
|
htmlStr.append(QStringLiteral("</table>"));
|
|
cursor.insertHtml(htmlStr);
|
|
|
|
cursor.movePosition(QTextCursor::End);
|
|
|
|
setNameOfExerciseAndOperator(session, nglPrinter);
|
|
nglPrinter.print(&doc);
|
|
}
|
|
|
|
|
|
}
|