mirror of
https://github.com/fralx/LimeReport.git
synced 2025-09-23 08:29:07 +03:00
Added support for formating numbers and dates by pattern.
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
#include <QtGui>
|
||||
#include <QTextLayout>
|
||||
#include <QtScript/QScriptEngine>
|
||||
#include <QLocale>
|
||||
#include <math.h>
|
||||
|
||||
#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)
|
||||
|
@@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user