KD Reports API Documentation  2.0
KDReportsHLineTextObject.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 
18 
19 #include <QColor>
20 #include <QPainter>
21 #include <QRectF>
22 #include <QTextDocument>
23 #include <QTextFormat>
24 #include <QTextFrame>
25 
26 #include <QDebug>
27 
28 namespace KDReports {
29 
30 Q_GLOBAL_STATIC(HLineTextObject, globalHLineInterface)
31 
32 void HLineTextObject::registerHLineObjectHandler(QTextDocument *doc)
33 {
34  HLineTextObject *hLineInterface = globalHLineInterface();
35 
36  // This assert is here because a bad build environment can cause this to fail. There is a note
37  // in the Qt source that indicates an error should be output, but there is no such output.
38  Q_ASSERT(qobject_cast<QTextObjectInterface *>(hLineInterface));
39 
40  doc->documentLayout()->registerHandler(HLineTextObject::HLineTextFormat, hLineInterface);
41 }
42 
43 //@cond PRIVATE
44 QSizeF HLineTextObject::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)
45 {
46  Q_UNUSED(posInDocument);
47 
48  QTextFrameFormat f = doc->rootFrame()->frameFormat();
49  const qreal width = doc->pageSize().width() - (f.leftMargin() + f.rightMargin());
50 
51  const double thickness = format.doubleProperty(Thickness);
52  const double margin = format.doubleProperty(Margin);
53  // qDebug() << "intrinsicSize: thickness=" << thickness << "margin=" << margin;
54 
55  return QSizeF(width, thickness + 2 * margin);
56 }
57 
58 void HLineTextObject::drawObject(QPainter *painter, const QRectF &r, QTextDocument *doc, int posInDocument, const QTextFormat &format)
59 {
60  Q_UNUSED(doc);
61  Q_UNUSED(posInDocument);
62 
63  const QColor color = qvariant_cast<QColor>(format.property(Color));
64 
65  QPen pen;
66  pen.setWidthF(format.doubleProperty(Thickness));
67 
68  pen.setColor(color);
69  painter->setPen(pen);
70 
71  const qreal y = r.top() + (r.height() / 2);
72  QLineF line(r.left(), y, r.right(), y);
73 
74  painter->drawLine(line);
75 }
76 //@endcond
77 }
KDReportsHLineTextObject_p.h
KDReports::HLineTextObject::registerHLineObjectHandler
static void registerHLineObjectHandler(QTextDocument *doc)
Definition: KDReportsHLineTextObject.cpp:46
KDReports::HLineTextObject::Color
@ Color
Definition: KDReportsHLineTextObject_p.h:40
KDReports::HLineTextObject::Thickness
@ Thickness
Definition: KDReportsHLineTextObject_p.h:41
KDReports::HLineTextObject::intrinsicSize
QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format) override
KDReports::HLineTextObject::Margin
@ Margin
Definition: KDReportsHLineTextObject_p.h:42
KDReports::HLineTextObject::HLineTextFormat
@ HLineTextFormat
Definition: KDReportsHLineTextObject_p.h:39
KDReports::HLineTextObject::drawObject
void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format) override
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