代码拉取完成,页面将自动刷新
同步操作将从 falion/QtDrawRegion 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "QGraphicsViews.h"
#include <QScrollBar>
#include <QDebug>
#include "ControlItem.h"
#include "BaseItem.h"
#include <QGLWidget>
QGraphicsViews::QGraphicsViews(QWidget *parent) : QGraphicsView(parent)
{
this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);//解决拖动是背景图片残影
setDragMode(QGraphicsView::ScrollHandDrag);
drawBg();
// 隐藏水平/竖直滚动条
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->setBackgroundBrush(Qt::gray);
// 设置场景范围
setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX);
//setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
// 反锯齿
setRenderHints(QPainter::Antialiasing);
item=new QGraphicsPixmapItem;
scene=new QGraphicsScenes;
scene->addItem(item);
this->setScene(scene);
}
void QGraphicsViews::DispImage(QImage &Image)
{
image=QPixmap::fromImage(Image);
item->setPixmap(image);
GetFit();
}
void QGraphicsViews::SetToFit()
{
ZoomFrame(ZoomFit);
QScrollBar *pHbar = this->horizontalScrollBar();
pHbar->setSliderPosition(PixX);
QScrollBar *pVbar = this->verticalScrollBar();
pVbar->setSliderPosition(PixY);
}
void QGraphicsViews::ClearObj()
{
foreach(auto item,scene->items())
{
if(item->type()==10)
{
delete item;
}
}
}
void QGraphicsViews::ZoomFrame(double value)
{
double tmp=value/ZoomValue;
ZoomValue*=tmp;
ControlItem::SetScale(ZoomValue);
BaseItem::SetScale(ZoomValue);
this->scale(tmp,tmp);
}
void QGraphicsViews::GetFit()
{
if(this->width()<1||image.width()<1)
{
return;
}
//图片自适应方法
double winWidth=this->width();
double winHeight=this->height();
double ScaleWidth=(image.width()+1)/winWidth;
double ScaleHeight=(image.height()+1)/winHeight;
double row1,column1;
double s=0;
if(ScaleWidth>=ScaleHeight)
{
row1= -(1) * ((winHeight * ScaleWidth) - image.height()) / 2;
column1 = 0 ;
s=1/ScaleWidth;
}
else
{
row1= 0;
column1 = -(1.0) * ((winWidth * ScaleHeight) - image.width()) / 2 ;
s=1/ScaleHeight;
}
if(ZoomFit!=s||PixX!=column1*s)
{
ZoomFit=s;
PixX=column1*s;
PixY=row1*s;
SetToFit();
}
}
void QGraphicsViews::drawBg()
{
bgPix.fill(color1);
QPainter painter(&bgPix);
painter.fillRect(0, 0, 32, 32, color2);
painter.fillRect(32, 32, 32, 32, color2);
painter.end();
//this->update();
}
void QGraphicsViews::mousePressEvent(QMouseEvent *event)
{
QGraphicsView::mousePressEvent(event);
}
void QGraphicsViews::resizeEvent(QResizeEvent *event)
{
GetFit();
QGraphicsView::resizeEvent(event);
}
void QGraphicsViews::mouseReleaseEvent(QMouseEvent *event)
{
QGraphicsView::mouseReleaseEvent(event);
}
void QGraphicsViews::mouseDoubleClickEvent(QMouseEvent *event)
{
SetToFit();
QGraphicsView::mouseDoubleClickEvent(event);
}
void QGraphicsViews::wheelEvent(QWheelEvent *event)
{
if((event->delta() > 0)&&(ZoomValue >= 50))//最大放大到原始图像的50倍
{
return;
}
else if((event->delta() < 0)&&(ZoomValue <= 0.01))//图像缩小到自适应大小之后就不继续缩小
{
return;
}
else
{
double tmp=ZoomValue;
if(event->delta() > 0)//鼠标滚轮向前滚动
{
tmp*=1.1;//每次放大10%
}
else
{
tmp*=0.9;//每次缩小10%
}
ZoomFrame(tmp);
qreal x=event->pos().x()-this->width()/2;
qreal y=event->pos().y()-this->height()/2;
//缩放时 鼠标位置跟随
if(event->delta() > 0)
{
QScrollBar *pHbar = this->horizontalScrollBar();
pHbar->setSliderPosition(pHbar->sliderPosition()+x*0.1);
QScrollBar *pVbar = this->verticalScrollBar();
pVbar->setSliderPosition(pVbar->sliderPosition()+y*0.1);
}
else
{
QScrollBar *pHbar = this->horizontalScrollBar();
pHbar->setSliderPosition(pHbar->sliderPosition()-x*0.1);
QScrollBar *pVbar = this->verticalScrollBar();
pVbar->setSliderPosition(pVbar->sliderPosition()-y*0.1);
}
}
}
void QGraphicsViews::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_UNUSED(rect);
painter->drawTiledPixmap(rect, bgPix);//绘制背景曾
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。