KD Reports API Documentation  2.0
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  TableElementPrivate()
50  : m_headerRowCount(0)
51  , m_headerColumnCount(0)
52  {
53  }
54  ~TableElementPrivate() { }
55 
56  KDReports::CellContentMap m_cellContentMap;
57  int m_headerRowCount;
58  int m_headerColumnCount;
59 };
60 
62 
64  : d(new TableElementPrivate)
65 {
66 }
67 
69  : AbstractTableElement(other)
70  , d(new TableElementPrivate(*other.d))
71 {
72 }
73 
75 {
76  if (&other == this)
77  return *this;
79  *d = *other.d;
80  return *this;
81 }
82 
84 {
85  delete d;
86 }
87 
89 {
90  // never used at the moment
91  return new TableElement(*this);
92 }
93 
95 {
96  d->m_headerRowCount = count;
97 }
98 
100 {
101  return d->m_headerRowCount;
102 }
103 
105 {
106  d->m_headerColumnCount = count;
107 }
108 
110 {
111  return d->m_headerColumnCount;
112 }
113 
115 {
116  const QPair<int, int> coord = qMakePair(row, column);
117  return d->m_cellContentMap[coord]; // find or create
118 }
119 
121 {
122  if (d->m_cellContentMap.isEmpty())
123  return;
124 
125  QTextCursor &textDocCursor = builder.cursor();
126 
127  int rowCount;
128  int columnCount;
129  d->m_cellContentMap.getSize(rowCount, columnCount);
130 
131  QTextTableFormat tableFormat;
132  tableFormat.setHeaderRowCount(d->m_headerRowCount);
133  tableFormat.setProperty(KDReports::HeaderColumnsProperty, d->m_headerColumnCount);
134  tableFormat.setAlignment(textDocCursor.blockFormat().alignment());
135  tableFormat.setBackground(background());
136  fillTableFormat(tableFormat, textDocCursor);
137  QTextCharFormat charFormat = textDocCursor.charFormat();
138 
139  QTextTable *textTable = textDocCursor.insertTable(rowCount, columnCount, tableFormat);
140 
141  CellContentMap::const_iterator it = d->m_cellContentMap.constBegin();
142  for (; it != d->m_cellContentMap.constEnd(); ++it) {
143  const int row = it.key().first;
144  const int column = it.key().second;
145  const Cell &cell = it.value();
146  if (cell.columnSpan() > 1 || cell.rowSpan() > 1)
147  textTable->mergeCells(row, column, cell.rowSpan(), cell.columnSpan());
148  QTextTableCell tableCell = textTable->cellAt(row, column);
149  Q_ASSERT(tableCell.isValid());
150  QTextCursor cellCursor = tableCell.firstCursorPosition();
151  QTextCharFormat tableCellFormat = charFormat;
152  tableCellFormat.setBackground(cell.background());
153  tableCellFormat.setTableCellColumnSpan(cell.columnSpan());
154  tableCellFormat.setTableCellRowSpan(cell.rowSpan());
155  tableCell.setFormat(tableCellFormat);
156  cellCursor.setCharFormat(tableCellFormat);
157  ReportBuilder cellBuilder(builder.currentDocumentData(), cellCursor, builder.report());
158  cellBuilder.copyStateFrom(builder);
159  cellBuilder.setDefaultFont(charFormat.font());
160  cell.build(cellBuilder);
161  }
162 
163  textDocCursor.movePosition(QTextCursor::End);
164 
165  builder.currentDocumentData().registerTable(textTable);
166 }
KDReportsTableElement.h
KDReports::TextDocumentData::registerTable
void registerTable(QTextTable *table)
Definition: KDReportsTextDocumentData.cpp:243
KDReports::ReportBuilder
Definition: KDReportsReportBuilder_p.h:41
KDReports::ReportBuilder::currentDocumentData
TextDocumentData & currentDocumentData()
Definition: KDReportsReportBuilder_p.h:69
KDReports::ReportBuilder::report
Report * report()
Definition: KDReportsReportBuilder_p.h:53
KDReports::Cell::build
void build(ReportBuilder &builder) const override
Definition: KDReportsCell.cpp:96
KDReports::TableElement::headerRowCount
int headerRowCount() const
Definition: KDReportsTableElement.cpp:99
KDReports::TableElement::clone
Element * clone() const override
Definition: KDReportsTableElement.cpp:88
KDReports::HeaderColumnsProperty
@ HeaderColumnsProperty
Definition: KDReportsTextDocument_p.h:48
KDReports::TableElement::operator=
TableElement & operator=(const TableElement &other)
Definition: KDReportsTableElement.cpp:74
KDReportsTextDocument_p.h
KDReports::Element
Definition: KDReportsElement.h:39
KDReports::ReportBuilder::copyStateFrom
void copyStateFrom(ReportBuilder &parentBuilder)
Definition: KDReportsReportBuilder.cpp:178
KDReports::Element::background
QBrush background() const
Definition: KDReportsElement.cpp:54
KDReports::ReportBuilder::setDefaultFont
void setDefaultFont(const QFont &font)
Definition: KDReportsReportBuilder_p.h:75
KDReports::TableElement
Definition: KDReportsTableElement.h:46
KDReportsReport.h
KDReports::TableElement::cell
Cell & cell(int row, int column)
Definition: KDReportsTableElement.cpp:114
KDReports::Cell
Definition: KDReportsCell.h:52
KDReportsReportBuilder_p.h
KDReports::AbstractTableElement::operator=
AbstractTableElement & operator=(const AbstractTableElement &other)
Definition: KDReportsAbstractTableElement.cpp:57
KDReportsCell.h
KDReports::TableElement::TableElement
TableElement()
Definition: KDReportsTableElement.cpp:63
KDReports::TableElement::headerColumnCount
int headerColumnCount() const
Definition: KDReportsTableElement.cpp:109
KDReports::TableElement::build
void build(ReportBuilder &) const override
Definition: KDReportsTableElement.cpp:120
KDReportsLayoutHelper_p.h
KDReports::Cell::columnSpan
int columnSpan() const
Definition: KDReportsCell.cpp:66
KDReports::TableElement::~TableElement
~TableElement() override
Definition: KDReportsTableElement.cpp:83
KDReports::ReportBuilder::cursor
QTextCursor & cursor()
Definition: KDReportsReportBuilder_p.h:48
KDReports::TableElement::setHeaderRowCount
void setHeaderRowCount(int count)
Definition: KDReportsTableElement.cpp:94
KDReports::Cell::rowSpan
int rowSpan() const
Definition: KDReportsCell.cpp:76
KDReports::TableElement::setHeaderColumnCount
void setHeaderColumnCount(int count)
Definition: KDReportsTableElement.cpp:104
KDReports::AbstractTableElement
Definition: KDReportsAbstractTableElement.h:38
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 Sat Jan 8 2022 02:38:32 for KD Reports API Documentation by doxygen 1.8.17