Skip to content

Commit 11dead9

Browse files
committedSep 19, 2012
In progress support for retrieval of html composer and items by id from map composition. Also refactoring python composer tests.
1 parent 014175e commit 11dead9

File tree

7 files changed

+206
-81
lines changed

7 files changed

+206
-81
lines changed
 

‎python/core/qgscomposermultiframe.sip

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public:
4242
/**Removes and deletes all frames from mComposition*/
4343
void deleteFrames();
4444

45-
int nFrames() const;
45+
int nFrames() const /Deprecated/;
46+
/** Return the number of frames associated with this multiframeset.
47+
@note added in 2.0, replaces nFrames
48+
**/
49+
int frameCount() const;
4650
QgsComposerFrame* frame( int i );
4751
};

‎src/core/composer/qgscomposermultiframe.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ void QgsComposerMultiFrame::deleteFrames()
238238
mResizeMode = bkResizeMode;
239239
}
240240

241-
QgsComposerFrame* QgsComposerMultiFrame::frame( int i )
241+
QgsComposerFrame* QgsComposerMultiFrame::frame( int i ) const
242242
{
243243
if ( i >= mFrameItems.size() )
244244
{

‎src/core/composer/qgscomposermultiframe.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ class CORE_EXPORT QgsComposerMultiFrame: public QObject
6969
/**Removes and deletes all frames from mComposition*/
7070
void deleteFrames();
7171

72-
int nFrames() const { return mFrameItems.size(); }
73-
QgsComposerFrame* frame( int i );
72+
73+
/** Deprecated in 2.0 use frameCount rather. **/
74+
Q_DECL_DEPRECATED int nFrames() const { return mFrameItems.size(); }
75+
/** Return the number of frames associated with this multiframeset.
76+
@note added in 2.0, replaces nFrames
77+
**/
78+
int frameCount() const { return mFrameItems.size(); }
79+
QgsComposerFrame* frame( int i ) const;
7480

7581
protected:
7682
QgsComposition* mComposition;

‎src/core/composer/qgscomposition.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ QList<const QgsComposerMap*> QgsComposition::composerMapItems() const
185185

186186
const QgsComposerMap* QgsComposition::getComposerMapById( int id ) const
187187
{
188-
QList<const QgsComposerMap*> resultList;
189-
190188
QList<QGraphicsItem *> itemList = items();
191189
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
192190
for ( ; itemIt != itemList.end(); ++itemIt )
@@ -200,7 +198,47 @@ const QgsComposerMap* QgsComposition::getComposerMapById( int id ) const
200198
}
201199
}
202200
}
201+
return 0;
202+
}
203203

204+
const QgsComposerHtml* QgsComposition::getComposerHtmlByItem( QgsComposerItem *item ) const
205+
{
206+
QList<QGraphicsItem *> itemList = items();
207+
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
208+
for ( ; itemIt != itemList.end(); ++itemIt )
209+
{
210+
const QgsComposerHtml* composerHtml = dynamic_cast<const QgsComposerHtml *>( *itemIt );
211+
if ( composerHtml )
212+
{
213+
//Now cycle through the items associated with this html composer
214+
//and return the composer if the item matches any of them
215+
for ( int i=0; i<composerHtml->frameCount(); i++ )
216+
{
217+
if ( composerHtml->frame(i)->id() == item->id() )
218+
{
219+
return composerHtml;
220+
}
221+
}
222+
}
223+
}
224+
return 0;
225+
}
226+
227+
const QgsComposerItem* QgsComposition::getComposerItemById( QString theId ) const
228+
{
229+
QList<QGraphicsItem *> itemList = items();
230+
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
231+
for ( ; itemIt != itemList.end(); ++itemIt )
232+
{
233+
const QgsComposerItem* mypItem = dynamic_cast<const QgsComposerItem *>( *itemIt );
234+
if ( mypItem )
235+
{
236+
if ( mypItem->id() == theId )
237+
{
238+
return mypItem;
239+
}
240+
}
241+
}
204242
return 0;
205243
}
206244

