KD Reports API Documentation  2.2
KDReportsAbstractTableElement.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 
13 #include <QDebug>
14 #include <QTextCursor>
15 #include <QTextTableFormat>
16 
17 class KDReports::AbstractTableElementPrivate
18 {
19 public:
20  void fillConstraints(QTextTableFormat *tableFormat) const;
21 
22  qreal m_border = 1.0;
23  QBrush m_borderBrush = QBrush(Qt::darkGray /*like Qt does*/);
24  qreal m_padding = 0.5;
25  qreal m_width = 0;
27  QFont m_defaultFont;
28  bool m_fontSpecified = false;
29  QVector<KDReports::AbstractTableElement::ColumnConstraint> m_constraints;
30 };
31 
33  : Element()
34  , d(new AbstractTableElementPrivate)
35 {
36 }
37 
39  : Element(other)
40  , d(new AbstractTableElementPrivate(*other.d))
41 {
42 }
43 
45 {
46  if (&other == this)
47  return *this;
48  Element::operator=(other);
49  *d = *other.d;
50  return *this;
51 }
52 
53 void KDReports::AbstractTableElement::setColumnConstraints(const QVector<KDReports::AbstractTableElement::ColumnConstraint> &constraints)
54 {
55  d->m_constraints = constraints;
56 }
57 
59 {
60 }
61 
63 {
64  d->m_border = border;
65 }
66 
68 {
69  return d->m_border;
70 }
71 
73 {
74  d->m_borderBrush = brush;
75 }
76 
78 {
79  return d->m_borderBrush;
80 }
81 
83 {
84  d->m_padding = padding;
85 }
86 
88 {
89  return d->m_padding;
90 }
91 
93 {
94  d->m_width = width;
95  d->m_unit = unit;
96 }
97 
99 {
100  return d->m_width;
101 }
102 
104 {
105  return d->m_unit;
106 }
107 
109 {
110  d->m_defaultFont = font;
111  d->m_fontSpecified = true;
112 }
113 
115 {
116  *isSet = d->m_fontSpecified;
117  return d->m_defaultFont;
118 }
119 
120 void KDReports::AbstractTableElementPrivate::fillConstraints(QTextTableFormat *tableFormat) const
121 {
122  if (!m_constraints.isEmpty()) {
123  QVector<QTextLength> constraints;
124  constraints.reserve(m_constraints.size());
125  for (const auto &c : m_constraints) {
126  QTextLength length; // Variable by default
127  switch (c.unit) {
128  case Millimeters:
129  length = QTextLength(QTextLength::FixedLength, mmToPixels(c.width));
130  break;
131  case Percent:
132  length = QTextLength(QTextLength::PercentageLength, c.width);
133  break;
134  }
135  constraints.append(length);
136  }
137  tableFormat->setColumnWidthConstraints(constraints);
138  }
139 }
140 
141 void KDReports::AbstractTableElement::fillTableFormat(QTextTableFormat &tableFormat, QTextCursor &textDocCursor) const
142 {
143  if (d->m_width) {
144  if (d->m_unit == Millimeters) {
145  tableFormat.setWidth(QTextLength(QTextLength::FixedLength, mmToPixels(d->m_width)));
146  } else {
147  tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, d->m_width));
148  }
149  }
150 
151  d->fillConstraints(&tableFormat);
152 
153  tableFormat.setBorder(border());
154  tableFormat.setBorderBrush(borderBrush());
155  tableFormat.setCellPadding(mmToPixels(padding()));
156  tableFormat.setCellSpacing(0); // HTML-like table borders look so old century
157  if (d->m_fontSpecified) {
158  QTextCharFormat charFormat = textDocCursor.charFormat();
159  charFormat.setFont(d->m_defaultFont);
160  textDocCursor.setCharFormat(charFormat);
161  }
162 }
void setColumnConstraints(const QVector< ColumnConstraint > &constraints)
AbstractTableElement & operator=(const AbstractTableElement &other)
void fillTableFormat(QTextTableFormat &tableFormat, QTextCursor &textDocCursor) const
void setWidth(qreal width, Unit unit=Millimeters)
Element & operator=(const Element &other)
@ Percent
Percentage of the text width, i.e. the page width minus margins.
Definition: KDReportsUnit.h:22
@ Millimeters
Millimeters (the default)
Definition: KDReportsUnit.h:21
KDREPORTS_EXPORT qreal mmToPixels(qreal mm)

© 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