Skip to content

Commit

Permalink
Improve handling of snap lines
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Feb 20, 2013
1 parent 90461f2 commit 717918d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
16 changes: 6 additions & 10 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -1074,9 +1074,10 @@ void QgsComposition::removeSnapLine( QGraphicsLineItem* line )
{
removeItem( line );
mSnapLines.removeAll( line );
delete line;
}

QGraphicsLineItem* QgsComposition::nearestSnapLine( double x, double y, double tolerance )
QGraphicsLineItem* QgsComposition::nearestSnapLine( bool horizontal, double x, double y, double tolerance )
{
bool xDirection = doubleNear( y, 0.0 );
double minSqrDist = DBL_MAX;
Expand All @@ -1089,20 +1090,15 @@ QGraphicsLineItem* QgsComposition::nearestSnapLine( double x, double y, double t
QList< QGraphicsLineItem* >::const_iterator it = mSnapLines.constBegin();
for ( ; it != mSnapLines.constEnd(); ++it )
{
currentXCoord = ( *it )->line().x1();
currentYCoord = ( *it )->line().y1();

if ( xDirection && !doubleNear( currentXCoord, 0.0 ) )
{
currentSqrDist = ( x - currentXCoord ) * ( x - currentXCoord );
}
else if ( !xDirection && !doubleNear( currentYCoord, 0.0 ) )
if ( horizontal )
{
currentYCoord = ( *it )->line().y1();
currentSqrDist = ( y - currentYCoord ) * ( y - currentYCoord );
}
else
{
continue;
currentXCoord = ( *it )->line().x1();
currentSqrDist = ( x - currentXCoord ) * ( x - currentXCoord );
}

if ( currentSqrDist < minSqrDist && currentSqrDist < sqrTolerance )
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposition.h
Expand Up @@ -261,10 +261,10 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene

/**Add a custom snap line (can be horizontal or vertical)*/
void addSnapLine( QGraphicsLineItem* line );
/**Remove custom snap line*/
/**Remove custom snap line (and delete the object)*/
void removeSnapLine( QGraphicsLineItem* line );
/**Get nearest snap line*/
QGraphicsLineItem* nearestSnapLine( double x, double y, double tolerance );
QGraphicsLineItem* nearestSnapLine( bool horizontal, double x, double y, double tolerance );

/**Allocates new item command and saves initial state in it
@param item target item
Expand Down
30 changes: 27 additions & 3 deletions src/gui/qgscomposerruler.cpp
Expand Up @@ -137,6 +137,23 @@ void QgsComposerRuler::mouseMoveEvent( QMouseEvent* event )
void QgsComposerRuler::mouseReleaseEvent( QMouseEvent* event )
{
Q_UNUSED( event );

//remove snap line if coordinate under 0
QPointF pos = mTransform.inverted().map( event->pos() );
bool removeItem = false;
if ( mDirection == Horizontal )
{
removeItem = pos.x() < 0 ? true : false;
}
else
{
removeItem = pos.y() < 0 ? true : false;
}

if ( removeItem )
{
mComposition->removeSnapLine( mLineSnapItem );
}
mLineSnapItem = 0;
}

Expand All @@ -153,7 +170,8 @@ void QgsComposerRuler::mousePressEvent( QMouseEvent* event )
y = mTransform.inverted().map( event->pos() ).y();
}

QGraphicsLineItem* line = mComposition->nearestSnapLine( x, y, 2.0 );
//horizontal ruler means vertical snap line
QGraphicsLineItem* line = mComposition->nearestSnapLine( mDirection != Horizontal, x, y, 10.0 );
if ( !line )
{
//create new snap line
Expand All @@ -176,11 +194,17 @@ void QgsComposerRuler::setSnapLinePosition( const QPointF& pos )
QPointF transformedPt = mTransform.inverted().map( pos );
if ( mDirection == Horizontal )
{
mLineSnapItem->setLine( QLineF( transformedPt.x(), 0, transformedPt.x(), mComposition->height() - 1 ) );
int numPages = mComposition->numPages();
double lineHeight = numPages * mComposition->paperHeight();
if ( numPages > 1 )
{
lineHeight += ( numPages - 1 ) * mComposition->spaceBetweenPages();
}
mLineSnapItem->setLine( QLineF( transformedPt.x(), 0, transformedPt.x(), lineHeight ) );
}
else //vertical
{
mLineSnapItem->setLine( QLineF( 0, transformedPt.y(), mComposition->width() - 1, transformedPt.y() ) );
mLineSnapItem->setLine( QLineF( 0, transformedPt.y(), mComposition->paperWidth(), transformedPt.y() ) );
}
}

Expand Down

0 comments on commit 717918d

Please sign in to comment.