28 , mMapPositionFixed( true )
29 , mOffsetFromReferencePoint( QPointF( 50, -50 ) )
30 , mBalloonSegment( -1 )
32 setFlag( QGraphicsItem::ItemIsSelectable,
true );
37 setData( 0,
"AnnotationItem" );
103 return QSizeF( 0, 0 );
108 prepareGeometryChange();
109 double halfSymbolSize = 0.0;
119 mBoundingRect = QRectF( xMinPos, yMinPos, xMaxPos - xMinPos, yMaxPos - yMinPos );
134 QList<QLineF> segmentList;
138 double minEdgeDist = DBL_MAX;
139 int minEdgeIndex = -1;
144 for (
int i = 0; i < 4; ++i )
146 QLineF currentSegment = segmentList.at( i );
148 double currentMinDist = origin.
sqrDistToSegment( currentSegment.x1(), currentSegment.y1(), currentSegment.x2(), currentSegment.y2(), currentMinDistPoint );
149 if ( currentMinDist < minEdgeDist )
152 minEdgePoint = currentMinDistPoint;
153 minEdgeDist = currentMinDist;
154 minEdge = currentSegment;
158 if ( minEdgeIndex < 0 )
164 double segmentPointWidth = 10;
167 QPointF minEdgeEnd = minEdge.p2();
169 if ( sqrt( minEdgePoint.
sqrDist( minEdgeEnd.x(), minEdgeEnd.y() ) ) < segmentPointWidth )
182 p->setPen( framePen );
184 p->setBrush( frameBrush );
185 p->setRenderHint( QPainter::Antialiasing,
true );
188 for (
int i = 0; i < 4; ++i )
190 QLineF currentSegment =
segment( i );
191 poly << currentSegment.p1();
195 poly << QPointF( 0, 0 );
198 poly << currentSegment.p2();
200 p->drawPolygon( poly );
240 if ( data( 1 ).toString() ==
"composer" )
245 double handlerSize = 10;
246 p->setPen( Qt::NoPen );
247 p->setBrush( QColor( 200, 200, 210, 120 ) );
277 double dx = directionPoint.x() - startPoint.x();
278 double dy = directionPoint.y() - startPoint.y();
279 double length = sqrt( dx * dx + dy * dy );
280 double scaleFactor = distance / length;
281 return QPointF( startPoint.x() + dx * scaleFactor, startPoint.y() + dy * scaleFactor );
286 QPointF itemPos = mapFromScene( pos );
288 int cursorSensitivity = 7;
290 if ( qAbs( itemPos.x() ) < cursorSensitivity && qAbs( itemPos.y() ) < cursorSensitivity )
295 bool left, right, up, down;
305 else if ( right && up )
309 else if ( left && down )
313 else if ( right && down )
345 switch ( moveAction )
348 return Qt::ArrowCursor;
351 return Qt::SizeAllCursor;
354 return Qt::SizeVerCursor;
357 return Qt::SizeHorCursor;
360 return Qt::SizeFDiagCursor;
363 return Qt::SizeBDiagCursor;
365 return Qt::ArrowCursor;
381 double dpmm =
mMapCanvas->logicalDpiX() / 25.4;
387 if ( itemElem.isNull() )
391 QDomElement annotationElem = doc.createElement(
"AnnotationItem" );
397 annotationElem.setAttribute(
"frameWidth", QString::number(
mFrameSize.width() ) );
398 annotationElem.setAttribute(
"frameHeight", QString::number(
mFrameSize.height() ) );
399 QPointF canvasPos = pos();
402 annotationElem.setAttribute(
"frameBorderWidth", QString::number(
mFrameBorderWidth ) );
403 annotationElem.setAttribute(
"frameColor",
mFrameColor.name() );
404 annotationElem.setAttribute(
"frameColorAlpha",
mFrameColor.alpha() );
407 annotationElem.setAttribute(
"visible", isVisible() );
411 if ( !symbolElem.isNull() )
413 annotationElem.appendChild( symbolElem );
416 itemElem.appendChild( annotationElem );
422 if ( annotationElem.isNull() )
427 pos.setX( annotationElem.attribute(
"canvasPosX",
"0" ).toDouble() );
428 pos.setY( annotationElem.attribute(
"canvasPosY",
"0" ).toDouble() );
431 mapPos.
setX( annotationElem.attribute(
"mapPosX",
"0" ).toDouble() );
432 mapPos.
setY( annotationElem.attribute(
"mapPosY",
"0" ).toDouble() );
434 mFrameBorderWidth = annotationElem.attribute(
"frameBorderWidth",
"0.5" ).toDouble();
435 mFrameColor.setNamedColor( annotationElem.attribute(
"frameColor",
"#000000" ) );
436 mFrameColor.setAlpha( annotationElem.attribute(
"frameColorAlpha",
"255" ).toInt() );
438 mFrameBackgroundColor.setAlpha( annotationElem.attribute(
"frameBackgroundColorAlpha",
"255" ).toInt() );
439 mFrameSize.setWidth( annotationElem.attribute(
"frameWidth",
"50" ).toDouble() );
440 mFrameSize.setHeight( annotationElem.attribute(
"frameHeight",
"50" ).toDouble() );
444 setVisible( annotationElem.attribute(
"visible",
"1" ).toInt() );
447 QDomElement symbolElem = annotationElem.firstChildElement(
"symbol" );
448 if ( !symbolElem.isNull() )
450 QgsMarkerSymbolV2* symbol = QgsSymbolLayerV2Utils::loadSymbol<QgsMarkerSymbolV2>( symbolElem );
virtual QSizeF minimumFrameSize() const
QgsMarkerSymbolV2 * mMarkerSymbol
Point symbol that is to be drawn at the map reference location.
QgsPoint toMapCoordinates(const QPoint &point) const
transformation from screen coordinates to map coordinates
void _readXML(const QDomDocument &doc, const QDomElement &annotationElem)
double mFrameBorderWidth
Width of the frame.
QgsAnnotationItem::MouseMoveAction moveActionForPosition(const QPointF &pos) const
Returns the mouse move behaviour for a given position.
QPointF toCanvasCoordinates(const QgsPoint &point) const
transformation from map coordinates to screen coordinates
QPointF mOffsetFromReferencePoint
Describes the shift of the item content box to the reference point.
An abstract class for items that can be placed on the map canvas.
double sqrDist(double x, double y) const
Returns the squared distance between this point and x,y.
virtual ~QgsAnnotationItem()
Map canvas is a class for displaying all GIS data types on a canvas.
void drawSelectionBoxes(QPainter *p)
void updatePosition() override
called on changed extent or resize event to update position of the item
void setMarkerSymbol(QgsMarkerSymbolV2 *symbol)
Set symbol that is drawn on map position.
QRectF mBoundingRect
Bounding rect (including item frame and balloon)
static QDomElement saveSymbol(QString symbolName, QgsSymbolV2 *symbol, QDomDocument &doc)
QPointF mBalloonSegmentPoint1
First segment point for drawing the connection (ccw direction)
virtual void setMapPosition(const QgsPoint &pos)
void startRender(QgsRenderContext &context, const QgsFields *fields=0)
QgsAnnotationItem(QgsMapCanvas *mapCanvas)
double sqrDistToSegment(double x1, double y1, double x2, double y2, QgsPoint &minDistPoint, double epsilon=DEFAULT_SEGMENT_EPSILON) const
Returns the minimum distance between this point and a segment.
QPointF pointOnLineWithDistance(const QPointF &startPoint, const QPointF &directionPoint, double distance) const
Returns a point on the line from startPoint to directionPoint that is a certain distance away from th...
Qt::CursorShape cursorShapeForAction(MouseMoveAction moveAction) const
Returns suitable cursor shape for mouse move action.
void drawMarkerSymbol(QPainter *p)
void renderPoint(const QPointF &point, const QgsFeature *f, QgsRenderContext &context, int layer=-1, bool selected=false)
int mBalloonSegment
Segment number where the connection to the map point is attached.
QRectF boundingRect() const override
default implementation for canvas items
A class to represent a point.
QString qgsDoubleToString(const double &a, const int &precision=17)
double scaledSymbolSize() const
Returns the symbol size scaled in (mapcanvas) pixels.
bool mMapPositionFixed
True: the item stays at the same map position, False: the item stays on same screen position...
QColor mFrameColor
Frame / balloon color.
Contains information about the context of a rendering operation.
void stopRender(QgsRenderContext &context)
void _writeXML(QDomDocument &doc, QDomElement &itemElem) const
void updateBalloon()
Check where to attach the balloon connection between frame and map point.
void setOffsetFromReferencePoint(const QPointF &offset)
QgsMapCanvas * mMapCanvas
pointer to map canvas
void setFrameSize(const QSizeF &size)
void drawFrame(QPainter *p)
void updateBoundingRect()
QColor mFrameBackgroundColor
QPointF mBalloonSegmentPoint2
Second segment point for drawing the balloon connection (ccw direction)
QgsPoint mMapPosition
Map position (in case mMapPositionFixed is true)
QSizeF mFrameSize
Size of the frame (without balloon)
void setMapPositionFixed(bool fixed)
bool setRenderContextVariables(QPainter *p, QgsRenderContext &context) const
Sets render context parameters.
QLineF segment(int index)
Returns frame width in painter units.