Skip to content
Permalink
Browse files

Show different cursor and paint guide markers darker when hovering th…

…em in rulers
  • Loading branch information
nyalldawson committed Jul 27, 2017
1 parent 4565d8d commit 33aa3525a3e5c3603d4c7f9068ed2adc42750794
Showing with 70 additions and 36 deletions.
  1. +62 −35 src/gui/layout/qgslayoutruler.cpp
  2. +8 −1 src/gui/layout/qgslayoutruler.h
@@ -285,12 +285,19 @@ void QgsLayoutRuler::drawGuideMarkers( QPainter *p, QgsLayout *layout )
QList< QgsLayoutGuide * > guides = layout->guides().guides( mOrientation == Qt::Horizontal ? QgsLayoutGuide::Vertical : QgsLayoutGuide::Horizontal );
p->save();
p->setRenderHint( QPainter::Antialiasing, true );
p->setBrush( QBrush( QColor( 255, 0, 0 ) ) );
p->setPen( Qt::NoPen );
Q_FOREACH ( QgsLayoutGuide *guide, guides )
{
if ( visiblePageNumbers.contains( guide->page() ) )
{
if ( guide == mHoverGuide )
{
p->setBrush( QBrush( QColor( 255, 0, 0, 225 ) ) );
}
else
{
p->setBrush( QBrush( QColor( 255, 0, 0, 150 ) ) );
}
QPointF point;
switch ( mOrientation )
{
@@ -354,6 +361,46 @@ QPoint QgsLayoutRuler::convertLayoutPointToLocal( QPointF layoutPoint ) const
return mapFromGlobal( mView->mapToGlobal( viewPoint ) );
}

QgsLayoutGuide *QgsLayoutRuler::guideAtPoint( QPoint localPoint ) const
{
QPointF layoutPoint = convertLocalPointToLayout( localPoint );
QList< int > visiblePageNumbers = mView->visiblePageNumbers();
QList< QgsLayoutGuide * > guides = mView->currentLayout()->guides().guides( mOrientation == Qt::Horizontal ? QgsLayoutGuide::Vertical : QgsLayoutGuide::Horizontal );
QgsLayoutGuide *closestGuide = nullptr;
double minDelta = DBL_MAX;
Q_FOREACH ( QgsLayoutGuide *guide, guides )
{
if ( visiblePageNumbers.contains( guide->page() ) )
{
double currentDelta = 0;
switch ( mOrientation )
{
case Qt::Horizontal:
currentDelta = qAbs( layoutPoint.x() - guide->layoutPosition() );
break;

case Qt::Vertical:
currentDelta = qAbs( layoutPoint.y() - guide->layoutPosition() );
break;
}
if ( currentDelta < minDelta )
{
minDelta = currentDelta;
closestGuide = guide;
}
}
}

if ( minDelta * mView->transform().m11() <= mDragGuideTolerance )
{
return closestGuide;
}
else
{
return nullptr;
}
}

void QgsLayoutRuler::drawRotatedText( QPainter *painter, QPointF pos, const QString &text )
{
painter->save();
@@ -589,6 +636,17 @@ void QgsLayoutRuler::mouseMoveEvent( QMouseEvent *event )
}
else
{
// is cursor over a guide marker?
mHoverGuide = guideAtPoint( event->pos() );
if ( mHoverGuide )
{
setCursor( mOrientation == Qt::Vertical ? Qt::SplitVCursor : Qt::SplitHCursor );
}
else
{
setCursor( Qt::ArrowCursor );
}

//update cursor position in status bar
displayPos = mTransform.inverted().map( event->posF() );
switch ( mOrientation )
@@ -614,41 +672,10 @@ void QgsLayoutRuler::mousePressEvent( QMouseEvent *event )
{
if ( event->button() == Qt::LeftButton )
{
// was click on an existing guide? huh?? was it??
QPointF layoutPoint = convertLocalPointToLayout( event->pos() );
QList< int > visiblePageNumbers = mView->visiblePageNumbers();
QList< QgsLayoutGuide * > guides = mView->currentLayout()->guides().guides( mOrientation == Qt::Horizontal ? QgsLayoutGuide::Vertical : QgsLayoutGuide::Horizontal );
QgsLayoutGuide *closestGuide = nullptr;
double minDelta = DBL_MAX;
Q_FOREACH ( QgsLayoutGuide *guide, guides )
{
if ( visiblePageNumbers.contains( guide->page() ) )
{
double currentDelta = 0;
switch ( mOrientation )
{
case Qt::Horizontal:
currentDelta = qAbs( layoutPoint.x() - guide->layoutPosition() );
break;

case Qt::Vertical:
currentDelta = qAbs( layoutPoint.y() - guide->layoutPosition() );
break;
}
if ( currentDelta < minDelta )
{
minDelta = currentDelta;
closestGuide = guide;
}
}
}

if ( minDelta * mView->transform().m11() <= mDragGuideTolerance )
{
mDraggingGuide = closestGuide;
}
else
mDraggingGuide = guideAtPoint( event->pos() );
if ( !mDraggingGuide )
{
// if no guide at the point, then we're creating one
mCreatingGuide = true;
createTemporaryGuideItem();
}
@@ -17,6 +17,7 @@

#include "qgscomposeritem.h"
#include <QWidget>
#include <QPointer>
#include "qgis_gui.h"

class QgsLayout;
@@ -117,14 +118,15 @@ class GUI_EXPORT QgsLayoutRuler: public QWidget

int mDragGuideTolerance = 0;
QgsLayoutGuide *mDraggingGuide = nullptr;
QgsLayoutGuide *mHoverGuide = nullptr;

bool mCreatingGuide = false;
std::unique_ptr< QGraphicsLineItem > mGuideItem;

//! Polygon for drawing guide markers
QPolygonF mGuideMarker;

QMenu *mMenu = nullptr;
QPointer< QMenu > mMenu;

//! Calculates the optimum labeled units for ruler so that labels are a good distance apart
int optimumScale( double minPixelDiff, int &magnitude, int &multiple );
@@ -159,6 +161,11 @@ class GUI_EXPORT QgsLayoutRuler: public QWidget

QPoint convertLayoutPointToLocal( QPointF layoutPoint ) const;

/**
* Returns the closest guide to a local ruler point, or nullptr if no guides
* are within the acceptable tolerance of the point.
*/
QgsLayoutGuide *guideAtPoint( QPoint localPoint ) const;

};

0 comments on commit 33aa352

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