Files
qt5-legatus-legacy/Lts/Common/LtsPrinting/printing_base.h
T
2026-08-13 18:33:10 +02:00

365 lines
11 KiB
C++
Executable File

/**
* @RDE_HEADER@
* Small QPrinter abstraction class
* $Id$
* $HeadURL$
* $Author$
*/
#pragma once
#include <QtCore/QSet>
#include <QtPrintSupport/QPrinter>
namespace NGL
{
class INGLLogger;
class Settings;
}
namespace KDReports
{
class Report;
}
class QPainter;
class QAbstractItemView;
class QAbstractItemModel;
class QModelIndex;
class QTextDocument;
namespace LtsPrinting
{
static constexpr int DECORATION_EXTENT = 50;
static constexpr int DOC_MARGIN = 60;
static constexpr int ProjectIconHeight = 40;
// HeaderText and FooterText :
static const QLatin1String HeaderText(
"<table width=\"100%\">"
" <tr>"
" <td align=\"left\" valign=\"bottom\">&exercise;</td>" // exercise name
" <td align=\"center\" valign=\"bottom\">&title;</td>" // title
" <td align=\"right\" valign=\"top\"><img src=\"&logo;\" height=\"&logoHeight;\" alt=\"Logo\" /></td>" // logo
" </tr>"
"</table>");
static const QLatin1String FooterText(
"<table width=\"100%\">"
" <tr>"
" <td align=\"left\">&date; -- &user;</td>" // date
" <td align=\"center\" valign=\"bottom\">&footertitle;</td>" // footertitle
" <td align=\"right\">&page;</td>" // pagenumber
" </tr>"
"</table>");
//
class Base
{
public:
/**
* constructor Printer with open QPrintDialog
* @param dialogParent parent of QPrintDialog
* @param dialogTitle windowTitle of QPrintDialog
*/
Base(QTextDocument &doc,
QWidget *dialogParent,
const QString &dialogTitle,
const QString &_fn,
QPrinter::PaperSize _ps = QPrinter::A4,
QPrinter::Orientation _or = QPrinter::Portrait,
QPrinter::ColorMode _cm = QPrinter::Color);
/**
* destructor
*/
virtual ~Base();
/**
* get the QPrinter
* @return the QPrinter
*/
QPrinter *printer();
/**
* set the default print destination (printer or file)
* @param printToFile print to file if true
*/
static void setPrintToFile(bool printToFile);
/**
* get the style sheet(html)
* @return the stle sheet
*/
static QString styleSheet();
/// Get page size
QPrinter::PageSize pageSize() const;
/// Set page size
void setPageSize(QPrinter::PageSize size);
/// Get page orientation
QPrinter::Orientation orientation() const;
/// Set page orientation
void setOrientation(QPrinter::Orientation orientation);
bool newPage();
/// Get left margin width
double leftMargin() const;
/// Set left margin width
void setLeftMargin(double margin);
/// Get right margin width
double rightMargin() const;
/// Set right margin width
void setRightMargin(double margin);
/// Get top margin width
double topMargin() const;
/// Set top margin width
void setTopMargin(double margin);
/// Get bottom margin width
double bottomMargin() const;
/// Set bottom margin width
void setBottomMargin(double margin);
/// Set all margins
void setMargins(double margin);
/// Get spacing between content and header and footer
double spacing() const;
/// Set spacing between content and header and footer
void setSpacing(double spacing);
/// Get header size
double headerSize() const;
/// Set header size
void setHeaderSize(double size);
/// Set header rule size
double headerRule() const;
/// Get header rule size
void setHeaderRule(double pointsize);
/// Get header text
const QString &headerText() const;
/// Set header text
void setHeaderText(const QString &text);
/// Get footer size
double footerSize() const;
/// Set footer size
void setFooterSize(double size);
/// Get footer rule size
double footerRule() const;
/// Set footer rule size
void setFooterRule(double pointsize);
/// Get footer text
const QString &footerText() const;
/// Set footer text
void setFooterText(const QString &text);
/// Set the footer title (the text in the center)
void setFooterTitle(const QString &footertitle) { footertitle_ = footertitle; }
/// Set the font size of a list/table/tree item :
/// between 1 (==very small) and 7 (==very large), 3 == normal
void setItemFontSize(int itemFontSize);
/**
* print title data (html) into a QString
* @param htmlStr data (html) will be append to str
* @param title the title of the data
* @param html heading type (h1, h2, h3, h4, h5, h6 - default h3)
* @return nothing
*/
static void printTitleData(QString &htmlStr, const QString &title, const QString &heading = QStringLiteral("h3"));
/**
* print list data (html) into a QString
* @param str data (html) will be append to str
* @param view the model-data of this view will be translate into html
* @param title the title of the data
* @param iconMode if true : icons only will be print
* @param iconExtent the extent (width and height) of an icon
* @return true on sucess
*/
bool printListData(QString &str,
const QAbstractItemView *view,
const QString &title,
bool asIcons = false,
int iconExtent = DECORATION_EXTENT);
/**
* print table data (html) into a QString
* @param str data (html) will be append to str
* @param view the model-data of this view will be translate into html
* @param title the title of the data
* @param suppressCols list of colums to be ignored
* @param iconExtent the extent (width and height) of an icon
* @return true on sucess
*/
bool printTableData(QString &str,
const QAbstractItemView *view,
const QString &title = QString(),
const QSet<int> &suppressCols = QSet<int>(),
int iconExtent = DECORATION_EXTENT);
/**
* append tree data (html) into a QString
* @param str data (html) will be append to str
* @param view the model-data of this view will be translate into html
* @param title the title of the data
* @param suppressCols list of colums to be ignored
* @param iconExtent the extent (width and height) of an icon
* @param tabtexts alternative colum texts (instead of headerData texts)
* @return true on sucess
*/
bool printTreeData(QString &str,
const QAbstractItemView *view,
const QString &title = QString(),
const QSet<int> &suppressCols = QSet<int>(),
int iconExtent = DECORATION_EXTENT,
const QList<QString> &tabtexts = QList<QString>());
/**
* print a text block into a doc
* @param doc destination
* @param text to be inserted
* @param title the title of the text block
* @return true on sucess
*/
static bool printDocTextBlock(QTextDocument &doc,
const QString &text,
const QString &title = QString());
/**
* print an image into a doc
* @param doc destination
* @param image to be inserted
* @param title the title of the image
* @return true on sucess
*/
bool printDocImage(QTextDocument &doc, const QImage &image,
const QString &title = QString(), const QString &description = QString(), const qreal alreadyUsedHeight = 0) const;
/**
* print a report into a doc
* @param doc destination
* @param report to be inserted
* @return true on sucess
*/
static bool printReportData(QTextDocument &doc, KDReports::Report &report);
/**
* prints out (to printer or to a file) the textDocument
* @param doc the doc to be printed
* @param printer the associated printer
*/
virtual void print(QTextDocument *doc);
virtual void printSinglePage(QTextDocument *doc);
virtual void printDone();
/**
* Get all hidden cols for the given view - useful to
* only print the columns which are visible to the user
*/
template<class T>
static QSet<int> getHiddenCols(T *view)
{
QSet<int> hiddenCols;
int colCount = view->model()->columnCount();
for (int i = 0; i < colCount; ++i)
{
if (view->isColumnHidden(i))
hiddenCols += i;
}
return hiddenCols;
}
/**
* Set exercise Name for prints
* @param exerciseName Exercise name to be used for printing
*/
void setCurrentExerciseName( const QString& exerciseName );
/**
* Set operator Name for prints
* @param operatorName Operator name to be used for printing
*/
void setOperatorName( const QString& operatorName );
// paint specific page
void paintPage(QTextDocument *document,
int pagenum);
QSize pageSizeInMM();
protected:
bool printTableItemData(QString &str,
const QAbstractItemModel *model,
const QModelIndex &mi,
int rowSpanSize,
int columnSpanSize,
int iconExtent = DECORATION_EXTENT);
bool printItemData(QString &str,
const QAbstractItemModel *model,
const QModelIndex &mi,
int iconExtent = DECORATION_EXTENT);
void printDecorationData(QString &str,
const QAbstractItemModel *model,
const QModelIndex &mi,
int iconExtent = DECORATION_EXTENT);
void printListPoint(QString &str,
const QAbstractItemView *view,
const QAbstractItemModel *vm,
const QModelIndex &miColumn0,
const QSet<int> &suppressCols = QSet<int>(),
int iconExtent = DECORATION_EXTENT);
static int hierarchyCount(const QModelIndex &child);
QString iconFilename(int nr) const;
QSize printerPageSize() const;
QSize contentPageSize() const;
QString currentExerciseName() const;
QString operatorName() const;
// return paper rect
QRectF paperRect(QPaintDevice *device);
// return printable rects
QRectF contentRect(QPaintDevice *device);
QRectF headerRect(QPaintDevice *device);
QRectF footerRect(QPaintDevice *device);
// common print routine
void printSinglePage();
void print();
private:
QPrinter *m_printer;
QPainter *m_painter;
static bool m_printToFile;
int m_iconFilenameNr;
QTextDocument *m_tempdoc;
double leftmargin_;
double rightmargin_;
double topmargin_;
double bottommargin_;
double spacing_;
double headersize_;
double headerrule_;
QString headertext_;
double footersize_;
double footerrule_;
QString footertext_;
QString footertitle_;
int itemFontSize_;
private:
NGL::Settings *m_settings;
NGL::INGLLogger &m_logger;
QString m_exerciseName;
QString m_operatorName;
int m_currentPage;
};
} // namespace LtsPrinting