Skip to content

Commit

Permalink
Add a label id to distinguish different composer labels. Allow label …
Browse files Browse the repository at this point in the history
…string replacement in GetPrint request of qgis server and return more verbose error messages

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15041 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jan 14, 2011
1 parent ee85b86 commit f1ca934
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 10 deletions.
3 changes: 3 additions & 0 deletions python/core/qgscomposerlabel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ class QgsComposerLabel: QgsComposerItem
* @param node is Dom node corresponding to 'ComposerLabel' tag
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

/**Get label identification number*/
int id() const;
};
31 changes: 31 additions & 0 deletions src/core/composer/qgscomposerlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerIt
{
//default font size is 10 point
mFont.setPointSizeF( 10 );

//get new id
mId = maximumLabelId( composition ) + 1;
}

QgsComposerLabel::~QgsComposerLabel()
Expand Down Expand Up @@ -127,6 +130,7 @@ bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const

composerLabelElem.setAttribute( "halign", mHAlignment );
composerLabelElem.setAttribute( "valign", mVAlignment );
composerLabelElem.setAttribute( "id", mId );


//font
Expand Down Expand Up @@ -168,6 +172,9 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
//Vertical alignment
mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "valign" ).toInt() );

//id
mId = itemElem.attribute( "id", "0" ).toInt();

//font
QDomNodeList labelFontList = itemElem.elementsByTagName( "LabelFont" );
if ( labelFontList.size() > 0 )
Expand Down Expand Up @@ -201,3 +208,27 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
emit itemChanged();
return true;
}

int QgsComposerLabel::maximumLabelId( const QgsComposition* c ) const
{
int id = -1;
if ( !c )
{
return id;
}

QList<QGraphicsItem *> itemList = c->items();
QList<QGraphicsItem *>::const_iterator itemIt = itemList.constBegin();
for ( ; itemIt != itemList.constEnd(); ++itemIt )
{
const QgsComposerLabel* label = dynamic_cast<const QgsComposerLabel *>( *itemIt );
if ( label )
{
if ( label->id() > id )
{
id = label->id();
}
}
}
return id;
}
9 changes: 9 additions & 0 deletions src/core/composer/qgscomposerlabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

/**Get label identification number*/
int id() const { return mId; }

private:
// Text
QString mText;
Expand All @@ -91,8 +94,14 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
// Vertical Alignment
Qt::AlignmentFlag mVAlignment;

// Label id (unique within the same composition)
int mId;

/**Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
void replaceDateText( QString& text ) const;

/**Returns maximum id of all label items or -1 if no item is in the scene. Used to generate new ids in the constructor*/
int maximumLabelId( const QgsComposition* c ) const;
};

#endif
Expand Down
21 changes: 20 additions & 1 deletion src/mapserver/qgsconfigparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "qgsconfigparser.h"
#include "qgsapplication.h"
#include "qgscomposerlabel.h"
#include "qgscomposermap.h"
#include "qgscomposition.h"
#include "qgsrasterlayer.h"
Expand Down Expand Up @@ -375,7 +376,25 @@ QgsComposition* QgsConfigParser::createPrintComposition( const QString& composer
}
}

//replace composer label text
//replace label text
QList<QgsComposerLabel*>::const_iterator labelIt = composerLabels.constBegin();
QgsComposerLabel* currentLabel = 0;

for ( ; labelIt != composerLabels.constEnd(); ++labelIt )
{
currentLabel = *labelIt;
QMap< QString, QString >::const_iterator titleIt = parameterMap.find( "LABEL" + QString::number( currentLabel->id() ) );
if ( titleIt == parameterMap.constEnd() )
{
//remove label
c->removeItem( *labelIt );
delete( *labelIt );
continue;
}

currentLabel->setText( titleIt.value() );
currentLabel->adjustSizeToText();
}

return c;
}
2 changes: 1 addition & 1 deletion src/mapserver/qgsconfigparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class QgsConfigParser
QgsComposition* createPrintComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, const QMap< QString, QString >& parameterMap ) const;

/**Creates a composition from the project file (probably delegated to the fallback parser)*/
virtual QgsComposition* initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* > labelList ) const = 0;
virtual QgsComposition* initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* >& labelList ) const = 0;

/**Adds print capabilities to xml document. ParentElem usually is the <Capabilities> element*/
virtual void printCapabilities( QDomElement& parentElement, QDomDocument& doc ) const = 0;
Expand Down
16 changes: 14 additions & 2 deletions src/mapserver/qgsprojectparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qgsepsgcache.h"
#include "qgsmslayercache.h"
#include "qgsmapserverlogger.h"
#include "qgsmapserviceexception.h"
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"

Expand All @@ -32,6 +33,7 @@
#include "qgscomposerscalebar.h"
#include "qgscomposershape.h"


