Line data Source code
1 : #include "replacer.hpp"
2 : #include <QRegularExpression>
3 : #include <QImage>
4 : #include <QByteArray>
5 : #include <QDebug>
6 :
7 : namespace qtreports
8 : {
9 : namespace detail
10 : {
11 :
12 72 : Replacer::Replacer() {}
13 :
14 72 : Replacer::~Replacer() {}
15 :
16 348 : QString Replacer::replaceParameters( const QString & text, const ReportPtr & report )
17 : {
18 348 : auto newText = text;
19 696 : QRegularExpression expr( "(\\$P\\{\\w+\\})", QRegularExpression::CaseInsensitiveOption );
20 696 : auto iterator = expr.globalMatch( newText );
21 556 : while( iterator.hasNext() )
22 : {
23 208 : auto match = iterator.next();
24 208 : auto name = match.captured( 1 ).remove( 0, 3 ).remove( -1, 1 );
25 208 : auto parameter = report->getParameter( name );
26 104 : newText.replace( match.captured( 1 ), parameter.toString() ); //need fix for classname
27 : }
28 :
29 696 : return newText;
30 : }
31 :
32 348 : bool Replacer::replaceParametersInTextWidget( const TextWidgetPtr & widget, const ReportPtr & report )
33 : {
34 696 : auto text = widget->getText();
35 696 : auto replacedText = replaceParameters( text, report );
36 348 : widget->setText( replacedText );
37 :
38 696 : return true;
39 : }
40 :
41 114 : bool Replacer::replaceParametersInSection( const SectionPtr & section, const ReportPtr & report )
42 : {
43 114 : if( section.isNull() )
44 : {
45 0 : m_lastError = "Section is empty.";
46 0 : return false;
47 : }
48 :
49 334 : for( auto && band : section->getBands() )
50 : {
51 568 : for( auto && textWidget : band->getTextWidgets() )
52 : {
53 348 : if( !replaceParametersInTextWidget( textWidget, report ) )
54 : {
55 0 : return false;
56 : }
57 : }
58 : }
59 :
60 114 : return true;
61 : }
62 :
63 66 : bool Replacer::replaceParameters( const ReportPtr & report )
64 : {
65 66 : if( report.isNull() )
66 : {
67 0 : m_lastError = "Report is empty.";
68 0 : return false;
69 : }
70 :
71 132 : auto detail = report->getDetail();
72 66 : if( detail.isNull() )
73 : {
74 0 : m_lastError = "Report->Detail is empty.";
75 0 : return false;
76 : }
77 :
78 66 : if( !replaceParametersInSection( detail, report ) )
79 : {
80 0 : return false;
81 : }
82 :
83 132 : auto title = report->getTitle();
84 66 : if( !title.isNull() && !replaceParametersInSection( title, report ) )
85 : {
86 0 : return false;
87 : }
88 :
89 66 : return true;
90 : }
91 :
92 244 : QString Replacer::replaceField( const QString & text, const ReportPtr & report, int i )
93 : {
94 244 : auto newText = text;
95 488 : QRegularExpression expr( "(\\$F\\{\\w+\\})", QRegularExpression::CaseInsensitiveOption );
96 488 : auto iterator = expr.globalMatch( newText );
97 508 : while( iterator.hasNext() )
98 : {
99 264 : auto match = iterator.next();
100 264 : auto name = match.captured( 1 ).remove( 0, 3 ).remove( -1, 1 );
101 264 : auto parameter = report->getField( name )->getData( i );
102 132 : newText.replace( match.captured( 1 ), parameter );
103 : }
104 :
105 488 : return newText;
106 : }
107 :
108 48 : QImage Replacer::replaceFieldImage( const QString & text, const ReportPtr & report, int i )
109 : {
110 96 : QRegularExpression expr( "(\\$F\\{\\w+\\})", QRegularExpression::CaseInsensitiveOption );
111 96 : auto iterator = expr.globalMatch( text );
112 48 : while( iterator.hasNext() )
113 : {
114 96 : auto match = iterator.next();
115 96 : auto name = match.captured( 1 ).remove( 0, 3 ).remove( -1, 1 );
116 96 : auto byteArray = report->getField( name )->getData< QByteArray >( i );
117 96 : auto image = QImage::fromData( byteArray );
118 48 : return image;
119 : }
120 :
121 0 : return QImage();
122 : }
123 :
124 244 : bool Replacer::replaceFieldInTextWidget( const TextWidgetPtr & widget, const ReportPtr & report, int i )
125 : {
126 488 : auto text = widget->getOriginalText();
127 488 : auto replacedText = replaceField( text, report, i );
128 244 : widget->setText( replacedText );
129 :
130 488 : return true;
131 : }
132 :
133 48 : bool Replacer::replaceFieldInImageWidget( const ImagePtr & widget, const ReportPtr & report, int i )
134 : {
135 96 : auto text = widget->getOriginalText();
136 96 : auto image = replaceFieldImage( text, report, i );
137 48 : auto size = widget->getSize();
138 48 : widget->setImage( image.scaled( size ) );
139 :
140 96 : return true;
141 : }
142 :
143 66 : bool Replacer::replaceFieldInSection( const SectionPtr & section, const ReportPtr & report, int i )
144 : {
145 66 : if( section.isNull() )
146 : {
147 0 : m_lastError = "Section is empty.";
148 0 : return false;
149 : }
150 :
151 230 : for( auto && band : section->getBands() )
152 : {
153 408 : for( auto && textWidget : band->getTextWidgets() )
154 : {
155 244 : if( !replaceFieldInTextWidget( textWidget, report, i ) )
156 : {
157 0 : return false;
158 : }
159 : }
160 :
161 212 : for( auto && imageWidget : band->getImages() )
162 : {
163 48 : if( !replaceFieldInImageWidget( imageWidget, report, i ) )
164 : {
165 0 : return false;
166 : }
167 : }
168 : }
169 :
170 66 : return true;
171 : }
172 :
173 66 : bool Replacer::replace( const SectionPtr & section, const ReportPtr & report, int i )
174 : {
175 66 : if( !replaceFieldInSection( section, report, i ) )
176 : {
177 0 : return false;
178 : }
179 66 : if( !replaceParameters( report ) )
180 : {
181 0 : return false;
182 : }
183 :
184 66 : return true;
185 : }
186 :
187 0 : const QString Replacer::getLastError() const
188 : {
189 0 : return m_lastError;
190 : }
191 :
192 : }
193 : }
|