22 #include <QDomDocument>
23 #include <QDomElement>
25 #include <QImageReader>
27 #include <QSvgRenderer>
49 Q_UNUSED( itemStyle );
58 int newDpi = ( painter->device()->logicalDpiX() + painter->device()->logicalDpiY() ) / 2;
74 double boundRectWidthMM = boundRect.width() / newDpi * 25.4;
75 double boundRectHeightMM = boundRect.height() / newDpi * 25.4;
78 painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
80 painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
84 mSVG.render( painter, QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) );
88 painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ),
mImage, QRectF( 0, 0,
mImage.width(),
mImage.height() ) );
111 QString sourceFileSuffix = sourceFileInfo.suffix();
112 if ( sourceFileSuffix.compare(
"svg", Qt::CaseInsensitive ) == 0 )
116 if (
mSVG.isValid() )
119 QRect viewBox =
mSVG.viewBox();
131 QImageReader imageReader(
mSourceFile.fileName() );
132 if ( imageReader.read( &
mImage ) )
144 setSceneRect( QRectF( transform().dx(), transform().dy(), rect().width(), rect().height() ) );
151 double imageToDeviceRatio;
152 if (
mImage.width() / deviceWidth >
mImage.height() / deviceHeight )
154 imageToDeviceRatio = deviceWidth /
mImage.width();
155 double height = imageToDeviceRatio *
mImage.height();
156 return QRectF( 0, 0, deviceWidth, height );
160 imageToDeviceRatio = deviceHeight /
mImage.height();
161 double width = imageToDeviceRatio *
mImage.width();
162 return QRectF( 0, 0, width, deviceHeight );
168 double imageToSvgRatio;
173 return QRectF( 0, 0, width, deviceHeight );
179 return QRectF( 0, 0, deviceWidth, height );
186 double imageToSvgRatio;
191 return QRectF( 0, 0, deviceWidth, height );
197 return QRectF( 0, 0, width, deviceHeight );
207 double newPictureWidth = rectangle.width();
208 double newPictureHeight = rectangle.height();
224 double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
225 double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
238 if ( composerMapId == -1 )
270 QDomElement composerPictureElem = doc.createElement(
"ComposerPicture" );
272 composerPictureElem.setAttribute(
"pictureWidth", QString::number(
mPictureWidth ) );
273 composerPictureElem.setAttribute(
"pictureHeight", QString::number(
mPictureHeight ) );
276 composerPictureElem.setAttribute(
"mapId", -1 );
284 elem.appendChild( composerPictureElem );
290 if ( itemElem.isNull() )
295 mPictureWidth = itemElem.attribute(
"pictureWidth",
"10" ).toDouble();
296 mPictureHeight = itemElem.attribute(
"pictureHeight",
"10" ).toDouble();
298 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
299 if ( composerItemList.size() > 0 )
301 _readXML( composerItemList.at( 0 ).toElement(), doc );
311 int rotationMapId = itemElem.attribute(
"mapId",
"-1" ).toInt();
312 if ( rotationMapId == -1 )
void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
sets state from Dom document
virtual void setRotation(double r)
A item that forms part of a map composition.
virtual void setRotation(double r)
Sets the rotation and adapts the item rect.
virtual void drawFrame(QPainter *p)
Draw black frame around item.
double mRotation
Item rotation in degrees, clockwise.
QString readPath(QString filename) const
turn filename read from the project file to an absolute path
void itemChanged()
Used e.g.
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
QRectF boundedSVGRect(double deviceWidth, double deviceHeight)
Calculates bounding rect for svg file (mSourcefile) such that aspect ratio is correct.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget)
Reimplementation of QCanvasItem::paint.
const QgsComposerMap * mRotationMap
Map that sets the rotation (or 0 if this picture uses map independent rotation)
virtual void drawSelectionBoxes(QPainter *p)
Draw selection boxes around item.
double mPictureHeight
Height of the picture (in mm)
void rotationChanged(double newRotation)
Is emitted on rotation change to notify north arrow pictures.
QgsComposition * mComposition
Graphics scene for map printing.
Object representing map window.
QString pictureFile() const
bool writeXML(QDomElement &elem, QDomDocument &doc) const
stores state in Dom element
void sizeChangedByRotation(double &width, double &height)
Calculates width / height of the bounding box of a rotated rectangle (mRotation)
int id() const
Get identification number.
void setPictureFile(const QString &path)
Sets the source file of the image (may be svg or a raster format)
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
virtual void drawBackground(QPainter *p)
Draw background.
static QgsProject * instance()
access to canonical QgsProject instance
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
double mPictureWidth
Width of the picture (in mm)
bool imageSizeConsideringRotation(double &width, double &height) const
Calculates width and hight of the picture (in mm) such that it fits into the item frame with the give...
void setRotationMap(int composerMapId)
Sets the map object for rotation (by id).
int rotationMap() const
Returns the id of the rotation map.
QRectF boundedImageRect(double deviceWidth, double deviceHeight)
Calculates bounding rect for image such that aspect ratio is correct.
const QgsComposerMap * getComposerMapById(int id) const
Returns the composer map with specified id.