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 "lrgroupfunctions.h"
|
2024-09-04 17:31:16 +03:00
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
#include "lrbanddesignintf.h"
|
2024-09-04 17:31:16 +03:00
|
|
|
#include "lrdatasourcemanager.h"
|
2016-02-17 10:11:00 +03:00
|
|
|
#include "lritemdesignintf.h"
|
2018-03-14 11:22:03 +03:00
|
|
|
#include "lrpageitemdesignintf.h"
|
2024-09-04 17:31:16 +03:00
|
|
|
#include "lrscriptenginemanager.h"
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2016-02-17 10:11:00 +03:00
|
|
|
#include <QRegExp>
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
namespace LimeReport {
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void GroupFunction::slotBandRendered(BandDesignIntf* band)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2017-01-28 02:20:15 +03:00
|
|
|
ScriptEngineManager& sm = ScriptEngineManager::instance();
|
|
|
|
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2017-01-28 02:20:15 +03:00
|
|
|
QRegExp rxField(Const::FIELD_RX);
|
|
|
|
QRegExp rxVar(Const::VARIABLE_RX);
|
2021-08-24 10:22:30 +03:00
|
|
|
#else
|
2022-07-16 02:33:39 +03:00
|
|
|
QRegularExpression rxField = getFieldRegEx();
|
|
|
|
QRegularExpression rxVar = getVariableRegEx();
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
2017-01-28 02:20:15 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
switch (m_dataType) {
|
|
|
|
case Field: {
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2024-09-04 17:31:16 +03:00
|
|
|
if (rxField.indexIn(m_data) != -1) {
|
2017-01-28 02:20:15 +03:00
|
|
|
QString field = rxField.cap(1);
|
2021-08-24 10:22:30 +03:00
|
|
|
#else
|
|
|
|
QRegularExpressionMatch matchField = rxField.match(m_data);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (matchField.hasMatch()) {
|
2021-08-24 10:22:30 +03:00
|
|
|
QString field = matchField.captured(1);
|
|
|
|
#endif
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_dataManager->containsField(field)) {
|
2017-01-28 02:20:15 +03:00
|
|
|
m_values.push_back(m_dataManager->fieldData(field));
|
2018-03-14 11:22:03 +03:00
|
|
|
m_valuesByBand.insert(band, m_dataManager->fieldData(field));
|
2017-01-28 02:20:15 +03:00
|
|
|
} else {
|
|
|
|
setInvalid(tr("Field \"%1\" not found").arg(m_data));
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
break;
|
2021-08-24 10:22:30 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
case Variable: {
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2024-09-04 17:31:16 +03:00
|
|
|
if (rxVar.indexIn(m_data) != -1) {
|
2017-01-28 02:20:15 +03:00
|
|
|
QString var = rxVar.cap(1);
|
2021-08-24 10:22:30 +03:00
|
|
|
#else
|
|
|
|
QRegularExpressionMatch matchVar = rxVar.match(m_data);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (matchVar.hasMatch()) {
|
2021-08-24 10:22:30 +03:00
|
|
|
QString var = matchVar.captured(1);
|
|
|
|
#endif
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_dataManager->containsVariable(var)) {
|
2017-01-28 02:20:15 +03:00
|
|
|
m_values.push_back(m_dataManager->variable(var));
|
2018-03-14 11:22:03 +03:00
|
|
|
m_valuesByBand.insert(band, m_dataManager->variable(var));
|
2017-01-28 02:20:15 +03:00
|
|
|
} else {
|
|
|
|
setInvalid(tr("Variable \"%1\" not found").arg(m_data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-08-24 10:22:30 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
case Script: {
|
2017-01-28 02:20:15 +03:00
|
|
|
QVariant value = sm.evaluateScript(m_data);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (value.isValid()) {
|
2017-01-28 02:20:15 +03:00
|
|
|
m_values.push_back(value);
|
2018-03-14 11:22:03 +03:00
|
|
|
m_valuesByBand.insert(band, value);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
2017-01-28 02:20:15 +03:00
|
|
|
setInvalid(tr("Wrong script syntax \"%1\" ").arg(m_data));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
break;
|
2017-01-28 02:20:15 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
case ContentItem: {
|
2017-01-28 02:20:15 +03:00
|
|
|
QString itemName = m_data;
|
2024-09-04 17:31:16 +03:00
|
|
|
ContentItemDesignIntf* item
|
|
|
|
= dynamic_cast<ContentItemDesignIntf*>(band->childByName(itemName.remove('"')));
|
|
|
|
if (item) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_values.push_back(item->content());
|
2018-03-14 11:22:03 +03:00
|
|
|
m_valuesByBand.insert(band, item->content());
|
2024-09-04 17:31:16 +03:00
|
|
|
} else if (m_name.compare("COUNT", Qt::CaseInsensitive) == 0) {
|
2016-12-24 14:01:35 +03:00
|
|
|
m_values.push_back(1);
|
2018-03-14 11:22:03 +03:00
|
|
|
m_valuesByBand.insert(band, 1);
|
2024-09-04 17:31:16 +03:00
|
|
|
} else
|
|
|
|
setInvalid(tr("Item \"%1\" not found").arg(m_data));
|
2016-12-24 14:01:35 +03:00
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void GroupFunction::slotBandReRendered(BandDesignIntf* oldBand, BandDesignIntf* newBand)
|
2020-07-14 19:57:29 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_valuesByBand.contains(oldBand)) {
|
2020-07-14 19:57:29 +03:00
|
|
|
m_valuesByBand.insert(newBand, m_valuesByBand.value(oldBand));
|
|
|
|
m_valuesByBand.remove(oldBand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
QVariant GroupFunction::addition(QVariant value1, QVariant value2)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
return value1.toDouble() + value2.toDouble();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant GroupFunction::subtraction(QVariant value1, QVariant value2)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
return value1.toDouble() - value2.toDouble();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant GroupFunction::division(QVariant value1, QVariant value2)
|
|
|
|
{
|
|
|
|
return value1.toDouble() / value2.toDouble();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant GroupFunction::multiplication(QVariant value1, QVariant value2)
|
|
|
|
{
|
|
|
|
return value1.toDouble() * value2.toDouble();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
GroupFunction::GroupFunction(const QString& expression, const QString& dataBandName,
|
|
|
|
DataSourceManager* dataManager):
|
|
|
|
m_data(expression),
|
|
|
|
m_dataBandName(dataBandName),
|
|
|
|
m_dataManager(dataManager),
|
|
|
|
m_isValid(true),
|
|
|
|
m_errorMessage("")
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2024-09-04 17:31:16 +03:00
|
|
|
QRegExp rxField(Const::FIELD_RX, Qt::CaseInsensitive);
|
|
|
|
QRegExp rxVariable(Const::VARIABLE_RX, Qt::CaseInsensitive);
|
|
|
|
QRegExp rxScript(Const::SCRIPT_RX, Qt::CaseInsensitive);
|
2021-08-24 10:22:30 +03:00
|
|
|
#else
|
2022-07-16 02:33:39 +03:00
|
|
|
QRegularExpression rxField = getFieldRegEx();
|
|
|
|
QRegularExpression rxVariable = getVariableRegEx();
|
|
|
|
QRegularExpression rxScript = getScriptRegEx();
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2024-09-04 17:31:16 +03:00
|
|
|
if (rxScript.indexIn(expression) != -1) {
|
2021-08-24 10:22:30 +03:00
|
|
|
#else
|
|
|
|
QRegularExpressionMatch matchScript = rxScript.match(expression);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (matchScript.hasMatch()) {
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
2017-01-28 02:20:15 +03:00
|
|
|
m_dataType = Script;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2024-09-04 17:31:16 +03:00
|
|
|
if (rxField.indexIn(expression) != -1) {
|
2021-08-24 10:22:30 +03:00
|
|
|
#else
|
|
|
|
QRegularExpressionMatch matchField = rxField.match(expression);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (matchField.hasMatch()) {
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
2024-09-04 17:31:16 +03:00
|
|
|
m_dataType = Field;
|
2016-02-17 10:11:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2024-09-04 17:31:16 +03:00
|
|
|
if (rxVariable.indexIn(expression) != -1) {
|
2021-08-24 10:22:30 +03:00
|
|
|
#else
|
|
|
|
QRegularExpressionMatch matchVariable = rxVariable.match(expression);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (matchVariable.hasMatch()) {
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
2017-01-28 02:20:15 +03:00
|
|
|
m_dataType = Variable;
|
2016-02-17 10:11:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dataType = ContentItem;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
GroupFunction* GroupFunctionFactory::createGroupFunction(const QString& functionName,
|
|
|
|
const QString& expression,
|
|
|
|
const QString& dataBandName,
|
|
|
|
DataSourceManager* dataManager)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_creators.contains(functionName)) {
|
|
|
|
return m_creators.value(functionName)
|
|
|
|
->createFunction(expression, dataBandName, dataManager);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupFunctionFactory::~GroupFunctionFactory()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (GroupFunctionCreator* creator, m_creators.values()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
delete creator;
|
|
|
|
}
|
|
|
|
m_creators.clear();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant SumGroupFunction::calculate(PageItemDesignIntf* page)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2018-03-14 11:22:03 +03:00
|
|
|
QVariant res = 0;
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!page) {
|
|
|
|
foreach (QVariant value, values()) {
|
|
|
|
res = addition(res, value);
|
2018-03-14 11:22:03 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach (BandDesignIntf* band, page->bands()) {
|
|
|
|
res = addition(res, m_valuesByBand.value(band));
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant AvgGroupFunction::calculate(PageItemDesignIntf* page)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2018-03-14 11:22:03 +03:00
|
|
|
QVariant res = QVariant();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!page) {
|
|
|
|
foreach (QVariant value, values()) {
|
|
|
|
res = addition(res, value);
|
2018-03-14 11:22:03 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach (BandDesignIntf* band, page->bands()) {
|
|
|
|
res = addition(res, m_valuesByBand.value(band));
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!res.isNull() && (values().count() > 0)) {
|
|
|
|
res = division(res, values().count());
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant MinGroupFunction::calculate(PageItemDesignIntf* page)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
// TODO: check variant type
|
2016-02-17 10:11:00 +03:00
|
|
|
QVariant res = QVariant();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!page) {
|
|
|
|
if (!values().empty())
|
|
|
|
res = values().at(0);
|
|
|
|
foreach (QVariant value, values()) {
|
|
|
|
if (res.toDouble() > value.toDouble())
|
|
|
|
res = value;
|
2018-03-14 11:22:03 +03:00
|
|
|
}
|
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!page->bands().empty())
|
|
|
|
res = m_valuesByBand.value(page->bands().at(0));
|
2018-03-14 11:22:03 +03:00
|
|
|
foreach (BandDesignIntf* band, page->bands()) {
|
2024-09-04 17:31:16 +03:00
|
|
|
if (res.toDouble() > m_valuesByBand.value(band).toDouble())
|
|
|
|
res = m_valuesByBand.value(band);
|
2018-03-14 11:22:03 +03:00
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant MaxGroupFunction::calculate(PageItemDesignIntf* page)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
// TODO: check variant type
|
2016-02-17 10:11:00 +03:00
|
|
|
QVariant res = QVariant();
|
2018-03-14 11:22:03 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!page) {
|
|
|
|
if (!values().empty())
|
|
|
|
res = values().at(0);
|
|
|
|
foreach (QVariant value, values()) {
|
|
|
|
if (res.toDouble() < value.toDouble())
|
|
|
|
res = value;
|
2018-03-14 11:22:03 +03:00
|
|
|
}
|
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!page->bands().empty())
|
|
|
|
res = m_valuesByBand.value(page->bands().at(0));
|
2018-03-14 11:22:03 +03:00
|
|
|
foreach (BandDesignIntf* band, page->bands()) {
|
2024-09-04 17:31:16 +03:00
|
|
|
if (res.toDouble() < m_valuesByBand.value(band).toDouble())
|
|
|
|
res = m_valuesByBand.value(band);
|
2018-03-14 11:22:03 +03:00
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant CountGroupFunction::calculate(PageItemDesignIntf* page)
|
|
|
|
{
|
|
|
|
if (!page) {
|
2018-03-14 11:22:03 +03:00
|
|
|
return values().count();
|
|
|
|
} else {
|
|
|
|
int res = 0;
|
|
|
|
foreach (BandDesignIntf* band, page->bands()) {
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_valuesByBand.value(band).isNull()) {
|
2018-03-14 11:22:03 +03:00
|
|
|
res++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
} // namespace LimeReport
|