KD Reports API Documentation  2.2
KDReportsTableElement.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Reports library.
4 **
5 ** SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #include "KDReportsTableElement.h"
12 #include "KDReportsCell.h"
14 #include "KDReportsReport.h"
17 #include <QAbstractTextDocumentLayout>
18 #include <QDebug>
19 #include <QPainter>
20 #include <QTextCursor>
21 #include <QTextTableCell>
22 
23 namespace KDReports {
24 using CellContentMap = QMap<QPair<int /*row*/, int /*column*/>, Cell>;
25 }
26 
27 class KDReports::TableElementPrivate
28 {
29 public:
30  void createCell(QTextTable *textTable, ReportBuilder &builder, int row, int column, const Cell &cell, QTextCharFormat charFormat) const;
31 
32  KDReports::CellContentMap m_cellContentMap;
33  int m_rowCount = 0;
34  int m_columnCount = 0;
35  int m_headerRowCount = 0;
36  int m_headerColumnCount = 0;
37 };
38 
40 
42  : d(new TableElementPrivate)
43 {
44 }
45 
47  : AbstractTableElement(other)
48  , d(new TableElementPrivate(*other.d))
49 {
50 }
51 
53 {
54  if (&other == this)
55  return *this;
57  *d = *other.d;
58  return *this;
59 }
60 
62 {
63 }
64 
66 {
67  // never used at the moment
68  return new TableElement(*this);
69 }
70 
72 {
73  d->m_headerRowCount = count;
74 }
75 
77 {
78  return d->m_headerRowCount;
79 }
80 
82 {
83  d->m_headerColumnCount = count;
84 }
85 
87 {
88  return d->m_headerColumnCount;
89 }
90 
92 {
93  return d->m_rowCount;
94 }
95 
97 {
98  return d->m_columnCount;
99 }
100 
102 {
103  d->m_rowCount = std::max(d->m_rowCount, row + 1);
104  d->m_columnCount = std::max(d->m_columnCount, column + 1);
105 
106  const QPair<int, int> coord = qMakePair(row, column);
107  return d->m_cellContentMap[coord]; // find or create
108 }
109 
110 void KDReports::TableElementPrivate::createCell(QTextTable *textTable, ReportBuilder &builder, int row, int column, const Cell &cell, QTextCharFormat charFormat) const
111 {
112  if (cell.columnSpan() > 1 || cell.rowSpan() > 1)
113  textTable->mergeCells(row, column, cell.rowSpan(), cell.columnSpan());
114  QTextTableCell tableCell = textTable->cellAt(row, column);
115  Q_ASSERT(tableCell.isValid());
116  QTextCursor cellCursor = tableCell.firstCursorPosition();
117  QTextTableCellFormat tableCellFormat(charFormat.toTableCellFormat());
118  if (cell.background().style() != Qt::NoBrush)
119  tableCellFormat.setBackground(cell.background());
120  tableCellFormat.setTableCellColumnSpan(cell.columnSpan());
121  tableCellFormat.setTableCellRowSpan(cell.rowSpan());
122  if (cell.verticalAlignment() != 0)
123  tableCellFormat.setVerticalAlignment(ReportBuilder::toVerticalAlignment(cell.verticalAlignment()));
124  if (auto func = cell.cellFormatFunction())
125  func(row, column, tableCellFormat);
126  tableCell.setFormat(tableCellFormat);
127  cellCursor.setCharFormat(tableCellFormat);
128  ReportBuilder cellBuilder(builder.currentDocumentData(), cellCursor, builder.report());
129  cellBuilder.copyStateFrom(builder);
130  cellBuilder.setDefaultFont(charFormat.font());
131  cell.build(cellBuilder);
132 }
133 
135 {
136  if (d->m_cellContentMap.isEmpty())
137  return;
138 
139  QTextCursor &textDocCursor = builder.cursor();
140 
141  QTextTableFormat tableFormat;
142  tableFormat.setHeaderRowCount(d->m_headerRowCount);
143  tableFormat.setProperty(KDReports::HeaderColumnsProperty, d->m_headerColumnCount);
144  tableFormat.setAlignment(textDocCursor.blockFormat().alignment());
145  tableFormat.setBackground(background());
146  fillTableFormat(tableFormat, textDocCursor);
147  QTextCharFormat charFormat = textDocCursor.charFormat();
148 
149  QTextTable *textTable = textDocCursor.insertTable(d->m_rowCount, d->m_columnCount, tableFormat);
150 
151  CellContentMap::const_iterator it = d->m_cellContentMap.constBegin();
152  for (; it != d->m_cellContentMap.constEnd(); ++it) {
153  const int row = it.key().first;
154  const int column = it.key().second;
155  const Cell &cell = it.value();
156  d->createCell(textTable, builder, row, column, cell, charFormat);
157  }
158 
159  textDocCursor.movePosition(QTextCursor::End);
160 
161  builder.currentDocumentData().registerTable(textTable);
162 }
AbstractTableElement & operator=(const AbstractTableElement &other)
void build(ReportBuilder &builder) const override
int rowSpan() const
Qt::AlignmentFlag verticalAlignment() const
Returns the vertical alignment of the cell contents.
CellFormatFunc cellFormatFunction() const
Returns the function passed to setCellFormatFunction()
int columnSpan() const
QBrush background() const
TextDocumentData & currentDocumentData()
static QTextCharFormat::VerticalAlignment toVerticalAlignment(Qt::Alignment alignment)
void build(ReportBuilder &) const override
Cell & cell(int row, int column)
TableElement & operator=(const TableElement &other)
Element * clone() const override
QMap< QPair< int, int >, Cell > CellContentMap

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-reports/
Generated by doxygen 1.9.1