[composer] Move code for creating groups to QgsComposition

Previously this code was located in QgsComposerView. Moving it to
QgsComposition simplifies grouping items for plugins. Also start
a new unit test for QgsComposerItemGroup.
This commit is contained in:
Nyall Dawson 2014-10-19 11:45:15 +11:00
parent 71fbe9c9ce
commit 993f0dcd3d
6 changed files with 164 additions and 11 deletions

View File

@ -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<QgsComposerItem*> 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 */

View File

@ -1791,6 +1791,26 @@ void QgsComposition::unlockAllItems()
QgsProject::instance()->dirty( true );
}
QgsComposerItemGroup *QgsComposition::groupItems( QList<QgsComposerItem *> items )
{
if ( items.size() < 2 )
{
//not enough items for a group
return 0;
}
QgsComposerItemGroup* itemGroup = new QgsComposerItemGroup( this );
QList<QgsComposerItem*>::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();

View File

@ -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<QgsComposerItem*> 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 */

View File

@ -1777,20 +1777,16 @@ void QgsComposerView::groupItems()
return;
}
//group selected items
QList<QgsComposerItem*> 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<QgsComposerItem*>::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 );
}

View File

@ -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)

View File

@ -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 <QObject>
#include <QtTest>
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 = "<h1>Composer Grouped Item Tests</h1>\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<QgsComposerItem*> 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"