|
QGIS API Documentation
master-59fd5e0
|
00001 /*************************************************************************** 00002 qgssvgannotationitem.cpp 00003 ------------------------ 00004 begin : November, 2012 00005 copyright : (C) 2012 by Marco Hugentobler 00006 email : marco dot hugentobler at sourcepole dot ch 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include "qgssvgannotationitem.h" 00019 #include "qgsproject.h" 00020 #include <QDomDocument> 00021 #include <QDomElement> 00022 00023 00024 QgsSvgAnnotationItem::QgsSvgAnnotationItem( QgsMapCanvas* canvas ): QgsAnnotationItem( canvas ) 00025 { 00026 00027 } 00028 00029 QgsSvgAnnotationItem::~QgsSvgAnnotationItem() 00030 { 00031 00032 } 00033 00034 void QgsSvgAnnotationItem::writeXML( QDomDocument& doc ) const 00035 { 00036 QDomElement documentElem = doc.documentElement(); 00037 if ( documentElem.isNull() ) 00038 { 00039 return; 00040 } 00041 00042 QDomElement svgAnnotationElem = doc.createElement( "SVGAnnotationItem" ); 00043 svgAnnotationElem.setAttribute( "file", QgsProject::instance()->writePath( mFilePath ) ); 00044 _writeXML( doc, svgAnnotationElem ); 00045 documentElem.appendChild( svgAnnotationElem ); 00046 } 00047 00048 void QgsSvgAnnotationItem::readXML( const QDomDocument& doc, const QDomElement& itemElem ) 00049 { 00050 QString filePath = QgsProject::instance()->readPath( itemElem.attribute( "file" ) ); 00051 setFilePath( filePath ); 00052 QDomElement annotationElem = itemElem.firstChildElement( "AnnotationItem" ); 00053 if ( !annotationElem.isNull() ) 00054 { 00055 _readXML( doc, annotationElem ); 00056 } 00057 } 00058 00059 void QgsSvgAnnotationItem::paint( QPainter* painter ) 00060 { 00061 if ( !painter ) 00062 { 00063 return; 00064 } 00065 00066 drawFrame( painter ); 00067 if ( mMapPositionFixed ) 00068 { 00069 drawMarkerSymbol( painter ); 00070 } 00071 00072 //keep width/height ratio of svg 00073 QRect viewBox = mSvgRenderer.viewBox(); 00074 if ( viewBox.isValid() ) 00075 { 00076 double widthRatio = mFrameSize.width() / viewBox.width(); 00077 double heightRatio = mFrameSize.height() / viewBox.height(); 00078 double renderWidth = 0; 00079 double renderHeight = 0; 00080 if ( widthRatio <= heightRatio ) 00081 { 00082 renderWidth = mFrameSize.width(); 00083 renderHeight = viewBox.height() * mFrameSize.width() / viewBox.width() ; 00084 } 00085 else 00086 { 00087 renderHeight = mFrameSize.height(); 00088 renderWidth = viewBox.width() * mFrameSize.height() / viewBox.height() ; 00089 } 00090 00091 mSvgRenderer.render( painter, QRectF( mOffsetFromReferencePoint.x(), mOffsetFromReferencePoint.y(), renderWidth, 00092 renderHeight ) ); 00093 } 00094 if ( isSelected() ) 00095 { 00096 drawSelectionBoxes( painter ); 00097 } 00098 } 00099 00100 void QgsSvgAnnotationItem::setFilePath( const QString& file ) 00101 { 00102 mFilePath = file; 00103 mSvgRenderer.load( mFilePath ); 00104 }