#include "fullScreenWidget.h"
fullScreenWidget::fullScreenWidget()
{
setWindowState(Qt::WindowActive|Qt::WindowFullScreen);
tipWidth = 300; //温馨提示框的宽度
tipHeight = 100; //温馨提示框的高度
infoWidth = 100; //坐标信息框的宽度
infoHeight = 50; //坐标信息框的高度
initFullScreenWidget();
}
void fullScreenWidget::initSelectedMenu()
{
savePixmapAction = new QAction(tr("保存选区"),this);
cancelAction = new QAction(tr("重选"),this);
quitAction = new QAction(tr("退出"),this);
contextMenu = new QMenu(this);
connect(savePixmapAction,SIGNAL(triggered()),this,SLOT(savePixmap()));
connect(cancelAction,SIGNAL(triggered()),this,SLOT(cancelSelectedRect()));
connect(quitAction,SIGNAL(triggered()),this,SLOT(hide()));
}
void fullScreenWidget::savePixmap()
{
QString fileName;
fileName = QFileDialog::getSaveFileName(this,tr("保存图片"),QDir::currentPath(),tr("Images (*.jpg *.png *.bmp)"));
if(fileName.isNull())
return;
shotPixmap.save(fileName);
hide();
}
void fullScreenWidget::loadBackgroundPixmap(const QPixmap &bgPixmap)
{
int width,height;
width = QApplication::desktop()->size().width();
height = QApplication::desktop()->size().height();
loadBackgroundPixmap(bgPixmap,0,0,width,height);
}
void fullScreenWidget::loadBackgroundPixmap(const QPixmap &bgPixmap, int x, int y, int width, int height)
{
loadPixmap = bgPixmap;
screenx = x;
screeny = y;
screenwidth = width;
screenheight = height;
initFullScreenWidget();
}
QPixmap fullScreenWidget::getFullScreenPixmap()
{
initFullScreenWidget();
QPixmap result = QPixmap();
result = QPixmap::grabWindow(QApplication::desktop()->winId()); //抓取当前屏幕的图片
return result;
}
void fullScreenWidget::paintEvent(QPaintEvent *event)
{
QColor shadowColor;
shadowColor= QColor(0,0,0,100); //阴影颜色设置
painter.begin(this); //进行重绘
painter.setPen(QPen(Qt::blue,2,Qt::SolidLine,Qt::FlatCap));//设置画笔
painter.drawPixmap(screenx,screeny,loadPixmap); //将背景图片画到窗体上
painter.fillRect(screenx,screeny,screenwidth,screenheight,shadowColor); //画影罩效果
switch(currentShotState){
case initShot:
drawTipsText();
break;
case beginShot:
case finishShot:
selectedRect = getRect(beginPoint,endPoint); //获取选区
drawSelectedPixmap();
break;
case beginMoveShot:
case finishMoveShot:
selectedRect = getMoveAllSelectedRect(); //获取选区
drawSelectedPixmap();
break;
case beginControl:
case finishControl:
selectedRect = getMoveControlSelectedRect();
drawSelectedPixmap();
break;
default:
break;
}
drawXYWHInfo(); //打印坐标信息
painter.end(); //重绘结束
if(currentShotState == finishMoveShot || currentShotState == finishControl){
updateBeginEndPointValue(selectedRect); //当移动完选区后,更新beginPoint,endPoint;为下一次移动做准备工作
}
}
void fullScreenWidget::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Escape){
initFullScreenWidget();
hide();
}
}
void fullScreenWidget::mousePressEvent(QMouseEvent *event)
{
//当开始进行拖动进行选择区域时,确定开始选取的beginPoint坐标
if(event->button() == Qt::LeftButton && currentShotState == initShot){
currentShotState = beginShot; //设置当前状态为beginShot状态
beginPoint = event->pos();
}
//移动选区改变选区的所在位置
if(event->button() == Qt::LeftButton && isInSelectedRect(event->pos()) &&
getMoveControlState(event->pos()) == moveControl0){
currentShotState = beginMoveShot; //启用开始移动选取选项,beginMoveShot状态
moveBeginPoint = event->pos();
}
//移动控制点改变选区大小
if(event->button() == Qt::LeftButton && getMoveControlState(event->pos()) != moveControl0){
currentShotState = beginControl; //开始移动控制点
controlValue = getMoveControlState(event->pos());
moveBeginPoint = event->pos();
}
}
void fullScreenWidget::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton && currentShotState == beginShot){
currentShotState = finishShot;
endPoint = event->pos();
update();
}
if(event->button() == Qt::LeftButton && currentShotState == beginMoveShot){
currentShotState = finishMoveShot;
moveEndPoint = event->pos();
update();
}
//当前状态为beginControl状态时,设置状态为finishControl
if(event->button() == Qt::LeftButton && currentShotState == beginControl){
currentShotState = finishControl;
moveEndPoint = event->pos();
update();
}
}
void fullScreenWidget::mouseMoveEvent(QMouseEvent *event)
{
//当拖动时,动态的更新所选择的区域
if(currentShotState == beginShot){
endPoint = event->pos();
update();
}
//当确定选区后,对选区进行移动操作
if(currentShotState == beginMoveShot || currentShotState == beginControl){
moveEndPoint = event->pos();
update();
}
updateMouseShape(event->pos()); //修改鼠标的形状
setMouseTracking(true);
}
void fullScreenWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
if(currentShotState == finishShot || currentShotState == finishMoveShot || currentShotState == finishControl){
emit finishPixmap(shotPixmap); //当完成时发送finishPixmap信号
hide();
}
}
QRect fullScreenWidget::getRect(const QPoint &beginPoint, const QPoint &endPoint)
{
int x,y,width,height;
width = qAbs(beginPoint.x() - endPoint.x());
height = qAbs(beginPoint.y() - endPoint.y());
x = beginPoint.x() < endPoint.x() ? beginPoint.x() : endPoint.x();
y = beginPoint.y() < endPoint.y() ? beginPoint.y() : endPoint.y();
return QRect(x,y,width,height);
}
void fullScreenWidget::initFullScreenWidget()
{
currentShotState = initShot;
controlValue = moveControl0;
beginPoint =QPoint(0,0);
endPoint = QPoint(0,0);
moveBeginPoint = QPoint(0,0);
moveEndPoint = QPoint(0,0);
tlRect = QRect(0,0,0,0); //左上点
trRect = QRect(0,0,0,0); //上右点
blRect = QRect(0,0,0,0); //左下点
brRect = QRect(0,0,0,0); //右下点
tcRect = QRect(0,0,0,0); //上中点
bcRect = QRect(0,0,0,0); //下中点
lcRect = QRect(0,0,0,0); //左中点
rcRect = QRect(0,0,0,0); //右中点
setCursor(Qt::CrossCursor);
}
bool fullScreenWidget::isInSelectedRect(const QPoint &point)
{
int x,y;
QRect selectedRect;
if(currentShotState == initShot || currentShotState == beginShot)
return false;
selectedRect = getSelectedRect();
x = point.x();
y = point.y();
return selectedRect.contains(x,y);
}
void fullScreenWidget::cancelSelectedRect()
{
initFullScreenWidget();
update(); //进行重绘,将选取区域去掉
}
void fullScreenWidget::contextMenuEvent(QContextMenuEvent *event)
{
initSelectedMenu();
if(isInSelectedRect(event->pos())){
contextMenu->addAction(savePixmapAction);
}
else{
contextMenu->addAction(cancelAction);
contextMenu->addAction(quitAction);
}
contextMenu->exec(event->pos());
}
void fullScreenWidget::drawTipsText()
{
int x = (screenwidth - tipWidth)/2;
int y = (screenheight - tipHeight)/2;
QColor color = QColor(100,100,100,200);
QRect rect = QRect(x,y,tipWidth,tipHeight);
QString strTipsText = QString(tr("温馨提示\n鼠标拖动进行截屏;截屏区域内右键保存;\n截屏区域外右键取消;ESC退出;"));
painter.fillRect(rect,color);
painter.setPen(QPen(Qt::white));//设置画笔的颜色为白色
painter.drawText(rect,Qt::AlignCenter,strTipsText);
}
QRect fullScreenWidget::getSelectedRect()
{
if(currentShotState == beginMoveShot){
return getMoveAllSelectedRect();
}
else if(currentShotState == beginControl){
return getMoveControlSelectedRect();
}
else{
return getRect(beginPoint,endPoint);
}
}
void fullScreenWidge
ubuntu opencv qt 三位一体的摄像头源码
1星 需积分: 9 84 浏览量
2013-10-14
17:26:44
上传
评论 3
收藏 46KB GZ 举报
a1992167443
- 粉丝: 0
- 资源: 1