KD Reports API Documentation  2.0
KDReportsChartTextObject.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 <QTextDocument>
18 
19 #ifdef HAVE_KDCHART
20 #include <KDChartChart>
21 
23 #include "KDReportsLayoutHelper_p.h" // mmToPixels
24 #include "KDReportsUnit.h"
25 
26 namespace KDReports {
27 
28 Q_GLOBAL_STATIC(ChartTextObject, globalChartInterface)
29 
30 void ChartTextObject::registerChartTextObjectHandler(QTextDocument *doc)
31 {
32  ChartTextObject *chartInterface = globalChartInterface();
33 
34  // This assert is here because a bad build environment can cause this to fail. There is a note
35  // in the Qt source that indicates an error should be output, but there is no such output.
36  QTextObjectInterface *iface = qobject_cast<QTextObjectInterface *>(chartInterface);
37  if (!iface) {
38  Q_ASSERT(iface);
39  }
40  doc->documentLayout()->registerHandler(ChartTextObject::ChartObjectTextFormat, chartInterface);
41 }
42 
43 QSizeF ChartTextObject::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)
44 {
45  Q_UNUSED(posInDocument);
46 
47  QSizeF size(format.property(Size).toSizeF());
48 
49  switch (format.property(Unit).toInt()) {
50  case Percent: {
51  const qreal pageWidth = doc->pageSize().width();
52  const qreal percent = size.width();
53  const qreal ratio = size.height() / size.width();
54 
55  qreal newWidth = pageWidth * percent / 100.0;
56 
57  // XXX For some reason 100% width charts will get cropped
58  // need to make a little room
59  if (percent == 100.0)
60  newWidth -= 9;
61 
62  return QSizeF(newWidth, newWidth * ratio);
63  }
64  case Millimeters:
65  return QSizeF(mmToPixels(size.width()), mmToPixels(size.height()));
66  }
67 
68  return QSizeF();
69 }
70 
71 void ChartTextObject::drawObject(QPainter *painter, const QRectF &r, QTextDocument *doc, int posInDocument, const QTextFormat &format)
72 {
73  Q_UNUSED(doc);
74  Q_UNUSED(posInDocument);
75  Q_UNUSED(format);
76 
77  KDChart::Chart *chart = qvariant_cast<KDChart::Chart *>(format.property(ChartObject));
78 
79  chart->paint(painter, r.toRect());
80 }
81 
82 } // namespace
83 
84 #endif
KDReports::Millimeters
@ Millimeters
Millimeters (the default)
Definition: KDReportsUnit.h:54
KDReports::Percent
@ Percent
Percentage of the text width, i.e. the page width minus margins.
Definition: KDReportsUnit.h:55
KDReportsChartTextObject_p.h
KDReports::Unit
Unit
Definition: KDReportsUnit.h:39
KDReports::mmToPixels
KDREPORTS_EXPORT qreal mmToPixels(qreal mm)
Definition: KDReportsLayoutHelper.cpp:23
KDReportsLayoutHelper_p.h
KDReportsUnit.h
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