1 /***************************************************************************
2 * This file is part of the Lime Report project *
3 * Copyright (C) 2015 by Alexander Arin *
4 * arin_a@bk.ru *
5 * *
6 ** GNU General Public License Usage **
7 * *
8 * This library is free software: you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation, either version 3 of the License, or *
11 * (at your option) any later version. *
12 * You should have received a copy of the GNU General Public License *
13 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
14 * *
15 ** GNU Lesser General Public License **
16 * *
17 * This library is free software: you can redistribute it and/or modify *
18 * it under the terms of the GNU Lesser General Public License as *
19 * published by the Free Software Foundation, either version 3 of the *
20 * License, or (at your option) any later version. *
21 * You should have received a copy of the GNU Lesser General Public *
22 * License along with this library. *
23 * If not, see <http://www.gnu.org/licenses/>. *
24 * *
25 * This library is distributed in the hope that it will be useful, *
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
28 * GNU General Public License for more details. *
29 ****************************************************************************/
30 #include "lrgroupfunctions.h"
31 #include "lrdatasourcemanager.h"
32 #include "lrbanddesignintf.h"
33 #include "lritemdesignintf.h"
34 #include "lrscriptenginemanager.h"
35 #include "lrpageitemdesignintf.h"
36
37 #include <QRegExp>
38
39 namespace LimeReport {
40
slotBandRendered(BandDesignIntf * band)41 void GroupFunction::slotBandRendered(BandDesignIntf *band)
42 {
43 ScriptEngineManager& sm = ScriptEngineManager::instance();
44
45 QRegExp rxField(Const::FIELD_RX);
46 QRegExp rxVar(Const::VARIABLE_RX);
47
48 switch (m_dataType){
49 case Field:
50 if (rxField.indexIn(m_data) != -1){
51 QString field = rxField.cap(1);
52 if (m_dataManager->containsField(field)){
53 m_values.push_back(m_dataManager->fieldData(field));
54 m_valuesByBand.insert(band, m_dataManager->fieldData(field));
55 } else {
56 setInvalid(tr("Field \"%1\" not found").arg(m_data));
57 }
58 }
59 break;
60 case Variable:
61 if (rxVar.indexIn(m_data) != -1){
62 QString var = rxVar.cap(1);
63 if (m_dataManager->containsVariable(var)){
64 m_values.push_back(m_dataManager->variable(var));
65 m_valuesByBand.insert(band, m_dataManager->variable(var));
66 } else {
67 setInvalid(tr("Variable \"%1\" not found").arg(m_data));
68 }
69 }
70 break;
71 case Script:
72 {
73 QVariant value = sm.evaluateScript(m_data);
74 if (value.isValid()){
75 m_values.push_back(value);
76 m_valuesByBand.insert(band, value);
77 } else {
78 setInvalid(tr("Wrong script syntax \"%1\" ").arg(m_data));
79 }
80 break;
81 }
82 case ContentItem:{
83 QString itemName = m_data;
84 ContentItemDesignIntf* item = dynamic_cast<ContentItemDesignIntf*>(band->childByName(itemName.remove('"')));
85 if (item){
86 m_values.push_back(item->content());
87 m_valuesByBand.insert(band, item->content());
88 } else if (m_name.compare("COUNT",Qt::CaseInsensitive) == 0) {
89 m_values.push_back(1);
90 m_valuesByBand.insert(band, 1);
91 } else setInvalid(tr("Item \"%1\" not found").arg(m_data));
92
93 break;
94 }
95 default:
96 break;
97 }
98 }
99
addition(QVariant value1,QVariant value2)100 QVariant GroupFunction::addition(QVariant value1, QVariant value2)
101 {
102 return value1.toDouble()+value2.toDouble();
103 }
104
subtraction(QVariant value1,QVariant value2)105 QVariant GroupFunction::subtraction(QVariant value1, QVariant value2)
106 {
107 return value1.toDouble()-value2.toDouble();
108 }
109
division(QVariant value1,QVariant value2)110 QVariant GroupFunction::division(QVariant value1, QVariant value2)
111 {
112 return value1.toDouble() / value2.toDouble();
113 }
114
multiplication(QVariant value1,QVariant value2)115 QVariant GroupFunction::multiplication(QVariant value1, QVariant value2)
116 {
117 return value1.toDouble() * value2.toDouble();
118 }
119
GroupFunction(const QString & expression,const QString & dataBandName,DataSourceManager * dataManager)120 GroupFunction::GroupFunction(const QString &expression, const QString &dataBandName, DataSourceManager* dataManager)
121 :m_data(expression), m_dataBandName(dataBandName), m_dataManager(dataManager), m_isValid(true), m_errorMessage("")
122 {
123 QRegExp rxField(Const::FIELD_RX,Qt::CaseInsensitive);
124 QRegExp rxVariable(Const::VARIABLE_RX,Qt::CaseInsensitive);
125 QRegExp rxScript(Const::SCRIPT_RX,Qt::CaseInsensitive);
126
127 if (rxScript.indexIn(expression) != -1){
128 m_dataType = Script;
129 return;
130 }
131
132 if (rxField.indexIn(expression) != -1){
133 m_dataType=Field;
134 return;
135 }
136
137 if (rxVariable.indexIn(expression) != -1){
138 m_dataType = Variable;
139 return;
140 }
141
142 m_dataType = ContentItem;
143 }
144
createGroupFunction(const QString & functionName,const QString & expression,const QString & dataBandName,DataSourceManager * dataManager)145 GroupFunction *GroupFunctionFactory::createGroupFunction(const QString &functionName, const QString &expression, const QString& dataBandName, DataSourceManager *dataManager)
146 {
147 if (m_creators.contains(functionName)){
148 return m_creators.value(functionName)->createFunction(expression, dataBandName, dataManager);
149 }
150 return 0;
151 }
152
~GroupFunctionFactory()153 GroupFunctionFactory::~GroupFunctionFactory()
154 {
155 foreach(GroupFunctionCreator* creator, m_creators.values()){
156 delete creator;
157 }
158 m_creators.clear();
159 }
160
calculate(PageItemDesignIntf * page)161 QVariant SumGroupFunction::calculate(PageItemDesignIntf *page)
162 {
163 QVariant res = 0;
164 if (!page){
165 foreach(QVariant value,values()){
166 res = addition(res,value);
167 }
168 } else {
169 foreach (BandDesignIntf* band, page->bands()) {
170 res = addition(res, m_valuesByBand.value(band));
171 }
172 }
173 return res;
174 }
175
calculate(PageItemDesignIntf * page)176 QVariant AvgGroupFunction::calculate(PageItemDesignIntf *page)
177 {
178 QVariant res = QVariant();
179 if (!page){
180 foreach(QVariant value,values()){
181 res=addition(res,value);
182 }
183 } else {
184 foreach (BandDesignIntf* band, page->bands()) {
185 res = addition(res, m_valuesByBand.value(band));
186 }
187 }
188 if (!res.isNull()&&(values().count()>0)){
189 res=division(res,values().count());
190 }
191 return res;
192 }
193
calculate(PageItemDesignIntf * page)194 QVariant MinGroupFunction::calculate(PageItemDesignIntf *page)
195 {
196 //TODO: check variant type
197 QVariant res = QVariant();
198 if (!page){
199 if (!values().empty()) res = values().at(0);
200 foreach(QVariant value, values()){
201 if (res.toDouble() > value.toDouble()) res = value;
202 }
203 } else {
204 if (!page->bands().empty()) res = m_valuesByBand.value(page->bands().at(0));
205 foreach (BandDesignIntf* band, page->bands()) {
206 if (res.toDouble() > m_valuesByBand.value(band).toDouble()) res = m_valuesByBand.value(band);
207 }
208 }
209
210 return res;
211 }
212
calculate(PageItemDesignIntf * page)213 QVariant MaxGroupFunction::calculate(PageItemDesignIntf *page)
214 {
215 //TODO: check variant type
216 QVariant res = QVariant();
217
218 if (!page){
219 if (!values().empty()) res = values().at(0);
220 foreach(QVariant value, values()){
221 if (res.toDouble() < value.toDouble()) res = value;
222 }
223 } else {
224 if (!page->bands().empty()) res = m_valuesByBand.value(page->bands().at(0));
225 foreach (BandDesignIntf* band, page->bands()) {
226 if (res.toDouble() < m_valuesByBand.value(band).toDouble()) res = m_valuesByBand.value(band);
227 }
228 }
229
230 return res;
231 }
232
calculate(PageItemDesignIntf * page)233 QVariant CountGroupFunction::calculate(PageItemDesignIntf *page){
234 if (!page){
235 return values().count();
236 } else {
237 int res = 0;
238 foreach (BandDesignIntf* band, page->bands()) {
239 if (!m_valuesByBand.value(band).isNull()){
240 res++;
241 }
242 }
243 return res;
244 }
245 }
246
247 } //namespace LimeReport
248
249