22 #include <QDomDocument>
23 #include <QDomElement>
25 #include <QImageReader>
27 #include <QSvgRenderer>
48 Q_UNUSED( itemStyle );
65 painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
67 painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
71 mSVG.render( painter, QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) );
75 painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ),
mImage, QRectF( 0, 0,
mImage.width(),
mImage.height() ) );
98 QString sourceFileSuffix = sourceFileInfo.suffix();
99 if ( sourceFileSuffix.compare(
"svg", Qt::CaseInsensitive ) == 0 )
103 if (
mSVG.isValid() )
106 QRect viewBox =
mSVG.viewBox();
118 QImageReader imageReader(
mSourceFile.fileName() );
119 if ( imageReader.read( &
mImage ) )
131 setSceneRect( QRectF( pos().x(), pos().y(), rect().width(), rect().height() ) );
138 double imageToDeviceRatio;
139 if (
mImage.width() / deviceWidth >
mImage.height() / deviceHeight )
141 imageToDeviceRatio = deviceWidth /
mImage.width();
142 double height = imageToDeviceRatio *
mImage.height();
143 return QRectF( 0, 0, deviceWidth, height );
147 imageToDeviceRatio = deviceHeight /
mImage.height();
148 double width = imageToDeviceRatio *
mImage.width();
149 return QRectF( 0, 0, width, deviceHeight );
155 double imageToSvgRatio;
160 return QRectF( 0, 0, width, deviceHeight );
166 return QRectF( 0, 0, deviceWidth, height );
182 return QSizeF( 0, 0 );
189 double imageToSvgRatio;
194 return QRectF( 0, 0, deviceWidth, height );
200 return QRectF( 0, 0, width, deviceHeight );
245 if ( composerMapId == -1 )
261 QObject::connect( map, SIGNAL( mapRotationChanged(
double ) ),
this, SLOT(
setPictureRotation(
double ) ) );
278 QDomElement composerPictureElem = doc.createElement(
"ComposerPicture" );
280 composerPictureElem.setAttribute(
"pictureWidth", QString::number(
mPictureWidth ) );
281 composerPictureElem.setAttribute(
"pictureHeight", QString::number(
mPictureHeight ) );
284 composerPictureElem.setAttribute(
"pictureRotation", QString::number(
mPictureRotation ) );
287 composerPictureElem.setAttribute(
"mapId", -1 );
295 elem.appendChild( composerPictureElem );
301 if ( itemElem.isNull() )
306 mPictureWidth = itemElem.attribute(
"pictureWidth",
"10" ).toDouble();
307 mPictureHeight = itemElem.attribute(
"pictureHeight",
"10" ).toDouble();
309 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
310 if ( composerItemList.size() > 0 )
312 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
314 if ( composerItemElem.attribute(
"rotation",
"0" ).toDouble() != 0 )
317 mPictureRotation = composerItemElem.attribute(
"rotation",
"0" ).toDouble();
329 if ( itemElem.attribute(
"pictureRotation",
"0" ).toDouble() != 0 )
335 int rotationMapId = itemElem.attribute(
"mapId",
"-1" ).toInt();
336 if ( rotationMapId == -1 )
345 QObject::disconnect(
mRotationMap, SIGNAL( mapRotationChanged(
double ) ),
this, SLOT(
setRotation(
double ) ) );
bool imageSizeConsideringRotation(double &width, double &height, double rotation) const
Calculates width and hight of the picture (in mm) such that it fits into the item frame with the give...
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
QSizeF pictureSize()
Returns size of current raster or svg picture.
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...
A item that forms part of a map composition.
virtual void setRotation(double r)
Sets the picture rotation within the item bounds.
virtual void drawFrame(QPainter *p)
Draw black frame around item.
double mapRotation() const
Returns the rotation used for drawing the map within the composer item.
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)
QRectF largestRotatedRectWithinBounds(QRectF originalRect, QRectF boundsRect, double rotation) const
Calculates the largest scaled version of originalRect which fits within boundsRect, when it is rotated by a specified amount.
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
bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height) const
Calculates corner point after rotation and scaling.
void pictureRotationChanged(double newRotation)
Is emitted on picture rotation change.
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 cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height, double rotation) const
Calculates corner point after rotation and scaling.
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)
void setRotationMap(int composerMapId)
Sets the map object for rotation (by id).
virtual void setPictureRotation(double r)
Sets the picture rotation within the item bounds.
double mPictureRotation
Image rotation.
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.
void sizeChangedByRotation(double &width, double &height, double rotation)
Calculates width / height of the bounding box of a rotated rectangle.
void sizeChangedByRotation(double &width, double &height)
Calculates width / height of the bounding box of a rotated rectangle.