ctai/src/sui.cpp

284 lines
8.8 KiB
C++
Raw Normal View History

2025-02-23 19:26:30 +08:00
#include "sui.h"
sui::sui(QWidget *parent)
: QWidget(parent),
m_layout(new QVBoxLayout),
m_widget(new QWidget),
m_press(false),
m_border_width(2),
ori(NONE)
{
m_title = new sui_title(this);
setObjectName(tr("m_widget"));
m_layout->addWidget(m_title);
m_title->setMouseTracking(true);
m_layout->setContentsMargins(5, 5, 5, 5);
m_layout->setSpacing(0);
setWindowFlags(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
setMouseTracking(true);
2025-02-25 21:28:37 +08:00
setBaseSize(1260, 800);
resize(1260, 800);
2025-02-23 19:26:30 +08:00
setLayout(m_layout);
connect(m_title, SIGNAL(signals_close()), this, SLOT(close()));
}
sui::~sui()
{
}
void sui::showEvent(QShowEvent *event)
{
this->setAttribute(Qt::WA_Mapped);
QWidget::showEvent(event);
QSize oldSize = this->size();
resize(oldSize + QSize(10, 10));
resize(oldSize);
}
void sui::paintEvent(QPaintEvent *event)
{
QStyleOption opt;
opt.initFrom(this);
QPainter painter(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawRoundedRect(this->rect(), 10, 10);
QWidget::paintEvent(event);
}
void sui::region(const QPoint &currentGlobalPoint)
{
// 获取窗体在屏幕上的位置区域topLeft为坐上角点rightButton为右下角点
QRect rect = this->rect();
// 将左上角的(0,0)转化为全局坐标
QPoint topLeft = this->mapToGlobal(rect.topLeft());
QPoint rightButton = this->mapToGlobal(rect.bottomRight());
// 当前鼠标的坐标
int x = currentGlobalPoint.x();
int y = currentGlobalPoint.y();
if (((topLeft.x() + m_border_width >= x) && (topLeft.x() <= x)) && ((topLeft.y() + m_border_width >= y) && (topLeft.y() <= y)))
{
// 左上角
ori = LEFTTOP;
// 设置光标形状
this->setCursor(QCursor(Qt::SizeFDiagCursor));
}
else if (((x >= rightButton.x() - m_border_width) && (x <= rightButton.x())) && ((y >= rightButton.y() - m_border_width) && (y <= rightButton.y())))
{
// 右下角
ori = RIGHTBOTTOM;
this->setCursor(QCursor(Qt::SizeFDiagCursor));
}
else if (((x <= topLeft.x() + m_border_width) && (x >= topLeft.x())) && ((y >= rightButton.y() - m_border_width) && (y <= rightButton.y())))
{
// 左下角
ori = LEFTBOTTOM;
this->setCursor(QCursor(Qt::SizeBDiagCursor));
}
else if (((x <= rightButton.x()) && (x >= rightButton.x() - m_border_width)) && ((y >= topLeft.y()) && (y <= topLeft.y() + m_border_width)))
{
// 右上角
ori = RIGHTTOP;
this->setCursor(QCursor(Qt::SizeBDiagCursor));
}
else if ((x <= topLeft.x() + m_border_width) && (x >= topLeft.x()))
{
// 左边
ori = LEFT;
this->setCursor(QCursor(Qt::SizeHorCursor));
}
else if ((x <= rightButton.x()) && (x >= rightButton.x() - m_border_width))
{
// 右边
ori = RIGHT;
this->setCursor(QCursor(Qt::SizeHorCursor));
}
else if ((y >= topLeft.y()) && (y <= topLeft.y() + m_border_width))
{
// 上边
ori = UP;
this->setCursor(QCursor(Qt::SizeVerCursor));
}
else if ((y <= rightButton.y()) && (y >= rightButton.y() - m_border_width))
{
// 下边
ori = DOWN;
this->setCursor(QCursor(Qt::SizeVerCursor));
}
else
{
// 默认
ori = NONE;
this->setCursor(QCursor(Qt::ArrowCursor));
}
}
void sui::mousePressEvent(QMouseEvent *event)
{
switch (event->button())
{
case Qt::LeftButton:
m_press = true;
if (ori != NONE)
{
// 返回当前抓取鼠标输入的窗口
this->mouseGrabber();
}
else
{
m_point = event->globalPosition().toPoint() - this->frameGeometry().topLeft();
// globalPos()鼠标位置topLeft()窗口左上角的位置
}
break;
case Qt::RightButton:
//this->setWindowState(Qt::WindowMinimized);
break;
default:
QWidget::mousePressEvent(event);
}
}
void sui::mouseMoveEvent(QMouseEvent *event)
{
QPoint globalPoint = event->globalPosition().toPoint();
QRect rect = this->rect();
QPoint topLeft = mapToGlobal(rect.topLeft());
QPoint bottomRight = mapToGlobal(rect.bottomRight());
if (this->windowState() != Qt::WindowMaximized)
{
// 没有按下左键时
if (!m_press)
{
// 窗口大小的改变——判断鼠标位置,改变光标形状
this->region(globalPoint);
}
else
{
if (ori != NONE)
{
QRect newRect(topLeft, bottomRight);
switch (ori)
{
case LEFT:
if (bottomRight.x() - globalPoint.x() <= this->minimumWidth())
{
// 小于界面的最小宽度时设置为左上角横坐标为窗口x
newRect.setLeft(topLeft.x());
// 只改变左边界
}
else
{
newRect.setLeft(globalPoint.x());
}
break;
case RIGHT:
// 只能改变右边界
newRect.setWidth(globalPoint.x() - topLeft.x());
break;
case UP:
if (bottomRight.y() - globalPoint.y() <= this->minimumHeight())
{
newRect.setY(topLeft.y());
}
else
{
newRect.setY(globalPoint.y());
}
break;
case DOWN:
newRect.setHeight(globalPoint.y() - topLeft.y());
break;
case LEFTTOP:
if (bottomRight.x() - globalPoint.x() <= this->minimumWidth())
{
newRect.setX(topLeft.x());
}
else
{
newRect.setX(globalPoint.x());
}
if (bottomRight.y() - globalPoint.y() <= this->minimumHeight())
{
newRect.setY(topLeft.y());
}
else
{
newRect.setY(globalPoint.y());
}
break;
case RIGHTTOP:
if (globalPoint.x() - topLeft.x() >= this->minimumWidth())
{
newRect.setWidth(globalPoint.x() - topLeft.x());
}
else
{
newRect.setWidth(bottomRight.x() - topLeft.x());
}
if (bottomRight.y() - globalPoint.y() >= this->minimumHeight())
{
newRect.setY(globalPoint.y());
}
else
{
newRect.setY(topLeft.y());
}
break;
case LEFTBOTTOM:
if (bottomRight.x() - globalPoint.x() >= this->minimumWidth())
{
newRect.setX(globalPoint.x());
}
else
{
newRect.setX(topLeft.x());
}
if (globalPoint.y() - topLeft.y() >= this->minimumHeight())
{
newRect.setHeight(globalPoint.y() - topLeft.y());
}
else
{
newRect.setHeight(bottomRight.y() - topLeft.y());
}
break;
case RIGHTBOTTOM:
newRect.setWidth(globalPoint.x() - topLeft.x());
newRect.setHeight(globalPoint.y() - topLeft.y());
break;
default:
break;
}
this->setGeometry(newRect);
}
else
{
// 移动窗口
move(event->globalPosition().toPoint() - m_point);
event->accept();
}
}
}
}
// 鼠标释放事件
void sui::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_press = false;
if (ori != NONE)
{
// 释放鼠标抓取
this->releaseMouse();
this->setCursor(QCursor(Qt::ArrowCursor));
ori = NONE;
}
}
}