#include "WinNoFrameUI.h"
#include <QToolButton>
#include <QMouseEvent>
#include <QStyle>
#include <QHBoxLayout>
#include <QApplication>
#include <QDesktopWidget>
#include <QDebug>
WinNoFrameUI::WinNoFrameUI(QWidget *parent)
: QWidget(parent)
{
m_bMoved = false;
this->setMouseTracking(true); // 伸缩窗口时必须设置
curPos=0;//标记鼠标左击时的位置
CreateWindowButton();
}
WinNoFrameUI::~WinNoFrameUI()
{
}
void WinNoFrameUI::SetWindowTitle(const QString title)
{
m_pTitleTBtn->setText(title);
}
void WinNoFrameUI::SetWindowTitleIcon(const QIcon icon)
{
m_pTitleTBtn->setIcon(icon);
}
void WinNoFrameUI::SetWindowTitleColor(const QString sColor)
{
m_pTitleTBtn->setStyleSheet(QString("color:%1").arg(sColor));
}
void WinNoFrameUI::CreateWindowButton()
{
// 去掉窗口边框
setWindowFlags(Qt::FramelessWindowHint);
// 设置窗口名称
m_pTitleTBtn = new QToolButton;
m_pTitleTBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
m_pTitleTBtn->setStyleSheet("background-color:transparent;border:none;");
// 构建最小化、最大化、关闭按钮
m_pMinTBtn = new QToolButton(this);
m_pMaxTBtn = new QToolButton(this);
m_pCloseTBtn = new QToolButton(this);
// 设置最小化、最大化、关闭按钮图标
m_pMinTBtn->setIcon(style()->standardPixmap(QStyle::SP_TitleBarMinButton));
m_pMaxTBtn->setIcon(style()->standardPixmap(QStyle::SP_TitleBarMaxButton));
m_pCloseTBtn->setIcon(style()->standardPixmap(QStyle::SP_TitleBarCloseButton));
// 设置鼠标移至按钮上的提示信息
m_pMinTBtn->setToolTip(tr("最小化"));
m_pMaxTBtn->setToolTip(tr("最大化"));
m_pCloseTBtn->setToolTip(tr("关闭"));
// 设置最小化、关闭按钮的样式
m_pMinTBtn->setStyleSheet("background-color:transparent;border:none;");
m_pMaxTBtn->setStyleSheet("background-color:transparent;border:none;");
m_pCloseTBtn->setStyleSheet("background-color:transparent;border:none;");
//////////////////////////////////////////////////////////////////////////
// 设置分割线
m_pLine = new QFrame;
m_pLine->setFrameShape(QFrame::HLine);
m_pLine->setFrameShadow(QFrame::Sunken);
m_pLine->setStyleSheet("QFrame{background:#40a7b1;}");
//////////////////////////////////////////////////////////////////////////
QHBoxLayout * pHLayout = new QHBoxLayout;
pHLayout->setMargin(0);
pHLayout->addWidget(m_pTitleTBtn);
pHLayout->addStretch();
pHLayout->addWidget(m_pMinTBtn);
pHLayout->addWidget(m_pMaxTBtn);
pHLayout->addWidget(m_pCloseTBtn);
m_pWidget = new QWidget;
QVBoxLayout *pVLayout = new QVBoxLayout(m_pWidget);
pVLayout->setMargin(0);
pVLayout->addLayout(pHLayout);
pVLayout->addWidget(m_pLine);
// 关联最小化、最大化、关闭按钮的槽函数
connect(m_pMinTBtn, SIGNAL(clicked()), this, SLOT(showMinWindow()));
connect(m_pMaxTBtn, SIGNAL(clicked()), this, SLOT(showMaxWindow()));
connect(m_pCloseTBtn, SIGNAL(clicked()), this, SLOT(close()));
}
// 计算鼠标在哪一列和哪一行
int WinNoFrameUI::countFlag(QPoint p,int row)
{
if(p.y() < MARGIN)
return 10 + row;
else if(p.y() > this->height() - MARGIN)
return 30 + row;
else
return 20 + row;
}
// 根据鼠标所在位置改变鼠标指针形状
void WinNoFrameUI::setCursorType(int flag)
{
Qt::CursorShape cursor;
switch(flag)
{
case 11:
case 33:
cursor = Qt::SizeFDiagCursor;
break;
case 13:
case 31:
cursor = Qt::SizeBDiagCursor;
break;
case 21:
case 23:
cursor = Qt::SizeHorCursor;
break;
case 12:
case 32:
cursor = Qt::SizeVerCursor;
break;
case 22:
cursor = Qt::ArrowCursor;
break;
//cursor=Qt::OpenHandCursor;break;
default:
// QApplication::restoreOverrideCursor();//恢复鼠标指针性状
break;
}
setCursor(cursor);
}
// 计算在哪一列
int WinNoFrameUI::countRow(QPoint p)
{
return (p.x() < MARGIN) ? 1 : (p.x() > (this->width() - MARGIN) ? 3 : 2);
}
void WinNoFrameUI::showMinWindow()
{
this->showMinimized();
}
void WinNoFrameUI::showMaxWindow()
{
if (isMaximized())
{
showNormal();
m_pMaxTBtn->setIcon(style()->standardPixmap(QStyle::SP_TitleBarMaxButton));
}
else
{
showMaximized();
m_pMaxTBtn->setIcon(style()->standardPixmap(QStyle::SP_TitleBarNormalButton));
}
}
void WinNoFrameUI::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_Drag = true;
m_bMoved = true;
m_DragPosition = event->globalPos() - this->pos();
curPos=countFlag(event->pos(),countRow(event->pos()));
event->accept();
}
}
void WinNoFrameUI::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
showMaxWindow();
event->accept();
}
}
void WinNoFrameUI::mouseMoveEvent(QMouseEvent *event)
{
int poss = countFlag(event->pos(),countRow(event->pos()));
if (!m_bMoved)
{
setCursorType(poss);
}
if(m_Drag && (event->buttons() && Qt::LeftButton))//是否左击
{
QPoint cursorPos = QCursor::pos();
if(curPos == 22)
{
// 移动窗口
if (isMaximized())
{
showNormal();
m_pMaxTBtn->setIcon(style()->standardPixmap(QStyle::SP_TitleBarMaxButton));
QRect wid = geometry();
m_DragPosition = event->globalPos() - QPoint(wid.left(), cursorPos.y());
}
move(event->globalPos() - m_DragPosition);
}
else
{
QRect wid = geometry();
QSize winSize = this->minimumSize();
int pX1, pX2, pY1, pY2;
// 改变窗口的大小
switch(curPos)
{
case 11: // 左上角
pX1 = cursorPos.x();
pX2 = wid.right();
pY1 = cursorPos.y();
pY2 = wid.bottom();
if (abs(pX2 - pX1) > winSize.width() && abs(pY2 - pY1) > winSize.height())
{
wid.setTopLeft(cursorPos);
}
else if (abs(pX2 - pX1) > winSize.width())
{
wid.setLeft(cursorPos.x());
}
else if (abs(pY2 - pY1) > winSize.height())
{
wid.setTop(cursorPos.y());
}
break;
case 13: // 右上角
pX1 = cursorPos.x();
pX2 = wid.left();
pY1 = cursorPos.y();
pY2 = wid.bottom();
if (abs(pX2 - pX1) > winSize.width() && abs(pY2 - pY1) > winSize.height())
{
wid.setTopRight(cursorPos);
}
else if (abs(pX2 - pX1) > winSize.width())
{
wid.setRight(cursorPos.x());
}
else if (abs(pY2 - pY1) > winSize.height())
{
wid.setTop(cursorPos.y());
}
break;
case 31: // 左下角
pX1 = cursorPos.x();
pX2 = wid.right();
pY1 = cursorPos.y();
pY2 = wid.top();
if (abs(pX2 - pX1) > winSize.width() && abs(pY2 - pY1) > winSize.height())
{
wid.setBottomLeft(cursorPos);
}
else if (abs(pX2 - pX1) > winSize.width())
{
wid.setLeft(cursorPos.x());
}
else if (abs(pY2 - pY1) > winSize.height())
{
wid.setBottom(cursorPos.y());
}
break;
case 33: // 右下角
wid.setBottomRight(cursorPos);
break;
case 12: // 中上角
pY1 = cursorPos.y();
pY2 = wid.bottom();
if (abs(pY2 - pY1) > winSize.height())
{
wid.setTop(cursorPos.y());
}
break;
case 21: // 中左角
pX1 = cursorPos.x();
pX2 = wid.right();
if (abs(pX2 - pX1) > winSize.width())
{
wid.setLeft(cursorPos.x());
}
break;
case 23: // 中右角
wid.setRight(cursorPos.x());
break;
case 32: // 中下角
wid.setBottom(cursorPos.y());
break;
}
setGeometry(wid);
}
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
}
void WinNoFrameUI::mouseReleaseEvent(QMouseEvent *event)
{
m_Drag = false;
m_bMoved = false;
setCursor(Qt::ArrowCursor);
}