@@ -118,6 +118,8 @@ class TestQgsLegendRenderer : public QObject
118118 void testThreeColumns ();
119119 void testFilterByMap ();
120120 void testFilterByMapSameSymbol ();
121+ void testColumns_data ();
122+ void testColumns ();
121123 void testRasterBorder ();
122124 void testFilterByPolygon ();
123125 void testFilterByExpression ();
@@ -131,6 +133,7 @@ class TestQgsLegendRenderer : public QObject
131133 QgsVectorLayer* mVL3 ; // point
132134 QgsRasterLayer* mRL ;
133135 QString mReport ;
136+ bool _testLegendColumns ( int itemCount, int columnCount, const QString& testName );
134137};
135138
136139
@@ -459,6 +462,65 @@ void TestQgsLegendRenderer::testFilterByMapSameSymbol()
459462 QgsMapLayerRegistry::instance ()->removeMapLayer ( vl4 );
460463}
461464
465+ bool TestQgsLegendRenderer::_testLegendColumns ( int itemCount, int columnCount, const QString& testName )
466+ {
467+ QgsFillSymbol* sym = new QgsFillSymbol ();
468+ sym->setColor ( Qt::cyan );
469+
470+ QgsLayerTreeGroup* root = new QgsLayerTreeGroup ();
471+
472+ QList< QgsVectorLayer* > layers;
473+ for ( int i = 1 ; i <= itemCount; ++i )
474+ {
475+ QgsVectorLayer* vl = new QgsVectorLayer ( " Polygon" , QString ( " Layer %1" ).arg ( i ), " memory" );
476+ QgsMapLayerRegistry::instance ()->addMapLayer ( vl );
477+ vl->setRenderer ( new QgsSingleSymbolRenderer ( sym->clone () ) );
478+ root->addLayer ( vl );
479+ layers << vl;
480+ }
481+ delete sym;
482+
483+ QgsLayerTreeModel legendModel ( root );
484+ QgsLegendSettings settings;
485+ settings.setColumnCount ( columnCount );
486+ _setStandardTestFont ( settings, " Bold" );
487+ _renderLegend ( testName, &legendModel, settings );
488+ bool result = _verifyImage ( testName, mReport );
489+
490+ Q_FOREACH ( QgsVectorLayer* l, layers )
491+ {
492+ QgsMapLayerRegistry::instance ()->removeMapLayer ( l );
493+ }
494+ return result;
495+ }
496+
497+ void TestQgsLegendRenderer::testColumns_data ()
498+ {
499+ QTest::addColumn<QString>( " testName" );
500+ QTest::addColumn<int >( " items" );
501+ QTest::addColumn<int >( " columns" );
502+
503+ QTest::newRow ( " 2 items, 2 columns" ) << " legend_2_by_2" << 2 << 2 ;
504+ QTest::newRow ( " 3 items, 2 columns" ) << " legend_3_by_2" << 3 << 2 ;
505+ QTest::newRow ( " 4 items, 2 columns" ) << " legend_4_by_2" << 4 << 2 ;
506+ QTest::newRow ( " 5 items, 2 columns" ) << " legend_5_by_2" << 5 << 2 ;
507+ QTest::newRow ( " 3 items, 3 columns" ) << " legend_3_by_3" << 3 << 3 ;
508+ QTest::newRow ( " 4 items, 3 columns" ) << " legend_4_by_3" << 4 << 3 ;
509+ QTest::newRow ( " 5 items, 3 columns" ) << " legend_5_by_3" << 5 << 3 ;
510+ QTest::newRow ( " 6 items, 3 columns" ) << " legend_6_by_3" << 6 << 3 ;
511+ QTest::newRow ( " 7 items, 3 columns" ) << " legend_7_by_3" << 7 << 3 ;
512+ }
513+
514+ void TestQgsLegendRenderer::testColumns ()
515+ {
516+ // test rendering legend with different combinations of columns and items
517+
518+ QFETCH ( QString, testName );
519+ QFETCH ( int , items );
520+ QFETCH ( int , columns );
521+ QVERIFY ( _testLegendColumns ( items, columns, testName ) );
522+ }
523+
462524void TestQgsLegendRenderer::testRasterBorder ()
463525{
464526 QString testName = " legend_raster_border" ;
0 commit comments