Skip to content

Commit

Permalink
Re-enable watermark items
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Apr 6, 2014
1 parent cd01a87 commit 9af8498
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/mapserver/qgsserverprojectparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,36 @@ QString QgsServerProjectParser::layerId( const QDomElement& layerElem ) const
return idElem.text();
}

QgsRectangle QgsServerProjectParser::projectExtent() const
{
QgsRectangle extent;
if ( !mXMLDoc )
{
return extent;
}

QDomElement qgisElem = mXMLDoc->documentElement();
QDomElement mapCanvasElem = qgisElem.firstChildElement( "mapcanvas" );
if ( mapCanvasElem.isNull() )
{
return extent;
}

QDomElement extentElem = mapCanvasElem.firstChildElement( "extent" );
bool xminOk, xmaxOk, yminOk, ymaxOk;
double xMin = extentElem.firstChildElement( "xmin" ).text().toDouble( &xminOk );
double xMax = extentElem.firstChildElement( "xmax" ).text().toDouble( &xmaxOk );
double yMin = extentElem.firstChildElement( "ymin" ).text().toDouble( &yminOk );
double yMax = extentElem.firstChildElement( "ymax" ).text().toDouble( &ymaxOk );

if ( xminOk && xmaxOk && yminOk && ymaxOk )
{
extent = QgsRectangle( xMin, yMin, xMax, yMax );
}

return extent;
}

QString QgsServerProjectParser::layerName( const QDomElement& layerElem ) const
{
if ( layerElem.isNull() )
Expand Down
2 changes: 2 additions & 0 deletions src/mapserver/qgsserverprojectparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class QgsServerProjectParser
@return id or a null string in case of error*/
QString layerId( const QDomElement& layerElem ) const;

QgsRectangle projectExtent() const;

private:

/**Content of project file*/
Expand Down
117 changes: 114 additions & 3 deletions src/mapserver/qgswmsprojectparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ QgsWMSProjectParser::QgsWMSProjectParser( QDomDocument* xmlDoc, const QString& f
{
mLegendLayerFont.fromString( mProjectParser.firstComposerLegendElement().attribute( "layerFont" ) );
mLegendItemFont.fromString( mProjectParser.firstComposerLegendElement().attribute( "itemFont" ) );
createTextAnnotationItems();
createSvgAnnotationItems();
}

QgsWMSProjectParser::~QgsWMSProjectParser()
{
cleanupTextAnnotationItems();
cleanupSvgAnnotationItems();
}

void QgsWMSProjectParser::layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc, const QString& version, bool fullProjectSettings ) const
Expand Down Expand Up @@ -1555,13 +1559,12 @@ bool QgsWMSProjectParser::featureInfoFormatSIA2045() const

void QgsWMSProjectParser::drawOverlays( QPainter* p, int dpi, int width, int height ) const
{
#if 0
Q_UNUSED( width );
Q_UNUSED( height );

//consider DPI
double scaleFactor = dpi / 88.0; //assume 88 as standard dpi
QgsRectangle prjExtent = projectExtent();
QgsRectangle prjExtent = mProjectParser.projectExtent();

//text annotations
QList< QPair< QTextDocument*, QDomElement > >::const_iterator textIt = mTextAnnotationItems.constBegin();
Expand Down Expand Up @@ -1633,7 +1636,6 @@ void QgsWMSProjectParser::drawOverlays( QPainter* p, int dpi, int width, int hei
renderHeight ) );
}
}
#endif //0
}

QDomElement QgsWMSProjectParser::composerByName( const QString& composerName ) const
Expand All @@ -1657,3 +1659,112 @@ QDomElement QgsWMSProjectParser::composerByName( const QString& composerName ) c

return composerElem;
}

bool QgsWMSProjectParser::annotationPosition( const QDomElement& elem, double scaleFactor, double& xPos, double& yPos )
{
Q_UNUSED( scaleFactor );

xPos = elem.attribute( "canvasPosX" ).toDouble() / scaleFactor;
yPos = elem.attribute( "canvasPosY" ).toDouble() / scaleFactor;
return true;
}

void QgsWMSProjectParser::drawAnnotationRectangle( QPainter* p, const QDomElement& elem, double scaleFactor, double xPos, double yPos, int itemWidth, int itemHeight )
{
Q_UNUSED( scaleFactor );
if ( !p )
{
return;
}

QColor backgroundColor( elem.attribute( "frameBackgroundColor", "#000000" ) );
backgroundColor.setAlpha( elem.attribute( "frameBackgroundColorAlpha", "255" ).toInt() );
p->setBrush( QBrush( backgroundColor ) );
QColor frameColor( elem.attribute( "frameColor", "#000000" ) );
frameColor.setAlpha( elem.attribute( "frameColorAlpha", "255" ).toInt() );
QPen framePen( frameColor );
framePen.setWidth( elem.attribute( "frameBorderWidth", "1" ).toInt() );
p->setPen( framePen );

p->drawRect( QRectF( xPos, yPos, itemWidth, itemHeight ) );
}