QgsProjectParser::QgsProjectParser( QDomDocument* xmlDoc ): QgsConfigParser(), mXMLDoc( xmlDoc )
{
mOutputUnits = QgsMapRenderer::Millimeters;
Expand Down Expand Up @@ -824,13 +826,13 @@ QString QgsProjectParser::layerIdFromLegendLayer( const QDomElement& legendLayer
return legendLayerFileList.at( 0 ).toElement().attribute( "layerid" );
}

QgsComposition* QgsProjectParser::initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* > labelList ) const
QgsComposition* QgsProjectParser::initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* >& labelList ) const
{
//Create composition from xml
QDomElement composerElem = composerByName( composerTemplate );
if ( composerElem.isNull() )
{
return 0;
throw QgsMapServiceException( "Error", "Composer template not found" );
}

QDomElement compositionElem = composerElem.firstChildElement( "Composition" );
Expand Down Expand Up @@ -957,6 +959,16 @@ void QgsProjectParser::printCapabilities( QDomElement& parentElement, QDomDocume
composerTemplateElem.appendChild( composerMapElem );
}

//add available composer labels
QDomNodeList composerLabelList = currentComposerElem.elementsByTagName( "ComposerLabel" );
for ( int j = 0; j < composerLabelList.size(); ++j )
{
QDomElement clabel = composerLabelList.at( j ).toElement();
QDomElement composerLabelElem = doc.createElement( "ComposerLabel" );
composerLabelElem.setAttribute( "name", "label" + clabel.attribute( "id" ) );
composerTemplateElem.appendChild( composerLabelElem );
}

parentElement.appendChild( composerTemplateElem );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/mapserver/qgsprojectparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class QgsProjectParser: public QgsConfigParser
const QDomDocument* xmlDoc() const { return mXMLDoc; }

/**Creates a composition from the project file (probably delegated to the fallback parser)*/
QgsComposition* initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* > labelList ) const;
QgsComposition* initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* >& labelList ) const;

/**Adds print capabilities to xml document. ParentElem usually is the <Capabilities> element*/
void printCapabilities( QDomElement& parentElement, QDomDocument& doc ) const;
Expand Down
2 changes: 1 addition & 1 deletion src/mapserver/qgssldparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ void QgsSLDParser::setCrsForLayer( const QDomElement& layerElem, QgsMapLayer* ml
}
}

QgsComposition* QgsSLDParser::initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* > labelList ) const
QgsComposition* QgsSLDParser::initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* >& labelList ) const
{
if ( mFallbackParser )
{
Expand Down
2 changes: 1 addition & 1 deletion src/mapserver/qgssldparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class QgsSLDParser: public QgsConfigParser
virtual void setParameterMap( const std::map<QString, QString>& parameterMap ) { mParameterMap = parameterMap; }

/**Creates a composition from the project file (delegated to the fallback parser)*/
QgsComposition* initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* > labelList ) const;
QgsComposition* initComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, QList< QgsComposerMap*>& mapList, QList< QgsComposerLabel* >& labelList ) const;

/**Adds print capabilities to xml document. Delegated to fallback parser*/
void printCapabilities( QDomElement& parentElement, QDomDocument& doc ) const;
Expand Down
10 changes: 7 additions & 3 deletions src/mapserver/qgswmsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ QByteArray* QgsWMSServer::getPrint( QString& formatString )
std::map<QString, QString>::const_iterator templateIt = mParameterMap.find( "TEMPLATE" );
if ( templateIt == mParameterMap.end() )
{
return 0;
throw QgsMapServiceException( "ParameterMissing", "The TEMPLATE parameter is required for the GetPrint request" );
}

QgsComposition* c = mConfigParser->createPrintComposition( templateIt->second, mMapRenderer, QMap<QString, QString>( mParameterMap ) );
Expand Down Expand Up @@ -401,7 +401,7 @@ QByteArray* QgsWMSServer::getPrint( QString& formatString )
c->render( &p, targetArea, sourceArea );
p.end();
}
else if ( formatString.compare( "png", Qt::CaseInsensitive ) == 0 )
else if ( formatString.compare( "png", Qt::CaseInsensitive ) == 0 || formatString.compare( "jpg", Qt::CaseInsensitive ) == 0 )
{
int width = ( int )( c->paperWidth() * c->printResolution() / 25.4 ); //width in pixel
int height = ( int )( c->paperHeight() * c->printResolution() / 25.4 ); //height in pixel
Expand All @@ -417,7 +417,7 @@ QByteArray* QgsWMSServer::getPrint( QString& formatString )
ba = new QByteArray();
QBuffer buffer( ba );
buffer.open( QIODevice::WriteOnly );
image.save( &buffer, "png", -1 );
image.save( &buffer, formatString.toLocal8Bit().data(), -1 );
}
else if ( formatString.compare( "pdf", Qt::CaseInsensitive ) == 0 )
{
Expand All @@ -443,6 +443,10 @@ QByteArray* QgsWMSServer::getPrint( QString& formatString )
ba = new QByteArray();
*ba = tempFile.readAll();
}
else //unknown format
{
throw QgsMapServiceException( "InvalidFormat", "Output format '" + formatString + "' is not supported in the GetPrint request" );
}

delete c;
return ba;
Expand Down

0 comments on commit f1ca934

Please sign in to comment.