KD Reports API Documentation  2.2
KDReportsFrame.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 "KDReportsFrame.h"
12 #include "KDReportsElement.h"
13 #include "KDReportsElementData_p.h"
16 
17 #include <QDebug>
18 #include <QTextFrame>
19 
20 class KDReports::FramePrivate
21 {
22 public:
23  FramePrivate()
24  : m_width(0)
25  , m_height(0)
26  , m_padding(0.5)
27  , m_border(1.0)
28  , m_widthUnit(KDReports::Millimeters)
29  , m_heightUnit(KDReports::Millimeters)
30  {
31  }
32  ~FramePrivate()
33  {
34  }
36  qreal m_width;
37  qreal m_height;
38  qreal m_padding;
39  qreal m_border;
40  KDReports::Unit m_widthUnit;
41  KDReports::Unit m_heightUnit;
43 };
44 
46  : d(new FramePrivate)
47 {
48 }
49 
51  : Element(other)
52  , d(new FramePrivate(*other.d))
53 {
54 }
55 
57 {
58  if (&other == this)
59  return *this;
60  Element::operator=(other);
61  *d = *other.d;
62  return *this;
63 }
64 
66 {
67  d->m_width = width;
68  d->m_widthUnit = unit;
69 }
70 
72 {
73  d->m_height = height;
74  d->m_heightUnit = unit;
75 }
76 
77 void KDReports::Frame::setPadding(qreal padding)
78 {
79  d->m_padding = padding;
80 }
81 
83 {
84  return d->m_padding;
85 }
86 
87 void KDReports::Frame::setBorder(qreal border)
88 {
89  d->m_border = border;
90 }
91 
93 {
94  return d->m_border;
95 }
96 
98 {
99  d->m_position = pos;
100 }
101 
103 {
104  return d->m_position;
105 }
106 
108 {
109 }
110 
112 {
113  d->m_elements.append(KDReports::ElementData(element.clone()));
114 }
115 
116 void KDReports::Frame::addElement(const Element &element, Qt::AlignmentFlag horizontalAlignment)
117 {
118  d->m_elements.append(KDReports::ElementData(element.clone(), horizontalAlignment));
119 }
120 
122 {
123  d->m_elements.append(KDReports::ElementData(variable));
124 }
125 
127 {
128  // prepare the frame
129  QTextFrameFormat format;
130  if (d->m_width) {
131  if (d->m_widthUnit == Millimeters) {
132  format.setWidth(QTextLength(QTextLength::FixedLength, mmToPixels(d->m_width)));
133  } else {
134  format.setWidth(QTextLength(QTextLength::PercentageLength, d->m_width));
135  }
136  }
137  if (d->m_height) {
138  if (d->m_heightUnit == Millimeters) {
139  format.setHeight(QTextLength(QTextLength::FixedLength, mmToPixels(d->m_height)));
140  } else {
141  format.setHeight(QTextLength(QTextLength::PercentageLength, d->m_height));
142  }
143  }
144 
145  format.setPadding(mmToPixels(padding()));
146  format.setBorder(d->m_border);
147  // TODO borderBrush like in AbstractTableElement
148  format.setPosition(static_cast<QTextFrameFormat::Position>(d->m_position)); // those enums have the same value
149 
150  QTextCursor &textDocCursor = builder.cursor();
151 
152  QTextFrame *frame = textDocCursor.insertFrame(format);
153 
154  QTextCursor contentsCursor = frame->firstCursorPosition();
155 
156  ReportBuilder contentsBuilder(builder.currentDocumentData(), contentsCursor, builder.report());
157  contentsBuilder.copyStateFrom(builder);
158 
159  foreach (const KDReports::ElementData &ed, d->m_elements) {
160  switch (ed.m_type) {
162  contentsBuilder.addInlineElement(*ed.m_element);
163  break;
165  contentsBuilder.addBlockElement(*ed.m_element, ed.m_align);
166  break;
168  contentsBuilder.addVariable(ed.m_variableType);
169  break;
171  contentsBuilder.addVerticalSpacing(ed.m_value);
172  break;
173  }
174  }
175 
176  textDocCursor.movePosition(QTextCursor::End);
177 }
178 
180 {
181  return new Frame(*this);
182 }
virtual Element * clone() const =0
Element & operator=(const Element &other)
void addVariable(VariableType variable)
void setBorder(qreal border)
@ InFlow
The frame is part of the flow of the paragraph (default)
qreal padding() const
Frame & operator=(const Frame &other)
void setWidth(qreal width, Unit unit=Millimeters)
Element * clone() const override
void setPosition(Position pos)
qreal border() const
void addInlineElement(const Element &element)
void setHeight(qreal height, Unit unit=Millimeters)
void addElement(const Element &element, Qt::AlignmentFlag horizontalAlignment=Qt::AlignLeft)
void setPadding(qreal padding)
void build(ReportBuilder &builder) const override
Position position() const
TextDocumentData & currentDocumentData()
virtual void addVerticalSpacing(qreal space)
virtual void addBlockElement(const Element &element, Qt::AlignmentFlag horizontalAlignment, const QColor &backgroundColor=QColor())
virtual void addVariable(KDReports::VariableType variable)
virtual void addInlineElement(const Element &element)
void copyStateFrom(const ReportBuilder &parentBuilder)
@ Millimeters
Millimeters (the default)
Definition: KDReportsUnit.h:21
KDREPORTS_EXPORT qreal mmToPixels(qreal mm)
KDReports::VariableType m_variableType

© 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