21 #include <QSvgRenderer>
32 , mMarkerMode( DefaultMarker )
33 , mArrowColor( QColor( 0, 0, 0 ) )
34 , mBoundsBehaviour( 24 )
41 , mStartPoint( startPoint )
42 , mStopPoint( stopPoint )
43 , mMarkerMode( DefaultMarker )
44 , mArrowColor( QColor( 0, 0, 0 ) )
45 , mBoundsBehaviour( 24 )
61 mPen.setColor( QColor( 0, 0, 0 ) );
65 setPen( QPen( QColor( 255, 255, 255, 0 ) ) );
66 setBrush( QBrush( QColor( 255, 255, 255, 0 ) ) );
71 Q_UNUSED( itemStyle );
85 arrowPen.setCapStyle( Qt::FlatCap );
88 painter->setPen( arrowPen );
111 if ( rectangle.width() < 0 )
115 if ( rectangle.height() < 0 )
123 QRectF rect = rectangle.united( QRectF( rectangle.x(), rectangle.y(), 2. * margin, 2. * margin ) );
126 double x[2] = {rect.x(), rect.x() + rect.width()};
127 double y[2] = {rect.y(), rect.y() + rect.height()};
141 QBrush arrowBrush = p->brush();
143 p->setBrush( arrowBrush );
159 Q_UNUSED( markerPath );
162 double arrowHeadHeight;
173 int dpi = ( p->device()->logicalDpiX() + p->device()->logicalDpiY() ) / 2;
176 int imageHeight = arrowHeadHeight / 25.4 * dpi;
181 imageWidth *= qMin( viewScaleFactor, 10.0 );
182 imageHeight *= qMin( viewScaleFactor, 10.0 );
184 QImage markerImage( imageWidth, imageHeight, QImage::Format_ARGB32 );
185 QColor markerBG( 255, 255, 255, 0 );
186 markerImage.fill( markerBG.rgba() );
188 QPointF imageFixPoint;
199 imageFixPoint.setY( 0 );
219 QPainter imagePainter( &markerImage );
220 r.render( &imagePainter );
230 fixPoint.setX( 0 ); fixPoint.setY( arrowHeadHeight / 2.0 );
234 fixPoint.setX( 0 ); fixPoint.setY( -arrowHeadHeight / 2.0 );
236 QPointF rotatedFixPoint;
237 double angleRad = ang / 180 *
M_PI;
238 rotatedFixPoint.setX( fixPoint.x() * cos( angleRad ) + fixPoint.y() * -sin( angleRad ) );
239 rotatedFixPoint.setY( fixPoint.x() * sin( angleRad ) + fixPoint.y() * cos( angleRad ) );
240 p->translate( canvasPoint.x() - rotatedFixPoint.x() , canvasPoint.y() - rotatedFixPoint.y() );
244 p->translate( canvasPoint.x() , canvasPoint.y() );
250 p->drawImage( QRectF( 0, 0,
mArrowHeadWidth, arrowHeadHeight ), markerImage, QRectF( 0, 0, imageWidth, imageHeight ) );
259 if ( svgPath.isEmpty() || !r.load( svgPath ) )
267 QRect viewBox = r.viewBox();
275 if ( svgPath.isEmpty() || !r.load( svgPath ) )
283 QRect viewBox = r.viewBox();
290 mPen.setWidthF( width );
315 margin =
mPen.widthF() / 2.0;
331 margin =
mPen.widthF() / std::sqrt( 2.0 );
337 double markerMargin = qMax( startMarkerMargin, stopMarkerMargin );
338 margin = qMax(
mPen.widthF() / std::sqrt( 2.0 ), markerMargin );
350 rect.adjust( -enlarge, -enlarge, enlarge, enlarge );
362 QDomElement composerArrowElem = doc.createElement(
"ComposerArrow" );
363 composerArrowElem.setAttribute(
"outlineWidth", QString::number(
outlineWidth() ) );
364 composerArrowElem.setAttribute(
"arrowHeadWidth", QString::number(
mArrowHeadWidth ) );
365 composerArrowElem.setAttribute(
"markerMode",
mMarkerMode );
367 composerArrowElem.setAttribute(
"endMarkerFile",
mEndMarkerFile );
368 composerArrowElem.setAttribute(
"boundsBehaviourVersion", QString::number(
mBoundsBehaviour ) );
371 QDomElement arrowColorElem = doc.createElement(
"ArrowColor" );
372 arrowColorElem.setAttribute(
"red",
mArrowColor.red() );
373 arrowColorElem.setAttribute(
"green",
mArrowColor.green() );
374 arrowColorElem.setAttribute(
"blue",
mArrowColor.blue() );
375 arrowColorElem.setAttribute(
"alpha",
mArrowColor.alpha() );
376 composerArrowElem.appendChild( arrowColorElem );
379 QDomElement startPointElem = doc.createElement(
"StartPoint" );
380 startPointElem.setAttribute(
"x", QString::number(
mStartPoint.x() ) );
381 startPointElem.setAttribute(
"y", QString::number(
mStartPoint.y() ) );
382 composerArrowElem.appendChild( startPointElem );
385 QDomElement stopPointElem = doc.createElement(
"StopPoint" );
386 stopPointElem.setAttribute(
"x", QString::number(
mStopPoint.x() ) );
387 stopPointElem.setAttribute(
"y", QString::number(
mStopPoint.y() ) );
388 composerArrowElem.appendChild( stopPointElem );
390 elem.appendChild( composerArrowElem );
391 return _writeXML( composerArrowElem, doc );
396 mArrowHeadWidth = itemElem.attribute(
"arrowHeadWidth",
"2.0" ).toDouble();
397 mPen.setWidthF( itemElem.attribute(
"outlineWidth",
"1.0" ).toDouble() );
399 setEndMarker( itemElem.attribute(
"endMarkerFile",
"" ) );
402 mBoundsBehaviour = itemElem.attribute(
"boundsBehaviourVersion",
"22" ).toInt();
405 QDomNodeList arrowColorList = itemElem.elementsByTagName(
"ArrowColor" );
406 if ( arrowColorList.size() > 0 )
408 QDomElement arrowColorElem = arrowColorList.at( 0 ).toElement();
409 int red = arrowColorElem.attribute(
"red",
"0" ).toInt();
410 int green = arrowColorElem.attribute(
"green",
"0" ).toInt();
411 int blue = arrowColorElem.attribute(
"blue",
"0" ).toInt();
412 int alpha = arrowColorElem.attribute(
"alpha",
"255" ).toInt();
418 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
419 if ( composerItemList.size() > 0 )
421 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
426 QDomNodeList startPointList = itemElem.elementsByTagName(
"StartPoint" );
427 if ( startPointList.size() > 0 )
429 QDomElement startPointElem = startPointList.at( 0 ).toElement();
430 mStartPoint.setX( startPointElem.attribute(
"x",
"0.0" ).toDouble() );
431 mStartPoint.setY( startPointElem.attribute(
"y",
"0.0" ).toDouble() );
435 QDomNodeList stopPointList = itemElem.elementsByTagName(
"StopPoint" );
436 if ( stopPointList.size() > 0 )
438 QDomElement stopPointElem = stopPointList.at( 0 ).toElement();
439 mStopPoint.setX( stopPointElem.attribute(
"x",
"0.0" ).toDouble() );
440 mStopPoint.setY( stopPointElem.attribute(
"y",
"0.0" ).toDouble() );
QgsComposerArrow(QgsComposition *c)
int mStartXIdx
Considering the rectangle as spanning [x[0], x[1]] x [y[0], y[1]], these indices specify which index ...
void setOutlineWidth(double width)
A item that forms part of a map composition.
void setStartMarker(const QString &svgPath)
virtual void drawFrame(QPainter *p)
Draw black frame around item.
QString mStartMarkerFile
Path to the start marker file.
void setArrowHeadWidth(double width)
Sets the width of the arrow head in mm.
void adaptItemSceneRect()
Adapts the item scene rect to contain the start point, the stop point including the arrow marker and ...
double outlineWidth() const
void itemChanged()
Used e.g.
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
double horizontalViewScaleFactor() const
Returns the zoom factor of the graphics view.
MarkerMode mMarkerMode
Default marker, no marker or svg marker.
virtual void drawSelectionBoxes(QPainter *p)
Draw selection boxes around item.
double computeMarkerMargin() const
Computes the margin around the line necessary to include the markers.
void drawSVGMarker(QPainter *p, MarkerType type, const QString &markerPath)
Draws a user-defined marker (must be an svg file)
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget)
Reimplementation of QCanvasItem::paint - draw on canvas.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
sets state from Dom document
void drawHardcodedMarker(QPainter *p, MarkerType type)
Draws the default marker at the line end.
void setMarkerMode(MarkerMode mode)
int mBoundsBehaviour
Indicates QGIS version to mimic bounding box behaviour for.
QgsComposition * mComposition
Graphics scene for map printing.
void setSceneRect(const QRectF &rectangle)
Modifies position of start and endpoint and calls QgsComposerItem::setSceneRect.
double mArrowHeadWidth
Width of the arrow marker in mm.
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
QString mEndMarkerFile
Path to the end marker file.
virtual void drawBackground(QPainter *p)
Draw background.
void initGraphicsSettings()
Apply default graphics settings.
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 angle(const QPointF &p1, const QPointF &p2) const
Returns angle of the line from p1 to p2 (clockwise, starting at N)
void drawArrowHead(QPainter *p, double x, double y, double angle, double arrowHeadWidth) const
Draws arrowhead.
QgsComposition::PlotStyle plotStyle() const
double mStartArrowHeadHeight
Height of the arrow marker in mm.
void setEndMarker(const QString &svgPath)
double mStopArrowHeadHeight
bool writeXML(QDomElement &elem, QDomDocument &doc) const
stores state in Dom element