Skip to content

Commit

Permalink
In progress support for retrieval of html composer and items by id fr…
Browse files Browse the repository at this point in the history
…om map composition. Also refactoring python composer tests.
  • Loading branch information
timlinux committed Sep 19, 2012
1 parent 014175e commit 11dead9
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 81 deletions.
6 changes: 5 additions & 1 deletion python/core/qgscomposermultiframe.sip
Expand Up @@ -42,6 +42,10 @@ public:
/**Removes and deletes all frames from mComposition*/
void deleteFrames();

int nFrames() const;
int nFrames() const /Deprecated/;
/** Return the number of frames associated with this multiframeset.
@note added in 2.0, replaces nFrames
**/
int frameCount() const;
QgsComposerFrame* frame( int i );
};
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposermultiframe.cpp
Expand Up @@ -238,7 +238,7 @@ void QgsComposerMultiFrame::deleteFrames()
mResizeMode = bkResizeMode;
}

QgsComposerFrame* QgsComposerMultiFrame::frame( int i )
QgsComposerFrame* QgsComposerMultiFrame::frame( int i ) const
{
if ( i >= mFrameItems.size() )
{
Expand Down
10 changes: 8 additions & 2 deletions src/core/composer/qgscomposermultiframe.h
Expand Up @@ -69,8 +69,14 @@ class CORE_EXPORT QgsComposerMultiFrame: public QObject
/**Removes and deletes all frames from mComposition*/
void deleteFrames();

int nFrames() const { return mFrameItems.size(); }
QgsComposerFrame* frame( int i );

/** Deprecated in 2.0 use frameCount rather. **/
Q_DECL_DEPRECATED int nFrames() const { return mFrameItems.size(); }
/** Return the number of frames associated with this multiframeset.
@note added in 2.0, replaces nFrames
**/
int frameCount() const { return mFrameItems.size(); }
QgsComposerFrame* frame( int i ) const;

protected:
QgsComposition* mComposition;
Expand Down
44 changes: 41 additions & 3 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -185,8 +185,6 @@ QList<const QgsComposerMap*> QgsComposition::composerMapItems() const

const QgsComposerMap* QgsComposition::getComposerMapById( int id ) const
{
QList<const QgsComposerMap*> resultList;

QList<QGraphicsItem *> itemList = items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
Expand All @@ -200,7 +198,47 @@ const QgsComposerMap* QgsComposition::getComposerMapById( int id ) const
}
}
}
return 0;
}

const QgsComposerHtml* QgsComposition::getComposerHtmlByItem( QgsComposerItem *item ) const
{
QList<QGraphicsItem *> itemList = items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
const QgsComposerHtml* composerHtml = dynamic_cast<const QgsComposerHtml *>( *itemIt );
if ( composerHtml )
{
//Now cycle through the items associated with this html composer
//and return the composer if the item matches any of them
for ( int i=0; i<composerHtml->frameCount(); i++ )
{
if ( composerHtml->frame(i)->id() == item->id() )
{
return composerHtml;
}
}
}
}
return 0;
}

const QgsComposerItem* QgsComposition::getComposerItemById( QString theId ) const
{
QList<QGraphicsItem *> itemList = items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
const QgsComposerItem* mypItem = dynamic_cast<const QgsComposerItem *>( *itemIt );
if ( mypItem )
{
if ( mypItem->id() == theId )
{
return mypItem;
}
}
}
return 0;
}

