0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-27 01:48:09 +03:00
LimeReport/limereport/lrvariablesholder.cpp

152 lines
5.3 KiB
C++
Raw Normal View History

2016-02-17 10:11:00 +03:00
/***************************************************************************
* This file is part of the Lime Report project *
* Copyright (C) 2015 by Alexander Arin *
* 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"
#include <stdexcept>
#include <QStringList>
#include <QDebug>
namespace LimeReport{
2016-06-07 00:44:21 +03:00
VariablesHolder::VariablesHolder(QObject *parent) :
2016-02-17 10:11:00 +03:00
QObject(parent)
{
}
2016-06-07 00:44:21 +03:00
VariablesHolder::~VariablesHolder()
2016-02-17 10:11:00 +03:00
{
QMap<QString,VarDesc*>::iterator it = m_varNames.begin();
while(it!=m_varNames.end()){
delete *it;
it++;
}
m_varNames.clear();
m_userVariables.clear();
}
2016-06-07 00:44:21 +03:00
void VariablesHolder::addVariable(const QString& name, const QVariant& value, VarDesc::VarType type, RenderPass pass)
2016-02-17 10:11:00 +03:00
{
if (!m_varNames.contains(name)){
VarDesc* varValue = new VarDesc;
varValue->setName(name);
varValue->setValue(value);
varValue->setVarType(type);
varValue->setRenderPass(pass);
m_varNames.insert(name,varValue);
2016-06-29 21:24:54 +03:00
if (type==VarDesc::Report)
2016-02-17 10:11:00 +03:00
m_userVariables.append(varValue);
emit variableHasBeenAdded(name);
2016-02-17 10:11:00 +03:00
} else {
2017-06-28 16:58:01 +03:00
throw ReportError(tr("variable with name ")+name+tr(" already exists!"));
2016-02-17 10:11:00 +03:00
}
}
2016-06-07 00:44:21 +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();
else return QVariant();
}
2016-06-07 00:44:21 +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();
2017-06-28 16:58:01 +03:00
else 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::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);
emit variableHasBennDeleted(name);
2016-02-17 10:11:00 +03:00
}
}
2016-06-07 00:44:21 +03:00
void VariablesHolder::changeVariable(const QString &name, const QVariant &value)
2016-02-17 10:11:00 +03:00
{
if(m_varNames.contains(name)) {
m_varNames.value(name)->setValue(value);
emit variableHasBeenChanged(name);
2016-02-17 10:11:00 +03:00
} else
2017-06-28 16:58:01 +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
{
QMap<QString,VarDesc*>::iterator it = m_varNames.begin();
while (it != m_varNames.end()){
2016-06-07 00:44:21 +03:00
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;
}
}
}
2016-06-07 00:44:21 +03:00
bool VariablesHolder::containsVariable(const QString &name)
2016-02-17 10:11:00 +03:00
{
return m_varNames.contains(name);
}
2016-06-07 00:44:21 +03:00
int VariablesHolder::userVariablesCount()
2016-02-17 10:11:00 +03:00
{
return m_userVariables.count();
}
2016-06-07 00:44:21 +03:00
VarDesc *VariablesHolder::userVariableAt(int index)
2016-02-17 10:11:00 +03:00
{
return m_userVariables.at(index);
}
2016-06-07 00:44:21 +03:00
QStringList VariablesHolder::variableNames()
2016-02-17 10:11:00 +03:00
{
QStringList result;
foreach(QString varName,m_varNames.keys()){
result<<varName;
}
return result;
}
2016-06-07 00:44:21 +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();
2017-06-28 16:58:01 +03:00
else throw ReportError(tr("variable with name ")+name+tr(" does not exists!"));
2016-02-17 10:11:00 +03:00
}
}// namespace LimeReport