@@ -1282,7 +1320,7 @@ void QgsComposition::removeComposerItem( QgsComposerItem* item, bool createComma
12821320
//check if there are frames left. If not, remove the multi frame
12831321
if ( frameItem && multiFrame )
12841322
{
1285-
if ( multiFrame->nFrames() < 1 )
1323+
if ( multiFrame->frameCount() < 1 )
12861324
{
12871325
removeMultiFrame( multiFrame );
12881326
if ( createCommand )

‎src/core/composer/qgscomposition.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,25 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
124124
template<class T> void composerItems( QList<T*>& itemList );
125125

126126
/**Returns the composer map with specified id
127-
@return id or 0 pointer if the composer map item does not exist*/
127+
@return QgsComposerMap or 0 pointer if the composer map item does not exist*/
128128
const QgsComposerMap* getComposerMapById( int id ) const;
129129

130+
/*Returns the composer html with specified id (a string as named in the
131+
composer user interface item properties).
132+
@note Added in QGIS 2.0
133+
@param id - A QString representing the id of the item.
134+
@return QgsComposerHtml pointer or 0 pointer if no such item exists.
135+
*/
136+
const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;
137+
138+
/**Returns a composer item given its text identifier.
139+
@note added in 2.0
140+
@param theId - A QString representing the identifier of the item to
141+
retrieve.
142+
@return QgsComposerItem pointer or 0 pointer if no such item exists.
143+
**/
144+
const QgsComposerItem* getComposerItemById( QString theId ) const;
145+
130146
int printResolution() const {return mPrintResolution;}
131147
void setPrintResolution( int dpi ) {mPrintResolution = dpi;}
132148

‎tests/src/python/qgscompositionchecker.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,55 +14,74 @@
1414
* *
1515
***************************************************************************/
1616
'''
17-
from PyQt4.QtCore import *
17+
from PyQt4.QtCore import *
1818
from PyQt4.QtGui import *
1919
from qgis.core import *
2020

2121
class QgsCompositionChecker:
2222

2323
def testComposition(self, mTestName, mComposition, mExpectedImageFile, page=0 ):
2424
if ( mComposition == None):
25-
return false
25+
myMessage = "Composition not valid"
26+
return False, myMessage
2627

2728

2829
#load expected image
2930
expectedImage = QImage( mExpectedImageFile )
30-
31+
3132
#get width/height, create image and render the composition to it
3233
width = expectedImage.width();
3334
height = expectedImage.height();
3435
outputImage = QImage( QSize( width, height ), QImage.Format_ARGB32 )
35-
36+
3637
mComposition.setPlotStyle( QgsComposition.Print )
3738
outputImage.setDotsPerMeterX( expectedImage.dotsPerMeterX() )
3839
outputImage.setDotsPerMeterY( expectedImage.dotsPerMeterX() )
3940
outputImage.fill( 0 )
4041
p = QPainter( outputImage )
4142
mComposition.renderPage( p, page )
4243
p.end()
43-
44+
4445
renderedFilePath = QDir.tempPath() + QDir.separator() + QFileInfo( mExpectedImageFile ).baseName() + "_rendered_python.png"
4546
outputImage.save( renderedFilePath, "PNG" )
46-
47+
4748
diffFilePath = QDir.tempPath() + QDir.separator() + QFileInfo( mExpectedImageFile ).baseName() + "_diff_python.png"
4849
testResult = self.compareImages( expectedImage, outputImage, diffFilePath )
49-
50-
myDashMessage = "<DartMeasurementFile name=\"Rendered Image " + mTestName + "\"" + " type=\"image/png\">" + renderedFilePath + "</DartMeasurementFile>" + "\n" + "<DartMeasurementFile name=\"Expected Image " + mTestName + "\"" + " type=\"image/png\">" + mExpectedImageFile + "</DartMeasurementFile>" + "\n" + "<DartMeasurementFile name=\"Difference Image " + mTestName + "\"" + " type=\"image/png\">" + diffFilePath + "</DartMeasurementFile>"
50+
51+
myDashMessage = (('<DartMeasurementFile name="Rendered Image '
52+
'%s" type="image/png">'
53+
'%s</DartMeasurementFile>'
54+
'<DartMeasurementFile name="Expected Image '
55+
'%s" type="image/png">'
56+
'%s</DartMeasurementFile>\n'
57+
'<DartMeasurementFile name="Difference Image '
58+
'%s" type="image/png">'
59+
'%s</DartMeasurementFile>') %
60+
(mTestName, renderedFilePath, mTestName,
61+
mExpectedImageFile, mTestName, diffFilePath )
62+
)
5163
qDebug( myDashMessage )
52-
return testResult
64+
if not testResult:
65+
myMessage = ('Expected: %s\nGot: %s\nDifference: %s\n' %
66+
(mExpectedImageFile, renderedFilePath, diffFilePath))
67+
else:
68+
myMessage = 'Control and test images matched.'
69+
return testResult, myMessage
5370

5471
def compareImages( self, imgExpected, imgRendered, differenceImagePath ):
5572

56-
if ( imgExpected.width() != imgRendered.width() or imgExpected.height() != imgRendered.height() ):
57-
return false
58-
73+
if ( imgExpected.width() != imgRendered.width()
74+
or imgExpected.height() != imgRendered.height() ):
75+
return False
76+
5977
imageWidth = imgExpected.width()
6078
imageHeight = imgExpected.height()
6179
mismatchCount = 0
62-
63-
differenceImage = QImage( imageWidth, imageHeight, QImage.Format_ARGB32_Premultiplied )
80+
81+
differenceImage = QImage(
82+
imageWidth, imageHeight, QImage.Format_ARGB32_Premultiplied )
6483
differenceImage.fill( qRgb( 152, 219, 249 ) )
65-
84+
6685
pixel1 = QColor().rgb()
6786
pixel2 = QColor().rgb()
6887
for i in range( imageHeight ):
@@ -72,13 +91,12 @@ def compareImages( self, imgExpected, imgRendered, differenceImagePath ):
7291
if ( pixel1 != pixel2 ):
7392
mismatchCount = mismatchCount + 1
7493
differenceImage.setPixel( j, i, qRgb( 255, 0, 0 ) )
75-
94+
7695
if not differenceImagePath.isEmpty():
7796
differenceImage.save( differenceImagePath, "PNG" )
78-
97+
7998
#allow pixel deviation of 1 percent
8099
pixelCount = imageWidth * imageHeight;
81100
# print "MismatchCount: "+str(mismatchCount)
82101
# print "PixelCount: "+str(pixelCount)
83102
return (float(mismatchCount) / float(pixelCount) ) < 0.01
84-

0 commit comments

Comments
 (0)