Use Qt methods for translating node coords to scene coords

This commit is contained in:
Nyall Dawson 2016-04-01 14:23:12 +11:00
parent 4efbb8fcf5
commit 90b6f46bf1
3 changed files with 12 additions and 43 deletions

View File

@ -39,7 +39,7 @@ class QgsComposerNodesItem: QgsComposerItem
* limited in space. * limited in space.
* @param radius is only used if searchInRadius is true * @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. /** Gets the position of a node in scene coordinate.
* @param index of the node * @param index of the node
@ -103,9 +103,6 @@ class QgsComposerNodesItem: QgsComposerItem
/** Compute an euclidian distance between 2 nodes. */ /** Compute an euclidian distance between 2 nodes. */
double computeDistance(const QPointF &pt1, const QPointF &pt2) const; 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. */ /** Update the current scene rectangle for this item. */
void updateSceneRect(); void updateSceneRect();
}; };

View File

@ -60,7 +60,7 @@ bool QgsComposerNodesItem::addNode( const QPointF &pt,
const bool checkArea, const bool checkArea,
const double radius ) const double radius )
{ {
const QPointF start = convertToItemCoordinate( pt ); const QPointF start = mapFromScene( pt );
double minDistance = std::numeric_limits<double>::max(); double minDistance = std::numeric_limits<double>::max();
double maxDistance = ( checkArea ) ? radius : minDistance; double maxDistance = ( checkArea ) ? radius : minDistance;
bool rc = false; bool rc = false;
@ -127,13 +127,6 @@ bool QgsComposerNodesItem::addNode( const QPointF &pt,
return rc; return rc;
} }
QPointF QgsComposerNodesItem::convertToItemCoordinate( QPointF node )
{
QTransform transform = QTransform().rotate( -mItemRotation );
node -= scenePos();
return transform.map( node );
}
void QgsComposerNodesItem::drawNodes( QPainter *painter ) const void QgsComposerNodesItem::drawNodes( QPainter *painter ) const
{ {
double rectSize = 3.0 / horizontalViewScaleFactor(); double rectSize = 3.0 / horizontalViewScaleFactor();
@ -223,11 +216,11 @@ void QgsComposerNodesItem::paint( QPainter* painter,
painter->restore(); painter->restore();
} }
int QgsComposerNodesItem::nodeAtPosition( const QPointF &node, int QgsComposerNodesItem::nodeAtPosition( QPointF node,
const bool searchInRadius, const bool searchInRadius,
const double radius ) const double radius )
{ {
const QPointF pt = convertToItemCoordinate( node ); const QPointF pt = mapFromScene( node );
double nearestDistance = std::numeric_limits<double>::max(); double nearestDistance = std::numeric_limits<double>::max();
double maxDistance = ( searchInRadius ) ? radius : nearestDistance; double maxDistance = ( searchInRadius ) ? radius : nearestDistance;
double distance = 0; double distance = 0;
@ -253,18 +246,7 @@ bool QgsComposerNodesItem::nodePosition( const int index, QPointF &position )
if ( index >= 0 && index < mPolygon.size() ) if ( index >= 0 && index < mPolygon.size() )
{ {
// get position in item coordinate position = mapToScene( mPolygon.at( index ) );
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() );
rc = true; rc = true;
} }
@ -303,7 +285,7 @@ bool QgsComposerNodesItem::moveNode( const int index, const QPointF &pt )
if ( index >= 0 && index < mPolygon.size() ) if ( index >= 0 && index < mPolygon.size() )
{ {
QPointF nodeItem = convertToItemCoordinate( pt ); QPointF nodeItem = mapFromScene( pt );
mPolygon.replace( index, nodeItem ); mPolygon.replace( index, nodeItem );
updateSceneRect(); updateSceneRect();
@ -382,15 +364,8 @@ void QgsComposerNodesItem::updateSceneRect()
// set the new scene rectangle // set the new scene rectangle
const QRectF br = mPolygon.boundingRect(); const QRectF br = mPolygon.boundingRect();
const QPointF topLeft = br.topLeft(); const QPointF topLeft = mapToScene( br.topLeft() );
setSceneRect( QRectF( topLeft.x(), topLeft.y(), br.width(), br.height() ) );
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 );
// update polygon position // update polygon position
mPolygon.translate( -br.topLeft().x(), -br.topLeft().y() ); mPolygon.translate( -br.topLeft().x(), -br.topLeft().y() );

View File

@ -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. /** Set a tag to indicate if we want to draw or not the shape's nodes.
* @param display * @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. /** Move a node to a new position.
* @param index the index of the node to move * @param index the index of the node to move
@ -77,7 +77,7 @@ class CORE_EXPORT QgsComposerNodesItem: public QgsComposerItem
* limited in space. * limited in space.
* @param radius is only used if searchInRadius is true * @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. /** Gets the position of a node in scene coordinate.
* @param index of the node * @param index of the node
@ -108,11 +108,11 @@ class CORE_EXPORT QgsComposerNodesItem: public QgsComposerItem
/** Returns the currently selected node. /** Returns the currently selected node.
* @return the index of the selected node, -1 otherwise * @return the index of the selected node, -1 otherwise
*/ */
int selectedNode() { return mSelectedNode; }; int selectedNode() { return mSelectedNode; }
/** Unselect a node. /** Unselect a node.
*/ */
void unselectNode() { mSelectedNode = -1; }; void unselectNode() { mSelectedNode = -1; }
/** Stores state in Dom element /** Stores state in Dom element
* @param elem is Dom element corresponding to 'Composer' tag * @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. */ /** Compute an euclidian distance between 2 nodes. */
double computeDistance( const QPointF &pt1, const QPointF &pt2 ) const; 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. */ /** Update the current scene rectangle for this item. */
void updateSceneRect(); void updateSceneRect();