New functionality has been added to the report's variables

This commit is contained in:
Arin Alexander 2017-09-26 00:19:59 +03:00
parent 0ab92681b0
commit af589e31ba
11 changed files with 242 additions and 72 deletions

View File

@ -31,6 +31,7 @@
#define LRDATASOURCEMANAGERINTF_H
#include "lrcallbackdatasourceintf.h"
#include "lrglobal.h"
class QVariant;
class QString;
@ -56,6 +57,11 @@ public:
virtual void clearUserVariables()=0;
virtual ICallbackDatasource* createCallbackDatasource(const QString& name) = 0;
virtual void registerDbCredentialsProvider(IDbCredentialsProvider* provider) = 0;
virtual QStringList variableNames() = 0;
virtual bool variableIsMandatory(const QString& name) = 0;
virtual VariableDataType variableDataType(const QString& name) = 0;
virtual bool variableIsSystem(const QString& name) = 0;
};
}

View File

@ -157,6 +157,17 @@ namespace Const{
typedef QScriptValue ScriptValueType;
#endif
class Enums
{
public:
enum VariableDataType {Undefined, String, Bool, Int, Real, Date, Time, DateTime};
Q_ENUM(VariableDataType)
private:
Enums(){}
Q_GADGET
};
typedef Enums::VariableDataType VariableDataType;
} // namespace LimeReport
Q_DECLARE_OPERATORS_FOR_FLAGS(LimeReport::PreviewHints)

View File

