Skip to content
Permalink
Browse files
Show alignment lines
  • Loading branch information
mhugent committed Jan 17, 2013
1 parent e7e2869 commit cfcac52b5059fea24400b76cb998402a56a653cf
@@ -17,6 +17,7 @@
#include <QWidget>
#include <QDomNode>
#include <QFile>
#include <QGraphicsLineItem>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
@@ -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 )
@@ -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 )
@@ -90,6 +95,7 @@ QgsComposerItem::~QgsComposerItem()
}

delete mBoundingResizeRectangle;
deleteAlignItems();
}

void QgsComposerItem::setSelected( bool s )
@@ -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 );
@@ -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 );
@@ -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();

@@ -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();
@@ -24,6 +24,7 @@
class QWidget;
class QDomDocument;
class QDomElement;
class QGraphicsLineItem;

class QqsComposition;

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

0 comments on commit cfcac52

Please sign in to comment.