Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Show alignment lines
  • Loading branch information
mhugent committed Jan 17, 2013
1 parent e7e2869 commit cfcac52
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
78 changes: 78 additions & 0 deletions src/core/composer/qgscomposeritem.cpp
Expand Up @@ -17,6 +17,7 @@
#include <QWidget>
#include <QDomNode>
#include <QFile>
#include <QGraphicsLineItem>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
Expand All @@ -42,6 +43,8 @@ QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue
, QGraphicsRectItem( 0 )
, mComposition( composition )
, mBoundingResizeRectangle( 0 )
, mHAlignSnapItem( 0 )
, mVAlignSnapItem( 0 )
, mFrame( false )
, mItemPositionLocked( false )
, mLastValidViewScaleFactor( -1 )
Expand All @@ -55,6 +58,8 @@ QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, Q
, QGraphicsRectItem( 0, 0, width, height, 0 )
, mComposition( composition )
, mBoundingResizeRectangle( 0 )
, mHAlignSnapItem( 0 )
, mVAlignSnapItem( 0 )
, mFrame( false )
, mItemPositionLocked( false )
, mLastValidViewScaleFactor( -1 )
Expand Down Expand Up @@ -90,6 +95,7 @@ QgsComposerItem::~QgsComposerItem()
}

delete mBoundingResizeRectangle;
deleteAlignItems();
}

void QgsComposerItem::setSelected( bool s )
Expand Down Expand Up @@ -336,6 +342,8 @@ void QgsComposerItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
delete mBoundingResizeRectangle;
mBoundingResizeRectangle = 0;
}
deleteAlignItems();

//create and show bounding rectangle
mBoundingResizeRectangle = new QGraphicsRectItem( 0 );
scene()->addItem( mBoundingResizeRectangle );
Expand Down Expand Up @@ -385,6 +393,8 @@ void QgsComposerItem::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
changeItemRectangle( mouseMoveStopPoint, mMouseMoveStartPos, this, diffX, diffY, this );
endItemCommand();

deleteAlignItems();

//reset default action
mCurrentMouseMoveAction = QgsComposerItem::MoveItem;
setCursor( Qt::ArrowCursor );
Expand Down Expand Up @@ -569,6 +579,26 @@ void QgsComposerItem::changeItemRectangle( const QPointF& currentPosition,
double alignX = 0;
double alignY = 0;
snappedLeftPoint = mComposition->alignItem( dynamic_cast<const QgsComposerItem*>( originalItem ), alignX, alignY, moveX, moveY );
if ( alignX != -1 )
{
QGraphicsLineItem* item = hAlignSnapItem();
item->setLine( QLineF( alignX, 0, alignX, mComposition->paperHeight() ) );
item->show();
}
else
{
deleteHAlignSnapItem();
}
if ( alignY != -1 )
{
QGraphicsLineItem* item = vAlignSnapItem();
item->setLine( QLineF( 0, alignY, mComposition->paperWidth(), alignY ) );
item->show();
}
else
{
deleteVAlignSnapItem();
}
double moveRectX = snappedLeftPoint.x() - originalItem->transform().dx();
double moveRectY = snappedLeftPoint.y() - originalItem->transform().dy();

Expand Down Expand Up @@ -1097,6 +1127,54 @@ void QgsComposerItem::rotate( double angle, double& x, double& y ) const
y = yRot;
}

QGraphicsLineItem* QgsComposerItem::hAlignSnapItem()
{
if ( !mHAlignSnapItem )
{
mHAlignSnapItem = new QGraphicsLineItem( 0 );
mHAlignSnapItem->setPen( QPen( QColor( Qt::red ) ) );
scene()->addItem( mHAlignSnapItem );
}
return mHAlignSnapItem;
}

QGraphicsLineItem* QgsComposerItem::vAlignSnapItem()
{
if ( !mVAlignSnapItem )
{
mVAlignSnapItem = new QGraphicsLineItem( 0 );
mVAlignSnapItem->setPen( QPen( QColor( Qt::red ) ) );
scene()->addItem( mVAlignSnapItem );
}
return mVAlignSnapItem;
}

void QgsComposerItem::deleteHAlignSnapItem()
{
if ( mHAlignSnapItem )
{
scene()->removeItem( mHAlignSnapItem );
delete mHAlignSnapItem;
mHAlignSnapItem = 0;
}
}

void QgsComposerItem::deleteVAlignSnapItem()
{
if ( mVAlignSnapItem )
{
scene()->removeItem( mVAlignSnapItem );
delete mVAlignSnapItem;
mVAlignSnapItem = 0;
}
}

void QgsComposerItem::deleteAlignItems()
{
deleteHAlignSnapItem();
deleteVAlignSnapItem();
}

void QgsComposerItem::repaint()
{
update();
Expand Down
11 changes: 11 additions & 0 deletions src/core/composer/qgscomposeritem.h
Expand Up @@ -24,6 +24,7 @@
class QWidget;
class QDomDocument;
class QDomElement;
class QGraphicsLineItem;

class QqsComposition;

Expand Down Expand Up @@ -265,6 +266,8 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem

/**Rectangle used during move and resize actions*/
QGraphicsRectItem* mBoundingResizeRectangle;
QGraphicsLineItem* mHAlignSnapItem;
QGraphicsLineItem* mVAlignSnapItem;

/**True if item fram needs to be painted*/
bool mFrame;
Expand Down Expand Up @@ -345,6 +348,14 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
@param y in/out: y cooreinate before / after the rotation*/
void rotate( double angle, double& x, double& y ) const;

/**Return horizontal align snap item. Creates a new graphics line if 0*/
QGraphicsLineItem* hAlignSnapItem();
void deleteHAlignSnapItem();
/**Return vertical align snap item. Creates a new graphics line if 0*/
QGraphicsLineItem* vAlignSnapItem();
void deleteVAlignSnapItem();
void deleteAlignItems();

signals:
/**Is emitted on rotation change to notify north arrow pictures*/
void rotationChanged( double newRotation );
Expand Down
8 changes: 4 additions & 4 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -1006,10 +1006,10 @@ QPointF QgsComposition::alignItem( const QgsComposerItem* item, double& alignX,
}
xAlignCoordinates.insert( currentItem->transform().dx(), currentItem );
xAlignCoordinates.insert( currentItem->transform().dx() + currentItem->rect().width(), currentItem );
xAlignCoordinates.insert( currentItem->transform().dx() + currentItem->rect().width() / 2.0, currentItem );
yAlignCoordinates.insert( currentItem->rect().top(), currentItem );
yAlignCoordinates.insert( currentItem->rect().center().y(), currentItem );
yAlignCoordinates.insert( currentItem->rect().bottom(), currentItem );
xAlignCoordinates.insert( currentItem->transform().dx() + currentItem->rect().center().x(), currentItem );
yAlignCoordinates.insert( currentItem->transform().dy() + currentItem->rect().top(), currentItem );
yAlignCoordinates.insert( currentItem->transform().dy() + currentItem->rect().center().y(), currentItem );
yAlignCoordinates.insert( currentItem->transform().dy() + currentItem->rect().bottom(), currentItem );
}
}

Expand Down

0 comments on commit cfcac52

Please sign in to comment.