Skip to content
Permalink
Browse files

Use Qt methods for translating node coords to scene coords

  • Loading branch information
nyalldawson committed Apr 1, 2016
1 parent 4efbb8f commit 90b6f46bf1295ab0932eec5e2a60d881a0541524
@@ -39,7 +39,7 @@ class QgsComposerNodesItem: QgsComposerItem
* limited in space.
* @param radius is only used if searchInRadius is true
*/
int nodeAtPosition( const QPointF &node, const bool searchInRadius = true, const double radius = 10 );
int nodeAtPosition( QPointF node, const bool searchInRadius = true, const double radius = 10 );

/** Gets the position of a node in scene coordinate.
* @param index of the node
@@ -103,9 +103,6 @@ class QgsComposerNodesItem: QgsComposerItem
/** Compute an euclidian distance between 2 nodes. */
double computeDistance(const QPointF &pt1, const QPointF &pt2) const;

/** Convert scene coordinate to item coordinates */
QPointF convertToItemCoordinate(const QPointF &node);

/** Update the current scene rectangle for this item. */
void updateSceneRect();
};
@@ -60,7 +60,7 @@ bool QgsComposerNodesItem::addNode( const QPointF &pt,
const bool checkArea,
const double radius )
{
const QPointF start = convertToItemCoordinate( pt );
const QPointF start = mapFromScene( pt );
double minDistance = std::numeric_limits<double>::max();
double maxDistance = ( checkArea ) ? radius : minDistance;
bool rc = false;
@@ -127,13 +127,6 @@ bool QgsComposerNodesItem::addNode( const QPointF &pt,
return rc;
}

QPointF QgsComposerNodesItem::convertToItemCoordinate( QPointF node )
{
QTransform transform = QTransform().rotate( -mItemRotation );
node -= scenePos();
return transform.map( node );
}

void QgsComposerNodesItem::drawNodes( QPainter *painter ) const
{
double rectSize = 3.0 / horizontalViewScaleFactor();
@@ -223,11 +216,11 @@ void QgsComposerNodesItem::paint( QPainter* painter,
painter->restore();
}

int QgsComposerNodesItem::nodeAtPosition( const QPointF &node,
int QgsComposerNodesItem::nodeAtPosition( QPointF node,
const bool searchInRadius,
const double radius )
{
const QPointF pt = convertToItemCoordinate( node );
const QPointF pt = mapFromScene( node );
double nearestDistance = std::numeric_limits<double>::max();
double maxDistance = ( searchInRadius ) ? radius : nearestDistance;
double distance = 0;
@@ -253,18 +246,7 @@ bool QgsComposerNodesItem::nodePosition( const int index, QPointF &position )

if ( index >= 0 && index < mPolygon.size() )
{
// get position in item coordinate
position = mPolygon.at( index );

// transform in scene coordinate
const double rotRad = mItemRotation * M_PI / 180.;
const double hypo = sqrt( pow( position.x(), 2 ) + pow( position.y(), 2 ) );
const double betaRad = acos( position.x() / hypo );
const double gammaRad = rotRad + betaRad;

position.setX( cos( gammaRad ) * hypo + scenePos().x() );
position.setY( sin( gammaRad ) * hypo + scenePos().y() );

position = mapToScene( mPolygon.at( index ) );
rc = true;
}

@@ -303,7 +285,7 @@ bool QgsComposerNodesItem::moveNode( const int index, const QPointF &pt )

if ( index >= 0 && index < mPolygon.size() )
{
QPointF nodeItem = convertToItemCoordinate( pt );
QPointF nodeItem = mapFromScene( pt );
mPolygon.replace( index, nodeItem );
updateSceneRect();

@@ -382,15 +364,8 @@ void QgsComposerNodesItem::updateSceneRect()
// set the new scene rectangle
const QRectF br = mPolygon.boundingRect();

const QPointF topLeft = br.topLeft();

const double angle = mItemRotation * M_PI / 180.;
const double member = topLeft.x() - tan( angle ) * topLeft.y();
const double newTopLeftX = cos( angle ) * member + scenePos().x();
const double newTopLeftY = topLeft.y() / cos( angle ) + sin( angle ) * member + scenePos().y();

QRectF sceneRect = QRectF( newTopLeftX, newTopLeftY, br.width(), br.height() );
setSceneRect( sceneRect );
const QPointF topLeft = mapToScene( br.topLeft() );
setSceneRect( QRectF( topLeft.x(), topLeft.y(), br.width(), br.height() ) );

// update polygon position
mPolygon.translate( -br.topLeft().x(), -br.topLeft().y() );
@@ -59,7 +59,7 @@ class CORE_EXPORT QgsComposerNodesItem: public QgsComposerItem
/** Set a tag to indicate if we want to draw or not the shape's nodes.
* @param display
*/
void setDisplayNodes( const bool display = true ) { mDrawNodes = display; };
void setDisplayNodes( const bool display = true ) { mDrawNodes = display; }

/** Move a node to a new position.
* @param index the index of the node to move
@@ -77,7 +77,7 @@ class CORE_EXPORT QgsComposerNodesItem: public QgsComposerItem
* limited in space.
* @param radius is only used if searchInRadius is true
*/
int nodeAtPosition( const QPointF &node, const bool searchInRadius = true, const double radius = 10 );
int nodeAtPosition( QPointF node, const bool searchInRadius = true, const double radius = 10 );

/** Gets the position of a node in scene coordinate.
* @param index of the node
@@ -108,11 +108,11 @@ class CORE_EXPORT QgsComposerNodesItem: public QgsComposerItem
/** Returns the currently selected node.
* @return the index of the selected node, -1 otherwise
*/
int selectedNode() { return mSelectedNode; };
int selectedNode() { return mSelectedNode; }

/** Unselect a node.
*/
void unselectNode() { mSelectedNode = -1; };
void unselectNode() { mSelectedNode = -1; }

/** Stores state in Dom element
* @param elem is Dom element corresponding to 'Composer' tag
@@ -144,9 +144,6 @@ class CORE_EXPORT QgsComposerNodesItem: public QgsComposerItem
/** Compute an euclidian distance between 2 nodes. */
double computeDistance( const QPointF &pt1, const QPointF &pt2 ) const;

/** Convert scene coordinate to item coordinates */
QPointF convertToItemCoordinate( QPointF node );

/** Update the current scene rectangle for this item. */
void updateSceneRect();

0 comments on commit 90b6f46

Please sign in to comment.
You can’t perform that action at this time.