From b12f4a24780c830f2fcbad6ee7e091f51ed8cae7 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Tue, 8 Sep 2015 17:53:37 -0400 Subject: [PATCH] Improved detection of hover - account for presence of fill or outline colors - allow for a couple of pixels of slop --- glabels/LabelModelBoxObject.cpp | 30 +++++++++++++++++++++++++++--- glabels/LabelModelBoxObject.h | 2 +- glabels/LabelModelObject.cpp | 15 +++++++++------ glabels/LabelModelObject.h | 2 +- glabels/Outline.cpp | 4 ++-- glabels/Outline.h | 4 ++-- glabels/View.cpp | 2 +- 7 files changed, 43 insertions(+), 16 deletions(-) diff --git a/glabels/LabelModelBoxObject.cpp b/glabels/LabelModelBoxObject.cpp index 0e85c8b..d2c5ccd 100644 --- a/glabels/LabelModelBoxObject.cpp +++ b/glabels/LabelModelBoxObject.cpp @@ -24,6 +24,12 @@ #include +namespace +{ + const double slopPixels = 2; +} + + namespace glabels { @@ -105,12 +111,30 @@ namespace glabels /// - /// Path representing object + /// Path to test for hover condition /// - QPainterPath LabelModelBoxObject::path() const + QPainterPath LabelModelBoxObject::hoverPath( double scale ) const { + double s = 1 / scale; + QPainterPath path; - path.addRect( 0, 0, mW, mH ); + + if ( mFillColorNode.color().alpha() && mLineColorNode.color().alpha() ) + { + path.addRect( -mLineWidth/2, -mLineWidth/2, mW+mLineWidth, mH+mLineWidth ); + } + else if ( mFillColorNode.color().alpha() && !(mLineColorNode.color().alpha()) ) + { + path.addRect( 0, 0, mW, mH ); + } + else if ( mLineColorNode.color().alpha() ) + { + path.addRect( (-mLineWidth/2)-s*slopPixels, (-mLineWidth/2)-s*slopPixels, + mW+mLineWidth+s*2*slopPixels, mH+mLineWidth+s*2*slopPixels ); + path.closeSubpath(); + path.addRect( mLineWidth/2+s*slopPixels, mLineWidth/2+s*slopPixels, + mW-mLineWidth-s*2*slopPixels, mH-mLineWidth-s*2*slopPixels ); + } return path; } diff --git a/glabels/LabelModelBoxObject.h b/glabels/LabelModelBoxObject.h index 1ef8046..3cb74cd 100644 --- a/glabels/LabelModelBoxObject.h +++ b/glabels/LabelModelBoxObject.h @@ -49,7 +49,7 @@ namespace glabels protected: virtual void drawShadow( QPainter* painter, bool inEditor, MergeRecord* record ) const; virtual void drawObject( QPainter* painter, bool inEditor, MergeRecord* record ) const; - virtual QPainterPath path() const; + virtual QPainterPath hoverPath( double scale ) const; }; diff --git a/glabels/LabelModelObject.cpp b/glabels/LabelModelObject.cpp index b85e2fc..da4d294 100644 --- a/glabels/LabelModelObject.cpp +++ b/glabels/LabelModelObject.cpp @@ -905,17 +905,20 @@ namespace glabels bool LabelModelObject::isLocatedAt( double scale, double x, double y ) const { QPointF p( x, y ); - p -= QPointF( mX0, mY0 ); // Translate point to x0,y0 - QPainterPath objectPath = mMatrix.map( path() ); - if ( objectPath.contains( p ) ) + /* + * Change point to object relative coordinates + */ + p -= QPointF( mX0, mY0 ); // Translate point to x0,y0 + p = mMatrix.inverted().map( p ); + + if ( hoverPath( scale ).contains( p ) ) { return true; } - else if ( mOutline ) + else if ( isSelected() && mOutline ) { - QPainterPath outlinePath = mMatrix.map( mOutline->path( scale ) ); - if ( outlinePath.contains( p ) ) + if ( mOutline->hoverPath( scale ).contains( p ) ) { return true; } diff --git a/glabels/LabelModelObject.h b/glabels/LabelModelObject.h index 9a0ca63..289bd29 100644 --- a/glabels/LabelModelObject.h +++ b/glabels/LabelModelObject.h @@ -399,7 +399,7 @@ namespace glabels protected: virtual void drawShadow( QPainter* painter, bool inEditor, MergeRecord* record ) const = 0; virtual void drawObject( QPainter* painter, bool inEditor, MergeRecord* record ) const = 0; - virtual QPainterPath path() const = 0; + virtual QPainterPath hoverPath( double scale ) const = 0; /////////////////////////////////////////////////////////////// diff --git a/glabels/Outline.cpp b/glabels/Outline.cpp index 2cedc6d..9f66527 100644 --- a/glabels/Outline.cpp +++ b/glabels/Outline.cpp @@ -88,9 +88,9 @@ void glabels::Outline::draw( QPainter* painter ) const /// -/// Create Outline path +/// Create path for testing for hover condition /// -QPainterPath glabels::Outline::path( double scale ) const +QPainterPath glabels::Outline::hoverPath( double scale ) const { double s = 1 / scale; diff --git a/glabels/Outline.h b/glabels/Outline.h index 72386d1..b4067b5 100644 --- a/glabels/Outline.h +++ b/glabels/Outline.h @@ -49,8 +49,8 @@ namespace glabels // Drawing Methods //////////////////////////// public: - void draw( QPainter* painter ) const; - QPainterPath path( double scale ) const; + void draw( QPainter* painter ) const; + QPainterPath hoverPath( double scale ) const; //////////////////////////// diff --git a/glabels/View.cpp b/glabels/View.cpp index c283f11..a6fb78e 100644 --- a/glabels/View.cpp +++ b/glabels/View.cpp @@ -730,7 +730,7 @@ glabels::View::handleResizeMotion( double xWorld, double yWorld ) Handle::Location location = mResizeHandle->location(); /* - * Change to item relative coordinates + * Change point to object relative coordinates */ p -= QPointF( mResizeObject->x0(), mResizeObject->y0() ); p = mResizeObject->matrix().inverted().map( p );