35 #include <QDomDocument> 36 #include <QDomElement> 38 #include <QImageReader> 40 #include <QSvgRenderer> 41 #include <QNetworkRequest> 42 #include <QNetworkReply> 44 #include <QCoreApplication> 49 , mPictureRotation( 0 )
50 , mRotationMap( nullptr )
51 , mNorthMode( GridNorth )
54 , mPictureAnchor( UpperLeft )
55 , mSvgFillColor(
QColor( 255, 255, 255 ) )
56 , mSvgBorderColor(
QColor( 0, 0, 0 ) )
57 , mSvgBorderWidth( 0.2 )
58 , mHasExpressionError( false )
59 , mLoadingSvg( false )
68 , mPictureRotation( 0 )
69 , mRotationMap(
nullptr )
74 , mSvgFillColor(
QColor( 255, 255, 255 ) )
75 , mSvgBorderColor(
QColor( 0, 0, 0 ) )
76 , mSvgBorderWidth( 0.2 )
77 , mHasExpressionError(
false )
78 , mLoadingSvg(
false )
84 void QgsComposerPicture::init()
113 Q_UNUSED( itemStyle );
131 double boundRectWidthMM;
132 double boundRectHeightMM;
136 boundRectWidthMM = mPictureWidth;
137 boundRectHeightMM = mPictureHeight;
150 int imageRectWidthPixels = mImage.
width();
151 int imageRectHeightPixels = mImage.
height();
152 imageRect = clippedImageRect( boundRectWidthMM, boundRectHeightMM,
153 QSize( imageRectWidthPixels, imageRectHeightPixels ) );
167 if ( mResizeMode ==
Zoom )
174 painter->
rotate( mPictureRotation );
175 painter->
translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
180 double diffX =
rect().
width() - boundRectWidthMM;
181 double diffY =
rect().
height() - boundRectHeightMM;
185 switch ( mPictureAnchor )
203 switch ( mPictureAnchor )
229 painter->
rotate( mPictureRotation );
230 painter->
translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
236 mSVG.
render( painter,
QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) );
238 else if ( mMode ==
RASTER )
240 painter->
drawImage(
QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, imageRect );
254 QRect QgsComposerPicture::clippedImageRect(
double &boundRectWidthMM,
double &boundRectHeightMM,
QSize imageRectPixels )
268 switch ( mPictureAnchor )
278 leftClip = ( imageRectPixels.
width() - boundRectWidthPixels ) / 2;
283 leftClip = imageRectPixels.
width() - boundRectWidthPixels;
288 switch ( mPictureAnchor )
298 topClip = ( imageRectPixels.
height() - boundRectHeightPixels ) / 2;
303 topClip = imageRectPixels.
height() - boundRectHeightPixels;
307 return QRect( leftClip, topClip, boundRectWidthPixels, boundRectHeightPixels );
322 evalContext = scopedContext.
data();
328 mHasExpressionError =
false;
339 mHasExpressionError =
true;
345 loadPicture( source );
348 void QgsComposerPicture::loadRemotePicture(
const QString &url )
356 connect( &fetcher, SIGNAL( finished() ),
this, SLOT( remotePictureLoaded() ) );
360 qApp->processEvents();
367 mImage = imageReader.
read();
377 void QgsComposerPicture::loadLocalPicture(
const QString &path )
390 if ( sourceFileSuffix.
compare(
"svg", Qt::CaseInsensitive ) == 0 )
395 mSVG.
load( svgContent );
412 if ( imageReader.read( &mImage ) )
425 void QgsComposerPicture::remotePictureLoaded()
430 void QgsComposerPicture::updateMapRotation()
439 switch ( mNorthMode )
463 rotation += mNorthOffset;
467 void QgsComposerPicture::loadPicture(
const QString &path )
472 loadRemotePicture( path );
477 loadLocalPicture( path );
483 else if ( mHasExpressionError || !( path.
isEmpty() ) )
487 QString badFile(
":/images/composer/missing_image.svg" );
488 mSVG.
load( badFile );
502 QRectF QgsComposerPicture::boundedImageRect(
double deviceWidth,
double deviceHeight )
504 double imageToDeviceRatio;
505 if ( mImage.
width() / deviceWidth > mImage.
height() / deviceHeight )
507 imageToDeviceRatio = deviceWidth / mImage.
width();
508 double height = imageToDeviceRatio * mImage.
height();
509 return QRectF( 0, 0, deviceWidth, height );
513 imageToDeviceRatio = deviceHeight / mImage.
height();
514 double width = imageToDeviceRatio * mImage.
width();
515 return QRectF( 0, 0, width, deviceHeight );
519 QRectF QgsComposerPicture::boundedSVGRect(
double deviceWidth,
double deviceHeight )
521 double imageToSvgRatio;
522 if ( deviceWidth / mDefaultSvgSize.
width() > deviceHeight / mDefaultSvgSize.
height() )
524 imageToSvgRatio = deviceHeight / mDefaultSvgSize.
height();
525 double width = mDefaultSvgSize.
width() * imageToSvgRatio;
526 return QRectF( 0, 0, width, deviceHeight );
530 imageToSvgRatio = deviceWidth / mDefaultSvgSize.
width();
531 double height = mDefaultSvgSize.
height() * imageToSvgRatio;
532 return QRectF( 0, 0, deviceWidth, height );
536 QSizeF QgsComposerPicture::pictureSize()
540 return mDefaultSvgSize;
542 else if ( mMode ==
RASTER )
553 QRectF QgsComposerPicture::boundedSVGRect(
double deviceWidth,
double deviceHeight )
555 double imageToSvgRatio;
556 if ( deviceWidth / mDefaultSvgSize.
width() < deviceHeight / mDefaultSvgSize.
height() )
558 imageToSvgRatio = deviceWidth / mDefaultSvgSize.
width();
559 double height = mDefaultSvgSize.
height() * imageToSvgRatio;
560 return QRectF( 0, 0, deviceWidth, height );
564 imageToSvgRatio = deviceHeight / mDefaultSvgSize.
height();
565 double width = mDefaultSvgSize.
width() * imageToSvgRatio;
566 return QRectF( 0, 0, width, deviceHeight );
573 QSizeF currentPictureSize = pictureSize();
578 mPictureWidth = rectangle.
width();
579 mPictureHeight = rectangle.
height();
583 QRectF newRect = rectangle;
590 targetImageSize = currentPictureSize;
596 tr.
rotate( mPictureRotation );
603 if ( qAbs(
rect().width() - rectangle.
width() ) <
615 if ( !( currentPictureSize.
isEmpty() ) )
626 mPictureWidth = rotatedImageRect.
width();
627 mPictureHeight = rotatedImageRect.
height();
631 mPictureWidth = newRect.
width();
632 mPictureHeight = newRect.
height();
639 if ( mMode ==
SVG && !mLoadingSvg )
655 double oldRotation = mPictureRotation;
656 mPictureRotation = r;
658 if ( mResizeMode ==
Zoom )
661 QSizeF currentPictureSize = pictureSize();
663 mPictureWidth = rotatedImageRect.
width();
664 mPictureHeight = rotatedImageRect.
height();
669 QSizeF currentPictureSize = pictureSize();
677 tr.rotate( mPictureRotation );
696 if ( composerMapId == -1 )
698 disconnect( mRotationMap, SIGNAL( mapRotationChanged(
double ) ),
this, SLOT( updateMapRotation() ) );
699 disconnect( mRotationMap, SIGNAL( extentChanged() ),
this, SLOT( updateMapRotation() ) );
700 mRotationMap =
nullptr;
710 disconnect( mRotationMap, SIGNAL( mapRotationChanged(
double ) ),
this, SLOT( updateMapRotation() ) );
711 disconnect( mRotationMap, SIGNAL( extentChanged() ),
this, SLOT( updateMapRotation() ) );
714 connect( map, SIGNAL( mapRotationChanged(
double ) ),
this, SLOT( updateMapRotation() ) );
715 connect( map, SIGNAL( extentChanged() ),
this, SLOT( updateMapRotation() ) );
747 evalContext = scopedContext.
data();
812 composerPictureElem.
setAttribute(
"northMode", mNorthMode );
813 composerPictureElem.
setAttribute(
"northOffset", mNorthOffset );
838 if ( !composerItemList.
isEmpty() )
851 mDefaultSvgSize =
QSize( 0, 0 );
858 bool expressionActive;
859 if ( useExpression.
compare(
"true", Qt::CaseInsensitive ) == 0 )
861 expressionActive =
true;
865 expressionActive =
false;
884 if ( rotationMapId == -1 )
886 mRotationMap =
nullptr;
893 disconnect( mRotationMap, SIGNAL( mapRotationChanged(
double ) ),
this, SLOT( updateMapRotation() ) );
894 disconnect( mRotationMap, SIGNAL( extentChanged() ),
this, SLOT( updateMapRotation() ) );
897 connect( mRotationMap, SIGNAL( mapRotationChanged(
double ) ),
this, SLOT( updateMapRotation() ) );
898 connect( mRotationMap, SIGNAL( extentChanged() ),
this, SLOT( updateMapRotation() ) );
915 return mRotationMap->
id();
927 mNorthOffset = offset;
933 mPictureAnchor = anchor;
939 mSvgFillColor = color;
945 mSvgBorderColor = color;
951 mSvgBorderWidth = width;
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height) const
Calculates corner point after rotation and scaling.
QDomNodeList elementsByTagName(const QString &tagname) const
void setActive(bool active)
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
Sets state from Dom document.
void setHeight(int height)
QgsPoint center() const
Center point of the rectangle.
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
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)
static QString encodeColor(const QColor &color)
int printResolution() const
void setSvgBorderWidth(double width)
Sets the border width used for parameterized SVG files.
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
Q_DECL_DEPRECATED QString pictureExpression() const
Returns the expression the item is using for the picture source.
void setFileName(const QString &name)
int id() const
Get identification number.
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height, double rotation) const
Calculates corner point after rotation and scaling.
virtual void drawFrame(QPainter *p)
Draw black frame around item.
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...
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
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)
Q_DECL_DEPRECATED QString pictureFile() const
Returns the path of the source image file.
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
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 setSvgFillColor(const QColor &color)
Sets the fill color used for parameterized SVG files.
void setHeight(qreal height)
Q_DECL_DEPRECATED double rotation() const
Returns the rotation used for drawing the picture within the composer item.
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
static QgsSvgCache * instance()
QDomElement toElement() const
int rotationMap() const
Returns the id of the rotation map.
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.
NorthMode
Method for syncing rotation to a map's North direction.
bool hasAttribute(const QString &name) const
HTTP network content fetcher.
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
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.
QString expressionString() const
Returns the expression string of this QgsDataDefined.
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
void setNorthMode(NorthMode mode)
Sets the mode used to align the picture to a map's North.
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)
bool dataDefinedEvaluate(const QgsComposerObject::DataDefinedProperty property, QVariant &expressionValue, const QgsExpressionContext &context=QgsExpressionContext()) const
Evaluate a data defined property and return the calculated value.
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...
void setNorthOffset(double offset)
Sets the offset added to the picture's rotation from a map's North.
virtual void setUsePictureExpression(bool useExpression)
Sets whether the picture should use an expression based image source path.
const QByteArray & svgContent(const QString &file, double size, const QColor &fill, const QColor &outline, double outlineWidth, double widthScaleFactor, double rasterScaleFactor)
Get SVG content.
Q_DECL_DEPRECATED bool usePictureExpression() const
Returns whether the picture item is using an expression for the image source.
virtual void setPictureExpression(const QString &expression)
Sets an expression to use for the picture source.
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
A class to represent a point.
Graphics scene for map printing.
void moveCenter(const QPointF &position)
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
Object representing map window.
void pictureRotationChanged(double newRotation)
Is emitted on picture rotation change.
QgsDataDefined * dataDefinedProperty(const DataDefinedProperty property) const
Returns a reference to the data defined settings for one of the item's data defined properties...
#define Q_NOWARN_DEPRECATED_POP
Q_DECL_DEPRECATED void setPictureFile(const QString &path)
Sets the source file of the image (may be svg or a raster format).
QgsComposition * mComposition
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, QFlags< Qt::ImageConversionFlag > flags)
void setSvgBorderColor(const QColor &color)
Sets the border color used for parameterized SVG files.
void setWidth(qreal width)
virtual void drawBackground(QPainter *p)
Draw background.
Mode mode() const
Returns the current picture mode (image format).
QString readPath(QString filename, const QString &relativeBasePath=QString()) const
Turn filename read from the project file to an absolute path.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Class for storing a coordinate reference system (CRS)
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()
static QColor decodeColor(const QString &str)
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).
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...
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)
double mapRotation(QgsComposerObject::PropertyValueType valueType=QgsComposerObject::EvaluatedValue) const
Returns the rotation used for drawing the map within the composer item.
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 compare(const QString &other) const
static double bearingTrueNorth(const QgsCoordinateReferenceSystem &crs, const QgsPoint &point)
Returns the direction to true north from a specified point and for a specified coordinate reference s...
Defines a qgis exception class.
void setExpressionString(const QString &expr)
Sets the expression for this QgsDataDefined.
const QgsRectangle * currentMapExtent() const
Returns a pointer to the current map extent, which is either the original user specified extent or th...
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
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 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.
QString picturePath() const
Returns the path of the source image.
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.