Skip to content

Commit

Permalink
Show different cursor and paint guide markers darker when hovering th…
Browse files Browse the repository at this point in the history
…em in rulers
  • Loading branch information
nyalldawson committed Aug 7, 2017
1 parent 4565d8d commit 33aa352
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 36 deletions.
97 changes: 62 additions & 35 deletions src/gui/layout/qgslayoutruler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 )
Expand All @@ -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();
}
Expand Down
9 changes: 8 additions & 1 deletion src/gui/layout/qgslayoutruler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

class QgsLayout;
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;

};

Expand Down

0 comments on commit 33aa352

Please sign in to comment.