CustomGraphicsItem::CustomGraphicsItem()
{
QGraphicsItem::setCacheMode(QGraphicsItem::NoCache);
QGraphicsItem::setAcceptDrops(true);
m_scaleValue = 1;
m_scaleDafault = 1;
m_isMove = false;
m_hasPix=false;
wid=0;
hei=0;
}
QRectF CustomGraphicsItem::boundingRect() const
{
float w=1024;
float h=1040;
if (m_hasPix){
return QRectF(-wid / 2, -hei / 2, wid, hei);
}else{
return QRectF(-w/2,-h/2, w, h);
}
}
void CustomGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
if (m_hasPix){
wid=m_pix.width();
hei=m_pix.height();
QPixmap scaledPixmap = m_pix.scaled(wid*m_scaleValue, hei*m_scaleValue);
wid=scaledPixmap.width();
hei=scaledPixmap.height();
painter->drawPixmap(-wid / 2, -hei / 2, scaledPixmap);
}
}
void CustomGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
m_startPos = event->pos();
m_isMove = true;
QApplication::setOverrideCursor(Qt::OpenHandCursor);
}
}
void CustomGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(m_hasPix)
{
QPointF point = (event->pos() - m_startPos)*m_scaleValue; moveBy(point.x(), point.y());
}
}
void CustomGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
m_isMove = false;
QApplication::setOverrideCursor(Qt::ArrowCursor);
}
void CustomGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event)
{
qDebug() << "wheel"<<m_scaleValue;
if((event->delta() > 0)&&(m_scaleValue >= 2))
{
ResetItemPos();
}
else
{
qreal qrealOriginScale = m_scaleValue;
if(event->delta() > 0)
{
m_scaleValue*=1.1;
}
else
{
m_scaleValue*=0.9;
}
if(event->delta() > 0)
{
moveBy(-event->pos().x()*qrealOriginScale*0.1, -event->pos().y()*qrealOriginScale*0.1);
}
else
{
moveBy(event->pos().x()*qrealOriginScale*0.1, event->pos().y()*qrealOriginScale*0.1);
}
return;
}
}
void CustomGraphicsItem::setQGraphicsViewWH(int nwidth, int nheight)
{
qDebug()<< nwidth<<nheight;
int nImgWidth = 1024;
int nImgHeight = 1040;
qreal temp1 = nwidth*1.0/nImgWidth;
qreal temp2 = nheight*1.0/nImgHeight;
if(temp1>temp2)
{
m_scaleDafault = temp2;
} else
{
m_scaleDafault = temp1;
}
m_scaleValue = m_scaleDafault;
}
void CustomGraphicsItem::updatePix(QPixmap &pixmap)
{
m_hasPix=true;
m_pix = pixmap;
wid=m_pix.width();
hei=m_pix.height();
update();
}
void CustomGraphicsItem::ResetItemPos()
{
m_scaleValue = m_scaleDafault;
setPos(0,0);
}
qreal CustomGraphicsItem::getScaleValue() const
{
return m_scaleValue;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)