diff --git a/python/core/composer/qgscomposition.sip b/python/core/composer/qgscomposition.sip index 11b79a6a741..44b05da64dd 100644 --- a/python/core/composer/qgscomposition.sip +++ b/python/core/composer/qgscomposition.sip @@ -407,6 +407,13 @@ class QgsComposition : QGraphicsScene void lockSelectedItems(); /**Unlock all items*/ void unlockAllItems(); + + /**Creates a new group from a list of composer items and adds it to the composition. + * @param items items to include in group + * @returns QgsComposerItemGroup of grouped items, if grouping was possible + * @note added in QGIS 2.6 + */ + QgsComposerItemGroup* groupItems( QList items ); /**Sorts the zList. The only time where this function needs to be called is from QgsComposer * after reading all the items from xml file @@ -521,7 +528,10 @@ class QgsComposition : QGraphicsScene QImage printPageAsRaster( int page ); /**Render a page to a paint device - @note added in version 1.9*/ + * @param p destination painter + * @param page page number, 0 based such that the first page is page 0 + * @note added in version 1.9 + */ void renderPage( QPainter* p, int page ); /** Compute world file parameters */ diff --git a/src/core/composer/qgscomposition.cpp b/src/core/composer/qgscomposition.cpp index d22fe7d8208..008166910f3 100644 --- a/src/core/composer/qgscomposition.cpp +++ b/src/core/composer/qgscomposition.cpp @@ -1791,6 +1791,26 @@ void QgsComposition::unlockAllItems() QgsProject::instance()->dirty( true ); } +QgsComposerItemGroup *QgsComposition::groupItems( QList items ) +{ + if ( items.size() < 2 ) + { + //not enough items for a group + return 0; + } + + QgsComposerItemGroup* itemGroup = new QgsComposerItemGroup( this ); + + QList::iterator itemIter = items.begin(); + for ( ; itemIter != items.end(); ++itemIter ) + { + itemGroup->addItem( *itemIter ); + } + + addItem( itemGroup ); + return itemGroup; +} + void QgsComposition::updateZValues( const bool addUndoCommands ) { int counter = mItemsModel->zOrderListSize(); diff --git a/src/core/composer/qgscomposition.h b/src/core/composer/qgscomposition.h index a58f23b1479..35a9435f182 100644 --- a/src/core/composer/qgscomposition.h +++ b/src/core/composer/qgscomposition.h @@ -47,6 +47,7 @@ class QgsComposerMouseHandles; class QgsComposerHtml; class QgsComposerTableV2; class QgsComposerItem; +class QgsComposerItemGroup; class QgsComposerLabel; class QgsComposerLegend; class QgsComposerMap; @@ -470,6 +471,13 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene /**Unlock all items*/ void unlockAllItems(); + /**Creates a new group from a list of composer items and adds it to the composition. + * @param items items to include in group + * @returns QgsComposerItemGroup of grouped items, if grouping was possible + * @note adde in QGIS 2.6 + */ + QgsComposerItemGroup* groupItems( QList items ); + /**Sorts the zList. The only time where this function needs to be called is from QgsComposer * after reading all the items from xml file * @deprecated use refreshZList instead @@ -583,7 +591,9 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene QImage printPageAsRaster( int page ); /**Render a page to a paint device - @note added in version 1.9*/ + * @param p destination painter + * @param page page number, 0 based such that the first page is page 0 + * @note added in version 1.9*/ void renderPage( QPainter* p, int page ); /** Compute world file parameters */ diff --git a/src/gui/qgscomposerview.cpp b/src/gui/qgscomposerview.cpp index 788c0c3f823..1e62b553f7a 100644 --- a/src/gui/qgscomposerview.cpp +++ b/src/gui/qgscomposerview.cpp @@ -1777,20 +1777,16 @@ void QgsComposerView::groupItems() return; } + //group selected items QList selectionList = composition()->selectedComposerItems(); - if ( selectionList.size() < 2 ) - { - return; //not enough items for a group - } - QgsComposerItemGroup* itemGroup = new QgsComposerItemGroup( composition() ); + QgsComposerItemGroup* itemGroup = composition()->groupItems( selectionList ); - QList::iterator itemIter = selectionList.begin(); - for ( ; itemIter != selectionList.end(); ++itemIter ) + if ( !itemGroup ) { - itemGroup->addItem( *itemIter ); + //group could not be created + return; } - composition()->addItem( itemGroup ); itemGroup->setSelected( true ); emit selectedItemChanged( itemGroup ); } diff --git a/tests/src/core/CMakeLists.txt b/tests/src/core/CMakeLists.txt index 3c37976cf5a..1735d456534 100644 --- a/tests/src/core/CMakeLists.txt +++ b/tests/src/core/CMakeLists.txt @@ -111,6 +111,7 @@ ADD_QGIS_TEST(composerutils testqgscomposerutils.cpp) ADD_QGIS_TEST(compositiontest testqgscomposition.cpp) ADD_QGIS_TEST(composermodel testqgscomposermodel.cpp) ADD_QGIS_TEST(composermultiframetest testqgscomposermultiframe.cpp) +ADD_QGIS_TEST(composergrouptest testqgscomposergroup.cpp) ADD_QGIS_TEST(composerpapertest testqgscomposerpaper.cpp) ADD_QGIS_TEST(composermaptest testqgscomposermap.cpp) ADD_QGIS_TEST(composermapgridtest testqgscomposermapgrid.cpp) diff --git a/tests/src/core/testqgscomposergroup.cpp b/tests/src/core/testqgscomposergroup.cpp new file mode 100644 index 00000000000..eae184a6241 --- /dev/null +++ b/tests/src/core/testqgscomposergroup.cpp @@ -0,0 +1,116 @@ +/*************************************************************************** + testqgscomposergroup.cpp + ----------------------- + begin : October 2014 + copyright : (C) 2014 by Nyall Dawson + email : nyall dot dawson at gmail dot com + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "qgscomposeritemgroup.h" +#include "qgscomposerlabel.h" +#include "qgscomposition.h" +#include "qgscompositionchecker.h" +#include +#include + +class TestQgsComposerGroup: public QObject +{ + Q_OBJECT; + private slots: + void initTestCase();// will be called before the first testfunction is executed. + void cleanupTestCase();// will be called after the last testfunction was executed. + void init();// will be called before each testfunction is executed. + void cleanup();// will be called after every testfunction. + void createGroup(); //test grouping items + void ungroup(); //test ungrouping items + void deleteGroup(); //test deleting group works + void undoRedo(); //test that group/ungroup undo/redo commands don't crash + + private: + QgsComposition* mComposition; + QgsMapSettings mMapSettings; + QgsComposerLabel* mItem1; + QgsComposerLabel* mItem2; + QString mReport; +}; + +void TestQgsComposerGroup::initTestCase() +{ + mComposition = new QgsComposition( mMapSettings ); + mComposition->setPaperSize( 297, 210 ); //A4 landscape + + //create some items + mItem1 = new QgsComposerLabel( mComposition ); + mComposition->addItem( mItem1 ); + mItem2 = new QgsComposerLabel( mComposition ); + mComposition->addItem( mItem2 ); + + mReport = "

Composer Grouped Item Tests

\n"; +} + +void TestQgsComposerGroup::cleanupTestCase() +{ + delete mComposition; + + QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html"; + QFile myFile( myReportFile ); + if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) + { + QTextStream myQTextStream( &myFile ); + myQTextStream << mReport; + myFile.close(); + } +} + +void TestQgsComposerGroup::init() +{ + +} + +void TestQgsComposerGroup::cleanup() +{ + +} + +void TestQgsComposerGroup::createGroup() +{ + //group items + QList items; + items << mItem1 << mItem2; + QgsComposerItemGroup* group = mComposition->groupItems( items ); + + //check result + QVERIFY( group ); + QCOMPARE( group->items().size(), 2 ); + QVERIFY( group->items().contains( mItem1 ) ); + QVERIFY( group->items().contains( mItem2 ) ); + QVERIFY( mItem1->isGroupMember() ); + QVERIFY( mItem2->isGroupMember() ); +} + +void TestQgsComposerGroup::ungroup() +{ + +} + +void TestQgsComposerGroup::deleteGroup() +{ + +} + +void TestQgsComposerGroup::undoRedo() +{ + +} + +QTEST_MAIN( TestQgsComposerGroup ) +#include "moc_testqgscomposergroup.cxx"