Skip to content

Commit

Permalink
Start on snapping marker
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent f57212e commit 798ec83
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
2 changes: 2 additions & 0 deletions python/gui/layout/qgslayoutview.sip
Expand Up @@ -231,6 +231,8 @@ Return a newly created menu instance (or null pointer on error)
%End
};



/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
52 changes: 49 additions & 3 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -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()
Expand All @@ -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 );
}
Expand All @@ -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();
Expand Down Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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() );
}
Expand Down Expand Up @@ -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
30 changes: 30 additions & 0 deletions src/gui/layout/qgslayoutview.h
Expand Up @@ -22,6 +22,7 @@
#include "qgis_gui.h"
#include <QPointer>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <memory>

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

/**
* \ingroup gui
Expand Down Expand Up @@ -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;

};
Expand All @@ -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.