31 #include <QDomDocument> 32 #include <QDomElement> 34 #include <QImageReader> 36 #include <QSvgRenderer> 37 #include <QNetworkRequest> 38 #include <QNetworkReply> 40 #include <QCoreApplication> 45 , mPictureRotation( 0 )
46 , mRotationMap( nullptr )
48 , mPictureAnchor( UpperLeft )
49 , mSvgFillColor(
QColor( 255, 255, 255 ) )
50 , mSvgBorderColor(
QColor( 0, 0, 0 ) )
51 , mSvgBorderWidth( 0.2 )
52 , mHasExpressionError( false )
53 , mLoadingSvg( false )
62 , mPictureRotation( 0 )
63 , mRotationMap(
nullptr )
66 , mSvgFillColor(
QColor( 255, 255, 255 ) )
67 , mSvgBorderColor(
QColor( 0, 0, 0 ) )
68 , mSvgBorderWidth( 0.2 )
69 , mHasExpressionError(
false )
70 , mLoadingSvg(
false )
76 void QgsComposerPicture::init()
105 Q_UNUSED( itemStyle );
123 double boundRectWidthMM;
124 double boundRectHeightMM;
128 boundRectWidthMM = mPictureWidth;
129 boundRectHeightMM = mPictureHeight;
142 int imageRectWidthPixels = mImage.
width();
143 int imageRectHeightPixels = mImage.
height();
144 imageRect = clippedImageRect( boundRectWidthMM, boundRectHeightMM,
145 QSize( imageRectWidthPixels, imageRectHeightPixels ) );
159 if ( mResizeMode ==
Zoom )
166 painter->
rotate( mPictureRotation );
167 painter->
translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
172 double diffX =
rect().
width() - boundRectWidthMM;
173 double diffY =
rect().
height() - boundRectHeightMM;
177 switch ( mPictureAnchor )
195 switch ( mPictureAnchor )
221 painter->
rotate( mPictureRotation );
222 painter->
translate( -boundRectWidthMM / 2.0, -boundRectHeightMM / 2.0 );
228 mSVG.
render( painter,
QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ) );
230 else if ( mMode ==
RASTER )
232 painter->
drawImage(
QRectF( 0, 0, boundRectWidthMM, boundRectHeightMM ), mImage, imageRect );
246 QRect QgsComposerPicture::clippedImageRect(
double &boundRectWidthMM,
double &boundRectHeightMM,
QSize imageRectPixels )
260 switch ( mPictureAnchor )
270 leftClip = ( imageRectPixels.
width() - boundRectWidthPixels ) / 2;
275 leftClip = imageRectPixels.
width() - boundRectWidthPixels;
280 switch ( mPictureAnchor )
290 topClip = ( imageRectPixels.
height() - boundRectHeightPixels ) / 2;
295 topClip = imageRectPixels.
height() - boundRectHeightPixels;
299 return QRect( leftClip, topClip, boundRectWidthPixels, boundRectHeightPixels );
314 evalContext = scopedContext.
data();
320 mHasExpressionError =
false;
331 mHasExpressionError =
true;
337 loadPicture( source );
340 void QgsComposerPicture::loadRemotePicture(
const QString &url )
348 connect( &fetcher, SIGNAL( finished() ),
this, SLOT( remotePictureLoaded() ) );
352 qApp->processEvents();
359 mImage = imageReader.
read();
369 void QgsComposerPicture::loadLocalPicture(
const QString &path )
382 if ( sourceFileSuffix.
compare(
"svg", Qt::CaseInsensitive ) == 0 )
387 mSVG.
load( svgContent );
404 if ( imageReader.read( &mImage ) )
417 void QgsComposerPicture::remotePictureLoaded()
422 void QgsComposerPicture::loadPicture(
const QString &path )
427 loadRemotePicture( path );
432 loadLocalPicture( path );
438 else if ( mHasExpressionError || !( path.
isEmpty() ) )
442 QString badFile(
":/images/composer/missing_image.svg" );
443 mSVG.
load( badFile );
457 QRectF QgsComposerPicture::boundedImageRect(
double deviceWidth,
double deviceHeight )
459 double imageToDeviceRatio;
460 if ( mImage.
width() / deviceWidth > mImage.
height() / deviceHeight )
462 imageToDeviceRatio = deviceWidth / mImage.
width();
463 double height = imageToDeviceRatio * mImage.
height();
464 return QRectF( 0, 0, deviceWidth, height );
468 imageToDeviceRatio = deviceHeight / mImage.
height();
469 double width = imageToDeviceRatio * mImage.
width();
470 return QRectF( 0, 0, width, deviceHeight );
474 QRectF QgsComposerPicture::boundedSVGRect(
double deviceWidth,
double deviceHeight )
476 double imageToSvgRatio;
477 if ( deviceWidth / mDefaultSvgSize.
width() > deviceHeight / mDefaultSvgSize.
height() )
479 imageToSvgRatio = deviceHeight / mDefaultSvgSize.
height();
480 double width = mDefaultSvgSize.
width() * imageToSvgRatio;
481 return QRectF( 0, 0, width, deviceHeight );
485 imageToSvgRatio = deviceWidth / mDefaultSvgSize.
width();
486 double height = mDefaultSvgSize.
height() * imageToSvgRatio;
487 return QRectF( 0, 0, deviceWidth, height );
491 QSizeF QgsComposerPicture::pictureSize()
495 return mDefaultSvgSize;
497 else if ( mMode ==
RASTER )
508 QRectF QgsComposerPicture::boundedSVGRect(
double deviceWidth,
double deviceHeight )
510 double imageToSvgRatio;
511 if ( deviceWidth / mDefaultSvgSize.
width() < deviceHeight / mDefaultSvgSize.
height() )
513 imageToSvgRatio = deviceWidth / mDefaultSvgSize.
width();
514 double height = mDefaultSvgSize.
height() * imageToSvgRatio;
515 return QRectF( 0, 0, deviceWidth, height );
519 imageToSvgRatio = deviceHeight / mDefaultSvgSize.
height();
520 double width = mDefaultSvgSize.
width() * imageToSvgRatio;
521 return QRectF( 0, 0, width, deviceHeight );
528 QSizeF currentPictureSize = pictureSize();
533 mPictureWidth = rectangle.
width();
534 mPictureHeight = rectangle.
height();
538 QRectF newRect = rectangle;
545 targetImageSize = currentPictureSize;
551 tr.
rotate( mPictureRotation );
558 if ( qAbs(
rect().width() - rectangle.
width() ) <
570 if ( !( currentPictureSize.
isEmpty() ) )
581 mPictureWidth = rotatedImageRect.
width();
582 mPictureHeight = rotatedImageRect.
height();
586 mPictureWidth = newRect.
width();
587 mPictureHeight = newRect.
height();
594 if ( mMode ==
SVG && !mLoadingSvg )
610 double oldRotation = mPictureRotation;
611 mPictureRotation = r;
613 if ( mResizeMode ==
Zoom )
616 QSizeF currentPictureSize = pictureSize();
618 mPictureWidth = rotatedImageRect.
width();
619 mPictureHeight = rotatedImageRect.
height();
624 QSizeF currentPictureSize = pictureSize();
632 tr.rotate( mPictureRotation );
651 if ( composerMapId == -1 )
654 mRotationMap =
nullptr;
699 evalContext = scopedContext.
data();
788 if ( !composerItemList.
isEmpty() )
801 mDefaultSvgSize =
QSize( 0, 0 );
808 bool expressionActive;
809 if ( useExpression.
compare(
"true", Qt::CaseInsensitive ) == 0 )
811 expressionActive =
true;
815 expressionActive =
false;
831 if ( rotationMapId == -1 )
833 mRotationMap =
nullptr;
860 return mRotationMap->
id();
866 mPictureAnchor = anchor;
872 mSvgFillColor = color;
878 mSvgBorderColor = color;
884 mSvgBorderWidth = width;
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)
static QString encodeColor(const QColor &color)
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).
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
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 setSvgFillColor(const QColor &color)
Sets the fill color used for parameterized SVG files.
void setHeight(qreal height)
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
static QgsSvgCache * instance()
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.
const QByteArray & svgContent(const QString &file, double size, const QColor &fill, const QColor &outline, double outlineWidth, double widthScaleFactor, double rasterScaleFactor)
Get SVG content.
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)
void setSvgBorderColor(const QColor &color)
Sets the border color used for parameterized SVG files.
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()
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).
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.