#include <QWidget>
#include <QMouseEvent>
#include <QEvent>
#include <QRubberBand>
#include "WidgetData.h"
#include "FramelessHelperPrivate.h"
/***** WidgetData *****/
WidgetData::WidgetData(FramelessHelperPrivate *_d, QWidget *pTopLevelWidget)
{
d = _d;
m_pWidget = pTopLevelWidget;
m_bLeftButtonPressed = false;
m_bCursorShapeChanged = false;
m_bLeftButtonTitlePressed = false;
m_pRubberBand = NULL;
m_windowFlags = m_pWidget->windowFlags();
m_pWidget->setMouseTracking(true);
m_pWidget->setAttribute(Qt::WA_Hover, true);
updateRubberBandStatus();
}
WidgetData::~WidgetData()
{
m_pWidget->setMouseTracking(false);
m_pWidget->setWindowFlags(m_windowFlags);
m_pWidget->setAttribute(Qt::WA_Hover, false);
delete m_pRubberBand;
m_pRubberBand = NULL;
}
QWidget* WidgetData::widget()
{
return m_pWidget;
}
void WidgetData::handleWidgetEvent(QEvent *event)
{
switch (event->type())
{
default:
break;
case QEvent::MouseButtonPress:
handleMousePressEvent(static_cast<QMouseEvent*>(event));
break;
case QEvent::MouseButtonRelease:
handleMouseReleaseEvent(static_cast<QMouseEvent*>(event));
break;
case QEvent::MouseMove:
handleMouseMoveEvent(static_cast<QMouseEvent*>(event));
break;
case QEvent::Leave:
handleLeaveEvent(static_cast<QMouseEvent*>(event));
break;
case QEvent::HoverMove:
handleHoverMoveEvent(static_cast<QHoverEvent*>(event));
break;
}
}
void WidgetData::updateRubberBandStatus()
{
if (d->m_bRubberBandOnMove || d->m_bRubberBandOnResize)
{
if (NULL == m_pRubberBand)
m_pRubberBand = new QRubberBand(QRubberBand::Rectangle);
}
else
{
delete m_pRubberBand;
m_pRubberBand = NULL;
}
}
void WidgetData::updateCursorShape(const QPoint &gMousePos)
{
if (m_pWidget->isFullScreen() || m_pWidget->isMaximized())
{
if (m_bCursorShapeChanged)
{
m_pWidget->unsetCursor();
m_bCursorShapeChanged = false;
}
return;
}
m_moveMousePos.recalculate(gMousePos, m_pWidget->frameGeometry());
if (d->m_bWidgetResizable) {
if (m_moveMousePos.m_bOnTopLeftEdge || m_moveMousePos.m_bOnBottomRightEdge)
{
m_pWidget->setCursor(Qt::SizeFDiagCursor);
m_bCursorShapeChanged = true;
return;
}
else if (m_moveMousePos.m_bOnTopRightEdge || m_moveMousePos.m_bOnBottomLeftEdge)
{
m_pWidget->setCursor(Qt::SizeBDiagCursor);
m_bCursorShapeChanged = true;
return;
}
else if (m_moveMousePos.m_bOnLeftEdge || m_moveMousePos.m_bOnRightEdge)
{
m_pWidget->setCursor(Qt::SizeHorCursor);
m_bCursorShapeChanged = true;
return;
}
else if (m_moveMousePos.m_bOnTopEdge || m_moveMousePos.m_bOnBottomEdge)
{
m_pWidget->setCursor(Qt::SizeVerCursor);
m_bCursorShapeChanged = true;
return;
}
}
if (d->m_bWidgetMovable) {
if (m_moveMousePos.m_bOnTitle)
{
m_pWidget->setCursor(Qt::SizeAllCursor);
m_bCursorShapeChanged = true;
return;
}
}
if (m_bCursorShapeChanged)
{
m_pWidget->unsetCursor();
m_bCursorShapeChanged = false;
}
}
void WidgetData::resizeWidget(const QPoint &gMousePos)
{
QRect origRect;
if (d->m_bRubberBandOnResize)
origRect = m_pRubberBand->frameGeometry();
else
origRect = m_pWidget->frameGeometry();
int left = origRect.left();
int top = origRect.top();
int right = origRect.right();
int bottom = origRect.bottom();
origRect.getCoords(&left, &top, &right, &bottom);
int minWidth = m_pWidget->minimumWidth();
int minHeight = m_pWidget->minimumHeight();
if (m_pressedMousePos.m_bOnTopLeftEdge)
{
left = gMousePos.x();
top = gMousePos.y();
}
else if (m_pressedMousePos.m_bOnBottomLeftEdge)
{
left = gMousePos.x();
bottom = gMousePos.y();
}
else if (m_pressedMousePos.m_bOnTopRightEdge)
{
right = gMousePos.x();
top = gMousePos.y();
}
else if (m_pressedMousePos.m_bOnBottomRightEdge)
{
right = gMousePos.x();
bottom = gMousePos.y();
}
else if (m_pressedMousePos.m_bOnLeftEdge)
{
left = gMousePos.x();
}
else if (m_pressedMousePos.m_bOnRightEdge)
{
right = gMousePos.x();
}
else if (m_pressedMousePos.m_bOnTopEdge)
{
top = gMousePos.y();
}
else if (m_pressedMousePos.m_bOnBottomEdge)
{
bottom = gMousePos.y();
}
QRect newRect(QPoint(left, top), QPoint(right, bottom));
if (newRect.isValid())
{
if (minWidth > newRect.width())
{
if (left != origRect.left())
newRect.setLeft(origRect.left());
else
newRect.setRight(origRect.right());
}
if (minHeight > newRect.height())
{
if (top != origRect.top())
newRect.setTop(origRect.top());
else
newRect.setBottom(origRect.bottom());
}
if (d->m_bRubberBandOnResize)
{
m_pRubberBand->setGeometry(newRect);
}
else
{
m_pWidget->setGeometry(newRect);
}
}
}
void WidgetData::moveWidget(const QPoint& gMousePos)
{
if (d->m_bRubberBandOnMove)
{
m_pRubberBand->move(gMousePos - m_ptDragPos);
}
else
{
m_pWidget->move(gMousePos - m_ptDragPos);
}
}
void WidgetData::handleMousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_bLeftButtonPressed = true;
m_bLeftButtonTitlePressed = event->pos().y() < m_moveMousePos.m_nTitleHeight;
QRect frameRect = m_pWidget->frameGeometry();
m_pressedMousePos.recalculate(event->globalPos(), frameRect);
m_ptDragPos = event->globalPos() - frameRect.topLeft();
if (m_pressedMousePos.m_bOnEdges)
{
if (d->m_bRubberBandOnResize)
{
m_pRubberBand->setGeometry(frameRect);
m_pRubberBand->show();
}
}
else if (d->m_bRubberBandOnMove && m_bLeftButtonTitlePressed)
{
m_pRubberBand->setGeometry(frameRect);
m_pRubberBand->show();
}
}
}
void WidgetData::handleMouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_bLeftButtonPressed = false;
m_bLeftButtonTitlePressed = false;
m_pressedMousePos.reset();
if (m_pRubberBand && m_pRubberBand->isVisible())
{
m_pRubberBand->hide();
m_pWidget->setGeometry(m_pRubberBand->geometry());
}
}
}
void WidgetData::handleMouseMoveEvent(QMouseEvent *event)
{
if (m_bLeftButtonPressed)
{
if (d->m_bWidgetResizable && m_pressedMousePos.m_bOnEdges)
{
resizeWidget(event->globalPos());
}
else if (d->m_bWidgetMovable && m_bLeftButtonTitlePressed)
{
moveWidget(event->globalPos());
}
}
else if (d->m_bWidgetResizable || d->m_bWidgetMovable)
{
updateCursorShape(event->globalPos());
}
}
void WidgetData::handleLeaveEvent(QEvent *event)
{
Q_UNUSED(event)
if (!m_bLeftButtonPressed)
{
m_pWidget->unsetCursor();
}
}
void WidgetData::handleHoverMoveEvent(QHoverEvent *event)
{
if (d->m_bWidgetResizable)
{
updateCursorShape(m_pWidget->mapToGlobal(event->pos()));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt 自定义界面(窗体缩放-跨平台终极版) 无边框 frameless helper widget
共17个文件
h:5个
cpp:5个
log:2个
5星 · 超过95%的资源 需积分: 50 209 下载量 66 浏览量
2017-11-15
15:20:03
上传
评论 13
收藏 11KB RAR 举报
温馨提示
使用 installfilter 截获窗口事件来缩放和移动窗口, 不需要派生, 适合任何从QWidget 派生而来的窗口。 http://blog.csdn.net/liang19890820/article/details/50557240 修改自 一去丶二三里 整理的代码。
资源推荐
资源详情
资源评论
收起资源包目录
frameless_helper_test.rar (17个子文件)
frameless_helper_test
frameless_helper_test.cpp 718B
framelesshelper
CursorPosCalculator.cpp 2KB
CursorPosCalculator.h 836B
WidgetData.h 1KB
WidgetData.cpp 8KB
FramelessHelperPrivate.h 440B
frameless_helper.cpp 3KB
frameless_helper.h 1KB
Resources
frameless_helper_test.vcxproj.user 631B
frameless_helper_test.vcxproj.filters 4KB
frameless_helper_test.h 273B
GeneratedFiles
Release
Debug
Debug
frameles.60E2F368.tlog
frameless_helper_test.Build.CppClean.log 2KB
frameless_helper_test.log 3B
frameless_helper_test.vcxproj 12KB
frameless_helper_test.qrc 81B
main.cpp 201B
frameless_helper_test.ui 860B
共 17 条
- 1
xh286286
- 粉丝: 3
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页