Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use Qt methods for translating node coords to scene coords
  • Loading branch information
nyalldawson committed Apr 1, 2016
1 parent 4efbb8f commit 90b6f46
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 43 deletions.
5 changes: 1 addition & 4 deletions python/core/composer/qgscomposernodesitem.sip
Expand Up @@ -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
Expand Down Expand Up @@ -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();
};
39 changes: 7 additions & 32 deletions src/core/composer/qgscomposernodesitem.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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() );
Expand Down
11 changes: 4 additions & 7 deletions src/core/composer/qgscomposernodesitem.h
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 90b6f46

Please sign in to comment.