#include <QtGui>
#include <cmath>
#include "ResizeWindow.h"
#include "ZoomRatio.h"
#include "DisplayRule.h"
using namespace std;
//------------------------------------------------------------------------
/**
\brief 构造函数
*/
//------------------------------------------------------------------------
CResizeWindow::CResizeWindow(QWidget *parent)
:QLabel(parent)
,m_dZoomRatio(100.0)
,m_dLastZoomRatio(0.0)
,m_emZoomStyle(ZOOM_FIT_SIZE)
,m_emLastZoomStyle(ZOOM_FIT_SIZE)
{
//初始化变量
m_objCurZoomCenter = QPoint(0, 0);
m_objDstRect = QRectF(0, 0, 0, 0);
m_LastMove = m_objMoveAgency.GetMovement();
//跟踪鼠标
this->setMouseTracking(true);
//获取系统背景色
QRgb nBkRgb = QApplication::palette().color(QPalette::Base).rgb();
//设置背景颜色
const int WIDTH = 2;
const int BASE = 16;
const QChar FILL_CHAR('0');
QString strBkColor = QString("background-color:#%1%2%3")
.arg(qRed(nBkRgb), WIDTH, BASE, FILL_CHAR)
.arg(qGreen(nBkRgb), WIDTH, BASE, FILL_CHAR)
.arg(qBlue(nBkRgb), WIDTH, BASE, FILL_CHAR);
this->setStyleSheet(strBkColor);
}
//------------------------------------------------------------------------
/**
\brief 析构函数
*/
//------------------------------------------------------------------------
CResizeWindow::~CResizeWindow()
{
}
//----------------------------------------------------------------------------------
/**
\brief 鼠标按键按下事件
\param event 参见Qt帮助文档
*/
//----------------------------------------------------------------------------------
void CResizeWindow::mousePressEvent ( QMouseEvent * event)
{
//左键按下
if(Qt::LeftButton == event->button())
{
//非适合大小的缩放方式才执行操作
if(ZOOM_FIT_SIZE != m_emZoomStyle)
{
m_objMoveAgency.Start(event->pos());
}
else
{
}
}
else if(Qt::MiddleButton == event->button()) //中键按下
{
//切换缩放方式
switch(m_emZoomStyle)
{
case ZOOM_FIT_SIZE:
m_emZoomStyle = ZOOM_REAL_SIZE;
break;
case ZOOM_REAL_SIZE:
m_emZoomStyle = ZOOM_FIX_RATIO;
break;
case ZOOM_FIX_RATIO:
m_emZoomStyle = ZOOM_FIT_SIZE;
break;
default:
m_emZoomStyle = ZOOM_FIT_SIZE;
break;
}
//更新显示
this->update();
}
else{} //if(Qt::LeftButton == event->button())
}
//----------------------------------------------------------------------------------
/**
\brief 鼠标按键弹起事件
\param event 参见Qt帮助文档
*/
//----------------------------------------------------------------------------------
void CResizeWindow::mouseReleaseEvent ( QMouseEvent * event)
{
if(Qt::LeftButton == event->button())
{
//非适合大小的缩放方式才执行操作
if(ZOOM_FIT_SIZE != m_emZoomStyle)
{
m_objMoveAgency.MoveTo(event->pos()); //最后一次移动图像
m_objMoveAgency.Stop(); //左键弹起说明停止移动
this->update(); //重绘
}
else
{
}// if(ZOOM_FIT_SIZE != m_emZoomStyle)
}
}
//----------------------------------------------------------------------------------
/**
\brief 鼠标移动事件
\param event 参见Qt帮助文档
*/
//----------------------------------------------------------------------------------
void CResizeWindow::mouseMoveEvent ( QMouseEvent * event )
{
//判断鼠标左键是否按下
bool bLeftButtonDown = (event->buttons() & Qt::LeftButton);
//非适合大小的缩放方式才执行操作
if(ZOOM_FIT_SIZE != m_emZoomStyle)
{
if(!bLeftButtonDown)
{
//如果左键已经弹起,则停止移动
m_objMoveAgency.Stop();
}
else
{
//否则计算移动量并重绘
m_objMoveAgency.MoveTo(event->pos());
this->update();
}//end of if(!bLeftButtonDown)
}
else//否则什么也不做
{
} //if(ZOOM_FIT_SIZE == m_emZoomStyle)
//只计算鼠标左键弹起,即图像不移动时的情况
if(!bLeftButtonDown)
{
//只计算当前窗口是焦点窗口的情况
if(this->hasFocus())
{
//获取当前图像
const QImage& objImg = this->_DoGetCurrentImg();
//检验像素坐标和像素值变化,并发射信号
QRgb nRgb = 0;
QPoint objPixelPos;
bool bRgb = this->_GetCursorRgb(event->pos(), objImg, nRgb, objPixelPos);
if(bRgb)
{
emit SigPixelValueChanged(nRgb, this); //发射像素值变化信号
emit SigPixelPosChanged(objPixelPos, this); //发射像素坐标变化信号
}//end of if(bRgb)
}//end of if(this->isActiveWindow())
}//end of if(!bLeftButtonDown)
}
//----------------------------------------------------------------------------------
/**
\brief 获取鼠标所在位置的RGB值
\param objCursorPos 鼠标相对当前窗口的坐标
\param objImg 当前图像
\param nRgbOut[out] 返回指定位置对应的像素RGB值
\param objPixelPos[out] 返回指定位置对应的像素坐标
\return 返回true表示成功,返回false表示鼠标所在的位置超出了图片的范围或图像为空
*/
//----------------------------------------------------------------------------------
bool CResizeWindow::_GetCursorRgb(const QPoint& objPos, const QImage& objImg,
QRgb& nRgbOut, QPoint& objPixelPos)
{
const QRectF& objDstRect = m_objDstRect;
bool bResult = true;
do
{
//图像是否为空
if(objImg.isNull())
{
bResult = false;
break;
}
//计算当前位置的像素坐标(左上角像素的坐标范围是0<=x<1、0<=y<1)
double dAbsoluteZoomRatio = m_dLastZoomRatio / 100.0; //去百分比后的缩放倍率
QPointF objLastLeftTop = objDstRect.topLeft();
double dPixelX = (objPos.x() - objLastLeftTop.x()) / dAbsoluteZoomRatio;
double dPixelY = (objPos.y() - objLastLeftTop.y()) / dAbsoluteZoomRatio;
int nPixelX = int(dPixelX); //截断
int nPixelY = int(dPixelY); //截断
if(nPixelX < 0 || nPixelY < 0 || nPixelX >= objImg.width() ||
nPixelY >= objImg.height())
{
bResult = false;
break;
}
//返回RGB值
nRgbOut = objImg.pixel(nPixelX, nPixelY );
//返回像素坐标
objPixelPos = QPoint(nPixelX, nPixelY);
} while (0);
return bResult;
}
//----------------------------------------------------------------------------------
/**
\brief 鼠标滚轮事件
\param event 参见Qt帮助文档
*/
//----------------------------------------------------------------------------------
void CResizeWindow::wheelEvent(QWheelEvent * event )
{
//如果Ctrl键没有被按下,则不执行缩放
bool bCtrlPressed = ((event->modifiers() & Qt::ControlModifier) != 0);
if(!bCtrlPressed)
{
return;
}
//如果鼠标位于窗口外,则不执行缩放
QPoint objCursorPos = event->pos();
QRect objRect = this->rect();
bool bCursorInWindow = objRect.contains(objCursorPos);
if(!bCursorInWindow)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
TestResizeWindow.rar (14个子文件)
TestResizeWindow
TestResizeWindow.suo 20KB
TestResizeWindow.sln 914B
TestResizeWindow
ZoomRatio.h 1KB
ZoomRatio.cpp 4KB
DisplayRule.h 1KB
Resources
lena.png 463KB
ResizeWindow.h 3KB
TestResizeWindow.vcproj 8KB
main.cpp 924B
DisplayRule.cpp 5KB
UIDef.h 1011B
MoveAgency.h 958B
ResizeWindow.cpp 17KB
MoveAgency.cpp 2KB
共 14 条
- 1
资源评论
追梦北京
- 粉丝: 3
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功