Calculated condition has been added to group band header

This commit is contained in:
Arin Alexander 2016-11-18 01:12:23 +03:00
parent e9b32ec7d7
commit 61653c9bb3
6 changed files with 203 additions and 175 deletions

View File

@ -31,7 +31,6 @@
#include "lrglobal.h" #include "lrglobal.h"
#include "lrdatasourcemanager.h" #include "lrdatasourcemanager.h"
const QString xmlTagHeader = QLatin1String("GroupHeader"); const QString xmlTagHeader = QLatin1String("GroupHeader");
const QString xmlTagFooter = QLatin1String("GroupFooter"); const QString xmlTagFooter = QLatin1String("GroupFooter");
@ -98,6 +97,8 @@ void GroupBandHeader::startGroup(DataSourceManager* dataManager)
if (ds && ds->columnIndexByName(m_groupFiledName)!=-1) if (ds && ds->columnIndexByName(m_groupFiledName)!=-1)
m_groupFieldValue=ds->data(m_groupFiledName); m_groupFieldValue=ds->data(m_groupFiledName);
} }
if (!m_condition.isEmpty()) m_conditionValue = calcCondition(dataManager);
} }
QColor GroupBandHeader::bandColor() const QColor GroupBandHeader::bandColor() const
@ -114,20 +115,47 @@ QString GroupBandHeader::findDataSourceName(BandDesignIntf* parentBand){
} }
QString GroupBandHeader::condition() const
{
return m_condition;
}
void GroupBandHeader::setCondition(const QString &condition)
{
m_condition = condition;
}
QString GroupBandHeader::calcCondition(DataSourceManager* dataManager){
QString result = m_condition;
if (!m_condition.isEmpty()){
result=expandUserVariables(result, FirstPass, NoEscapeSymbols, dataManager);
result=expandScripts(result, dataManager);
result=expandDataFields(result, NoEscapeSymbols, dataManager);
}
return result;
}
bool GroupBandHeader::isNeedToClose(DataSourceManager* dataManager) bool GroupBandHeader::isNeedToClose(DataSourceManager* dataManager)
{ {
if (!m_groupStarted) return false; if (!m_groupStarted) return false;
if (m_groupFiledName.isNull() || m_groupFiledName.isEmpty()) if ((m_groupFiledName.isNull() || m_groupFiledName.isEmpty()) && condition().isEmpty()){
dataManager->putError(tr("Group field not found")); dataManager->putError(tr("Group field not found"));
QString datasourceName = findDataSourceName(parentBand()); return false;
if (dataManager->containsDatasource(datasourceName)){ }
IDataSource* ds = dataManager->dataSource(datasourceName);
if (ds){ if (!m_condition.isEmpty()){
if (ds->data(m_groupFiledName).isNull() && m_groupFieldValue.isNull()) return false; return m_conditionValue != calcCondition(dataManager);
return ds->data(m_groupFiledName)!=m_groupFieldValue;
}
} else { } else {
dataManager->putError(tr("Datasource \"%1\" not found !!!").arg(datasourceName)); QString datasourceName = findDataSourceName(parentBand());
if (dataManager->containsDatasource(datasourceName)){
IDataSource* ds = dataManager->dataSource(datasourceName);
if (ds){
if (ds->data(m_groupFiledName).isNull() && m_groupFieldValue.isNull()) return false;
return ds->data(m_groupFiledName)!=m_groupFieldValue;
}
} else {
dataManager->putError(tr("Datasource \"%1\" not found !!!").arg(datasourceName));
}
} }
return false; return false;
@ -141,6 +169,7 @@ bool GroupBandHeader::isStarted()
void GroupBandHeader::closeGroup() void GroupBandHeader::closeGroup()
{ {
m_groupFieldValue=QVariant(); m_groupFieldValue=QVariant();
m_conditionValue="";
m_groupStarted=false; m_groupStarted=false;
} }

View File

@ -43,6 +43,7 @@ class GroupBandHeader : public BandDesignIntf, public IGroupBand{
Q_PROPERTY(bool startNewPage READ startNewPage WRITE setStartNewPage) Q_PROPERTY(bool startNewPage READ startNewPage WRITE setStartNewPage)
Q_PROPERTY(bool resetPageNumber READ resetPageNumber WRITE setResetPageNumber) Q_PROPERTY(bool resetPageNumber READ resetPageNumber WRITE setResetPageNumber)
Q_PROPERTY(bool reprintOnEachPage READ reprintOnEachPage WRITE setReprintOnEachPage) Q_PROPERTY(bool reprintOnEachPage READ reprintOnEachPage WRITE setReprintOnEachPage)
Q_PROPERTY(QString condition READ condition WRITE setCondition)
public: public:
GroupBandHeader(QObject* owner = 0, QGraphicsItem* parent=0); GroupBandHeader(QObject* owner = 0, QGraphicsItem* parent=0);
virtual bool isUnique() const; virtual bool isUnique() const;
@ -57,6 +58,8 @@ public:
void setResetPageNumber(bool resetPageNumber); void setResetPageNumber(bool resetPageNumber);
bool isHeader() const{return true;} bool isHeader() const{return true;}
bool isGroupHeader() const {return true;} bool isGroupHeader() const {return true;}
QString condition() const;
void setCondition(const QString &condition);
private: private:
virtual BaseDesignIntf* createSameTypeItem(QObject* owner=0, QGraphicsItem* parent=0); virtual BaseDesignIntf* createSameTypeItem(QObject* owner=0, QGraphicsItem* parent=0);
void startGroup(DataSourceManager* dataManager); void startGroup(DataSourceManager* dataManager);
@ -65,12 +68,15 @@ private:
void closeGroup(); void closeGroup();
int index(); int index();
QString findDataSourceName(BandDesignIntf *parentBand); QString findDataSourceName(BandDesignIntf *parentBand);
QString calcCondition(DataSourceManager *dataManager);
private: private:
QVariant m_groupFieldValue; QVariant m_groupFieldValue;
QString m_groupFiledName; QString m_groupFiledName;
bool m_groupStarted; bool m_groupStarted;
//bool m_startNewPage; //bool m_startNewPage;
bool m_resetPageNumber; bool m_resetPageNumber;
QString m_condition;
QString m_conditionValue;
}; };
class GroupBandFooter : public BandDesignIntf{ class GroupBandFooter : public BandDesignIntf{

View File

@ -85,9 +85,6 @@ BaseDesignIntf::BaseDesignIntf(const QString &storageTypeName, QObject *owner, Q
m_font = QFont("Arial",10); m_font = QFont("Arial",10);
} }
initFlags(); initFlags();
//connect(this,SIGNAL(objectNameChanged(QString)),this,SLOT(slotObjectNameChanged(QString)));
} }
QRectF BaseDesignIntf::boundingRect() const QRectF BaseDesignIntf::boundingRect() const
@ -242,6 +239,152 @@ QFont BaseDesignIntf::transformToSceneFont(const QFont& value) const
return f; return f;
} }
QString BaseDesignIntf::escapeSimbols(const QString &value)
{
QString result = value;
result.replace("\"","\\\"");
result.replace('\n',"\\n");
return result;
}
QString BaseDesignIntf::replaceHTMLSymbols(const QString &value)
{
QString result = value;
result.replace("<","&lt;");
result.replace(">","&gt;");
return result;
}
QString BaseDesignIntf::expandDataFields(QString context, ExpandType expandType, DataSourceManager* dataManager)
{
QRegExp rx(Const::FIELD_RX);
if (context.contains(rx)){
while ((rx.indexIn(context))!=-1){
QString field=rx.cap(1);
if (dataManager->containsField(field)) {
QString fieldValue;
m_varValue = dataManager->fieldData(field);
if (expandType == EscapeSymbols) {
if (dataManager->fieldData(field).isNull()) {
fieldValue="\"\"";
} else {
fieldValue = escapeSimbols(m_varValue.toString());
switch (dataManager->fieldData(field).type()) {
case QVariant::Char:
case QVariant::String:
case QVariant::StringList:
case QVariant::Date:
case QVariant::DateTime:
fieldValue = "\""+fieldValue+"\"";
break;
default:
break;
}
}
} else {
if (expandType == ReplaceHTMLSymbols)
fieldValue = replaceHTMLSymbols(m_varValue.toString());
else fieldValue = m_varValue.toString();
}
context.replace(rx.cap(0),fieldValue);
} else {
QString error = QString("Field %1 not found in %2 !!! ").arg(field).arg(this->objectName());
dataManager->putError(error);
if (!reportSettings() || !reportSettings()->suppressAbsentFieldsAndVarsWarnings())
context.replace(rx.cap(0),error);
else
context.replace(rx.cap(0),"");
}
}
}
return context;
}
QString BaseDesignIntf::expandUserVariables(QString context, RenderPass pass, ExpandType expandType, DataSourceManager* dataManager)
{
QRegExp rx(Const::VARIABLE_RX);
if (context.contains(rx)){
int pos = 0;
while ((pos = rx.indexIn(context,pos))!=-1){
QString variable=rx.cap(1);
pos += rx.matchedLength();
if (dataManager->containsVariable(variable) ){
try {
if (pass==dataManager->variablePass(variable)){
m_varValue = dataManager->variable(variable);
switch (expandType){
case EscapeSymbols:
context.replace(rx.cap(0),escapeSimbols(m_varValue.toString()));
break;
case NoEscapeSymbols:
context.replace(rx.cap(0),m_varValue.toString());
break;
case ReplaceHTMLSymbols:
context.replace(rx.cap(0),replaceHTMLSymbols(m_varValue.toString()));
break;
}
pos=0;
}
} catch (ReportError e){
dataManager->putError(e.what());
if (!reportSettings() || reportSettings()->suppressAbsentFieldsAndVarsWarnings())
context.replace(rx.cap(0),e.what());
else
context.replace(rx.cap(0),"");
}
} else {
QString error;
error = tr("Variable %1 not found").arg(variable);
dataManager->putError(error);
if (!reportSettings() || reportSettings()->suppressAbsentFieldsAndVarsWarnings())
context.replace(rx.cap(0),error);
else
context.replace(rx.cap(0),"");
}
}
}
return context;
}
QString BaseDesignIntf::expandScripts(QString context, DataSourceManager* dataManager)
{
QRegExp rx(Const::SCRIPT_RX);
if (context.contains(rx)){
ScriptEngineManager::instance().setDataManager(dataManager);
QScriptEngine* se = ScriptEngineManager::instance().scriptEngine();
QScriptValue svThis = se->globalObject().property("THIS");
if (svThis.isValid()){
se->newQObject(svThis, this);
} else {
svThis = se->newQObject(this);
se->globalObject().setProperty("THIS",svThis);
}
ScriptExtractor scriptExtractor(context);
if (scriptExtractor.parse()){
for(int i=0; i<scriptExtractor.count();++i){
QString scriptBody = expandDataFields(scriptExtractor.bodyAt(i),EscapeSymbols, dataManager);
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());
}
}
}
}
return context;
}
void BaseDesignIntf::setupPainter(QPainter *painter) const void BaseDesignIntf::setupPainter(QPainter *painter) const
{ {
if (!painter) { if (!painter) {

View File

@ -116,6 +116,7 @@ public:
}; };
enum ObjectState {ObjectLoading, ObjectLoaded, ObjectCreated}; enum ObjectState {ObjectLoading, ObjectLoaded, ObjectCreated};
enum ItemAlign {LeftItemAlign,RightItemAlign,CenterItemAlign,ParentWidthItemAlign,DesignedItemAlign}; enum ItemAlign {LeftItemAlign,RightItemAlign,CenterItemAlign,ParentWidthItemAlign,DesignedItemAlign};
enum ExpandType {EscapeSymbols, NoEscapeSymbols, ReplaceHTMLSymbols};
Q_DECLARE_FLAGS(BorderLines, BorderSide) Q_DECLARE_FLAGS(BorderLines, BorderSide)
Q_DECLARE_FLAGS(ItemMode,ItemModes) Q_DECLARE_FLAGS(ItemMode,ItemModes)
friend class SelectionMarker; friend class SelectionMarker;
@ -305,6 +306,15 @@ protected:
virtual bool drawDesignBorders() const {return true;} virtual bool drawDesignBorders() const {return true;}
virtual QColor selectionMarkerColor(){ return Const::SELECTION_COLOR;} virtual QColor selectionMarkerColor(){ return Const::SELECTION_COLOR;}
QString escapeSimbols(const QString& value);
QString replaceHTMLSymbols(const QString& value);
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;
private: private:
void updateSelectionMarker(); void updateSelectionMarker();
int resizeDirectionFlags(QPointF position); int resizeDirectionFlags(QPointF position);

View File

@ -113,157 +113,6 @@ void ItemDesignIntf::initFlags()
} }
} }
QString ContentItemDesignIntf::expandDataFields(QString context, ExpandType expandType, DataSourceManager* dataManager)
{
QRegExp rx(Const::FIELD_RX);
if (context.contains(rx)){
while ((rx.indexIn(context))!=-1){
QString field=rx.cap(1);
if (dataManager->containsField(field)) {
QString fieldValue;
m_varValue = dataManager->fieldData(field);
if (expandType == EscapeSymbols) {
if (dataManager->fieldData(field).isNull()) {
fieldValue="\"\"";
} else {
fieldValue = escapeSimbols(m_varValue.toString());
switch (dataManager->fieldData(field).type()) {
case QVariant::Char:
case QVariant::String:
case QVariant::StringList:
case QVariant::Date:
case QVariant::DateTime:
fieldValue = "\""+fieldValue+"\"";
break;
default:
break;
}
}
} else {
if (expandType == ReplaceHTMLSymbols)
fieldValue = replaceHTMLSymbols(m_varValue.toString());
else fieldValue = m_varValue.toString();
}
context.replace(rx.cap(0),fieldValue);
} else {
QString error = QString("Field %1 not found in %2 !!! ").arg(field).arg(this->objectName());
dataManager->putError(error);
if (!reportSettings() || !reportSettings()->suppressAbsentFieldsAndVarsWarnings())
context.replace(rx.cap(0),error);
else
context.replace(rx.cap(0),"");
}
}
}
return context;
}
QString ContentItemDesignIntf::expandUserVariables(QString context, RenderPass pass, ExpandType expandType, DataSourceManager* dataManager)
{
QRegExp rx(Const::VARIABLE_RX);
if (context.contains(rx)){
int pos = 0;
while ((pos = rx.indexIn(context,pos))!=-1){
QString variable=rx.cap(1);
pos += rx.matchedLength();
if (dataManager->containsVariable(variable) ){
try {
if (pass==dataManager->variablePass(variable)){
m_varValue = dataManager->variable(variable);
switch (expandType){
case EscapeSymbols:
context.replace(rx.cap(0),escapeSimbols(m_varValue.toString()));
break;
case NoEscapeSymbols:
context.replace(rx.cap(0),m_varValue.toString());
break;
case ReplaceHTMLSymbols:
context.replace(rx.cap(0),replaceHTMLSymbols(m_varValue.toString()));
break;
}
pos=0;
}
} catch (ReportError e){
dataManager->putError(e.what());
if (!reportSettings() || reportSettings()->suppressAbsentFieldsAndVarsWarnings())
context.replace(rx.cap(0),e.what());
else
context.replace(rx.cap(0),"");
}
} else {
QString error;
error = tr("Variable %1 not found").arg(variable);
dataManager->putError(error);
if (!reportSettings() || reportSettings()->suppressAbsentFieldsAndVarsWarnings())
context.replace(rx.cap(0),error);
else
context.replace(rx.cap(0),"");
}
}
}
return context;
}
QString ContentItemDesignIntf::expandScripts(QString context, DataSourceManager* dataManager)
{
QRegExp rx(Const::SCRIPT_RX);
if (context.contains(rx)){
ScriptEngineManager::instance().setDataManager(dataManager);
QScriptEngine* se = ScriptEngineManager::instance().scriptEngine();
QScriptValue svThis = se->globalObject().property("THIS");
if (svThis.isValid()){
se->newQObject(svThis, this);
} else {
svThis = se->newQObject(this);
se->globalObject().setProperty("THIS",svThis);
}
ScriptExtractor scriptExtractor(context);
if (scriptExtractor.parse()){
for(int i=0; i<scriptExtractor.count();++i){
QString scriptBody = expandDataFields(scriptExtractor.bodyAt(i),EscapeSymbols, dataManager);
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());
}
}
}
}
return context;
}
QString ContentItemDesignIntf::content() const
{
return "";
}
QString ContentItemDesignIntf::escapeSimbols(const QString &value)
{
QString result = value;
result.replace("\"","\\\"");
result.replace('\n',"\\n");
return result;
}
QString ContentItemDesignIntf::replaceHTMLSymbols(const QString &value)
{
QString result = value;
result.replace("<","&lt;");
result.replace(">","&gt;");
return result;
}
Spacer::Spacer(QObject *owner, QGraphicsItem *parent) Spacer::Spacer(QObject *owner, QGraphicsItem *parent)
:ItemDesignIntf("Spacer",owner,parent){} :ItemDesignIntf("Spacer",owner,parent){}

View File

@ -73,17 +73,8 @@ class ContentItemDesignIntf : public ItemDesignIntf
public: public:
ContentItemDesignIntf(const QString& xmlTypeName, QObject* owner = 0,QGraphicsItem* parent = 0) ContentItemDesignIntf(const QString& xmlTypeName, QObject* owner = 0,QGraphicsItem* parent = 0)
:ItemDesignIntf(xmlTypeName,owner,parent){} :ItemDesignIntf(xmlTypeName,owner,parent){}
virtual QString content() const; virtual QString content() const = 0;
virtual void setContent(const QString& value)=0; virtual void setContent(const QString& value) = 0;
enum ExpandType {EscapeSymbols, NoEscapeSymbols, ReplaceHTMLSymbols};
protected:
QString escapeSimbols(const QString& value);
QString replaceHTMLSymbols(const QString& value);
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{ class LayoutDesignIntf : public ItemDesignIntf{