KD Reports API Documentation  2.1
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-2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDAB-KDReports OR LicenseRef-KDAB-KDReports-US
8 **
9 ** Licensees holding valid commercial KD Reports licenses may use this file in
10 ** accordance with the KD Reports Commercial License Agreement provided with
11 ** the Software.
12 **
13 ** Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 **
15 ****************************************************************************/
16 
17 #include "KDReportsTableElement.h"
18 #include "KDReportsCell.h"
20 #include "KDReportsReport.h"
23 #include <QAbstractTextDocumentLayout>
24 #include <QDebug>
25 #include <QPainter>
26 #include <QTextCursor>
27 #include <QTextTableCell>
28 
29 namespace KDReports {
30 class CellContentMap : public QMap<QPair<int /*row*/, int /*column*/>, Cell>
31 {
32 public:
33  CellContentMap() { }
34  void getSize(int &rows, int &columns) const
35  {
36  rows = 0;
37  columns = 0;
38  for (const_iterator it = begin(); it != end(); ++it) {
39  rows = qMax(rows, it.key().first + 1);
40  columns = qMax(columns, it.key().second + 1);
41  }
42  }
43 };
44 }
45 
46 class KDReports::TableElementPrivate
47 {
48 public:
49  KDReports::CellContentMap m_cellContentMap;
50  int m_headerRowCount = 0;
51  int m_headerColumnCount = 0;
52 };
53 
55 
57  : d(new TableElementPrivate)
58 {
59 }
60 
62  : AbstractTableElement(other)
63  , d(new TableElementPrivate(*other.d))
64 {
65 }
66 
68 {
69  if (&other == this)
70  return *this;
72  *d = *other.d;
73  return *this;
74 }
75 
77 {
78 }
79 
81 {
82  // never used at the moment
83  return new TableElement(*this);
84 }
85 
87 {
88  d->m_headerRowCount = count;
89 }
90 
92 {
93  return d->m_headerRowCount;
94 }
95 
97 {
98  d->m_headerColumnCount = count;
99 }
100 
102 {
103  return d->m_headerColumnCount;
104 }
105 
107 {
108  const QPair<int, int> coord = qMakePair(row, column);
109  return d->m_cellContentMap[coord]; // find or create
110 }
111 
113 {
114  if (d->m_cellContentMap.isEmpty())
115  return;
116 
117  QTextCursor &textDocCursor = builder.cursor();
118 
119  int rowCount;
120  int columnCount;
121  d->m_cellContentMap.getSize(rowCount, columnCount);
122 
123  QTextTableFormat tableFormat;
124  tableFormat.setHeaderRowCount(d->m_headerRowCount);
125  tableFormat.setProperty(KDReports::HeaderColumnsProperty, d->m_headerColumnCount);
126  tableFormat.setAlignment(textDocCursor.blockFormat().alignment());
127  tableFormat.setBackground(background());
128  fillTableFormat(tableFormat, textDocCursor);
129  QTextCharFormat charFormat = textDocCursor.charFormat();
130 
131  QTextTable *textTable = textDocCursor.insertTable(rowCount, columnCount, tableFormat);
132 
133  CellContentMap::const_iterator it = d->m_cellContentMap.constBegin();
134  for (; it != d->m_cellContentMap.constEnd(); ++it) {
135  const int row = it.key().first;
136  const int column = it.key().second;
137  const Cell &cell = it.value();
138  if (cell.columnSpan() > 1 || cell.rowSpan() > 1)
139  textTable->mergeCells(row, column, cell.rowSpan(), cell.columnSpan());
140  QTextTableCell tableCell = textTable->cellAt(row, column);
141  Q_ASSERT(tableCell.isValid());
142  QTextCursor cellCursor = tableCell.firstCursorPosition();
143  QTextCharFormat tableCellFormat = charFormat;
144  if (cell.background().style() != Qt::NoBrush)
145  tableCellFormat.setBackground(cell.background());
146  tableCellFormat.setTableCellColumnSpan(cell.columnSpan());
147  tableCellFormat.setTableCellRowSpan(cell.rowSpan());
148  tableCell.setFormat(tableCellFormat);
149  cellCursor.setCharFormat(tableCellFormat);
150  ReportBuilder cellBuilder(builder.currentDocumentData(), cellCursor, builder.report());
151  cellBuilder.copyStateFrom(builder);
152  cellBuilder.setDefaultFont(charFormat.font());
153  cell.build(cellBuilder);
154  }
155 
156  textDocCursor.movePosition(QTextCursor::End);
157 
158  builder.currentDocumentData().registerTable(textTable);
159 }
KDReportsTableElement.h
KDReports::HeaderColumnsProperty
@ HeaderColumnsProperty
Definition: KDReportsTextDocument_p.h:48
KDReports::TextDocumentData::registerTable
void registerTable(QTextTable *table)
Definition: KDReportsTextDocumentData.cpp:240
KDReports::ReportBuilder
Definition: KDReportsReportBuilder_p.h:42
QTextCharFormat::font
QFont font() const const
KDReports::ReportBuilder::currentDocumentData
TextDocumentData & currentDocumentData()
Definition: KDReportsReportBuilder_p.h:69
KDReports::ReportBuilder::report
Report * report()
Definition: KDReportsReportBuilder_p.h:53
QTextCharFormat
KDReports::Cell::build
void build(ReportBuilder &builder) const override
Definition: KDReportsCell.cpp:89
KDReports::TableElement::headerRowCount
int headerRowCount() const
Definition: KDReportsTableElement.cpp:91
QTextTableCell::firstCursorPosition
QTextCursor firstCursorPosition() const const
QTextTableFormat::setHeaderRowCount
void setHeaderRowCount(int count)
KDReports::TableElement::clone
Element * clone() const override
Definition: KDReportsTableElement.cpp:80
QMap< QPair< int, int >, Cell >::begin
QMap::iterator begin()
QTextTableCell::setFormat
void setFormat(const QTextCharFormat &format)
KDReports::TableElement::operator=
TableElement & operator=(const TableElement &other)
Definition: KDReportsTableElement.cpp:67
QBrush::style
Qt::BrushStyle style() const const
QTextTable
KDReportsTextDocument_p.h
KDReports::Element
Definition: KDReportsElement.h:41
QTextBlockFormat::alignment
Qt::Alignment alignment() const const
KDReports::Element::background
QBrush background() const
Definition: KDReportsElement.cpp:53
QTextFormat::setBackground
void setBackground(const QBrush &brush)
KDReports::ReportBuilder::setDefaultFont
void setDefaultFont(const QFont &font)
Definition: KDReportsReportBuilder_p.h:75
KDReports::TableElement
Definition: KDReportsTableElement.h:33
QMap< QPair< int, int >, Cell >::end
QMap::iterator end()
QTextTable::cellAt
QTextTableCell cellAt(int row, int column) const const
QTextCursor::setCharFormat
void setCharFormat(const QTextCharFormat &format)
QTextTableCell::isValid
bool isValid() const const
KDReportsReport.h
KDReports::TableElement::cell
Cell & cell(int row, int column)
Definition: KDReportsTableElement.cpp:106
KDReports::Cell
Definition: KDReportsCell.h:40
KDReportsReportBuilder_p.h
QTextFormat::setProperty
void setProperty(int propertyId, const QVariant &value)
QTextCursor::blockFormat
QTextBlockFormat blockFormat() const const
QTextCursor::insertTable
QTextTable * insertTable(int rows, int columns, const QTextTableFormat &format)
QMap
KDReports::AbstractTableElement::operator=
AbstractTableElement & operator=(const AbstractTableElement &other)
Definition: KDReportsAbstractTableElement.cpp:48
QTextTableFormat
KDReportsCell.h
KDReports::TableElement::TableElement
TableElement()
Definition: KDReportsTableElement.cpp:56
KDReports::TableElement::headerColumnCount
int headerColumnCount() const
Definition: KDReportsTableElement.cpp:101
KDReports::TableElement::build
void build(ReportBuilder &) const override
Definition: KDReportsTableElement.cpp:112
KDReportsLayoutHelper_p.h
KDReports::Cell::columnSpan
int columnSpan() const
Definition: KDReportsCell.cpp:59
QTextCursor::End
End
KDReports::TableElement::~TableElement
~TableElement() override
Definition: KDReportsTableElement.cpp:76
KDReports::ReportBuilder::cursor
QTextCursor & cursor()
Definition: KDReportsReportBuilder_p.h:48
KDReports::ReportBuilder::copyStateFrom
void copyStateFrom(const ReportBuilder &parentBuilder)
Definition: KDReportsReportBuilder.cpp:180
KDReports::TableElement::setHeaderRowCount
void setHeaderRowCount(int count)
Definition: KDReportsTableElement.cpp:86
KDReports::Cell::rowSpan
int rowSpan() const
Definition: KDReportsCell.cpp:69
QTextTableFormat::setAlignment
void setAlignment(Qt::Alignment alignment)
KDReports::TableElement::setHeaderColumnCount
void setHeaderColumnCount(int count)
Definition: KDReportsTableElement.cpp:96
QTextCursor::movePosition
bool movePosition(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode, int n)
KDReports::AbstractTableElement
Definition: KDReportsAbstractTableElement.h:39
QPair
Qt::NoBrush
NoBrush
QTextTable::mergeCells
void mergeCells(int row, int column, int numRows, int numCols)
QTextCursor
QTextTableCell
QTextCursor::charFormat
QTextCharFormat charFormat() const const
KDReports
Definition: KDReportsAbstractReportLayout_p.h:30

© 2007-2021 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 on Fri Jul 15 2022 13:09:07 for KD Reports API Documentation by doxygen 1.8.20