0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-25 00:54:39 +03:00
LimeReport/limereport/items/lrtextitem.cpp

1058 lines
32 KiB
C++
Raw Normal View History

2016-02-17 10:11:00 +03:00
/***************************************************************************
* This file is part of the Lime Report project *
2021-08-18 20:21:36 +03:00
* Copyright (C) 2021 by Alexander Arin *
2016-02-17 10:11:00 +03:00
* arin_a@bk.ru *
* *
** GNU General Public License Usage **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
** GNU Lesser General Public License **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library. *
* If not, see <http://www.gnu.org/licenses/>. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
****************************************************************************/
#include "lrtextitem.h"
#include "lrdatasourcemanager.h"
2016-02-17 10:11:00 +03:00
#include "lrdesignelementsfactory.h"
#include "lrglobal.h"
#include "lrpagedesignintf.h"
#include "lrreportengine_p.h"
2016-02-17 10:11:00 +03:00
#include "lrsimpletagparser.h"
#include "lrtextitemeditor.h"
#include <QLocale>
#include <QMenu>
#include <QMessageBox>
#include <QTextLayout>
#include <QtGui>
2016-02-17 10:11:00 +03:00
#include <math.h>
namespace {
2016-02-17 10:11:00 +03:00
const QString xmlTag = "TextItem";
LimeReport::BaseDesignIntf* createTextItem(QObject* owner, LimeReport::BaseDesignIntf* parent)
{
return new LimeReport::TextItem(owner, parent);
2016-02-17 10:11:00 +03:00
}
bool VARIABLE_IS_NOT_USED registred = LimeReport::DesignElementsFactory::instance().registerCreator(
xmlTag, LimeReport::ItemAttribs(QObject::tr("Text Item"), "TextItem"), createTextItem);
2016-02-17 10:11:00 +03:00
} // namespace
2016-02-17 10:11:00 +03:00
namespace LimeReport {
2016-02-17 10:11:00 +03:00
TextItem::TextItem(QObject* owner, QGraphicsItem* parent):
ContentItemDesignIntf(xmlTag, owner, parent),
m_angle(Angle0),
m_trimValue(true),
m_allowHTML(false),
m_allowHTMLInFields(false),
m_replaceCarriageReturns(false),
m_followTo(""),
m_follower(0),
m_textIndent(0),
m_textLayoutDirection(Qt::LayoutDirectionAuto),
m_hideIfEmpty(false),
m_fontLetterSpacing(0)
{
2016-11-01 14:56:06 +03:00
PageItemDesignIntf* pageItem = dynamic_cast<PageItemDesignIntf*>(parent);
BaseDesignIntf* parentItem = dynamic_cast<BaseDesignIntf*>(parent);
while (!pageItem && parentItem) {
2016-11-01 14:56:06 +03:00
parentItem = dynamic_cast<BaseDesignIntf*>(parentItem->parentItem());
pageItem = dynamic_cast<PageItemDesignIntf*>(parentItem);
}
if (pageItem) {
2016-11-01 14:56:06 +03:00
QFont defaultFont = pageItem->font();
setFont(defaultFont);
}
2016-02-17 10:11:00 +03:00
Init();
}
TextItem::~TextItem() { }
2016-02-17 10:11:00 +03:00
void TextItem::preparePopUpMenu(QMenu& menu)
{
QAction* editAction = menu.addAction(QIcon(":/report/images/edit_pecil2.png"), tr("Edit"));
menu.insertAction(menu.actions().at(0), editAction);
menu.insertSeparator(menu.actions().at(1));
menu.addSeparator();
QAction* action = menu.addAction(tr("Auto height"));
action->setCheckable(true);
action->setChecked(autoHeight());
action = menu.addAction(tr("Allow HTML"));
action->setCheckable(true);
action->setChecked(allowHTML());
action = menu.addAction(tr("Allow HTML in fields"));
action->setCheckable(true);
action->setChecked(allowHTMLInFields());
2017-02-28 23:22:48 +03:00
action = menu.addAction(tr("Stretch to max height"));
action->setCheckable(true);
action->setChecked(stretchToMaxHeight());
action = menu.addAction(tr("Transparent"));
action->setCheckable(true);
action->setChecked(backgroundMode() == TransparentMode);
action = menu.addAction(tr("Watermark"));
action->setCheckable(true);
action->setChecked(isWatermark());
2018-06-21 14:29:00 +03:00
action = menu.addAction(tr("Hide if empty"));
action->setCheckable(true);
action->setChecked(hideIfEmpty());
}
void TextItem::processPopUpAction(QAction* action)
{
if (action->text().compare(tr("Edit")) == 0) {
this->showEditorDialog();
}
if (page()) {
if (action->text().compare(tr("Auto height")) == 0) {
page()->setPropertyToSelectedItems("autoHeight", action->isChecked());
2019-01-18 20:18:45 +03:00
}
if (action->text().compare(tr("Allow HTML")) == 0) {
page()->setPropertyToSelectedItems("allowHTML", action->isChecked());
2019-01-18 20:18:45 +03:00
}
if (action->text().compare(tr("Allow HTML in fields")) == 0) {
page()->setPropertyToSelectedItems("allowHTMLInFields", action->isChecked());
2019-01-18 20:18:45 +03:00
}
if (action->text().compare(tr("Stretch to max height")) == 0) {
page()->setPropertyToSelectedItems("stretchToMaxHeight", action->isChecked());
2019-01-18 20:18:45 +03:00
}
}
if (action->text().compare(tr("Transparent")) == 0) {
if (action->isChecked()) {
setProperty("backgroundMode", TransparentMode);
} else {
setProperty("backgroundMode", OpaqueMode);
}
}
if (action->text().compare(tr("Watermark")) == 0) {
page()->setPropertyToSelectedItems("watermark", action->isChecked());
}
2018-06-21 14:29:00 +03:00
if (action->text().compare(tr("Hide if empty")) == 0) {
page()->setPropertyToSelectedItems("hideIfEmpty", action->isChecked());
2018-06-21 14:29:00 +03:00
}
ContentItemDesignIntf::processPopUpAction(action);
}
void TextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* style, QWidget* widget)
{
2016-02-17 10:11:00 +03:00
Q_UNUSED(widget);
Q_UNUSED(style);
2016-12-13 00:07:50 +03:00
TextPtr text = textDocument();
2016-02-17 10:39:17 +03:00
painter->save();
2016-02-17 10:11:00 +03:00
2016-02-17 10:39:17 +03:00
setupPainter(painter);
prepareRect(painter, style, widget);
2016-02-17 10:11:00 +03:00
QSizeF tmpSize = rect().size() - text->size();
2016-02-17 10:11:00 +03:00
if (!painter->clipRegion().isEmpty()) {
QRegion clipReg
= painter->clipRegion().xored(painter->clipRegion().subtracted(rect().toRect()));
2016-02-17 10:39:17 +03:00
painter->setClipRegion(clipReg);
2016-02-17 10:11:00 +03:00
} else {
2016-02-17 10:39:17 +03:00
painter->setClipRect(rect());
2016-02-17 10:11:00 +03:00
}
2016-12-13 00:07:50 +03:00
qreal hOffset = 0, vOffset = 0;
switch (m_angle) {
case Angle0:
hOffset = marginSize();
if ((tmpSize.height() > 0) && (m_alignment & Qt::AlignVCenter)) {
vOffset = tmpSize.height() / 2;
}
if ((tmpSize.height() > 0) && (m_alignment & Qt::AlignBottom)) // allow html
vOffset = tmpSize.height();
painter->translate(hOffset, vOffset);
2016-02-17 10:39:17 +03:00
break;
case Angle90:
hOffset = width() - marginSize();
vOffset = marginSize();
if (m_alignment & Qt::AlignVCenter) {
hOffset = (width() - text->size().height()) / 2 + text->size().height();
}
2016-02-17 10:39:17 +03:00
if (m_alignment & Qt::AlignBottom) {
hOffset = (text->size().height());
}
painter->translate(hOffset, vOffset);
painter->rotate(90);
2016-02-17 10:39:17 +03:00
break;
case Angle180:
hOffset = width() - marginSize();
vOffset = height() - marginSize();
if ((tmpSize.width() > 0) && (m_alignment & Qt::AlignVCenter)) {
vOffset = tmpSize.height() / 2 + text->size().height();
}
if ((tmpSize.height() > 0) && (m_alignment & Qt::AlignBottom)) {
vOffset = (text->size().height());
}
painter->translate(hOffset, vOffset);
painter->rotate(180);
2016-02-17 10:39:17 +03:00
break;
case Angle270:
hOffset = marginSize();
vOffset = height() - marginSize();
if (m_alignment & Qt::AlignVCenter) {
hOffset = (width() - text->size().height()) / 2;
}
2016-02-17 10:39:17 +03:00
if (m_alignment & Qt::AlignBottom) {
hOffset = (width() - text->size().height());
}
painter->translate(hOffset, vOffset);
painter->rotate(270);
2016-02-17 10:39:17 +03:00
break;
case Angle45:
painter->translate(width() / 2, 0);
painter->rotate(45);
text->setTextWidth(sqrt(2 * (pow(width() / 2, 2))));
2016-02-17 10:39:17 +03:00
break;
case Angle315:
painter->translate(0, height() / 2);
painter->rotate(315);
text->setTextWidth(sqrt(2 * (pow(height() / 2, 2))));
2016-02-17 10:39:17 +03:00
break;
2016-02-17 10:11:00 +03:00
}
2016-06-10 18:05:18 +03:00
int lineHeight = painter->fontMetrics().height();
qreal curpos = 0;
2016-02-17 10:39:17 +03:00
if (m_underlines) {
2016-06-10 18:05:18 +03:00
QPen pen = painter->pen();
pen.setWidth(m_underlineLineSize);
painter->setPen(pen);
}
2016-02-17 10:11:00 +03:00
painter->setOpacity(qreal(foregroundOpacity()) / 100);
2016-02-17 10:39:17 +03:00
QAbstractTextDocumentLayout::PaintContext ctx;
ctx.palette.setColor(QPalette::Text, fontColor());
2016-12-13 00:07:50 +03:00
for (QTextBlock it = text->begin(); it != text->end(); it = it.next()) {
it.blockFormat().setLineHeight(m_lineSpacing, QTextBlockFormat::LineDistanceHeight);
for (int i = 0; i < it.layout()->lineCount(); i++) {
2016-06-10 18:05:18 +03:00
QTextLine line = it.layout()->lineAt(i);
if (m_underlines) {
painter->drawLine(QPointF(0, line.rect().bottomLeft().y()),
QPoint(rect().width(), line.rect().bottomRight().y()));
lineHeight = line.height() + m_lineSpacing;
2016-06-10 18:05:18 +03:00
curpos = line.rect().bottom();
}
}
}
2016-12-13 00:07:50 +03:00
text->documentLayout()->draw(painter, ctx);
2016-02-17 10:11:00 +03:00
if (m_underlines) {
if (lineHeight < 0)
lineHeight = painter->fontMetrics().height();
for (curpos += lineHeight; curpos < rect().height(); curpos += lineHeight) {
painter->drawLine(QPointF(0, curpos), QPoint(rect().width(), curpos));
2016-06-10 18:05:18 +03:00
}
}
2016-02-17 10:39:17 +03:00
painter->restore();
BaseDesignIntf::paint(painter, style, widget);
2016-02-17 10:11:00 +03:00
}
void TextItem::Init()
{
2016-12-13 00:07:50 +03:00
m_autoWidth = NoneAutoWidth;
m_alignment = Qt::AlignLeft | Qt::AlignTop;
2016-12-13 00:07:50 +03:00
m_autoHeight = false;
m_textSize = QSizeF();
m_firstLineSize = 0;
2016-02-17 10:11:00 +03:00
m_foregroundOpacity = 100;
2016-06-10 18:05:18 +03:00
m_underlines = false;
m_adaptFontToSize = false;
m_underlineLineSize = 1;
m_lineSpacing = 1;
m_valueType = Default;
2016-02-17 10:11:00 +03:00
}
void TextItem::setContent(const QString& value)
2016-02-17 10:11:00 +03:00
{
if (m_strText.compare(value) != 0) {
2016-02-17 10:11:00 +03:00
QString oldValue = m_strText;
2016-12-13 00:07:50 +03:00
m_strText = value;
2016-02-17 10:39:17 +03:00
if (!isLoading()) {
2017-01-23 11:53:07 +03:00
if (autoHeight() || autoWidth() || hasFollower())
initTextSizes();
2016-06-10 18:05:18 +03:00
update(rect());
notify("content", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
}
QString TextItem::content() const { return m_strText; }
2016-02-17 10:28:27 +03:00
void TextItem::updateItemSize(DataSourceManager* dataManager, RenderPass pass, int maxHeight)
2016-02-17 10:11:00 +03:00
{
2016-02-17 10:11:00 +03:00
if (isNeedExpandContent())
2016-02-17 10:28:27 +03:00
expandContent(dataManager, pass);
if (!isLoading() && (autoHeight() || autoWidth() || hasFollower()))
2016-12-13 00:07:50 +03:00
initTextSizes();
2016-02-17 10:11:00 +03:00
if (m_textSize.width() > width()
&& ((m_autoWidth == MaxWordLength) || (m_autoWidth == MaxStringLength))) {
setWidth(m_textSize.width() + marginSize() * 2);
2016-02-17 10:11:00 +03:00
}
if (m_textSize.height() > height()) {
if (m_autoHeight)
setHeight(m_textSize.height() + borderLineSize() * 2);
else if (hasFollower() && !content().isEmpty()) {
follower()->setContent(getTextPart(0, height()));
setContent(getTextPart(height(), 0));
}
2016-02-17 10:11:00 +03:00
}
2016-02-17 10:28:27 +03:00
BaseDesignIntf::updateItemSize(dataManager, pass, maxHeight);
if (isEmpty() && hideIfEmpty())
setVisible(false);
2016-02-17 10:11:00 +03:00
}
void TextItem::updateLayout()
{
// m_layout.setFont(transformToSceneFont(font()));
// m_layout.setText(content());
// qreal linePos = 0;
// m_layout.beginLayout();
// while(true){
// QTextLine line = m_layout.createLine();
// if (!line.isValid()) break;
// line.setLineWidth(width()-marginSize()*2);
// line.setPosition(QPoint(marginSize(),linePos));
// linePos+=line.height();
// }
// m_layout.endLayout();
2016-02-17 10:11:00 +03:00
}
bool TextItem::isNeedExpandContent() const
{
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
2016-02-17 10:11:00 +03:00
QRegExp rx("$*\\{[^{]*\\}");
2021-08-24 08:37:40 +03:00
#else
bool result = false;
2021-11-02 22:30:29 +03:00
QRegularExpression rx("\\$*\\{[^{]*\\}");
result = content().contains(rx) || isContentBackedUp();
return result;
2021-08-24 08:37:40 +03:00
#endif
2017-08-31 02:53:34 +03:00
return content().contains(rx) || isContentBackedUp();
2016-02-17 10:11:00 +03:00
}
QString TextItem::replaceBR(QString text) const { return text.replace("<br/>", "\n"); }
2016-02-17 10:11:00 +03:00
QString TextItem::replaceReturns(QString text) const
2016-02-17 10:11:00 +03:00
{
QString result = text.replace("\r\n", "<br/>");
result = result.replace("\n", "<br/>");
2016-02-17 10:11:00 +03:00
return result;
}
void TextItem::setTextFont(TextPtr text, const QFont& value) const
{
2016-12-13 00:07:50 +03:00
text->setDefaultFont(value);
if ((m_angle == Angle0) || (m_angle == Angle180)) {
text->setTextWidth(rect().width() - marginSize() * 2);
2016-06-10 18:05:18 +03:00
} else {
text->setTextWidth(rect().height() - marginSize() * 2);
2016-06-10 18:05:18 +03:00
}
}
void TextItem::adaptFontSize(TextPtr text) const
{
2016-06-10 18:05:18 +03:00
QFont _font = transformToSceneFont(font());
do {
// qApp->processEvents();
setTextFont(text, _font);
if (_font.pixelSize() > 2)
_font.setPixelSize(_font.pixelSize() - 1);
else
break;
} while (text->size().height() > this->height()
|| text->size().width() > (this->width()) - marginSize() * 2);
2016-06-10 18:05:18 +03:00
}
2016-12-13 00:07:50 +03:00
int TextItem::underlineLineSize() const { return m_underlineLineSize; }
2016-06-10 18:05:18 +03:00
void TextItem::setUnderlineLineSize(int value)
{
int oldValue = m_underlineLineSize;
m_underlineLineSize = value;
update();
notify("underlineLineSize", oldValue, value);
2016-06-10 18:05:18 +03:00
}
int TextItem::lineSpacing() const { return m_lineSpacing; }
2016-06-10 18:05:18 +03:00
void TextItem::setLineSpacing(int value)
{
int oldValue = m_lineSpacing;
m_lineSpacing = value;
// if (autoHeight())
// initTextSizes();
2016-06-10 18:05:18 +03:00
update();
notify("lineSpacing", oldValue, value);
2016-06-10 18:05:18 +03:00
}
void TextItem::initTextSizes() const
2016-02-17 10:11:00 +03:00
{
2016-12-13 00:07:50 +03:00
TextPtr text = textDocument();
m_textSize = text->size();
2016-12-13 00:07:50 +03:00
if (text->begin().isValid() && text->begin().layout()->lineAt(0).isValid())
m_firstLineSize = text->begin().layout()->lineAt(0).height();
2016-02-17 10:11:00 +03:00
}
2016-02-17 10:39:17 +03:00
QString TextItem::formatDateTime(const QDateTime& value)
{
if (m_format.isEmpty()) {
return value.toString();
}
return value.toString(m_format);
}
QString TextItem::formatNumber(const double value)
{
QString str = QString::number(value);
if (m_format.contains("%")) {
2021-08-24 10:30:38 +03:00
#if QT_VERSION < 0x050500
str.sprintf(m_format.toStdString().c_str(), value);
2021-08-24 08:37:40 +03:00
#else
str.asprintf(m_format.toStdString().c_str(), value);
#endif
str = str.replace(",", QLocale::system().groupSeparator());
str = str.replace(".", QLocale::system().decimalPoint());
}
return str;
}
QString TextItem::formatFieldValue()
{
if (m_format.isEmpty()) {
return m_varValue.toString();
}
QVariant value = m_varValue;
if (m_valueType != Default) {
switch (m_valueType) {
case DateTime: {
QDateTime dt = QDateTime::fromString(value.toString(), Qt::ISODate);
value = (dt.isValid() ? QVariant(dt) : m_varValue);
break;
}
case Double: {
bool bOk = false;
double dbl = value.toDouble(&bOk);
value = (bOk ? QVariant(dbl) : m_varValue);
}
default:
break;
}
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
switch (value.type()) {
case QVariant::Date:
case QVariant::DateTime:
return formatDateTime(value.toDateTime());
case QVariant::Double:
return formatNumber(value.toDouble());
default:
return value.toString();
}
2022-07-19 00:43:54 +03:00
#else
switch (value.typeId()) {
case QMetaType::QDate:
case QMetaType::QDateTime:
return formatDateTime(value.toDateTime());
case QMetaType::Double:
return formatNumber(value.toDouble());
default:
return value.toString();
2022-07-19 00:43:54 +03:00
}
#endif
}
TextItem::TextPtr TextItem::textDocument() const
2016-12-13 00:07:50 +03:00
{
TextPtr text(new QTextDocument);
QString content = m_trimValue ? m_strText.trimmed() : m_strText;
2016-12-13 00:07:50 +03:00
if (allowHTML())
if (isReplaceCarriageReturns()) {
text->setHtml(replaceReturns(content));
} else {
text->setHtml(content);
}
2016-12-13 00:07:50 +03:00
else
text->setPlainText(content);
2016-12-13 00:07:50 +03:00
QTextOption to;
to.setAlignment(m_alignment);
to.setTextDirection(m_textLayoutDirection);
2016-12-13 00:07:50 +03:00
if (m_autoWidth != MaxStringLength)
2016-12-13 00:07:50 +03:00
if (m_adaptFontToSize && (!(m_autoHeight || m_autoWidth)))
to.setWrapMode(QTextOption::WordWrap);
else
to.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
else
to.setWrapMode(QTextOption::NoWrap);
2016-12-13 00:07:50 +03:00
text->setDocumentMargin(0);
text->setDefaultTextOption(to);
QFont _font = transformToSceneFont(font());
if (m_adaptFontToSize && (!(m_autoHeight || m_autoWidth))) {
2016-12-13 00:07:50 +03:00
adaptFontSize(text);
} else {
setTextFont(text, _font);
2016-12-13 00:07:50 +03:00
}
2017-05-15 20:30:54 +03:00
if (follower())
text->documentLayout();
if (m_lineSpacing != 1 || m_textIndent != 0) {
2017-04-20 21:09:52 +03:00
for (QTextBlock block = text->begin(); block.isValid(); block = block.next()) {
2017-04-20 21:09:52 +03:00
QTextCursor tc = QTextCursor(block);
QTextBlockFormat fmt = block.blockFormat();
fmt.setTextIndent(m_textIndent);
if (fmt.lineHeight() != m_lineSpacing) {
fmt.setLineHeight(m_lineSpacing, QTextBlockFormat::LineDistanceHeight);
2017-04-20 21:09:52 +03:00
}
tc.setBlockFormat(fmt);
2016-12-13 00:07:50 +03:00
}
}
return text;
}
int TextItem::fontLetterSpacing() const { return m_fontLetterSpacing; }
void TextItem::setFontLetterSpacing(int value)
{
if (m_fontLetterSpacing != value) {
int oldValue = m_fontLetterSpacing;
m_fontLetterSpacing = value;
QFont curFont = font();
curFont.setLetterSpacing(QFont::AbsoluteSpacing, m_fontLetterSpacing);
setFont(curFont);
notify("fontLetterSpacing", oldValue, value);
}
}
bool TextItem::hideIfEmpty() const { return m_hideIfEmpty; }
2018-06-21 14:29:00 +03:00
void TextItem::setHideIfEmpty(bool hideEmpty)
{
if (m_hideIfEmpty != hideEmpty) {
2018-06-21 14:29:00 +03:00
m_hideIfEmpty = hideEmpty;
notify("hideIfEmpty", !m_hideIfEmpty, m_hideIfEmpty);
2018-06-21 14:29:00 +03:00
}
}
bool TextItem::isReplaceCarriageReturns() const { return m_replaceCarriageReturns; }
void TextItem::setReplaceCarriageReturns(bool replaceCarriageReturns)
{
if (replaceCarriageReturns != m_replaceCarriageReturns) {
m_replaceCarriageReturns = replaceCarriageReturns;
update();
notify("replaceCRwithBR", !replaceCarriageReturns, replaceCarriageReturns);
}
}
qreal TextItem::textIndent() const { return m_textIndent; }
void TextItem::setTextIndent(const qreal& textIndent)
{
if (m_textIndent != textIndent) {
qreal oldValue = m_textIndent;
m_textIndent = textIndent;
update();
notify("textIndent", oldValue, textIndent);
}
}
Qt::LayoutDirection TextItem::textLayoutDirection() const { return m_textLayoutDirection; }
void TextItem::setTextLayoutDirection(const Qt::LayoutDirection& textLayoutDirection)
{
if (m_textLayoutDirection != textLayoutDirection) {
int oldValue = int(m_textLayoutDirection);
m_textLayoutDirection = textLayoutDirection;
update();
notify("textLayoutDirection", oldValue, int(textLayoutDirection));
}
}
2018-03-22 17:54:18 +03:00
void TextItem::setWatermark(bool watermark)
{
if (watermark) {
2018-03-28 12:04:53 +03:00
setBackgroundMode(TransparentMode);
}
2018-03-22 17:54:18 +03:00
BaseDesignIntf::setWatermark(watermark);
}
QString TextItem::followTo() const { return m_followTo; }
void TextItem::setFollowTo(const QString& followTo)
{
if (m_followTo != followTo) {
QString oldValue = m_followTo;
m_followTo = followTo;
if (!isLoading()) {
2016-12-24 11:49:56 +03:00
TextItem* fi = scene()->findChild<TextItem*>(oldValue);
if (fi)
fi->clearFollower();
2016-12-24 11:49:56 +03:00
fi = scene()->findChild<TextItem*>(followTo);
if (fi && fi != this) {
if (initFollower(followTo)) {
notify("followTo", oldValue, followTo);
2016-12-24 11:49:56 +03:00
} else {
m_followTo = "";
QMessageBox::critical(0, tr("Error"),
tr("TextItem \" %1 \" already has folower \" %2 \" ")
.arg(fi->objectName())
.arg(fi->follower()->objectName()));
notify("followTo", followTo, "");
2016-12-24 11:49:56 +03:00
}
} else if (m_followTo != "") {
QMessageBox::critical(0, tr("Error"),
tr("TextItem \" %1 \" not found!").arg(m_followTo));
notify("followTo", followTo, "");
}
}
}
}
void TextItem::setFollower(TextItem* follower)
{
if (!m_follower) {
m_follower = follower;
}
}
void TextItem::clearFollower() { m_follower = 0; }
2016-12-24 11:49:56 +03:00
bool TextItem::hasFollower() const { return m_follower != 0; }
bool TextItem::initFollower(QString follower)
{
TextItem* fi = scene()->findChild<TextItem*>(follower);
if (fi) {
if (!fi->hasFollower()) {
fi->setFollower(this);
return true;
}
}
return false;
}
void TextItem::pageObjectHasBeenLoaded()
{
if (!m_followTo.isEmpty()) {
initFollower(m_followTo);
}
}
TextItem::ValueType TextItem::valueType() const { return m_valueType; }
void TextItem::setValueType(const ValueType valueType) { m_valueType = valueType; }
QString TextItem::format() const { return m_format; }
void TextItem::setFormat(const QString& format) { m_format = format; }
bool TextItem::allowHTMLInFields() const { return m_allowHTMLInFields; }
void TextItem::setAllowHTMLInFields(bool allowHTMLInFields)
{
if (m_allowHTMLInFields != allowHTMLInFields) {
m_allowHTMLInFields = allowHTMLInFields;
notify("allowHTMLInFields", !m_allowHTMLInFields, allowHTMLInFields);
update();
}
}
bool TextItem::allowHTML() const { return m_allowHTML; }
2016-02-17 10:39:17 +03:00
void TextItem::setAllowHTML(bool allowHTML)
{
if (m_allowHTML != allowHTML) {
2016-02-17 10:39:17 +03:00
m_allowHTML = allowHTML;
// if (m_text){
// if (allowHTML)
// m_text->setHtml(m_strText);
// else
// m_text->setPlainText(m_strText);
// update();
// }
2016-12-13 00:07:50 +03:00
update();
notify("allowHTML", !m_allowHTML, allowHTML);
2016-02-17 10:39:17 +03:00
}
}
bool TextItem::trimValue() const { return m_trimValue; }
2016-02-17 10:11:00 +03:00
2016-06-10 18:05:18 +03:00
void TextItem::setTrimValue(bool value)
2016-02-17 10:11:00 +03:00
{
2016-06-10 18:05:18 +03:00
bool oldValue = m_trimValue;
m_trimValue = value;
update();
notify("trimValue", oldValue, value);
2016-02-17 10:11:00 +03:00
}
void TextItem::geometryChangedEvent(QRectF, QRectF) { }
2016-02-17 10:11:00 +03:00
bool TextItem::isNeedUpdateSize(RenderPass pass) const
{
Q_UNUSED(pass)
if ((autoHeight() || autoWidth()) || hasFollower()) {
initTextSizes();
}
bool res = (m_textSize.height() > geometry().height() && autoHeight())
|| (m_textSize.width() > geometry().width() && autoWidth()) || m_follower
|| isNeedExpandContent();
2016-02-17 10:11:00 +03:00
return res;
}
void TextItem::setAlignment(Qt::Alignment value)
{
if (m_alignment != value) {
2016-02-17 10:11:00 +03:00
Qt::Alignment oldValue = m_alignment;
m_alignment = value;
// m_layout.setTextOption(QTextOption(m_alignment));
if (!isLoading()) {
2016-02-17 10:11:00 +03:00
update(rect());
notify("alignment", QVariant(oldValue), QVariant(value));
2016-02-17 10:11:00 +03:00
}
}
}
2016-02-17 10:28:27 +03:00
void TextItem::expandContent(DataSourceManager* dataManager, RenderPass pass)
2016-02-17 10:11:00 +03:00
{
QString context = content();
2017-08-31 02:53:34 +03:00
foreach (QString variableName, dataManager->variableNamesByRenderPass(SecondPass)) {
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
2017-08-31 02:53:34 +03:00
QRegExp rx(QString(Const::NAMED_VARIABLE_RX).arg(variableName));
2021-08-24 08:37:40 +03:00
#else
QRegularExpression rx = getNamedVariableRegEx(variableName);
2021-08-24 08:37:40 +03:00
#endif
if (context.contains(rx) && pass == FirstPass) {
2017-08-31 02:53:34 +03:00
backupContent();
break;
}
}
ExpandType expandType
= (allowHTML() && !allowHTMLInFields()) ? ReplaceHTMLSymbols : NoEscapeSymbols;
switch (pass) {
2016-02-17 10:11:00 +03:00
case FirstPass:
if (!fillInSecondPass()) {
context = expandUserVariables(context, pass, expandType, dataManager);
context = expandScripts(context, dataManager);
context = expandDataFields(context, expandType, dataManager);
2017-08-31 02:53:34 +03:00
} else {
context = expandDataFields(context, expandType, dataManager);
2017-08-25 18:01:59 +03:00
}
2016-02-17 10:11:00 +03:00
break;
2017-08-31 02:53:34 +03:00
case SecondPass:
if (isContentBackedUp()) {
restoreContent();
context = content();
}
context = expandUserVariables(context, pass, expandType, dataManager);
context = expandScripts(context, dataManager);
2016-02-17 10:11:00 +03:00
}
if (expandType == NoEscapeSymbols && !m_varValue.isNull() && m_valueType != Default) {
setContent(formatFieldValue());
} else {
setContent(context);
}
2016-02-17 10:11:00 +03:00
}
void TextItem::setAutoHeight(bool value)
{
if (m_autoHeight != value) {
2016-02-17 10:11:00 +03:00
bool oldValue = m_autoHeight;
m_autoHeight = value;
notify("autoHeight", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
void TextItem::setAutoWidth(TextItem::AutoWidth value)
{
if (m_autoWidth != value) {
2016-02-17 10:11:00 +03:00
TextItem::AutoWidth oldValue = m_autoWidth;
m_autoWidth = value;
notify("autoWidth", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
2016-06-10 18:05:18 +03:00
void TextItem::setAdaptFontToSize(bool value)
{
if (m_adaptFontToSize != value) {
2016-06-10 18:05:18 +03:00
bool oldValue = m_adaptFontToSize;
m_adaptFontToSize = value;
// initText();
2016-06-10 18:05:18 +03:00
invalidateRect(rect());
notify("updateFontToSize", oldValue, value);
2016-06-10 18:05:18 +03:00
}
}
2016-02-17 10:11:00 +03:00
bool TextItem::canBeSplitted(int height) const
{
2016-12-13 00:07:50 +03:00
QFontMetrics fm(font());
return height > m_firstLineSize;
2016-02-17 10:11:00 +03:00
}
QString TextItem::extractText(QTextBlock& curBlock, int height)
{
2016-12-13 00:07:50 +03:00
int curLine = 0;
2017-12-07 19:52:52 +03:00
int linesHeight = 0;
QString resultText;
for (; curBlock != curBlock.document()->end() || curLine <= curBlock.lineCount();
curBlock = curBlock.next(), curLine = 0, resultText += '\n') {
linesHeight += curBlock.blockFormat().topMargin();
for (; curLine < curBlock.layout()->lineCount(); curLine++) {
2017-12-07 19:52:52 +03:00
linesHeight += curBlock.layout()->lineAt(curLine).height() + lineSpacing();
if (height > 0 && linesHeight > (height - borderLineSize() * 2)) {
goto loop_exit;
}
2017-12-07 19:52:52 +03:00
resultText += curBlock.text().mid(curBlock.layout()->lineAt(curLine).textStart(),
curBlock.layout()->lineAt(curLine).textLength());
2017-12-07 19:52:52 +03:00
}
}
loop_exit:
return resultText;
2017-12-07 19:52:52 +03:00
}
2016-12-13 00:07:50 +03:00
QString TextItem::getTextPart(int height, int skipHeight)
{
2016-12-13 00:07:50 +03:00
2017-12-07 19:52:52 +03:00
QString resultText = "";
2016-12-13 00:07:50 +03:00
TextPtr text = textDocument();
2017-12-07 02:39:00 +03:00
text->size().height();
2016-12-13 00:07:50 +03:00
QTextBlock curBlock = text->begin();
2017-12-07 02:39:00 +03:00
QTextCursor cursor(text.data());
cursor.movePosition(QTextCursor::Start);
2016-12-13 00:07:50 +03:00
if (skipHeight > 0) {
2017-12-07 19:52:52 +03:00
resultText = extractText(curBlock, skipHeight);
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, resultText.length());
}
2017-12-07 19:52:52 +03:00
resultText = extractText(curBlock, height);
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, resultText.length());
2016-11-25 23:54:23 +03:00
if (allowHTML()) {
2017-12-07 02:39:00 +03:00
resultText = cursor.selection().toHtml();
resultText.remove("<!--StartFragment-->");
resultText.remove("<!--EndFragment-->");
} else {
resultText = cursor.selection().toPlainText();
2016-02-17 10:11:00 +03:00
}
2017-12-07 19:52:52 +03:00
return resultText;
2016-02-17 10:11:00 +03:00
}
void TextItem::restoreLinksEvent()
2016-02-17 10:11:00 +03:00
{
if (!followTo().isEmpty()) {
BaseDesignIntf* pi = dynamic_cast<BaseDesignIntf*>(parentItem());
if (pi) {
foreach (BaseDesignIntf* bi, pi->childBaseItems()) {
if (bi->patternName().compare(followTo()) == 0) {
TextItem* ti = dynamic_cast<TextItem*>(bi);
if (ti) {
ti->setFollower(this);
}
}
}
2016-02-17 10:11:00 +03:00
}
}
}
BaseDesignIntf* TextItem::cloneUpperPart(int height, QObject* owner, QGraphicsItem* parent)
{
TextItem* upperPart = dynamic_cast<TextItem*>(cloneItem(itemMode(), owner, parent));
upperPart->setContent(getTextPart(height, 0));
2016-12-13 00:07:50 +03:00
upperPart->initTextSizes();
upperPart->setHeight(upperPart->textSize().height() + borderLineSize() * 2);
return upperPart;
}
BaseDesignIntf* TextItem::cloneBottomPart(int height, QObject* owner, QGraphicsItem* parent)
{
TextItem* bottomPart = dynamic_cast<TextItem*>(cloneItem(itemMode(), owner, parent));
bottomPart->setContent(getTextPart(0, height));
2016-12-13 00:07:50 +03:00
bottomPart->initTextSizes();
bottomPart->setHeight(bottomPart->textSize().height() + borderLineSize() * 2);
2016-02-17 10:11:00 +03:00
return bottomPart;
}
BaseDesignIntf* TextItem::createSameTypeItem(QObject* owner, QGraphicsItem* parent)
2016-02-17 10:11:00 +03:00
{
return new TextItem(owner, parent);
2016-02-17 10:11:00 +03:00
}
BaseDesignIntf* TextItem::cloneEmpty(int height, QObject* owner, QGraphicsItem* parent)
2016-02-17 10:11:00 +03:00
{
TextItem* empty = dynamic_cast<TextItem*>(cloneItem(itemMode(), owner, parent));
2016-02-17 10:11:00 +03:00
empty->setContent("");
empty->setHeight(height);
return empty;
}
void TextItem::objectLoadFinished()
{
ItemDesignIntf::objectLoadFinished();
// if (itemMode() == DesignMode || !isNeedExpandContent()){
// if (autoHeight() && autoWidth())
// initTextSizes();
// }
2016-02-17 10:11:00 +03:00
}
void TextItem::setTextItemFont(QFont value)
{
if (font() != value) {
2016-02-17 10:11:00 +03:00
QFont oldValue = font();
value.setLetterSpacing(QFont::AbsoluteSpacing, m_fontLetterSpacing);
2016-02-17 10:11:00 +03:00
setFont(value);
if (!isLoading())
update();
notify("font", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
QWidget* TextItem::defaultEditor()
2016-02-17 10:11:00 +03:00
{
QSettings* l_settings = (page()->settings() != 0) ? page()->settings()
: (page()->reportEditor() != 0) ? page()->reportEditor()->settings()
: 0;
QWidget* editor = new TextItemEditor(this, page(), l_settings);
2016-02-17 10:11:00 +03:00
editor->setAttribute(Qt::WA_DeleteOnClose);
return editor;
}
void TextItem::setBackgroundOpacity(int value)
{
if (opacity() != value) {
2016-02-17 10:11:00 +03:00
int oldValue = opacity();
setOpacity(value);
notify("backgroundOpacity", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
void TextItem::setBackgroundModeProperty(BaseDesignIntf::BGMode value)
{
if (value != backgroundMode()) {
2016-02-17 10:11:00 +03:00
BaseDesignIntf::BGMode oldValue = backgroundMode();
setBackgroundMode(value);
notify("backgroundMode", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
void TextItem::setBackgroundColorProperty(QColor value)
{
if (value != backgroundColor()) {
2016-02-17 10:11:00 +03:00
QColor oldValue = backgroundColor();
setBackgroundColor(value);
notify("backgroundColor", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
void TextItem::setFontColorProperty(QColor value)
{
if (value != fontColor()) {
2016-02-17 10:11:00 +03:00
QColor oldValue = fontColor();
setFontColor(value);
notify("fontColor", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
TextItem::AngleType TextItem::angle() const { return m_angle; }
2016-02-17 10:11:00 +03:00
void TextItem::setAngle(const AngleType& value)
{
if (m_angle != value) {
2016-02-17 10:11:00 +03:00
AngleType oldValue = m_angle;
m_angle = value;
if (!isLoading()) {
2016-02-17 10:11:00 +03:00
update();
notify("angle", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
}
void TextItem::setForegroundOpacity(int value)
{
if (value > 100) {
value = 100;
} else if (value < 0) {
value = 0;
2016-02-17 10:11:00 +03:00
}
if (m_foregroundOpacity != value) {
2016-02-17 10:11:00 +03:00
int oldValue = m_foregroundOpacity;
m_foregroundOpacity = value;
update();
notify("foregroundOpacity", oldValue, value);
2016-02-17 10:11:00 +03:00
}
}
2016-06-10 18:05:18 +03:00
void TextItem::setUnderlines(bool value)
{
if (m_underlines != value) {
2016-06-10 18:05:18 +03:00
bool oldValue = m_underlines;
m_underlines = value;
update();
notify("underlines", oldValue, value);
2016-06-10 18:05:18 +03:00
}
}
} // namespace LimeReport