Skip to content

Commit

Permalink
Add hover effect handling on AbstractAnnotationItem
Browse files Browse the repository at this point in the history
  • Loading branch information
DamirPorobic committed Jun 5, 2018
1 parent 339d872 commit 531dc66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
32 changes: 19 additions & 13 deletions src/annotations/items/AbstractAnnotationItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ AbstractAnnotationItem::AbstractAnnotationItem(const AnnotationProperties &prope
{
mProperties = new AnnotationProperties(properties);
mShape = new QPainterPath();
mShadowEffect = new ShadowEffect();

mPainterPen.setColor(mProperties->color());
mPainterPen.setWidth(mProperties->size());
Expand All @@ -31,7 +32,7 @@ AbstractAnnotationItem::AbstractAnnotationItem(const AnnotationProperties &prope

mStroker = new QPainterPathStroker(mPainterPen);

addShadowEffect();
setGraphicsEffect(mShadowEffect);
}

AbstractAnnotationItem::~AbstractAnnotationItem()
Expand Down Expand Up @@ -80,18 +81,6 @@ AnnotationProperties AbstractAnnotationItem::properties() const
return *mProperties;
}

void AbstractAnnotationItem::addShadowEffect()
{
auto shadowEffect = dynamic_cast<QGraphicsDropShadowEffect *>(graphicsEffect());
if (!shadowEffect) {
shadowEffect = new QGraphicsDropShadowEffect();
shadowEffect->setColor(QColor(63, 63, 63, 190));
shadowEffect->setBlurRadius(7);
shadowEffect->setOffset(QPoint(2, 2));
setGraphicsEffect(shadowEffect);
}
}

void AbstractAnnotationItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
shiftPainterForAllOddShapeWidth(painter);
Expand All @@ -104,6 +93,18 @@ void AbstractAnnotationItem::paint(QPainter *painter, const QStyleOptionGraphics
painter->drawPath(*mShape);
}

void AbstractAnnotationItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
mShadowEffect->setHoveredEnabled(true);
QGraphicsItem::hoverEnterEvent(event);
}

void AbstractAnnotationItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
mShadowEffect->setHoveredEnabled(false);
QGraphicsItem::hoverLeaveEvent(event);
}

void AbstractAnnotationItem::shiftPainterForAllOddShapeWidth(QPainter *painter) const
{
if (mPainterPen.width() % 2 != 0) {
Expand All @@ -115,3 +116,8 @@ bool AbstractAnnotationItem::hasFill() const
{
return mProperties->fillType() == FillTypes::Fill;
}

void AbstractAnnotationItem::setHoverEffectEnabled(bool enabled)
{
setAcceptHoverEvents(enabled);
}
11 changes: 7 additions & 4 deletions src/annotations/items/AbstractAnnotationItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
#include <QGraphicsItem>
#include <QPainter>
#include <QPainterPathStroker>
#include <QGraphicsDropShadowEffect>

#include "../core/AnnotationProperties.h"
#include "../../common/enum/FillTypes.h"
#include "../misc/ShadowEffect.h"

class AbstractAnnotationItem : public QGraphicsItem
{
public:
AbstractAnnotationItem(const AnnotationProperties &properties);
~AbstractAnnotationItem();
explicit AbstractAnnotationItem(const AnnotationProperties &properties);
virtual ~AbstractAnnotationItem();
virtual QRectF boundingRect() const override;
virtual QPainterPath shape() const override;
virtual bool intersects(const QRectF &rect) const;
Expand All @@ -42,18 +42,21 @@ class AbstractAnnotationItem : public QGraphicsItem
virtual void setPosition(const QPointF &newPosition) = 0;
virtual QPointF position();
AnnotationProperties properties() const;
void addShadowEffect();
void setHoverEffectEnabled(bool enabled);

protected:
void setShape(QPainterPath &newShape);
virtual void updateShape() = 0;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override;
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;

private:
AnnotationProperties *mProperties;
QPainterPath *mShape;
QPainterPathStroker *mStroker;
QPen mPainterPen;
ShadowEffect *mShadowEffect;

bool hasFill() const;
void shiftPainterForAllOddShapeWidth(QPainter *painter) const;
Expand Down

0 comments on commit 531dc66

Please sign in to comment.