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