2016-02-17 10:11:00 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* This file is part of the Lime Report project *
|
2021-08-18 20:21:36 +03:00
|
|
|
* Copyright (C) 2021 by Alexander Arin *
|
2016-02-17 10:11:00 +03:00
|
|
|
* arin_a@bk.ru *
|
|
|
|
* *
|
|
|
|
** GNU General Public License Usage **
|
|
|
|
* *
|
|
|
|
* This library is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
** GNU Lesser General Public License **
|
|
|
|
* *
|
|
|
|
* This library is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this library. *
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
* This library is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
****************************************************************************/
|
|
|
|
#include "lrvariablesholder.h"
|
2024-09-04 17:31:16 +03:00
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
#include <QDebug>
|
2024-09-04 17:31:16 +03:00
|
|
|
#include <QStringList>
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
#include <stdexcept>
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
namespace LimeReport {
|
|
|
|
|
|
|
|
VariablesHolder::VariablesHolder(QObject* parent): QObject(parent) { }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2016-06-07 00:44:21 +03:00
|
|
|
VariablesHolder::~VariablesHolder()
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
QMap<QString, VarDesc*>::iterator it = m_varNames.begin();
|
|
|
|
while (it != m_varNames.end()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
delete *it;
|
2019-06-28 13:08:33 +03:00
|
|
|
++it;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
m_varNames.clear();
|
|
|
|
m_userVariables.clear();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void VariablesHolder::addVariable(const QString& name, const QVariant& value, VarDesc::VarType type,
|
|
|
|
RenderPass pass)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_varNames.contains(name)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
VarDesc* varValue = new VarDesc;
|
|
|
|
varValue->setName(name);
|
|
|
|
varValue->setValue(value);
|
|
|
|
varValue->setVarType(type);
|
|
|
|
varValue->setRenderPass(pass);
|
2024-09-04 17:31:16 +03:00
|
|
|
m_varNames.insert(name, varValue);
|
|
|
|
if (type == VarDesc::Report)
|
2016-02-17 10:11:00 +03:00
|
|
|
m_userVariables.append(varValue);
|
2017-09-19 21:02:55 +03:00
|
|
|
emit variableHasBeenAdded(name);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
throw ReportError(tr("variable with name ") + name + tr(" already exists!"));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant VariablesHolder::variable(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
if (m_varNames.contains(name))
|
|
|
|
return m_varNames.value(name)->value();
|
2024-09-04 17:31:16 +03:00
|
|
|
else
|
|
|
|
return QVariant();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
VarDesc::VarType VariablesHolder::variableType(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
if (m_varNames.contains(name))
|
|
|
|
return m_varNames.value(name)->varType();
|
2024-09-04 17:31:16 +03:00
|
|
|
else
|
|
|
|
throw ReportError(tr("variable with name ") + name + tr(" does not exists!"));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void VariablesHolder::deleteVariable(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
if (m_varNames.contains(name)) {
|
|
|
|
m_userVariables.removeOne(m_varNames.value(name));
|
|
|
|
delete m_varNames.value(name);
|
|
|
|
m_varNames.remove(name);
|
2017-09-19 21:02:55 +03:00
|
|
|
emit variableHasBennDeleted(name);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void VariablesHolder::changeVariable(const QString& name, const QVariant& value)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_varNames.contains(name)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_varNames.value(name)->setValue(value);
|
2017-09-19 21:02:55 +03:00
|
|
|
emit variableHasBeenChanged(name);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else
|
2024-09-04 17:31:16 +03:00
|
|
|
throw ReportError(tr("variable with name ") + name + tr(" does not exists!"));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2016-06-07 00:44:21 +03:00
|
|
|
void VariablesHolder::clearUserVariables()
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
QMap<QString, VarDesc*>::iterator it = m_varNames.begin();
|
|
|
|
while (it != m_varNames.end()) {
|
|
|
|
if (it.value()->varType() == VarDesc::User || it.value()->varType() == VarDesc::Report) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_userVariables.removeAll(it.value());
|
|
|
|
delete it.value();
|
|
|
|
it = m_varNames.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool VariablesHolder::containsVariable(const QString& name) { return m_varNames.contains(name); }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
int VariablesHolder::variablesCount() { return m_userVariables.count(); }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2017-09-26 00:19:59 +03:00
|
|
|
VarDesc* VariablesHolder::variableByName(const QString& name)
|
|
|
|
{
|
|
|
|
if (m_varNames.contains(name))
|
|
|
|
return m_varNames.value(name);
|
2024-09-04 17:31:16 +03:00
|
|
|
else
|
|
|
|
return 0;
|
2017-09-26 00:19:59 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
VarDesc* VariablesHolder::variableAt(int index) { return m_userVariables.at(index); }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2017-09-26 00:19:59 +03:00
|
|
|
bool VariablesHolder::variableIsMandatory(const QString& name)
|
|
|
|
{
|
|
|
|
if (m_varNames.contains(name))
|
|
|
|
return m_varNames.value(name)->isMandatory();
|
2024-09-04 17:31:16 +03:00
|
|
|
else
|
|
|
|
return false;
|
2017-09-26 00:19:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2024-09-04 17:31:16 +03:00
|
|
|
else
|
|
|
|
return Enums::Undefined;
|
2017-09-26 00:19:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void VariablesHolder::setVariableDataType(const QString& name, VariableDataType value)
|
|
|
|
{
|
|
|
|
if (m_varNames.contains(name))
|
|
|
|
m_varNames.value(name)->setDataType(value);
|
|
|
|
}
|
|
|
|
|
2016-06-07 00:44:21 +03:00
|
|
|
QStringList VariablesHolder::variableNames()
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
QStringList result;
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString varName, m_varNames.keys()) {
|
|
|
|
result << varName;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
RenderPass VariablesHolder::variablePass(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
if (m_varNames.contains(name))
|
|
|
|
return m_varNames.value(name)->renderPass();
|
2024-09-04 17:31:16 +03:00
|
|
|
else
|
|
|
|
throw ReportError(tr("variable with name ") + name + tr(" does not exists!"));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool VarDesc::isMandatory() const { return m_mandatory; }
|
2017-09-26 00:19:59 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void VarDesc::setMandatory(bool mandatory) { m_mandatory = mandatory; }
|
2017-09-26 00:19:59 +03:00
|
|
|
|
|
|
|
void VarDesc::initFrom(VarDesc* value)
|
|
|
|
{
|
|
|
|
m_mandatory = value->isMandatory();
|
|
|
|
m_dataType = value->dataType();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
VariableDataType VarDesc::dataType() const { return m_dataType; }
|
2017-09-26 00:19:59 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void VarDesc::setDataType(const VariableDataType& dataType) { m_dataType = dataType; }
|
2017-09-26 00:19:59 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
int VarDesc::readDataTypeProperty() const { return static_cast<int>(m_dataType); }
|
2017-09-26 00:19:59 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void VarDesc::setDataTypeProperty(int value) { m_dataType = static_cast<VariableDataType>(value); }
|
2017-09-26 00:19:59 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
} // namespace LimeReport
|