131 lines
3.2 KiB
C++
131 lines
3.2 KiB
C++
|
|
#include "stdafx.h"
|
|||
|
|
#include "imagerSimulatioin.h"
|
|||
|
|
|
|||
|
|
imagerSimulatioin::imagerSimulatioin(QGraphicsItem *parent) :QGraphicsItem(parent)
|
|||
|
|
{
|
|||
|
|
/*this->setFlag(QGraphicsItem::ItemIsSelectable);
|
|||
|
|
this->setFlag(QGraphicsItem::ItemIsMovable);*/
|
|||
|
|
|
|||
|
|
setFlags(ItemIsSelectable | ItemIsMovable);
|
|||
|
|
|
|||
|
|
//itemChange<67><65>Ҫ<EFBFBD><D2AA>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);//<2F><><EFBFBD>Ըı<D4B8>item<65><6D><EFBFBD><EFBFBD>ԭ<EFBFBD>㣬<EFBFBD><E3A3AC>setSceneRect(qreal x, qreal y, qreal w, qreal h)һ<><D2BB>
|
|||
|
|
|
|||
|
|
|
|||
|
|
//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);//<2F><><EFBFBD>ø<EFBFBD><C3B8>౻<EFBFBD><E0B1BB><EFBFBD>ǵĺ<C7B5><C4BA><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
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();//<2F><><EFBFBD><EFBFBD>item<65><6D><EFBFBD>Ͻ<EFBFBD><CFBD><EFBFBD>scene<6E>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
//qDebug() << originOfItem;
|
|||
|
|
|
|||
|
|
QGraphicsItem::mouseMoveEvent(event);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void imagerSimulatioin::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|||
|
|
{
|
|||
|
|
QPointF x = pos();//<2F><><EFBFBD><EFBFBD>itemԭ<6D>㣨0<E3A3A8><30>0<EFBFBD><30><EFBFBD><EFBFBD>scene<6E>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
QPointF originOfItem = this->scenePos();//<2F><><EFBFBD><EFBFBD>itemԭ<6D>㣨0<E3A3A8><30>0<EFBFBD><30><EFBFBD><EFBFBD>scene<6E>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|
|||
|
|
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();//<2F><><EFBFBD><EFBFBD>ʵʱ<CAB5><CAB1><EFBFBD><EFBFBD>
|
|||
|
|
scene()->update();
|
|||
|
|
|
|||
|
|
QGraphicsItem::mouseReleaseEvent(event);
|
|||
|
|
}
|