Expand Down Expand Up @@ -1282,7 +1320,7 @@ void QgsComposition::removeComposerItem( QgsComposerItem* item, bool createComma
//check if there are frames left. If not, remove the multi frame
if ( frameItem && multiFrame )
{
if ( multiFrame->nFrames() < 1 )
if ( multiFrame->frameCount() < 1 )
{
removeMultiFrame( multiFrame );
if ( createCommand )
Expand Down
18 changes: 17 additions & 1 deletion src/core/composer/qgscomposition.h
Expand Up @@ -124,9 +124,25 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
template<class T> void composerItems( QList<T*>& itemList );

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

/*Returns the composer html with specified id (a string as named in the
composer user interface item properties).
@note Added in QGIS 2.0
@param id - A QString representing the id of the item.
@return QgsComposerHtml pointer or 0 pointer if no such item exists.
*/
const QgsComposerHtml* getComposerHtmlByItem( QgsComposerItem *item ) const;

/**Returns a composer item given its text identifier.
@note added in 2.0
@param theId - A QString representing the identifier of the item to
retrieve.
@return QgsComposerItem pointer or 0 pointer if no such item exists.
**/
const QgsComposerItem* getComposerItemById( QString theId ) const;

int printResolution() const {return mPrintResolution;}
void setPrintResolution( int dpi ) {mPrintResolution = dpi;}

Expand Down
54 changes: 36 additions & 18 deletions tests/src/python/qgscompositionchecker.py
Expand Up @@ -14,55 +14,74 @@
* *
***************************************************************************/
'''
from PyQt4.QtCore import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *

class QgsCompositionChecker:

def testComposition(self, mTestName, mComposition, mExpectedImageFile, page=0 ):
if ( mComposition == None):
return false
myMessage = "Composition not valid"
return False, myMessage


#load expected image
expectedImage = QImage( mExpectedImageFile )

#get width/height, create image and render the composition to it
width = expectedImage.width();
height = expectedImage.height();
outputImage = QImage( QSize( width, height ), QImage.Format_ARGB32 )

mComposition.setPlotStyle( QgsComposition.Print )
outputImage.setDotsPerMeterX( expectedImage.dotsPerMeterX() )
outputImage.setDotsPerMeterY( expectedImage.dotsPerMeterX() )
outputImage.fill( 0 )
p = QPainter( outputImage )
mComposition.renderPage( p, page )
p.end()

renderedFilePath = QDir.tempPath() + QDir.separator() + QFileInfo( mExpectedImageFile ).baseName() + "_rendered_python.png"
outputImage.save( renderedFilePath, "PNG" )

diffFilePath = QDir.tempPath() + QDir.separator() + QFileInfo( mExpectedImageFile ).baseName() + "_diff_python.png"
testResult = self.compareImages( expectedImage, outputImage, diffFilePath )

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>"

myDashMessage = (('<DartMeasurementFile name="Rendered Image '
'%s" type="image/png">'
'%s</DartMeasurementFile>'
'<DartMeasurementFile name="Expected Image '
'%s" type="image/png">'
'%s</DartMeasurementFile>\n'
'<DartMeasurementFile name="Difference Image '
'%s" type="image/png">'
'%s</DartMeasurementFile>') %
(mTestName, renderedFilePath, mTestName,
mExpectedImageFile, mTestName, diffFilePath )
)
qDebug( myDashMessage )
return testResult
if not testResult:
myMessage = ('Expected: %s\nGot: %s\nDifference: %s\n' %
(mExpectedImageFile, renderedFilePath, diffFilePath))
else:
myMessage = 'Control and test images matched.'
return testResult, myMessage

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

if ( imgExpected.width() != imgRendered.width() or imgExpected.height() != imgRendered.height() ):
return false

if ( imgExpected.width() != imgRendered.width()
or imgExpected.height() != imgRendered.height() ):
return False

imageWidth = imgExpected.width()
imageHeight = imgExpected.height()
mismatchCount = 0

differenceImage = QImage( imageWidth, imageHeight, QImage.Format_ARGB32_Premultiplied )

differenceImage = QImage(
imageWidth, imageHeight, QImage.Format_ARGB32_Premultiplied )
differenceImage.fill( qRgb( 152, 219, 249 ) )

pixel1 = QColor().rgb()
pixel2 = QColor().rgb()
for i in range( imageHeight ):
Expand All @@ -72,13 +91,12 @@ def compareImages( self, imgExpected, imgRendered, differenceImagePath ):
if ( pixel1 != pixel2 ):
mismatchCount = mismatchCount + 1
differenceImage.setPixel( j, i, qRgb( 255, 0, 0 ) )

if not differenceImagePath.isEmpty():
differenceImage.save( differenceImagePath, "PNG" )

#allow pixel deviation of 1 percent
pixelCount = imageWidth * imageHeight;
# print "MismatchCount: "+str(mismatchCount)
# print "PixelCount: "+str(pixelCount)
return (float(mismatchCount) / float(pixelCount) ) < 0.01

0 comments on commit 11dead9

Please sign in to comment.