@@ -37,6 +37,11 @@ class TestQgsLayoutItem: public QObject
37
37
void cleanup ();// will be called after every testfunction.
38
38
void creation (); // test creation of QgsLayoutItem
39
39
void registry ();
40
+ void shouldDrawDebug ();
41
+ void shouldDrawAntialiased ();
42
+ void preparePainter ();
43
+ void debugRect ();
44
+ void draw ();
40
45
41
46
private:
42
47
@@ -160,13 +165,98 @@ void TestQgsLayoutItem::registry()
160
165
QVERIFY ( !reg2.populate () );
161
166
}
162
167
168
+ void TestQgsLayoutItem::shouldDrawDebug ()
169
+ {
170
+ QgsProject p;
171
+ QgsLayout l ( &p );
172
+ TestItem *item = new TestItem ( &l );
173
+ l.context ().setFlag ( QgsLayoutContext::FlagDebug, true );
174
+ QVERIFY ( item->shouldDrawDebugRect () );
175
+ l.context ().setFlag ( QgsLayoutContext::FlagDebug, false );
176
+ QVERIFY ( !item->shouldDrawDebugRect () );
177
+ delete item;
178
+ }
179
+
180
+ void TestQgsLayoutItem::shouldDrawAntialiased ()
181
+ {
182
+ QgsProject p;
183
+ QgsLayout l ( &p );
184
+ TestItem *item = new TestItem ( &l );
185
+ l.context ().setFlag ( QgsLayoutContext::FlagAntialiasing, false );
186
+ QVERIFY ( !item->shouldDrawAntialiased () );
187
+ l.context ().setFlag ( QgsLayoutContext::FlagAntialiasing, true );
188
+ QVERIFY ( item->shouldDrawAntialiased () );
189
+ delete item;
190
+ }
191
+
192
+ void TestQgsLayoutItem::preparePainter ()
193
+ {
194
+ QgsProject p;
195
+ QgsLayout l ( &p );
196
+ TestItem *item = new TestItem ( &l );
197
+ // test with no painter
198
+ item->preparePainter ( nullptr );
199
+
200
+ // test antialiasing correctly set for painter
201
+ QImage image ( QSize ( 100 , 100 ), QImage::Format_ARGB32 );
202
+ QPainter painter;
203
+ painter.begin ( &image );
204
+ l.context ().setFlag ( QgsLayoutContext::FlagAntialiasing, false );
205
+ item->preparePainter ( &painter );
206
+ QVERIFY ( !( painter.renderHints () & QPainter::Antialiasing ) );
207
+ l.context ().setFlag ( QgsLayoutContext::FlagAntialiasing, true );
208
+ item->preparePainter ( &painter );
209
+ QVERIFY ( painter.renderHints () & QPainter::Antialiasing );
210
+ delete item;
211
+ }
212
+
213
+ void TestQgsLayoutItem::debugRect ()
214
+ {
215
+ QgsProject p;
216
+ QgsLayout l ( &p );
217
+ TestItem *item = new TestItem ( &l );
218
+ l.addItem ( item );
219
+ item->setPos ( 100 , 100 );
220
+ item->setRect ( 0 , 0 , 200 , 200 );
221
+ l.setSceneRect ( 0 , 0 , 400 , 400 );
222
+ l.context ().setFlag ( QgsLayoutContext::FlagDebug, true );
223
+ QImage image ( l.sceneRect ().size ().toSize (), QImage::Format_ARGB32 );
224
+ image.fill ( 0 );
225
+ QPainter painter ( &image );
226
+ l.render ( &painter );
227
+ painter.end ();
228
+
229
+ bool result = renderCheck ( " layoutitem_debugrect" , image, 0 );
230
+ QVERIFY ( result );
231
+ }
232
+
233
+ void TestQgsLayoutItem::draw ()
234
+ {
235
+ QgsProject p;
236
+ QgsLayout l ( &p );
237
+ TestItem *item = new TestItem ( &l );
238
+ l.addItem ( item );
239
+ item->setPos ( 100 , 100 );
240
+ item->setRect ( 0 , 0 , 200 , 200 );
241
+ l.setSceneRect ( 0 , 0 , 400 , 400 );
242
+ l.context ().setFlag ( QgsLayoutContext::FlagAntialiasing, false ); // disable antialiasing to limit cross platform differences
243
+ QImage image ( l.sceneRect ().size ().toSize (), QImage::Format_ARGB32 );
244
+ image.fill ( 0 );
245
+ QPainter painter ( &image );
246
+ l.render ( &painter );
247
+ painter.end ();
248
+ bool result = renderCheck ( " layoutitem_draw" , image, 0 );
249
+ QVERIFY ( result );
250
+ }
251
+
163
252
bool TestQgsLayoutItem::renderCheck ( QString testName, QImage &image, int mismatchCount )
164
253
{
165
254
mReport += " <h2>" + testName + " </h2>\n " ;
166
255
QString myTmpDir = QDir::tempPath () + QDir::separator ();
167
256
QString myFileName = myTmpDir + testName + " .png" ;
168
257
image.save ( myFileName, " PNG" );
169
258
QgsRenderChecker myChecker;
259
+ myChecker.setControlPathPrefix ( " layouts" );
170
260
myChecker.setControlName ( " expected_" + testName );
171
261
myChecker.setRenderedImage ( myFileName );
172
262
bool myResultFlag = myChecker.compareImages ( testName, mismatchCount );
0 commit comments