26 #include <QDomDocument>
27 #include <QDomElement>
29 #include <QImageReader>
31 #include <QSvgRenderer>
37 mUseSourceExpression( false ),
38 mPictureRotation( 0 ),
41 mPictureAnchor( UpperLeft ),
50 mUseSourceExpression( false ),
51 mPictureRotation( 0 ),
54 mPictureAnchor( UpperLeft ),
80 Q_UNUSED( itemStyle );
94 double boundRectWidthMM;
95 double boundRectHeightMM;
101 imageRect = QRect( 0, 0,
mImage.width(),
mImage.height() );
105 boundRectWidthMM = rect().width();
106 boundRectHeightMM = rect().height();
107 imageRect = QRect( 0, 0,
mImage.width(),
mImage.height() );
111 boundRectWidthMM = rect().width();
112 boundRectHeightMM = rect().height();
113 int imageRectWidthPixels =
mImage.width();
114 int imageRectHeightPixels =
mImage.height();
116 QSize( imageRectWidthPixels, imageRectHeightPixels ) );
120 boundRectWidthMM = rect().width();
121 boundRectHeightMM = rect().height();
134 painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
136 painter->translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
141 double diffX = rect().width() - boundRectWidthMM;
142 double diffY = rect().height() - boundRectHeightMM;
182 painter->translate( dX, dY );
188 mSVG.render( painter, QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) );
192 painter->drawImage( QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ),
mImage, imageRect );
230 leftClip = ( imageRectPixels.width() - boundRectWidthPixels ) / 2;
235 leftClip = imageRectPixels.width() - boundRectWidthPixels;
250 topClip = ( imageRectPixels.height() - boundRectHeightPixels ) / 2;
255 topClip = imageRectPixels.height() - boundRectHeightPixels;
260 return QRect( leftClip, topClip, boundRectWidthPixels, boundRectHeightPixels );
310 QVariant filenameRes;
328 return filenameRes.toString();
363 QFileInfo sourceFileInfo( file );
364 QString sourceFileSuffix = sourceFileInfo.suffix();
365 if ( sourceFileSuffix.compare(
"svg", Qt::CaseInsensitive ) == 0 )
368 mSVG.load( file.fileName() );
369 if (
mSVG.isValid() )
372 QRect viewBox =
mSVG.viewBox();
384 QImageReader imageReader( file.fileName() );
385 if ( imageReader.read( &
mImage ) )
404 QString badFile = QString(
":/images/composer/missing_image.svg" );
405 mSVG.load( badFile );
406 if (
mSVG.isValid() )
409 QRect viewBox =
mSVG.viewBox();
421 double imageToDeviceRatio;
422 if (
mImage.width() / deviceWidth >
mImage.height() / deviceHeight )
424 imageToDeviceRatio = deviceWidth /
mImage.width();
425 double height = imageToDeviceRatio *
mImage.height();
426 return QRectF( 0, 0, deviceWidth, height );
430 imageToDeviceRatio = deviceHeight /
mImage.height();
431 double width = imageToDeviceRatio *
mImage.width();
432 return QRectF( 0, 0, width, deviceHeight );
438 double imageToSvgRatio;
443 return QRectF( 0, 0, width, deviceHeight );
449 return QRectF( 0, 0, deviceWidth, height );
465 return QSizeF( 0, 0 );
472 double imageToSvgRatio;
477 return QRectF( 0, 0, deviceWidth, height );
483 return QRectF( 0, 0, width, deviceHeight );
501 QRectF newRect = rectangle;
507 if ( qAbs( rect().width() - rectangle.width() ) <
508 qAbs( rect().height() - rectangle.height() ) )
510 newRect.setHeight( currentPictureSize.height() * newRect.width() / currentPictureSize.width() );
514 newRect.setWidth( currentPictureSize.width() * newRect.height() / currentPictureSize.height() );
519 if ( !( currentPictureSize.isEmpty() ) )
573 if ( composerMapId == -1 )
589 QObject::connect( map, SIGNAL( mapRotationChanged(
double ) ),
this, SLOT(
setPictureRotation(
double ) ) );
611 setSceneRect( QRectF( pos().x(), pos().y(), rect().width(), rect().height() ) );
657 QDomElement composerPictureElem = doc.createElement(
"ComposerPicture" );
659 composerPictureElem.setAttribute(
"pictureWidth", QString::number(
mPictureWidth ) );
660 composerPictureElem.setAttribute(
"pictureHeight", QString::number(
mPictureHeight ) );
661 composerPictureElem.setAttribute(
"resizeMode", QString::number((
int )
mResizeMode ) );
662 composerPictureElem.setAttribute(
"anchorPoint", QString::number((
int )
mPictureAnchor ) );
666 composerPictureElem.setAttribute(
"useExpression",
"true" );
670 composerPictureElem.setAttribute(
"useExpression",
"false" );
675 composerPictureElem.setAttribute(
"pictureRotation", QString::number(
mPictureRotation ) );
678 composerPictureElem.setAttribute(
"mapId", -1 );
686 elem.appendChild( composerPictureElem );
692 if ( itemElem.isNull() )
697 mPictureWidth = itemElem.attribute(
"pictureWidth",
"10" ).toDouble();
698 mPictureHeight = itemElem.attribute(
"pictureHeight",
"10" ).toDouble();
703 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
704 if ( composerItemList.size() > 0 )
706 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
708 if ( composerItemElem.attribute(
"rotation",
"0" ).toDouble() != 0 )
711 mPictureRotation = composerItemElem.attribute(
"rotation",
"0" ).toDouble();
721 QString useExpression = itemElem.attribute(
"useExpression" );
722 if ( useExpression.compare(
"true", Qt::CaseInsensitive ) == 0 )
736 if ( itemElem.attribute(
"pictureRotation",
"0" ).toDouble() != 0 )
742 int rotationMapId = itemElem.attribute(
"mapId",
"-1" ).toInt();
743 if ( rotationMapId == -1 )
752 QObject::disconnect(
mRotationMap, SIGNAL( mapRotationChanged(
double ) ),
this, SLOT(
setRotation(
double ) ) );
Class for parsing and evaluation of expressions (formerly called "search strings").
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
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 hasParserError() const
Returns true if an error occurred when parsing the input expression.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
sets state from Dom document
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
QSizeF pictureSize()
Returns size of current raster or svg picture.
Q_DECL_DEPRECATED 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...
Mode mode() const
Returns the current picture mode (image format).
A item that forms part of a map composition.
bool enabled() const
Returns whether the atlas generation is enabled.
virtual void setRotation(double r)
Sets the picture rotation within the item bounds.
Container of fields for a vector layer.
QgsComposerItem::ItemPositionMode mPictureAnchor
virtual void drawFrame(QPainter *p)
Draw black frame around item.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
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
A composer class that displays svg files or raster format (jpg, png, ...)
static void logMessage(QString message, QString tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
void recalculateSize()
Forces a recalculation of the picture's frame size.
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 setPictureAnchor(QgsComposerItem::ItemPositionMode anchor)
Sets the picture's anchor point, which controls how it is placed within the picture item's frame...
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget)
Reimplementation of QCanvasItem::paint.
bool mUseSourceExpression
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)
int printResolution() const
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.
virtual void setResizeMode(ResizeMode mode)
Sets the resize mode used for drawing the picture within the item bounds.
void updatePictureExpression()
Prepares the picture's source expression after it is altered or the compositions atlas coverage layer...
virtual void setUsePictureExpression(bool useExpression)
Sets whether the picture should use an expression based image source path.
QRect clippedImageRect(double &boundRectWidthMM, double &boundRectHeightMM, QSize imageRectPixels)
Returns part of a raster image which will be shown, given current picture anchor settings.
QgsComposition * mComposition
Graphics scene for map printing.
QgsFeature * currentFeature()
Returns the current atlas feature.
Object representing map window.
QString pictureFile() const
Returns the path of the source image file.
bool writeXML(QDomElement &elem, QDomDocument &doc) const
stores state in Dom element
Q_DECL_DEPRECATED 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.
QString mSourceExpression
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).
void refreshPicture()
Recalculates the source image (if using an expression for picture's source) and reloads and redraws t...
bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height, double rotation) const
Calculates corner point after rotation and scaling.
QString evalPictureExpression()
evaluates the picture expression and returns the calculated image path
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 loadPicture(const QFile &file)
loads an image file into the picture item and redraws the item
Class used to render an Atlas, iterating over geometry features.
QgsAtlasComposition & atlasComposition()
void setRotationMap(int composerMapId)
Sets the map object for rotation (by id).
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
virtual void setPictureRotation(double r)
Sets the picture rotation within the item bounds.
Represents a vector layer which manages a vector based data sets.
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.
QString parserErrorString() const
Returns parser error.
void init()
sets up the picture item and connects to relevant signals
QString evalErrorString() const
Returns evaluation error.
virtual void setPictureExpression(QString expression)
Sets an expression to use for the picture source.
void sizeChangedByRotation(double &width, double &height, double rotation)
Calculates width / height of the bounding box of a rotated rectangle.
Q_DECL_DEPRECATED void sizeChangedByRotation(double &width, double &height)
Calculates width / height of the bounding box of a rotated rectangle.
QgsExpression * mPictureExpr