void QgsWMSProjectParser::createTextAnnotationItems()
{
cleanupTextAnnotationItems();

const QDomDocument* xmlDoc = mProjectParser.xmlDocument();
if ( !xmlDoc )
{
return;
}

//text annotations
QDomElement qgisElem = xmlDoc->documentElement();
QDomNodeList textAnnotationList = qgisElem.elementsByTagName( "TextAnnotationItem" );
QDomElement textAnnotationElem;
QDomElement annotationElem;
for ( int i = 0; i < textAnnotationList.size(); ++i )
{
textAnnotationElem = textAnnotationList.at( i ).toElement();
annotationElem = textAnnotationElem.firstChildElement( "AnnotationItem" );
if ( !annotationElem.isNull() && annotationElem.attribute( "mapPositionFixed" ) != "1" )
{
QTextDocument* textDoc = new QTextDocument();
textDoc->setHtml( textAnnotationElem.attribute( "document" ) );
mTextAnnotationItems.push_back( qMakePair( textDoc, annotationElem ) );
}
}
}

void QgsWMSProjectParser::createSvgAnnotationItems()
{
mSvgAnnotationElems.clear();
const QDomDocument* xmlDoc = mProjectParser.xmlDocument();
if ( !xmlDoc )
{
return;
}

QDomElement qgisElem = xmlDoc->documentElement();
QDomNodeList svgAnnotationList = qgisElem.elementsByTagName( "SVGAnnotationItem" );
QDomElement svgAnnotationElem;
QDomElement annotationElem;
for ( int i = 0; i < svgAnnotationList.size(); ++i )
{
svgAnnotationElem = svgAnnotationList.at( i ).toElement();
annotationElem = svgAnnotationElem.firstChildElement( "AnnotationItem" );
if ( !annotationElem.isNull() && annotationElem.attribute( "mapPositionFixed" ) != "1" )
{
QSvgRenderer* svg = new QSvgRenderer();
if ( svg->load( mProjectParser.convertToAbsolutePath( svgAnnotationElem.attribute( "file" ) ) ) )
{
mSvgAnnotationElems.push_back( qMakePair( svg, annotationElem ) );
}
else
{
delete svg;
}
}
}
}

void QgsWMSProjectParser::cleanupSvgAnnotationItems()
{
QList< QPair< QSvgRenderer*, QDomElement > >::const_iterator it = mSvgAnnotationElems.constBegin();
for ( ; it != mSvgAnnotationElems.constEnd(); ++it )
{
delete it->first;
}
mSvgAnnotationElems.clear();
}

void QgsWMSProjectParser::cleanupTextAnnotationItems()
{
QList< QPair< QTextDocument*, QDomElement > >::const_iterator it = mTextAnnotationItems.constBegin();
for ( ; it != mTextAnnotationItems.constEnd(); ++it )
{
delete it->first;
}
mTextAnnotationItems.clear();
}
15 changes: 15 additions & 0 deletions src/mapserver/qgswmsprojectparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "qgswmsconfigparser.h"
#include "qgsserverprojectparser.h"

class QTextDocument;
class QSvgRenderer;

class QgsWMSProjectParser: public QgsWMSConfigParser
{
public:
Expand Down Expand Up @@ -104,6 +107,11 @@ class QgsWMSProjectParser: public QgsWMSConfigParser

mutable QFont mLegendItemFont;

/**Watermark text items*/
QList< QPair< QTextDocument*, QDomElement > > mTextAnnotationItems;
/**Watermark items (content cached in QgsSVGCache)*/
QList< QPair< QSvgRenderer*, QDomElement > > mSvgAnnotationElems;

/**Returns an ID-list of layers which are not queryable (comes from <properties> -> <Identify> -> <disabledLayers in the project file*/
virtual QStringList identifyDisabledLayers() const;

Expand All @@ -126,6 +134,13 @@ class QgsWMSProjectParser: public QgsWMSConfigParser
const QString& strHref, QgsRectangle& combinedBBox, QString strGroup ) const;

QDomElement composerByName( const QString& composerName ) const;

static bool annotationPosition( const QDomElement& elem, double scaleFactor, double& xPos, double& yPos );
static void drawAnnotationRectangle( QPainter* p, const QDomElement& elem, double scaleFactor, double xPos, double yPos, int itemWidth, int itemHeight );
void createTextAnnotationItems();
void createSvgAnnotationItems();
void cleanupSvgAnnotationItems();
void cleanupTextAnnotationItems();
};

#endif // QGSWMSPROJECTPARSER_H

0 comments on commit 9af8498

Please sign in to comment.