@ -237,7 +237,7 @@ void DataBrowser::updateVariablesTree()
}
}
foreach(QString variableName,m_report->dataManager()->namesOfUserVariables()){
foreach(QString variableName,m_report->dataManager()->userVariableNames()){
if (!m_report->dataManager()->variableNames().contains(variableName)){
QStringList values;
values<<variableName+" ["+m_report->dataManager()->variable(variableName).toString()+"]"

View File

@ -30,8 +30,10 @@
#include "lrvariabledialog.h"
#include "ui_lrvariabledialog.h"
#include "lrglobal.h"
#include "lrvariablesholder.h"
#include <stdexcept>
#include <QMessageBox>
#include <QMetaEnum>
LRVariableDialog::LRVariableDialog(QWidget *parent) :
QDialog(parent),
@ -42,8 +44,14 @@ LRVariableDialog::LRVariableDialog(QWidget *parent) :
m_oldVariableName("")
{
ui->setupUi(this);
ui->cbbType->setVisible(false);
ui->lblType->setVisible(false);
static int enumIndex = LimeReport::Enums::staticMetaObject.indexOfEnumerator("VariableDataType");
QMetaEnum enumerator = LimeReport::Enums::staticMetaObject.enumerator(enumIndex);
for (int i = 0; i<enumerator.keyCount(); ++i){
ui->cbbType->addItem(enumerator.key(i));
}
//ui->cbbType->setVisible(false);
//ui->lblType->setVisible(false);
}
LRVariableDialog::~LRVariableDialog()
@ -66,14 +74,21 @@ void LRVariableDialog::setVariableName(const QString &value)
void LRVariableDialog::showEvent(QShowEvent *)
{
ui->leName->setText(m_variableName);
static int enumIndex = LimeReport::Enums::staticMetaObject.indexOfEnumerator("VariableDataType");
QMetaEnum enumerator = LimeReport::Enums::staticMetaObject.enumerator(enumIndex);
if (!m_variableName.isEmpty()&&m_variablesContainer&&m_variablesContainer->containsVariable(m_variableName)){
ui->leValue->setText(m_variablesContainer->variable(m_variableName).toString());
ui->cbbType->setCurrentText(enumerator.valueToKey(m_variablesContainer->variableDataType(m_variableName)));
ui->cbbMandatory->setChecked(m_variablesContainer->variableIsMandatory(m_variableName));
}
}
void LRVariableDialog::accept()
{
try{
static int enumIndex = LimeReport::Enums::staticMetaObject.indexOfEnumerator("VariableDataType");
QMetaEnum enumerator = LimeReport::Enums::staticMetaObject.enumerator(enumIndex);
if (m_variablesContainer&&!ui->leName->text().isEmpty()){
if (m_changeMode){
if (m_oldVariableName==ui->leName->text()){
@ -85,6 +100,11 @@ void LRVariableDialog::accept()
} else {
m_variablesContainer->addVariable(ui->leName->text(),value(), LimeReport::VarDesc::Report);
}
m_variablesContainer->setVarableMandatory(ui->leName->text(),ui->cbbMandatory->isChecked());
m_variablesContainer->setVariableDataType(
ui->leName->text(),
LimeReport::VariableDataType(enumerator.keysToValue(ui->cbbType->currentText().toLatin1()))
);
emit signalVariableAccepted(ui->leName->text());
QDialog::accept();
}

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>218</width>
<height>126</height>
<width>328</width>
<height>173</height>
</rect>
</property>
<property name="windowTitle">
@ -18,35 +18,8 @@
<normaloff>:/databrowser/images/value</normaloff>:/databrowser/images/value</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>2</number>
</property>
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>4</number>
</property>
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<number>4</number>
</property>
<item>
<layout class="QFormLayout" name="formLayout">
<property name="horizontalSpacing">
<number>7</number>
</property>
<property name="verticalSpacing">
<number>5</number>
</property>
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>8</number>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="lblName">
<property name="text">
@ -67,9 +40,6 @@
<item row="1" column="1">
<widget class="QLineEdit" name="leValue"/>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="cbbType"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="lblType">
<property name="text">
@ -77,8 +47,31 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="cbbType"/>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="cbbMandatory">
<property name="text">
<string>Mandatory</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">

View File

@ -211,7 +211,7 @@ void DataSourceModel::updateModel()
}
vars = m_rootNode->addChild(tr("External variables"),DataNode::Variables,QIcon(":/report/images/folder"));
foreach (QString name, m_dataManager->namesOfUserVariables()){
foreach (QString name, m_dataManager->userVariableNames()){
vars->addChild(name,DataNode::Variable,QIcon(":/report/images/value"));
}
}
@ -1072,7 +1072,7 @@ int DataSourceManager::elementsCount(const QString &collectionName)
return m_proxies.count();
}
if (collectionName=="variables"){
return m_reportVariables.userVariablesCount();
return m_reportVariables.variablesCount();
}
return 0;
}
@ -1092,7 +1092,7 @@ QObject* DataSourceManager::elementAt(const QString &collectionName, int index)
return m_proxies.at(index);
}
if (collectionName=="variables"){
return m_reportVariables.userVariableAt(index);
return m_reportVariables.variableAt(index);
}
return 0;
}
@ -1160,6 +1160,8 @@ void DataSourceManager::collectionLoadFinished(const QString &collectionName)
foreach (VarDesc* item, m_tempVars) {
if (!m_reportVariables.containsVariable(item->name())){
m_reportVariables.addVariable(item->name(),item->value(),VarDesc::Report,FirstPass);
VarDesc* currentVar = m_reportVariables.variableByName(item->name());
currentVar->initFrom(item);
}
delete item;
}
@ -1454,6 +1456,19 @@ bool DataSourceManager::variableIsSystem(const QString &name)
return false;
}
bool DataSourceManager::variableIsMandatory(const QString& name)
{
if (m_reportVariables.containsVariable(name))
return m_reportVariables.variableByName(name)->isMandatory();
return false;
}
void DataSourceManager::setVarableMandatory(const QString& name, bool value)
{
if (m_reportVariables.containsVariable(name))
m_reportVariables.variableByName(name)->setMandatory(value);
}
QStringList DataSourceManager::variableNames()
{
return m_reportVariables.variableNames();
@ -1470,7 +1485,7 @@ QStringList DataSourceManager::variableNamesByRenderPass(RenderPass pass)
return result;
}
QStringList DataSourceManager::namesOfUserVariables(){
QStringList DataSourceManager::userVariableNames(){
return m_userVariables.variableNames();
}
@ -1481,6 +1496,19 @@ VarDesc::VarType DataSourceManager::variableType(const QString &name)
return VarDesc::User;
}
VariableDataType DataSourceManager::variableDataType(const QString& name)
{
if (m_reportVariables.containsVariable(name))
return m_reportVariables.variableByName(name)->dataType();
return VariableDataType::Undefined;
}
void DataSourceManager::setVariableDataType(const QString& name, VariableDataType value)
{
if (m_reportVariables.containsVariable(name))
m_reportVariables.variableByName(name)->setDataType(value);
}
void DataSourceManager::setAllDatasourcesToFirst()
{
foreach(IDataSourceHolder* ds,m_datasources.values()) {

View File

@ -127,9 +127,13 @@ public:
RenderPass variablePass(const QString& name);
QStringList variableNames();
QStringList variableNamesByRenderPass(RenderPass pass);
QStringList namesOfUserVariables();
QStringList userVariableNames();
VarDesc::VarType variableType(const QString& name);
VariableDataType variableDataType(const QString& name);
void setVariableDataType(const QString &name, VariableDataType value);
bool variableIsSystem(const QString& name);
bool variableIsMandatory(const QString& name);
void setVarableMandatory(const QString &name, bool value);
QString queryText(const QString& dataSourceName);
QString connectionName(const QString& dataSourceName);
void removeDatasource(const QString& name);

View File

@ -31,6 +31,7 @@
#define LRDATASOURCEMANAGERINTF_H
#include "lrcallbackdatasourceintf.h"
#include "lrglobal.h"
class QVariant;
class QString;
@ -56,6 +57,11 @@ public:
virtual void clearUserVariables()=0;
virtual ICallbackDatasource* createCallbackDatasource(const QString& name) = 0;
virtual void registerDbCredentialsProvider(IDbCredentialsProvider* provider) = 0;
virtual QStringList variableNames() = 0;
virtual bool variableIsMandatory(const QString& name) = 0;
virtual VariableDataType variableDataType(const QString& name) = 0;
virtual bool variableIsSystem(const QString& name) = 0;
};
}

