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

© 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