19#include "KReportItemText.h"
20#include "KReportRenderObjects.h"
21#include "KReportUtils.h"
22#include "KReportUtils_p.h"
23#include "kreportplugin_debug.h"
25#include <KPropertyListData>
26#include <KPropertySet>
29#include <QApplication>
31#include <QFontMetrics>
32#include <QDomNodeList>
33#include <QRegularExpression>
35KReportItemText::KReportItemText()
36 : m_bottomPadding(0.0)
41KReportItemText::KReportItemText(
const QDomNode & element)
44 nameProperty()->setValue(KReportUtils::readNameAttribute(element.
toElement()));
57 for (
int i = 0; i < nl.
count(); i++) {
62 KReportTextStyleData ts;
63 if (parseReportTextStyleData(node.
toElement(), &ts)) {
64 m_backgroundColor->setValue(ts.backgroundColor);
65 m_foregroundColor->setValue(ts.foregroundColor);
66 m_backgroundOpacity->setValue(ts.backgroundOpacity);
67 m_font->setValue(ts.font);
72 if (parseReportLineStyleData(node.
toElement(), &ls)) {
73 m_lineWeight->setValue(ls.
weight());
74 m_lineColor->setValue(ls.
color());
75 m_lineStyle->setValue(
static_cast<int>(ls.
penStyle()));
78 kreportpluginWarning() <<
"while parsing field element encountered unknown element: " << n;
84KReportItemText::~KReportItemText()
92 t = m_horizontalAlignment->value().toString();
93 if (t == QLatin1String(
"center"))
95 else if (t == QLatin1String(
"right"))
100 t = m_verticalAlignment->value().toString();
101 if (t == QLatin1String(
"center"))
103 else if (t == QLatin1String(
"bottom"))
111void KReportItemText::createProperties()
113 createDataSourceProperty();
115 m_itemValue =
new KProperty(
"value", QString(),
tr(
"Value"),
tr(
"Value used if not bound to a field"));
117 KPropertyListData *listData =
new KPropertyListData(
118 { QLatin1String(
"left"), QLatin1String(
"center"), QLatin1String(
"right") },
119 QVariantList{
tr(
"Left"),
tr(
"Center"),
tr(
"Right") });
120 m_horizontalAlignment =
new KProperty(
"horizontal-align", listData, QLatin1String(
"left"),
121 tr(
"Horizontal Alignment"));
123 listData =
new KPropertyListData(
124 { QLatin1String(
"top"), QLatin1String(
"center"), QLatin1String(
"bottom") },
125 QVariantList{
tr(
"Top"),
tr(
"Center"),
tr(
"Bottom") });
126 m_verticalAlignment =
new KProperty(
"vertical-align", listData, QLatin1String(
"center"),
127 tr(
"Vertical Alignment"));
131 m_backgroundColor =
new KProperty(
"background-color", QColor(
Qt::white),
tr(
"Background Color"));
132 m_foregroundColor =
new KProperty(
"foreground-color", QColor(
Qt::black),
tr(
"Foreground Color"));
134 m_lineWeight =
new KProperty(
"line-weight", 1.0,
tr(
"Line Weight"));
135 m_lineWeight->setOption(
"step", 1.0);
136 m_lineColor =
new KProperty(
"line-color", QColor(
Qt::black),
tr(
"Line Color"));
138 m_backgroundOpacity =
new KProperty(
"background-opacity", QVariant(0),
tr(
"Background Opacity"));
139 m_backgroundOpacity->setOption(
"max", 100);
140 m_backgroundOpacity->setOption(
"min", 0);
141 m_backgroundOpacity->setOption(
"suffix", QLatin1String(
"%"));
143 propertySet()->addProperty(m_itemValue);
144 propertySet()->addProperty(m_horizontalAlignment);
145 propertySet()->addProperty(m_verticalAlignment);
146 propertySet()->addProperty(m_font);
147 propertySet()->addProperty(m_backgroundColor);
148 propertySet()->addProperty(m_foregroundColor);
149 propertySet()->addProperty(m_backgroundOpacity);
150 propertySet()->addProperty(m_lineWeight);
151 propertySet()->addProperty(m_lineColor);
152 propertySet()->addProperty(m_lineStyle);
156qreal KReportItemText::bottomPadding()
const
158 return m_bottomPadding;
161void KReportItemText::setBottomPadding(qreal bp)
163 if (m_bottomPadding != bp) {
164 m_bottomPadding = bp;
168KReportTextStyleData KReportItemText::textStyle()
const
170 KReportTextStyleData d;
171 d.backgroundColor = m_backgroundColor->value().value<QColor>();
172 d.foregroundColor = m_foregroundColor->value().value<QColor>();
173 d.font = m_font->value().value<QFont>();
174 d.backgroundOpacity = m_backgroundOpacity->value().toInt();
181 ls.setWeight(m_lineWeight->value().toReal());
182 ls.setColor(m_lineColor->value().value<QColor>());
183 ls.setPenStyle((
Qt::PenStyle)m_lineStyle->value().toInt());
188QString KReportItemText::typeName()
const
190 return QLatin1String(
"text");
194 const QVariant &data, KReportScriptHandler *script)
206 qstrValue = m_itemValue->value().toString();
213 QRectF trf(pos, siz);
214 qreal intStretch = trf.top() - offset.
y();
221 QRegularExpression re(QLatin1String(
"\\s"));
222 const QFontMetricsF fm(font(), KReportPrivate::highResolutionPrinter());
224 const int intRectWidth
225 = (int)((
size().width() / 72) * KReportPrivate::highResolutionPrinter()->resolution());
226 int intLineCounter = 0;
227 qreal intBaseTop = trf.top();
228 qreal intRectHeight = trf.height();
231 QRegularExpressionMatch
match = re.match(qstrValue);
232 int idx =
match.capturedStart(pos);
235 separator = QLatin1Char(
'\n');
237 separator = qstrValue.
at(idx);
239 if (fm.boundingRect(qstrValue.
left(idx)).width() < intRectWidth || pos == 0) {
241 if (separator == QLatin1Char(
'\n')) {
242 QString line = qstrValue.
left(idx);
244 qstrValue.
remove(0, idx + 1);
248 rect.
setTop(intBaseTop + (intLineCounter * intRectHeight));
251 OROTextBox * tb =
new OROTextBox();
252 tb->setPosition(rect.
topLeft());
253 tb->setSize(rect.
size());
256 tb->setFlags(textFlags());
257 tb->setTextStyle(textStyle());
258 tb->setLineStyle(lineStyle());
261 page->insertPrimitive(tb);
265 OROTextBox *tb2 =
dynamic_cast<OROTextBox*
>(tb->clone());
268 section->addPrimitive(tb2);
276 intStretch += intRectHeight;
280 QString line = qstrValue.
left(pos - 1);
284 rect.
setTop(intBaseTop + (intLineCounter * intRectHeight));
287 OROTextBox * tb =
new OROTextBox();
288 tb->setPosition(rect.
topLeft());
289 tb->setSize(rect.
size());
292 tb->setFlags(textFlags());
293 tb->setTextStyle(textStyle());
294 tb->setLineStyle(lineStyle());
296 page->insertPrimitive(tb);
301 intStretch += intRectHeight;
306 intStretch += (m_bottomPadding / 100.0);
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
QPointF position() const
Return the position in points.
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
QSizeF size() const
Return the size in points.
QString itemDataSource() const
The KReportLineStyle class represents line style.
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
Represents a single a single row in a document and may contain zero or more OROPrimitives.
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
QString attribute(const QString &name, const QString &defValue) const const
QDomNodeList childNodes() const const
QString nodeName() const const
QDomElement toElement() const const
QDomNode item(int index) const const
QString tr(const char *sourceText, const char *disambiguation, int n)
QSizeF size() const const
QPointF topLeft() const const
const QChar at(qsizetype position) const const
bool isEmpty() const const
QString left(qsizetype n) const const
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
double toDouble(bool *ok) const const
QString toString() const const