View File

@ -157,6 +157,17 @@ namespace Const{
typedef QScriptValue ScriptValueType;
#endif
class Enums
{
public:
enum VariableDataType {Undefined, String, Bool, Int, Real, Date, Time, DateTime};
Q_ENUM(VariableDataType)
private:
Enums(){}
Q_GADGET
};
typedef Enums::VariableDataType VariableDataType;
} // namespace LimeReport
Q_DECLARE_OPERATORS_FOR_FLAGS(LimeReport::PreviewHints)

View File

@ -122,16 +122,50 @@ bool VariablesHolder::containsVariable(const QString &name)
return m_varNames.contains(name);
}
int VariablesHolder::userVariablesCount()
int VariablesHolder::variablesCount()
{
return m_userVariables.count();
}
VarDesc *VariablesHolder::userVariableAt(int index)
VarDesc* VariablesHolder::variableByName(const QString& name)
{
if (m_varNames.contains(name))
return m_varNames.value(name);
else return 0;
}
VarDesc *VariablesHolder::variableAt(int index)
{
return m_userVariables.at(index);
}
bool VariablesHolder::variableIsMandatory(const QString& name)
{
if (m_varNames.contains(name))
return m_varNames.value(name)->isMandatory();
else return false;
}
void VariablesHolder::setVarableMandatory(const QString& name, bool value)
{
if (m_varNames.contains(name))
m_varNames.value(name)->setMandatory(value);
}
VariableDataType VariablesHolder::variableDataType(const QString& name)
{
if (m_varNames.contains(name))
return m_varNames.value(name)->dataType();
else return Enums::Undefined;
}
void VariablesHolder::setVariableDataType(const QString& name, VariableDataType value)
{
if (m_varNames.contains(name))
m_varNames.value(name)->setDataType(value);
}
QStringList VariablesHolder::variableNames()
{
QStringList result;
@ -148,4 +182,40 @@ RenderPass VariablesHolder::variablePass(const QString &name)
else throw ReportError(tr("variable with name ")+name+tr(" does not exists!"));
}
bool VarDesc::isMandatory() const
{
return m_mandatory;
}
void VarDesc::setMandatory(bool mandatory)
{
m_mandatory = mandatory;
}
void VarDesc::initFrom(VarDesc* value)
{
m_mandatory = value->isMandatory();
m_dataType = value->dataType();
}
VariableDataType VarDesc::dataType() const
{
return m_dataType;
}
void VarDesc::setDataType(const VariableDataType& dataType)
{
m_dataType = dataType;
}
int VarDesc::readDataTypeProperty() const
{
return static_cast<int>(m_dataType);
}
void VarDesc::setDataTypeProperty(int value)
{
m_dataType = static_cast<VariableDataType>(value);
}
}// namespace LimeReport

