Graphics item handler is bigger when overred

This commit is contained in:
joshua 2022-01-04 18:41:46 +01:00
parent ae9faa2192
commit 1a75eb19d0
2 changed files with 53 additions and 6 deletions

View File

@ -19,6 +19,7 @@
#include <QPainter>
#include <QDebug>
#include <utility>
#include <QPropertyAnimation>
/**
@brief QetGraphicsHandlerItem::QetGraphicsHandlerItem
@ -26,6 +27,7 @@
*/
QetGraphicsHandlerItem::QetGraphicsHandlerItem(qreal size)
{
setAcceptHoverEvents(true);
setFlag(QGraphicsItem::ItemIgnoresTransformations);
setSize(size);
}
@ -33,9 +35,9 @@ QetGraphicsHandlerItem::QetGraphicsHandlerItem(qreal size)
void QetGraphicsHandlerItem::setSize(qreal size)
{
prepareGeometryChange();
m_size = size;
m_handler_rect.setRect(0-m_size/2, 0-m_size/2, m_size, m_size);
m_br.setRect(-1-m_size/2, -1-m_size/2, m_size+2, m_size+2);
m_current_size = m_original_size = size;
m_handler_rect.setRect(0-m_current_size/2, 0-m_current_size/2, m_current_size, m_current_size);
m_br.setRect(-1-m_current_size/2, -1-m_current_size/2, m_current_size+2, m_current_size+2);
}
/**
@ -84,6 +86,38 @@ void QetGraphicsHandlerItem::paint(QPainter *painter,
painter->restore();
}
void QetGraphicsHandlerItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
auto animation_ = new QPropertyAnimation(this, "currentSize");
animation_->setStartValue(m_original_size);
animation_->setEndValue(m_original_size*1.5);
animation_->setDuration(200);
animation_->setEasingCurve(QEasingCurve::OutBack);
animation_->start(QAbstractAnimation::DeleteWhenStopped);
}
void QetGraphicsHandlerItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
auto animation_ = new QPropertyAnimation(this, "currentSize");
animation_->setStartValue(m_current_size);
animation_->setEndValue(m_original_size);
animation_->setDuration(200);
animation_->setEasingCurve(QEasingCurve::OutBack);
animation_->start(QAbstractAnimation::DeleteWhenStopped);
}
void QetGraphicsHandlerItem::setCurrentSize(qreal size)
{
prepareGeometryChange();
m_current_size = size;
m_handler_rect.setRect(0-m_current_size/2, 0-m_current_size/2, m_current_size, m_current_size);
m_br.setRect(-1-m_current_size/2, -1-m_current_size/2, m_current_size+2, m_current_size+2);
}
/**
@brief QetGraphicsHandlerItem::handlerForPoint
@param points

View File

@ -18,7 +18,7 @@
#ifndef QETGRAPHICSHANDLERITEM_H
#define QETGRAPHICSHANDLERITEM_H
#include <QGraphicsItem>
#include <QGraphicsObject>
#include <QPen>
/**
@ -33,8 +33,12 @@
need to reimplement "sceneEventFilter"
for create the modification behavior.
*/
class QetGraphicsHandlerItem : public QGraphicsItem
class QetGraphicsHandlerItem : public QGraphicsObject
{
Q_OBJECT
Q_PROPERTY(qreal currentSize READ currentSize WRITE setCurrentSize)
public:
QetGraphicsHandlerItem(qreal size = 10);
void setSize(qreal size);
@ -45,15 +49,24 @@ class QetGraphicsHandlerItem : public QGraphicsItem
void setColor(QColor color);
protected:
void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
qreal currentSize() const {return m_current_size;}
void setCurrentSize(qreal size);
private:
QRectF m_handler_rect,
m_br;
qreal m_size;
qreal m_current_size;
qreal m_original_size;
QColor m_color;
QPen m_pen;