Line data Source code
1 : #include "replacer.hpp"
2 :
3 : #include <QRegularExpression>
4 : #include <QImage>
5 : #include <QByteArray>
6 : #include <QDebug>
7 : #include <QList>
8 : #include <QSet>
9 :
10 : namespace qtreports
11 : {
12 : namespace detail
13 : {
14 :
15 244 : Replacer::Replacer() {}
16 :
17 244 : Replacer::~Replacer() {}
18 :
19 1572 : QString Replacer::replaceParameters(const QString & text, const ReportPtr & report)
20 : {
21 1572 : auto newText = text;
22 3144 : QRegularExpression expr("(\\$P\\{\\w+\\})", QRegularExpression::CaseInsensitiveOption);
23 3144 : auto iterator = expr.globalMatch(newText);
24 2250 : while(iterator.hasNext())
25 : {
26 678 : auto match = iterator.next();
27 678 : auto name = match.captured(1).remove(0, 3).remove(-1, 1);
28 678 : auto parameter = report->getParameter(name);
29 339 : newText.replace(match.captured(1), parameter.toString()); // TODO fix for classname
30 : }
31 :
32 3144 : return newText;
33 : }
34 :
35 1572 : bool Replacer::replaceParametersInTextWidget(const TextWidgetPtr & widget, const ReportPtr & report)
36 : {
37 3144 : auto text = widget->getText();
38 3144 : auto replacedText = replaceParameters(text, report);
39 1572 : widget->setText(replacedText);
40 :
41 3144 : return true;
42 : }
43 :
44 583 : bool Replacer::replaceParametersInSection(const SectionPtr & section, const ReportPtr & report)
45 : {
46 583 : if(section.isNull())
47 : {
48 0 : m_lastError = "Section is empty.";
49 0 : return false;
50 : }
51 :
52 1424 : for(auto && band : section->getBands())
53 : {
54 2413 : for(auto && textWidget : band->getTextWidgets())
55 : {
56 1572 : if(!replaceParametersInTextWidget(textWidget, report))
57 : {
58 0 : return false;
59 : }
60 : }
61 : }
62 :
63 583 : return true;
64 : }
65 :
66 209 : bool Replacer::replaceParameters(const ReportPtr & report)
67 : {
68 209 : if(report.isNull())
69 : {
70 0 : m_lastError = "Report is empty.";
71 0 : return false;
72 : }
73 :
74 418 : auto detail = report->getDetail();
75 209 : if(detail.isNull())
76 : {
77 0 : m_lastError = "Report->Detail is empty.";
78 0 : return false;
79 : }
80 :
81 209 : if(!replaceParametersInSection(detail, report))
82 : {
83 0 : return false;
84 : }
85 :
86 418 : auto title = report->getTitle();
87 209 : if(!title.isNull() && !replaceParametersInSection(title, report))
88 : {
89 0 : return false;
90 : }
91 :
92 418 : auto summary = report->getSummary();
93 209 : if(!summary.isNull() && !replaceParametersInSection(summary, report))
94 : {
95 0 : return false;
96 : }
97 :
98 209 : return true;
99 : }
100 :
101 812 : QString Replacer::replaceField(const QString & text, const ReportPtr & report, int i)
102 : {
103 812 : auto newText = text;
104 1624 : QRegularExpression expr("(\\$F\\{\\w+\\})", QRegularExpression::CaseInsensitiveOption);
105 1624 : auto iterator = expr.globalMatch(newText);
106 2112 : while(iterator.hasNext())
107 : {
108 1300 : auto match = iterator.next();
109 1300 : auto name = match.captured(1).remove(0, 3).remove(-1, 1);
110 1300 : auto parameter = report->getField(name)->getData(i);
111 650 : newText.replace(match.captured(1), parameter);
112 : }
113 :
114 1624 : return newText;
115 : }
116 :
117 60 : QImage Replacer::replaceFieldImage(const QString & text, const ReportPtr & report, int i)
118 : {
119 120 : QRegularExpression expr("(\\$F\\{\\w+\\})", QRegularExpression::CaseInsensitiveOption);
120 120 : auto iterator = expr.globalMatch(text);
121 60 : while(iterator.hasNext())
122 : {
123 120 : auto match = iterator.next();
124 120 : auto name = match.captured(1).remove(0, 3).remove(-1, 1);
125 120 : auto byteArray = report->getField(name)->getData< QByteArray >(i);
126 120 : auto image = QImage::fromData(byteArray);
127 60 : return image;
128 : }
129 :
130 0 : return QImage();
131 : }
132 :
133 6 : bool Replacer::replaceRowGroupsInCrosstab(const CrosstabPtr & crosstab, const ReportPtr & report, QList<QString> & rowGroups)
134 : {
135 6 : replaceRowGroupInCrosstab(crosstab, report, 0);
136 12 : QList<QString> oldValues;
137 12 : QString value = crosstab->getRowGroup()->getHeader()->getCellContents()->getTextField()->getText();
138 6 : rowGroups.append(value);
139 6 : oldValues.append(value);
140 78 : for(int i = 0; i < report->getFieldsDataCount(); i++)
141 : {
142 72 : replaceRowGroupInCrosstab(crosstab, report, i);
143 72 : value = crosstab->getRowGroup()->getHeader()->getCellContents()->getTextField()->getText();
144 72 : if(!oldValues.contains(value))
145 : {
146 18 : oldValues.append(value);
147 18 : rowGroups.append(value);
148 : }
149 : }
150 12 : return true;
151 : }
152 :
153 6 : bool Replacer::replaceColumnGroupsInCrosstab(const CrosstabPtr & crosstab, const ReportPtr & report, QList<QString> & columnGroups)
154 : {
155 6 : replaceColumnGroupInCrosstab(crosstab, report, 0);
156 12 : QList<QString> oldValues;
157 12 : QString value = crosstab->getColumnGroup()->getHeader()->getCellContents()->getTextField()->getText();
158 6 : columnGroups.append(value);
159 6 : oldValues.append(value);
160 78 : for(int i = 0; i < report->getFieldsDataCount(); i++)
161 : {
162 72 : replaceColumnGroupInCrosstab(crosstab, report, i);
163 72 : value = crosstab->getColumnGroup()->getHeader()->getCellContents()->getTextField()->getText();
164 72 : if(!oldValues.contains(value))
165 : {
166 18 : oldValues.append(value);
167 18 : columnGroups.append(value);
168 : }
169 : }
170 12 : return true;
171 : }
172 :
173 3 : bool Replacer::replaceCellsInCrosstab(const CrosstabPtr & crosstab, const ReportPtr & report, QList<QString> & cells)
174 : {
175 6 : QList<QString> uniqueRowValues;
176 3 : replaceRowGroupsInCrosstab(crosstab, report, uniqueRowValues);
177 :
178 6 : QList<QString> uniqueColValues;
179 3 : replaceColumnGroupsInCrosstab(crosstab, report, uniqueColValues);
180 :
181 6 : auto mapField = report->getFields();
182 :
183 6 : auto colGroup = crosstab->getColumnGroup();
184 6 : QString colName = colGroup->getHeader()->getCellContents()->getTextField()->getOriginalText();
185 3 : colName = colName.replace("$", "").replace("F", "").replace("{", "").replace("}", "");
186 :
187 6 : QString cellName = crosstab->getCrosstabCell()->getCellContents()->getTextField()->getOriginalText();
188 3 : cellName = cellName.replace("$", "").replace("F", "").replace("{", "").replace("}", "");
189 :
190 3 : int mapCount = 0;
191 15 : for (int i = 0; i < uniqueRowValues.size(); i++)
192 : {
193 24 : auto row = uniqueRowValues[i];
194 60 : for(int j = 0; j < uniqueColValues.size(); j++)
195 : {
196 96 : auto columnValue = mapField.value(colName)->getData(mapCount);
197 :
198 48 : if(columnValue != uniqueColValues[j])
199 12 : cells.append("");
200 : else
201 : {
202 36 : replaceCellInCrosstab(crosstab, report, mapCount);
203 72 : auto text = mapField.value(cellName)->getData(mapCount);
204 36 : cells.append(text);
205 36 : mapCount++;
206 : }
207 : }
208 : }
209 :
210 6 : return true;
211 : }
212 :
213 78 : bool Replacer::replaceRowGroupInCrosstab(const CrosstabPtr & crosstab, const ReportPtr & report, int i)
214 : {
215 156 : auto rowGroupText = crosstab->getRowGroup()->getHeader()->getCellContents()->getTextField()->getOriginalText();
216 156 : auto rowGroupReplacedText = replaceField(rowGroupText, report, i);
217 78 : crosstab->getRowGroup()->getHeader()->getCellContents()->getTextField()->setText(rowGroupReplacedText);
218 :
219 156 : return true;
220 : }
221 :
222 78 : bool Replacer::replaceColumnGroupInCrosstab(const CrosstabPtr & crosstab, const ReportPtr & report, int i)
223 : {
224 156 : auto colGroupText = crosstab->getColumnGroup()->getHeader()->getCellContents()->getTextField()->getOriginalText();
225 156 : auto colGroupReplacedText = replaceField(colGroupText, report, i);
226 78 : crosstab->getColumnGroup()->getHeader()->getCellContents()->getTextField()->setText(colGroupReplacedText);
227 :
228 156 : return true;
229 : }
230 :
231 36 : bool Replacer::replaceCellInCrosstab(const CrosstabPtr & crosstab, const ReportPtr & report, int i)
232 : {
233 72 : auto cellText = crosstab->getCrosstabCell()->getCellContents()->getTextField()->getOriginalText();
234 72 : auto cellReplacedText = replaceField(cellText, report, i);
235 36 : crosstab->getCrosstabCell()->getCellContents()->getTextField()->setText(cellReplacedText);
236 :
237 72 : return true;
238 : }
239 :
240 413 : bool Replacer::replaceFieldInTextWidget(const TextWidgetPtr & widget, const ReportPtr & report, int i)
241 : {
242 826 : auto text = widget->getOriginalText();
243 826 : auto replacedText = replaceField(text, report, i);
244 413 : widget->setText(replacedText);
245 :
246 826 : return true;
247 : }
248 :
249 60 : bool Replacer::replaceFieldInImageWidget(const ImagePtr & widget, const ReportPtr & report, int i)
250 : {
251 120 : auto text = widget->getOriginalText();
252 120 : auto image = replaceFieldImage(text, report, i);
253 60 : auto size = widget->getSize();
254 60 : widget->setImage(image.scaled(size));
255 :
256 120 : return true;
257 : }
258 :
259 209 : bool Replacer::replaceFieldInSection(const SectionPtr & section, const ReportPtr & report, int i)
260 : {
261 209 : if(section.isNull())
262 : {
263 0 : m_lastError = "Section is empty.";
264 0 : return false;
265 : }
266 :
267 502 : for(auto && band : section->getBands())
268 : {
269 706 : for(auto && textWidget : band->getTextWidgets())
270 : {
271 413 : if(!replaceFieldInTextWidget(textWidget, report, i))
272 : {
273 0 : return false;
274 : }
275 : }
276 :
277 353 : for(auto && imageWidget : band->getImages())
278 : {
279 60 : if(!replaceFieldInImageWidget(imageWidget, report, i))
280 : {
281 0 : return false;
282 : }
283 : }
284 : }
285 :
286 209 : return true;
287 : }
288 :
289 209 : bool Replacer::replace(const SectionPtr & section, const ReportPtr & report, int i)
290 : {
291 209 : if(!replaceFieldInSection(section, report, i))
292 : {
293 0 : return false;
294 : }
295 209 : if(!replaceParameters(report))
296 : {
297 0 : return false;
298 : }
299 :
300 209 : return true;
301 : }
302 :
303 0 : const QString Replacer::getLastError() const
304 : {
305 0 : return m_lastError;
306 : }
307 :
308 : }
309 : }
|