From 88ef131148de3fae504322a019f1eca12889622c Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Tue, 26 Jul 2016 16:01:14 +0200 Subject: [PATCH 1/2] Added support for formating numbers and dates by pattern. --- limereport/items/lrtextitem.cpp | 89 ++++++++++++++++++++++++++++++++- limereport/items/lrtextitem.h | 16 ++++++ limereport/lritemdesignintf.cpp | 15 +++--- limereport/lritemdesignintf.h | 2 + 4 files changed, 115 insertions(+), 7 deletions(-) diff --git a/limereport/items/lrtextitem.cpp b/limereport/items/lrtextitem.cpp index d9197f7..d0b699d 100644 --- a/limereport/items/lrtextitem.cpp +++ b/limereport/items/lrtextitem.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "lrpagedesignintf.h" @@ -186,6 +187,7 @@ void TextItem::Init() // m_text->setDefaultFont(transformToSceneFont(font())); m_textSize=QSizeF(); m_foregroundOpacity = 100; + m_valueType = Default; } void TextItem::setContent(const QString &value) @@ -282,6 +284,86 @@ void TextItem::initText() m_textSize=m_text->size(); } +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("%")) + { + str.sprintf(m_format.toStdString().c_str(), value); + 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); + } + } + } + + switch (value.type()) { + case QVariant::Date: + case QVariant::DateTime: + return formatDateTime(value.toDateTime()); + case QVariant::Double: + return formatNumber(value.toDouble()); + default: + return value.toString(); + } +} + +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; @@ -374,7 +456,12 @@ void TextItem::expandContent(DataSourceManager* dataManager, RenderPass pass) context=expandUserVariables(context, pass, expandType, dataManager); context=expandScripts(context, dataManager); } - setContent(context); + + if (expandType == NoEscapeSymbols) { + setContent(formatFieldValue()); + } else { + setContent(context); + } } void TextItem::setAutoHeight(bool value) diff --git a/limereport/items/lrtextitem.h b/limereport/items/lrtextitem.h index d95cb3c..39a878e 100644 --- a/limereport/items/lrtextitem.h +++ b/limereport/items/lrtextitem.h @@ -44,6 +44,7 @@ class TextItem : public LimeReport::ContentItemDesignIntf { Q_OBJECT Q_ENUMS(AutoWidth) Q_ENUMS(AngleType) + Q_ENUMS(ValueType) Q_PROPERTY(QString content READ content WRITE setContent) Q_PROPERTY(int margin READ marginSize WRITE setMarginSize) Q_PROPERTY(Qt::Alignment alignment READ alignment() WRITE setAlignment) @@ -59,10 +60,13 @@ class TextItem : public LimeReport::ContentItemDesignIntf { Q_PROPERTY(bool trimValue READ trimValue WRITE setTrimValue) Q_PROPERTY(bool allowHTML READ allowHTML WRITE setAllowHTML) Q_PROPERTY(bool allowHTMLInFields READ allowHTMLInFields WRITE setAllowHTMLInFields) + Q_PROPERTY(QString format READ format WRITE setFormat) + Q_PROPERTY(ValueType valueType READ valueType WRITE setValueType) public: enum AutoWidth{NoneAutoWidth,MaxWordLength,MaxStringLength}; enum AngleType{Angle0,Angle90,Angle180,Angle270,Angle45,Angle315}; + enum ValueType{Default,DateTime,Double}; void Init(); TextItem(QObject* owner=0, QGraphicsItem* parent=0); @@ -118,6 +122,12 @@ public: bool allowHTMLInFields() const; void setAllowHTMLInFields(bool allowHTMLInFields); + QString format() const; + void setFormat(const QString &format); + + ValueType valueType() const; + void setValueType(const ValueType valueType); + protected: void updateLayout(); bool isNeedExpandContent() const; @@ -126,6 +136,9 @@ protected: int fakeMarginSize(); private: void initText(); + QString formatDateTime(const QDateTime &value); + QString formatNumber(const double value); + QString formatFieldValue(); private: QString m_strText; @@ -140,6 +153,9 @@ private: bool m_trimValue; bool m_allowHTML; bool m_allowHTMLInFields; + + QString m_format; + ValueType m_valueType; }; } diff --git a/limereport/lritemdesignintf.cpp b/limereport/lritemdesignintf.cpp index 1cf1774..424c569 100644 --- a/limereport/lritemdesignintf.cpp +++ b/limereport/lritemdesignintf.cpp @@ -123,11 +123,12 @@ QString ContentItemDesignIntf::expandDataFields(QString context, ExpandType expa if (dataManager->containsField(field)) { QString fieldValue; + m_varValue = dataManager->fieldData(field); if (expandType == EscapeSymbols) { if (dataManager->fieldData(field).isNull()) { fieldValue="\"\""; } else { - fieldValue = escapeSimbols(dataManager->fieldData(field).toString()); + fieldValue = escapeSimbols(m_varValue.toString()); switch (dataManager->fieldData(field).type()) { case QVariant::Char: case QVariant::String: @@ -142,8 +143,8 @@ QString ContentItemDesignIntf::expandDataFields(QString context, ExpandType expa } } else { if (expandType == ReplaceHTMLSymbols) - fieldValue = replaceHTMLSymbols(dataManager->fieldData(field).toString()); - else fieldValue = dataManager->fieldData(field).toString(); + fieldValue = replaceHTMLSymbols(m_varValue.toString()); + else fieldValue = m_varValue.toString(); } context.replace(rx.cap(0),fieldValue); @@ -172,15 +173,16 @@ QString ContentItemDesignIntf::expandUserVariables(QString context, RenderPass p pos += rx.matchedLength(); if (dataManager->containsVariable(variable) ){ if (pass==dataManager->variablePass(variable)){ + m_varValue = dataManager->variable(variable); switch (expandType){ case EscapeSymbols: - context.replace(rx.cap(0),escapeSimbols(dataManager->variable(variable).toString())); + context.replace(rx.cap(0),escapeSimbols(m_varValue.toString())); break; case NoEscapeSymbols: - context.replace(rx.cap(0),dataManager->variable(variable).toString()); + context.replace(rx.cap(0),m_varValue.toString()); break; case ReplaceHTMLSymbols: - context.replace(rx.cap(0),replaceHTMLSymbols(dataManager->variable(variable).toString())); + context.replace(rx.cap(0),replaceHTMLSymbols(m_varValue.toString())); break; } pos=0; @@ -222,6 +224,7 @@ QString ContentItemDesignIntf::expandScripts(QString context, DataSourceManager* scriptBody = expandUserVariables(scriptBody, FirstPass, EscapeSymbols, dataManager); QScriptValue value = se->evaluate(scriptBody); if (!se->hasUncaughtException()) { + m_varValue = value.toVariant(); context.replace(scriptExtractor.scriptAt(i),value.toString()); } else { context.replace(scriptExtractor.scriptAt(i),se->uncaughtException().toString()); diff --git a/limereport/lritemdesignintf.h b/limereport/lritemdesignintf.h index 75e11be..e6f6170 100644 --- a/limereport/lritemdesignintf.h +++ b/limereport/lritemdesignintf.h @@ -82,6 +82,8 @@ protected: virtual QString expandUserVariables(QString context, RenderPass pass, ExpandType expandType, DataSourceManager *dataManager); virtual QString expandDataFields(QString context, ExpandType expandType, DataSourceManager *dataManager); virtual QString expandScripts(QString context, DataSourceManager *dataManager); + + QVariant m_varValue; }; class LayoutDesignIntf : public ItemDesignIntf{ From 8e632855b4a5270f0fc109a7db54688986117b38 Mon Sep 17 00:00:00 2001 From: Arin Alex Date: Wed, 27 Jul 2016 00:41:24 +0300 Subject: [PATCH 2/2] repeatOnEachRow property has been added to DataHeader --- limereport/bands/lrdataband.h | 1 + limereport/lrbanddesignintf.cpp | 13 ++++++++++++- limereport/lrbanddesignintf.h | 4 +++- limereport/lrreportrender.cpp | 3 +++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/limereport/bands/lrdataband.h b/limereport/bands/lrdataband.h index 5e73d56..d19d4b9 100644 --- a/limereport/bands/lrdataband.h +++ b/limereport/bands/lrdataband.h @@ -63,6 +63,7 @@ class DataHeaderBand : public BandDesignIntf Q_PROPERTY(bool reprintOnEachPage READ reprintOnEachPage WRITE setReprintOnEachPage) Q_PROPERTY(int columnsCount READ columnsCount WRITE setColumnsCount) Q_PROPERTY(bool printAlways READ printAlways() WRITE setPrintAlways()) + Q_PROPERTY(bool repeatOnEachRow READ repeatOnEachRow WRITE setRepeatOnEachRow) public: DataHeaderBand(QObject* owner=0, QGraphicsItem* parent=0); bool isUnique() const {return false;} diff --git a/limereport/lrbanddesignintf.cpp b/limereport/lrbanddesignintf.cpp index 28fedc2..2cf1707 100644 --- a/limereport/lrbanddesignintf.cpp +++ b/limereport/lrbanddesignintf.cpp @@ -144,7 +144,8 @@ BandDesignIntf::BandDesignIntf(BandsType bandType, const QString &xmlTypeName, Q m_reprintOnEachPage(false), m_startNewPage(false), m_startFromNewPage(false), - m_printAlways(false) + m_printAlways(false), + m_repeatOnEachRow(false) { setPosibleResizeDirectionFlags(ResizeBottom); setPosibleMoveFlags(TopBotom); @@ -718,6 +719,16 @@ void BandDesignIntf::childBandDeleted(QObject *band) m_childBands.removeAt(m_childBands.indexOf(reinterpret_cast(band))); } +bool BandDesignIntf::repeatOnEachRow() const +{ + return m_repeatOnEachRow; +} + +void BandDesignIntf::setRepeatOnEachRow(bool repeatOnEachRow) +{ + m_repeatOnEachRow = repeatOnEachRow; +} + bool BandDesignIntf::printAlways() const { return m_printAlways; diff --git a/limereport/lrbanddesignintf.h b/limereport/lrbanddesignintf.h index 835aadf..6b7fb06 100644 --- a/limereport/lrbanddesignintf.h +++ b/limereport/lrbanddesignintf.h @@ -213,7 +213,8 @@ public: bool canContainChildren(){ return true;} bool printAlways() const; void setPrintAlways(bool printAlways); - + bool repeatOnEachRow() const; + void setRepeatOnEachRow(bool repeatOnEachRow); signals: void bandRendered(BandDesignIntf* band); protected: @@ -266,6 +267,7 @@ private: bool m_startNewPage; bool m_startFromNewPage; bool m_printAlways; + bool m_repeatOnEachRow; }; class DataBandDesignIntf : public BandDesignIntf{ diff --git a/limereport/lrreportrender.cpp b/limereport/lrreportrender.cpp index 508bf20..768f6aa 100644 --- a/limereport/lrreportrender.cpp +++ b/limereport/lrreportrender.cpp @@ -401,6 +401,9 @@ void ReportRender::renderDataBand(BandDesignIntf *dataBand) datasources()->updateChildrenData(dataBand->datasourceName()); m_lastDataBand = dataBand; + if (header && !firstTime && header->repeatOnEachRow()) + renderBand(header,StartNewPageAsNeeded); + renderBand(dataBand,StartNewPageAsNeeded,!bandDatasource->hasNext()); renderChildBands(dataBand);