diff --git a/limereport/bands/lrgroupbands.cpp b/limereport/bands/lrgroupbands.cpp index 965982b..74b55eb 100644 --- a/limereport/bands/lrgroupbands.cpp +++ b/limereport/bands/lrgroupbands.cpp @@ -31,7 +31,6 @@ #include "lrglobal.h" #include "lrdatasourcemanager.h" - const QString xmlTagHeader = QLatin1String("GroupHeader"); const QString xmlTagFooter = QLatin1String("GroupFooter"); @@ -98,6 +97,8 @@ void GroupBandHeader::startGroup(DataSourceManager* dataManager) if (ds && ds->columnIndexByName(m_groupFiledName)!=-1) m_groupFieldValue=ds->data(m_groupFiledName); } + + if (!m_condition.isEmpty()) m_conditionValue = calcCondition(dataManager); } 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) { 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")); - 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; - } + return false; + } + + if (!m_condition.isEmpty()){ + return m_conditionValue != calcCondition(dataManager); } 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; @@ -141,6 +169,7 @@ bool GroupBandHeader::isStarted() void GroupBandHeader::closeGroup() { m_groupFieldValue=QVariant(); + m_conditionValue=""; m_groupStarted=false; } diff --git a/limereport/bands/lrgroupbands.h b/limereport/bands/lrgroupbands.h index 9fa3eaa..42285d8 100644 --- a/limereport/bands/lrgroupbands.h +++ b/limereport/bands/lrgroupbands.h @@ -43,6 +43,7 @@ class GroupBandHeader : public BandDesignIntf, public IGroupBand{ Q_PROPERTY(bool startNewPage READ startNewPage WRITE setStartNewPage) Q_PROPERTY(bool resetPageNumber READ resetPageNumber WRITE setResetPageNumber) Q_PROPERTY(bool reprintOnEachPage READ reprintOnEachPage WRITE setReprintOnEachPage) + Q_PROPERTY(QString condition READ condition WRITE setCondition) public: GroupBandHeader(QObject* owner = 0, QGraphicsItem* parent=0); virtual bool isUnique() const; @@ -57,6 +58,8 @@ public: void setResetPageNumber(bool resetPageNumber); bool isHeader() const{return true;} bool isGroupHeader() const {return true;} + QString condition() const; + void setCondition(const QString &condition); private: virtual BaseDesignIntf* createSameTypeItem(QObject* owner=0, QGraphicsItem* parent=0); void startGroup(DataSourceManager* dataManager); @@ -65,12 +68,15 @@ private: void closeGroup(); int index(); QString findDataSourceName(BandDesignIntf *parentBand); + QString calcCondition(DataSourceManager *dataManager); private: QVariant m_groupFieldValue; QString m_groupFiledName; bool m_groupStarted; //bool m_startNewPage; bool m_resetPageNumber; + QString m_condition; + QString m_conditionValue; }; class GroupBandFooter : public BandDesignIntf{ diff --git a/limereport/lrbasedesignintf.cpp b/limereport/lrbasedesignintf.cpp index 8b632d5..2f00d93 100644 --- a/limereport/lrbasedesignintf.cpp +++ b/limereport/lrbasedesignintf.cpp @@ -85,9 +85,6 @@ BaseDesignIntf::BaseDesignIntf(const QString &storageTypeName, QObject *owner, Q m_font = QFont("Arial",10); } initFlags(); - - - //connect(this,SIGNAL(objectNameChanged(QString)),this,SLOT(slotObjectNameChanged(QString))); } QRectF BaseDesignIntf::boundingRect() const @@ -242,6 +239,152 @@ QFont BaseDesignIntf::transformToSceneFont(const QFont& value) const 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("<","<"); + result.replace(">",">"); + 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; ievaluate(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 { if (!painter) { diff --git a/limereport/lrbasedesignintf.h b/limereport/lrbasedesignintf.h index f31bf73..cae3d77 100644 --- a/limereport/lrbasedesignintf.h +++ b/limereport/lrbasedesignintf.h @@ -116,6 +116,7 @@ public: }; enum ObjectState {ObjectLoading, ObjectLoaded, ObjectCreated}; enum ItemAlign {LeftItemAlign,RightItemAlign,CenterItemAlign,ParentWidthItemAlign,DesignedItemAlign}; + enum ExpandType {EscapeSymbols, NoEscapeSymbols, ReplaceHTMLSymbols}; Q_DECLARE_FLAGS(BorderLines, BorderSide) Q_DECLARE_FLAGS(ItemMode,ItemModes) friend class SelectionMarker; @@ -305,6 +306,15 @@ protected: virtual bool drawDesignBorders() const {return true;} 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: void updateSelectionMarker(); int resizeDirectionFlags(QPointF position); diff --git a/limereport/lritemdesignintf.cpp b/limereport/lritemdesignintf.cpp index db18eb2..5987086 100644 --- a/limereport/lritemdesignintf.cpp +++ b/limereport/lritemdesignintf.cpp @@ -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; ievaluate(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("<","<"); - result.replace(">",">"); - return result; -} - Spacer::Spacer(QObject *owner, QGraphicsItem *parent) :ItemDesignIntf("Spacer",owner,parent){} diff --git a/limereport/lritemdesignintf.h b/limereport/lritemdesignintf.h index e6f6170..a9c8d7a 100644 --- a/limereport/lritemdesignintf.h +++ b/limereport/lritemdesignintf.h @@ -73,17 +73,8 @@ class ContentItemDesignIntf : public ItemDesignIntf public: ContentItemDesignIntf(const QString& xmlTypeName, QObject* owner = 0,QGraphicsItem* parent = 0) :ItemDesignIntf(xmlTypeName,owner,parent){} - virtual QString content() const; - 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; + virtual QString content() const = 0; + virtual void setContent(const QString& value) = 0; }; class LayoutDesignIntf : public ItemDesignIntf{