Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'composer_inference_lines'
  • Loading branch information
mhugent committed Jan 18, 2013
2 parents 7809cbd + 1910621 commit 353d697
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 77 deletions.
6 changes: 6 additions & 0 deletions python/core/composer/qgscomposition.sip
Expand Up @@ -65,6 +65,12 @@ class QgsComposition : QGraphicsScene
void setGridStyle( GridStyle s );
GridStyle gridStyle() const;

void setAlignmentSnap( bool s );
bool alignmentSnap() const;

void setAlignmentSnapTolerance( double t );
double alignmentSnapTolerance() const;

/**Returns pointer to undo/redo command storage*/
QUndoStack* undoStack();

Expand Down
21 changes: 21 additions & 0 deletions src/app/composer/qgscompositionwidget.cpp
Expand Up @@ -56,6 +56,9 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
mPrintAsRasterCheckBox->setCheckState( Qt::Unchecked );
}

mAlignmentSnapCheckBox->setCheckState( mComposition->alignmentSnap() ? Qt::Checked : Qt::Unchecked );
mAlignmentToleranceSpinBox->setValue( mComposition->alignmentSnapTolerance() );

//snap grid
if ( mComposition->snapToGridEnabled() )
{
Expand Down Expand Up @@ -524,6 +527,22 @@ void QgsCompositionWidget::on_mSelectionToleranceSpinBox_valueChanged( double d
}
}

void QgsCompositionWidget::on_mAlignmentSnapCheckBox_stateChanged( int state )
{
if ( mComposition )
{
mComposition->setAlignmentSnap( state == Qt::Checked ? true : false );
}
}

void QgsCompositionWidget::on_mAlignmentToleranceSpinBox_valueChanged( double d )
{
if ( mComposition )
{
mComposition->setAlignmentSnapTolerance( d );
}
}

void QgsCompositionWidget::blockSignals( bool block )
{
mPaperSizeComboBox->blockSignals( block );
Expand All @@ -542,4 +561,6 @@ void QgsCompositionWidget::blockSignals( bool block )
mGridColorButton->blockSignals( block );
mGridStyleComboBox->blockSignals( block );
mSelectionToleranceSpinBox->blockSignals( block );
mAlignmentSnapCheckBox->blockSignals( block );
mAlignmentToleranceSpinBox->blockSignals( block );
}
2 changes: 2 additions & 0 deletions src/app/composer/qgscompositionwidget.h
Expand Up @@ -57,6 +57,8 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
void on_mGridStyleComboBox_currentIndexChanged( const QString& text );
void on_mPenWidthSpinBox_valueChanged( double d );
void on_mSelectionToleranceSpinBox_valueChanged( double d );
void on_mAlignmentSnapCheckBox_stateChanged( int state );
void on_mAlignmentToleranceSpinBox_valueChanged( double d );

/**Sets GUI elements to width/height from composition*/
void displayCompositionWidthHeight();
Expand Down
122 changes: 120 additions & 2 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 @@ -503,8 +513,36 @@ void QgsComposerItem::changeItemRectangle( const QPointF& currentPosition,

double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
QPointF snappedPosition = mComposition->snapPointToGrid( currentPosition );
//double diffX = snappedPosition.x() - mouseMoveStartPos.x();
//double diffY = snappedPosition.y() - mouseMoveStartPos.y();

//snap to grid and align to other items
if ( mComposition->alignmentSnap() && mCurrentMouseMoveAction != QgsComposerItem::MoveItem )
{
double alignX = 0;
double alignY = 0;
snappedPosition = mComposition->alignPos( snappedPosition, dynamic_cast<const QgsComposerItem*>( originalItem ), alignX, alignY );
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 diffX = 0;
double diffY = 0;

Expand Down Expand Up @@ -566,6 +604,36 @@ void QgsComposerItem::changeItemRectangle( const QPointF& currentPosition,
QPointF upperLeftPoint( originalItem->transform().dx() + moveX, originalItem->transform().dy() + moveY );
QPointF snappedLeftPoint = mComposition->snapPointToGrid( upperLeftPoint );

if ( snappedLeftPoint != upperLeftPoint ) //don't do align snap if grid snap has been done
{
deleteAlignItems();
}
else if ( mComposition->alignmentSnap() ) //align item
{
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 @@ -1094,6 +1162,56 @@ 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 );
mHAlignSnapItem->setZValue( 90 );
}
return mHAlignSnapItem;
}

QGraphicsLineItem* QgsComposerItem::vAlignSnapItem()
{
if ( !mVAlignSnapItem )
{
mVAlignSnapItem = new QGraphicsLineItem( 0 );
mVAlignSnapItem->setPen( QPen( QColor( Qt::red ) ) );
scene()->addItem( mVAlignSnapItem );
mVAlignSnapItem->setZValue( 90 );
}
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

0 comments on commit 353d697

Please sign in to comment.