29 #include <QDomDocument> 30 #include <QDomElement> 32 #include <QImageReader> 34 #include <QSvgRenderer> 35 #include <QNetworkRequest> 36 #include <QNetworkReply> 38 #include <QCoreApplication> 43 , mPictureRotation( 0 )
44 , mRotationMap( nullptr )
46 , mPictureAnchor( UpperLeft )
47 , mHasExpressionError( false )
55 mPictureRotation( 0 ),
56 mRotationMap(
nullptr ),
59 mHasExpressionError(
false )
65 void QgsComposerPicture::init()
94 Q_UNUSED( itemStyle );
112 double boundRectWidthMM;
113 double boundRectHeightMM;
117 boundRectWidthMM = mPictureWidth;
118 boundRectHeightMM = mPictureHeight;
131 int imageRectWidthPixels = mImage.
width();
132 int imageRectHeightPixels = mImage.
height();
133 imageRect = clippedImageRect( boundRectWidthMM, boundRectHeightMM,
134 QSize( imageRectWidthPixels, imageRectHeightPixels ) );
148 if ( mResizeMode ==
Zoom )
155 painter->
rotate( mPictureRotation );
156 painter->
translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
161 double diffX =
rect().
width() - boundRectWidthMM;
162 double diffY =
rect().
height() - boundRectHeightMM;
166 switch ( mPictureAnchor )
184 switch ( mPictureAnchor )
210 painter->
rotate( mPictureRotation );
211 painter->
translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
217 mSVG.
render( painter,
QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) );
219 else if ( mMode ==
RASTER )
221 painter->
drawImage(
QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, imageRect );
235 QRect QgsComposerPicture::clippedImageRect(
double &boundRectWidthMM,
double &boundRectHeightMM,
QSize imageRectPixels )
249 switch ( mPictureAnchor )
259 leftClip = ( imageRectPixels.
width() - boundRectWidthPixels ) / 2;
264 leftClip = imageRectPixels.
width() - boundRectWidthPixels;
269 switch ( mPictureAnchor )
279 topClip = ( imageRectPixels.
height() - boundRectHeightPixels ) / 2;
284 topClip = imageRectPixels.
height() - boundRectHeightPixels;
288 return QRect( leftClip, topClip, boundRectWidthPixels, boundRectHeightPixels );
303 evalContext = scopedContext.
data();
309 mHasExpressionError =
false;
320 mHasExpressionError =
true;
326 loadPicture( source );
329 void QgsComposerPicture::loadRemotePicture(
const QString &url )
337 connect( &fetcher, SIGNAL( finished() ),
this, SLOT( remotePictureLoaded() ) );
341 qApp->processEvents();
348 mImage = imageReader.
read();
358 void QgsComposerPicture::loadLocalPicture(
const QString &path )
371 if ( sourceFileSuffix.
compare(
"svg", Qt::CaseInsensitive ) == 0 )
391 if ( imageReader.read( &mImage ) )
404 void QgsComposerPicture::remotePictureLoaded()
409 void QgsComposerPicture::loadPicture(
const QString &path )
414 loadRemotePicture( path );
419 loadLocalPicture( path );
425 else if ( mHasExpressionError || !( path.
isEmpty() ) )
429 QString badFile(
":/images/composer/missing_image.svg" );
430 mSVG.
load( badFile );
444 QRectF QgsComposerPicture::boundedImageRect(
double deviceWidth,
double deviceHeight )
446 double imageToDeviceRatio;
447 if ( mImage.
width() / deviceWidth > mImage.
height() / deviceHeight )
449 imageToDeviceRatio = deviceWidth / mImage.
width();
450 double height = imageToDeviceRatio * mImage.
height();
451 return QRectF( 0, 0, deviceWidth, height );
455 imageToDeviceRatio = deviceHeight / mImage.
height();
456 double width = imageToDeviceRatio * mImage.
width();
457 return QRectF( 0, 0, width, deviceHeight );
461 QRectF QgsComposerPicture::boundedSVGRect(
double deviceWidth,
double deviceHeight )
463 double imageToSvgRatio;
464 if ( deviceWidth / mDefaultSvgSize.
width() > deviceHeight / mDefaultSvgSize.
height() )
466 imageToSvgRatio = deviceHeight / mDefaultSvgSize.
height();
467 double width = mDefaultSvgSize.
width() * imageToSvgRatio;
468 return QRectF( 0, 0, width, deviceHeight );
472 imageToSvgRatio = deviceWidth / mDefaultSvgSize.
width();
473 double height = mDefaultSvgSize.
height() * imageToSvgRatio;
474 return QRectF( 0, 0, deviceWidth, height );
478 QSizeF QgsComposerPicture::pictureSize()
482 return mDefaultSvgSize;
484 else if ( mMode ==
RASTER )
495 QRectF QgsComposerPicture::boundedSVGRect(
double deviceWidth,
double deviceHeight )
497 double imageToSvgRatio;
498 if ( deviceWidth / mDefaultSvgSize.
width() < deviceHeight / mDefaultSvgSize.
height() )
500 imageToSvgRatio = deviceWidth / mDefaultSvgSize.
width();
501 double height = mDefaultSvgSize.
height() * imageToSvgRatio;
502 return QRectF( 0, 0, deviceWidth, height );
506 imageToSvgRatio = deviceHeight / mDefaultSvgSize.
height();
507 double width = mDefaultSvgSize.
width() * imageToSvgRatio;
508 return QRectF( 0, 0, width, deviceHeight );
516 QSizeF currentPictureSize = pictureSize();
521 mPictureWidth = rectangle.
width();
522 mPictureHeight = rectangle.
height();
526 QRectF newRect = rectangle;
533 targetImageSize = currentPictureSize;
539 tr.
rotate( mPictureRotation );
546 if ( qAbs(
rect().width() - rectangle.
width() ) <
558 if ( !( currentPictureSize.
isEmpty() ) )
569 mPictureWidth = rotatedImageRect.
width();
570 mPictureHeight = rotatedImageRect.
height();
574 mPictureWidth = newRect.
width();
575 mPictureHeight = newRect.
height();
590 double oldRotation = mPictureRotation;
591 mPictureRotation = r;
593 if ( mResizeMode ==
Zoom )
596 QSizeF currentPictureSize = pictureSize();
598 mPictureWidth = rotatedImageRect.
width();
599 mPictureHeight = rotatedImageRect.
height();
604 QSizeF currentPictureSize = pictureSize();
612 tr.rotate( mPictureRotation );
631 if ( composerMapId == -1 )
634 mRotationMap =
nullptr;
679 evalContext = scopedContext.
data();
761 if ( !composerItemList.
isEmpty() )
774 mDefaultSvgSize =
QSize( 0, 0 );
781 bool expressionActive;
782 if ( useExpression.
compare(
"true", Qt::CaseInsensitive ) == 0 )
784 expressionActive =
true;
788 expressionActive =
false;
804 if ( rotationMapId == -1 )
806 mRotationMap =
nullptr;
833 return mRotationMap->
id();
839 mPictureAnchor = anchor;
QDomNodeList elementsByTagName(const QString &tagname) const
void setActive(bool active)
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
Sets state from Dom document.
Q_DECL_DEPRECATED 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 setHeight(int height)
void setRenderHint(RenderHint hint, bool on)
QDomNode appendChild(const QDomNode &newChild)
void render(QPainter *painter)
QString attribute(const QString &name, const QString &defValue) const
void itemChanged()
Emitted when the item changes.
bool writeXML(QDomElement &elem, QDomDocument &doc) const override
Stores state in Dom element.
QgsComposerPicture(QgsComposition *composition)
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).
QMap< QgsComposerObject::DataDefinedProperty, QString > mDataDefinedNames
Map of data defined properties for the item to string name to use when exporting item to xml...
A item that forms part of a map composition.
#define Q_NOWARN_DEPRECATED_PUSH
void setFileName(const QString &name)
QString expressionString() const
Returns the expression string of this QgsDataDefined.
virtual void drawFrame(QPainter *p)
Draw black frame around item.
QString picturePath() const
Returns the path of the source image.
QNetworkReply * reply()
Returns a reference to the network reply.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
double toDouble(bool *ok) const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
virtual QgsExpressionContext * createExpressionContext() const override
Creates an expression context relating to the item's current state.
QString tr(const char *sourceText, const char *disambiguation, int n)
QString readPath(QString filename) const
Turn filename read from the project file to an absolute path.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
void update(const QRectF &rect)
DataDefinedProperty
Data defined properties for different item types.
A composer class that displays svg files or raster format (jpg, png, ...)
void recalculateSize()
Forces a recalculation of the picture's frame size.
void setHeight(qreal height)
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
QDomElement toElement() const
QgsDataDefined * dataDefinedProperty(const DataDefinedProperty property) const
Returns a reference to the data defined settings for one of the item's data defined properties...
QString number(int n, int base)
void setPictureAnchor(QgsComposerItem::ItemPositionMode anchor)
Sets the picture's anchor point, which controls how it is placed within the picture item's frame...
QVariant property(const char *name) const
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint.
bool load(const QString &filename)
ResizeMode
Controls how pictures are scaled within the item's frame.
bool hasAttribute(const QString &name) const
HTTP network content fetcher.
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
int printResolution() const
void setAttribute(const QString &name, const QString &value)
virtual void setResizeMode(ResizeMode mode)
Sets the resize mode used for drawing the picture within the item bounds.
int toInt(bool *ok, int base) const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
static QRectF largestRotatedRectWithinBounds(const QRectF &originalRect, const QRectF &boundsRect, const double rotation)
Calculates the largest scaled version of originalRect which fits within boundsRect, when it is rotated by a specified amount.
static void logMessage(const QString &message, const QString &tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties, const QgsExpressionContext *context=nullptr) override
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
virtual void setUsePictureExpression(bool useExpression)
Sets whether the picture should use an expression based image source path.
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
virtual void setPictureExpression(const QString &expression)
Sets an expression to use for the picture source.
double mapRotation(QgsComposerObject::PropertyValueType valueType=QgsComposerObject::EvaluatedValue) const
Returns the rotation used for drawing the map within the composer item.
void setBackgroundEnabled(const bool drawBackground)
Set whether this item has a Background drawn around it or not.
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties, const QgsExpressionContext *context=nullptr) override
Graphics scene for map printing.
void moveCenter(const QPointF &position)
Object representing map window.
Q_DECL_DEPRECATED QString pictureFile() const
Returns the path of the source image file.
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height) const
Calculates corner point after rotation and scaling.
bool dataDefinedEvaluate(const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue, const QgsExpressionContext &context=QgsExpressionContext()) const
Evaluate a data defined property and return the calculated value.
void pictureRotationChanged(double newRotation)
Is emitted on picture rotation change.
#define Q_NOWARN_DEPRECATED_POP
int id() const
Get identification number.
Q_DECL_DEPRECATED void setPictureFile(const QString &path)
Sets the source file of the image (may be svg or a raster format).
QgsComposition * mComposition
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height, double rotation) const
Calculates corner point after rotation and scaling.
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, QFlags< Qt::ImageConversionFlag > flags)
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
void setWidth(qreal width)
virtual void drawBackground(QPainter *p)
Draw background.
Q_DECL_DEPRECATED QString pictureExpression() const
Returns the expression the item is using for the picture source.
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...
void setDataDefinedProperty(const DataDefinedProperty property, const bool active, const bool useExpression, const QString &expression, const QString &field)
Sets parameters for a data defined property for the item.
void translate(const QPointF &offset)
void setSceneRect(const QRectF &rectangle) override
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
QgsAtlasComposition & atlasComposition()
virtual void setRotation(double r) override
Sets the picture rotation within the item bounds.
iterator insert(const Key &key, const T &value)
void setRotationMap(int composerMapId)
Sets the map object for rotation (by id).
void setPicturePath(const QString &path)
Sets the source path of the image (may be svg or a raster format).
QgsComposerItem(QgsComposition *composition, bool manageZValue=true)
Constructor.
virtual void setPictureRotation(double r)
Sets the picture rotation within the item bounds.
QDomElement createElement(const QString &tagName)
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void fetchContent(const QUrl &url)
Fetches content from a remote URL and handles redirects.
int rotationMap() const
Returns the id of the rotation map.
int compare(const QString &other) const
void setExpressionString(const QString &expr)
Sets the expression for this QgsDataDefined.
void refreshPicture(const QgsExpressionContext *context=nullptr)
Recalculates the source image (if using an expression for picture's source) and reloads and redraws t...
Q_DECL_DEPRECATED void sizeChangedByRotation(double &width, double &height, double rotation)
Calculates width / height of the bounding box of a rotated rectangle.
Q_DECL_DEPRECATED bool usePictureExpression() const
Returns whether the picture item is using an expression for the image source.
QDomNode at(int index) const
Q_DECL_DEPRECATED void sizeChangedByRotation(double &width, double &height)
Calculates width / height of the bounding box of a rotated rectangle.