Skip to content
Permalink
Browse files
Improve handling of snap lines
  • Loading branch information
mhugent committed Feb 20, 2013
1 parent 90461f2 commit 717918d705b2a6b057ea12eed4ad6aee4936cf4f
Showing with 35 additions and 15 deletions.
  1. +6 −10 src/core/composer/qgscomposition.cpp
  2. +2 −2 src/core/composer/qgscomposition.h
  3. +27 −3 src/gui/qgscomposerruler.cpp
@@ -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;
@@ -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 )
@@ -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
@@ -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;
}

@@ -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
@@ -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() ) );
}
}

0 comments on commit 717918d

Please sign in to comment.