Skip to content
Permalink
Browse files

Start on snapping marker

  • Loading branch information
nyalldawson committed Jul 24, 2017
1 parent f57212e commit 798ec83681cd38eb794e9a8a794b0c3e9ba930b8
Showing with 81 additions and 3 deletions.
  1. +2 −0 python/gui/layout/qgslayoutview.sip
  2. +49 −3 src/gui/layout/qgslayoutview.cpp
  3. +30 −0 src/gui/layout/qgslayoutview.h
@@ -231,6 +231,8 @@ Return a newly created menu instance (or null pointer on error)
%End
};



/************************************************************************
* This file has been generated automatically from *
* *
@@ -49,6 +49,7 @@ QgsLayoutView::QgsLayoutView( QWidget *parent )
mSpacePanTool = new QgsLayoutViewToolTemporaryKeyPan( this );
mMidMouseButtonPanTool = new QgsLayoutViewToolTemporaryMousePan( this );
mSpaceZoomTool = new QgsLayoutViewToolTemporaryKeyZoom( this );
mSnapMarker.reset( new QgsLayoutViewSnapMarker() );
}

QgsLayout *QgsLayoutView::currentLayout()
@@ -63,6 +64,10 @@ void QgsLayoutView::setCurrentLayout( QgsLayout *layout )
connect( layout->pageCollection(), &QgsLayoutPageCollection::changed, this, &QgsLayoutView::updateRulers );
updateRulers();

mSnapMarker.reset( new QgsLayoutViewSnapMarker() );
mSnapMarker->hide();
layout->addItem( mSnapMarker.get() );

//emit layoutSet, so that designer dialogs can update for the new layout
emit layoutSet( layout );
}
@@ -82,6 +87,8 @@ void QgsLayoutView::setTool( QgsLayoutViewTool *tool )
mTool->deactivate();
}

mSnapMarker->setVisible( false );

// activate new tool before setting it - gives tools a chance
// to respond to whatever the current tool is
tool->activate();
@@ -207,6 +214,8 @@ void QgsLayoutView::emitZoomLevelChanged()

void QgsLayoutView::mousePressEvent( QMouseEvent *event )
{
mSnapMarker->setVisible( false );

if ( mTool )
{
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
@@ -264,12 +273,18 @@ void QgsLayoutView::mouseMoveEvent( QMouseEvent *event )

if ( mTool )
{
if ( !event->buttons() && mTool->flags() & QgsLayoutViewTool::FlagSnaps )
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
if ( mTool->flags() & QgsLayoutViewTool::FlagSnaps )
{
//draw snapping point indicator

if ( me->isSnapped() )
{
mSnapMarker->setPos( me->snappedPoint() );
mSnapMarker->setVisible( true );
}
else
mSnapMarker->setVisible( false );
}
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
mTool->layoutMoveEvent( me.get() );
event->setAccepted( me->isAccepted() );
}
@@ -408,3 +423,34 @@ void QgsLayoutView::wheelZoom( QWheelEvent *event )
scaleSafe( 1 / zoomFactor );
}
}


//
// QgsLayoutViewSnapMarker
//

///@cond PRIVATE
QgsLayoutViewSnapMarker::QgsLayoutViewSnapMarker()
: QGraphicsRectItem( QRectF( 0, 0, 0, 0 ) )
{
QFont f;
QFontMetrics fm( f );
mSize = fm.width( "X" );
setPen( QPen( Qt::transparent, mSize ) );

setFlags( flags() | QGraphicsItem::ItemIgnoresTransformations );
}

void QgsLayoutViewSnapMarker::paint( QPainter *p, const QStyleOptionGraphicsItem *, QWidget * )
{
QPen pen( QColor( 255, 0, 0 ) );
pen.setWidth( 0 );
p->setPen( pen );
p->setBrush( Qt::NoBrush );

double halfSize = mSize / 2.0;
p->drawLine( QLineF( -halfSize, -halfSize, halfSize, halfSize ) );
p->drawLine( QLineF( -halfSize, halfSize, halfSize, -halfSize ) );
}

///@endcond
@@ -22,6 +22,7 @@
#include "qgis_gui.h"
#include <QPointer>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <memory>

class QMenu;
@@ -32,6 +33,7 @@ class QgsLayoutViewToolTemporaryKeyZoom;
class QgsLayoutViewToolTemporaryMousePan;
class QgsLayoutRuler;
class QgsLayoutViewMenuProvider;
class QgsLayoutViewSnapMarker;

/**
* \ingroup gui
@@ -245,6 +247,8 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
QgsLayoutRuler *mVerticalRuler = nullptr;
std::unique_ptr< QgsLayoutViewMenuProvider > mMenuProvider;

std::unique_ptr< QgsLayoutViewSnapMarker > mSnapMarker;

friend class TestQgsLayoutView;

};
@@ -270,4 +274,30 @@ class GUI_EXPORT QgsLayoutViewMenuProvider
virtual QMenu *createContextMenu( QWidget *parent SIP_TRANSFER, QgsLayout *layout, QPointF layoutPoint ) const = 0 SIP_FACTORY;
};


#ifndef SIP_RUN
///@cond PRIVATE


/**
* \ingroup gui
* A simple graphics item rendered as an 'x'.
*/
class GUI_EXPORT QgsLayoutViewSnapMarker : public QGraphicsRectItem
{
public:

QgsLayoutViewSnapMarker();

void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr ) override;

private:

int mSize = 0;

};

///@endcond
#endif

#endif // QGSLAYOUTVIEW_H

0 comments on commit 798ec83

Please sign in to comment.
You can’t perform that action at this time.