View File

@ -42,7 +42,10 @@ class VarDesc : public QObject{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QVariant value READ value WRITE setValue)
Q_PROPERTY(bool isMandatory READ isMandatory WRITE setMandatory)
Q_PROPERTY(int dataType READ readDataTypeProperty WRITE setDataTypeProperty)
public:
VarDesc() : m_dataType(VariableDataType::Undefined), m_mandatory(false){}
enum VarType {System, User, Report};
void setVarType(VarType value){m_varType=value;}
VarType varType(){return m_varType;}
@ -52,26 +55,39 @@ public:
QString name(){return m_name;}
void setValue(QVariant value){m_value=value;}
QVariant value(){return m_value;}
VariableDataType dataType() const;
void setDataType(const VariableDataType& dataType);
int readDataTypeProperty() const;
void setDataTypeProperty(int value);
bool isMandatory() const;
void setMandatory(bool isMandatory);
void initFrom(VarDesc* value);
private:
VarType m_varType;
RenderPass m_varPass;
QString m_name;
QVariant m_value;
VariableDataType m_dataType;
bool m_mandatory;
};
class IVariablesContainer
{
public:
virtual ~IVariablesContainer(){}
virtual void addVariable(const QString &name, const QVariant &value, VarDesc::VarType type=VarDesc::User, RenderPass pass=FirstPass)=0;
virtual void deleteVariable(const QString &name)=0;
virtual void changeVariable(const QString &name, const QVariant &value)=0;
virtual void clearUserVariables()=0;
virtual QVariant variable(const QString &name)=0;
virtual VarDesc::VarType variableType(const QString &name)=0;
virtual RenderPass variablePass(const QString &name)=0;
virtual bool containsVariable(const QString &name)=0;
virtual QStringList variableNames()=0;
virtual void addVariable(const QString& name, const QVariant &value, VarDesc::VarType type=VarDesc::User, RenderPass pass=FirstPass) = 0;
virtual void deleteVariable(const QString& name) = 0;
virtual void changeVariable(const QString& name, const QVariant &value) = 0;
virtual void clearUserVariables() = 0;
virtual QVariant variable(const QString& name) = 0;
virtual VarDesc::VarType variableType(const QString& name) = 0;
virtual RenderPass variablePass(const QString& name) = 0;
virtual bool containsVariable(const QString& name) = 0;
virtual QStringList variableNames() = 0;
virtual bool variableIsMandatory(const QString& name) = 0;
virtual void setVarableMandatory(const QString& name, bool value) = 0;
virtual VariableDataType variableDataType(const QString& name) = 0;
virtual void setVariableDataType(const QString& name, VariableDataType value) = 0;
};
class VariablesHolder : public QObject, public IVariablesContainer
@ -85,12 +101,17 @@ public:
void changeVariable(const QString &name, const QVariant &value);
void clearUserVariables();
QVariant variable(const QString &name);
VarDesc::VarType variableType(const QString &name);
VarDesc::VarType variableType(const QString& name);
RenderPass variablePass(const QString &name);
bool containsVariable(const QString &name);
QStringList variableNames();
int userVariablesCount();
VarDesc* userVariableAt(int index);
int variablesCount();
VarDesc* variableByName(const QString& name);
VarDesc* variableAt(int index);
bool variableIsMandatory(const QString& name);
void setVarableMandatory(const QString &name, bool value);
VariableDataType variableDataType(const QString& name);
void setVariableDataType(const QString &name, VariableDataType value);
signals:
void variableHasBeenAdded(const QString& variableName);
void variableHasBeenChanged(const QString& variableName);