21 #include <QSvgRenderer>
29 , mMarkerMode( DefaultMarker )
30 , mArrowColor( QColor( 0, 0, 0 ) )
37 , mStartPoint( startPoint )
38 , mStopPoint( stopPoint )
39 , mMarkerMode( DefaultMarker )
40 , mArrowColor( QColor( 0, 0, 0 ) )
54 mPen.setColor( QColor( 0, 0, 0 ) );
58 setPen( QPen( QColor( 255, 255, 255, 0 ) ) );
59 setBrush( QBrush( QColor( 255, 255, 255, 0 ) ) );
64 Q_UNUSED( itemStyle );
75 arrowPen.setCapStyle( Qt::FlatCap );
77 painter->setPen( arrowPen );
101 double startPointXPos = (
mStartPoint.x() - pos().x() ) / rect().width();
102 double startPointYPos = (
mStartPoint.y() - pos().y() ) / rect().height();
103 double stopPointXPos = (
mStopPoint.x() - pos().x() ) / rect().width();
104 double stopPointYPos = (
mStopPoint.y() - pos().y() ) / rect().height();
106 mStartPoint.setX( rectangle.left() + startPointXPos * rectangle.width() );
107 mStartPoint.setY( rectangle.top() + startPointYPos * rectangle.height() );
108 mStopPoint.setX( rectangle.left() + stopPointXPos * rectangle.width() );
109 mStopPoint.setY( rectangle.top() + stopPointYPos * rectangle.height() );
117 QBrush arrowBrush = p->brush();
119 p->setBrush( arrowBrush );
125 Q_UNUSED( markerPath );
128 double arrowHeadHeight;
139 int dpi = ( p->device()->logicalDpiX() + p->device()->logicalDpiY() ) / 2;
142 int imageHeight = arrowHeadHeight / 25.4 * dpi;
147 imageWidth *= qMin( viewScaleFactor, 10.0 );
148 imageHeight *= qMin( viewScaleFactor, 10.0 );
150 QImage markerImage( imageWidth, imageHeight, QImage::Format_ARGB32 );
151 QColor markerBG( 255, 255, 255, 0 );
152 markerImage.fill( markerBG.rgba() );
154 QPointF imageFixPoint;
165 imageFixPoint.setY( 0 );
189 fixPoint.setX( 0 ); fixPoint.setY( arrowHeadHeight / 2.0 );
193 fixPoint.setX( 0 ); fixPoint.setY( -arrowHeadHeight / 2.0 );
195 QPointF rotatedFixPoint;
196 double angleRad = ang / 180 *
M_PI;
197 rotatedFixPoint.setX( fixPoint.x() * cos( angleRad ) + fixPoint.y() * -sin( angleRad ) );
198 rotatedFixPoint.setY( fixPoint.x() * sin( angleRad ) + fixPoint.y() * cos( angleRad ) );
201 QPainter imagePainter( &markerImage );
202 r.render( &imagePainter );
205 p->translate( canvasPoint.x() - rotatedFixPoint.x() , canvasPoint.y() - rotatedFixPoint.y() );
209 p->drawImage( QRectF( 0, 0,
mArrowHeadWidth, arrowHeadHeight ), markerImage, QRectF( 0, 0, imageWidth, imageHeight ) );
218 if ( !r.load( svgPath ) )
226 QRect viewBox = r.viewBox();
234 if ( !r.load( svgPath ) )
242 QRect viewBox = r.viewBox();
249 mPen.setWidthF( width );
273 enlarge =
mPen.widthF() / 2.0;
281 rect.adjust( -enlarge, -enlarge, enlarge, enlarge );
287 QDomElement composerArrowElem = doc.createElement(
"ComposerArrow" );
288 composerArrowElem.setAttribute(
"outlineWidth", QString::number(
outlineWidth() ) );
289 composerArrowElem.setAttribute(
"arrowHeadWidth", QString::number(
mArrowHeadWidth ) );
290 composerArrowElem.setAttribute(
"markerMode",
mMarkerMode );
292 composerArrowElem.setAttribute(
"endMarkerFile",
mEndMarkerFile );
295 QDomElement arrowColorElem = doc.createElement(
"ArrowColor" );
296 arrowColorElem.setAttribute(
"red",
mArrowColor.red() );
297 arrowColorElem.setAttribute(
"green",
mArrowColor.green() );
298 arrowColorElem.setAttribute(
"blue",
mArrowColor.blue() );
299 arrowColorElem.setAttribute(
"alpha",
mArrowColor.alpha() );
300 composerArrowElem.appendChild( arrowColorElem );
303 QDomElement startPointElem = doc.createElement(
"StartPoint" );
304 startPointElem.setAttribute(
"x", QString::number(
mStartPoint.x() ) );
305 startPointElem.setAttribute(
"y", QString::number(
mStartPoint.y() ) );
306 composerArrowElem.appendChild( startPointElem );
309 QDomElement stopPointElem = doc.createElement(
"StopPoint" );
310 stopPointElem.setAttribute(
"x", QString::number(
mStopPoint.x() ) );
311 stopPointElem.setAttribute(
"y", QString::number(
mStopPoint.y() ) );
312 composerArrowElem.appendChild( stopPointElem );
314 elem.appendChild( composerArrowElem );
315 return _writeXML( composerArrowElem, doc );
320 mArrowHeadWidth = itemElem.attribute(
"arrowHeadWidth",
"2.0" ).toDouble();
321 mPen.setWidthF( itemElem.attribute(
"outlineWidth",
"1.0" ).toDouble() );
323 setEndMarker( itemElem.attribute(
"endMarkerFile",
"" ) );
327 QDomNodeList arrowColorList = itemElem.elementsByTagName(
"ArrowColor" );
328 if ( arrowColorList.size() > 0 )
330 QDomElement arrowColorElem = arrowColorList.at( 0 ).toElement();
331 int red = arrowColorElem.attribute(
"red",
"0" ).toInt();
332 int green = arrowColorElem.attribute(
"green",
"0" ).toInt();
333 int blue = arrowColorElem.attribute(
"blue",
"0" ).toInt();
334 int alpha = arrowColorElem.attribute(
"alpha",
"255" ).toInt();
340 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
341 if ( composerItemList.size() > 0 )
343 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
348 QDomNodeList startPointList = itemElem.elementsByTagName(
"StartPoint" );
349 if ( startPointList.size() > 0 )
351 QDomElement startPointElem = startPointList.at( 0 ).toElement();
352 mStartPoint.setX( startPointElem.attribute(
"x",
"0.0" ).toDouble() );
353 mStartPoint.setY( startPointElem.attribute(
"y",
"0.0" ).toDouble() );
357 QDomNodeList stopPointList = itemElem.elementsByTagName(
"StopPoint" );
358 if ( stopPointList.size() > 0 )
360 QDomElement stopPointElem = stopPointList.at( 0 ).toElement();
361 mStopPoint.setX( stopPointElem.attribute(
"x",
"0.0" ).toDouble() );
362 mStopPoint.setY( stopPointElem.attribute(
"y",
"0.0" ).toDouble() );
QgsComposerArrow(QgsComposition *c)
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.
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.
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