Files
HPPA/HPPA/imagerSimulatioin.cpp
2026-04-02 10:34:57 +08:00

131 lines
3.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "stdafx.h"
#include "imagerSimulatioin.h"
imagerSimulatioin::imagerSimulatioin(QGraphicsItem *parent) :QGraphicsItem(parent)
{
/*this->setFlag(QGraphicsItem::ItemIsSelectable);
this->setFlag(QGraphicsItem::ItemIsMovable);*/
setFlags(ItemIsSelectable | ItemIsMovable);
//itemChange需要ItemSendsGeometryChanges flag is needed to capture the change in position of QGraphicsItem
this->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
m_height = 50;
m_width = 50;
}
imagerSimulatioin::~imagerSimulatioin()
{
}
QRectF imagerSimulatioin::boundingRect() const
{
qreal penWidth = 1;
return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
20 + penWidth, 20 + penWidth);
}
void imagerSimulatioin::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QBrush blueBrush(Qt::green);
QColor blue(32, 159, 223);
blueBrush.setColor(blue);
QPen outlinePen(Qt::black);
painter->setPen(outlinePen);
painter->setBrush(blueBrush);
painter->drawRect(-m_width / 2, -m_height / 2, m_width, m_height);//可以改变item坐标原点和setSceneRect(qreal x, qreal y, qreal w, qreal h)一样
//QPen outlinePen(Qt::red);
painter->drawPoint(0, 0);
painter->drawEllipse(QPoint(0, 0), 10, 10);
//painter->drawRoundedRect(-10, -10, 100, 100, 5, 5);
}
void imagerSimulatioin::setPos(qreal x, qreal y)
{
if (x>0)
{
x = -x;
}
if (y>0)
{
y = -y;
}
x = floor(x);
y = floor(y);
QGraphicsItem::setPos(x, y);//调用父类被覆盖的函数执行相应操作
scene()->update();
}
QVariant imagerSimulatioin::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene())
{
// value is the new position.
QPointF newPos = value.toPointF();
QRectF rect = scene()->sceneRect();
if (!rect.contains(newPos))
{
// Keep the item inside the scene rect.
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
//qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
return newPos;
}
}
//qDebug() << "---------------";
return QGraphicsItem::itemChange(change, value);
}
void imagerSimulatioin::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
scene()->update();
QPointF originOfItem = this->scenePos();//这是item左上角在scene中的坐标
//qDebug() << originOfItem;
QGraphicsItem::mouseMoveEvent(event);
}
void imagerSimulatioin::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QPointF x = pos();//这是item原点00在scene中的坐标
QPointF originOfItem = this->scenePos();//这是item原点00在scene中的坐标
QPointF viewPos = event->pos();
//qDebug() << "---------------event coordinate: " << x;
//qDebug() << "---------------event coordinate: " << viewPos;
//qDebug() << "---------------event scene coordinate: " << this->mapToScene(viewPos);
//qDebug() << "---------------imagerSimulatioin origin coordinate: " << originOfItem;
//qDebug() << "---------------imagerSimulatioin center coordinate: " << centerOfItem;
qDebug() << "---------------imagerSimulatioin origin coordinate: " << originOfItem;
emit leftMouseButtonPressed(originOfItem.x(), originOfItem.y());
//update();//不会实时绘制
scene()->update();
QGraphicsItem::mouseReleaseEvent(event);
}