#include "AutoHideWidget.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QStyle>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QApplication>
#include <QDesktopWidget>
AutoHideWidget::AutoHideWidget(QWidget *parent)
: QWidget(parent)
, m_mouseMovePoint(0, 0)
, m_bMousePressed(false)
, m_enDriection(None)
, m_bIsAutoHide(false)
{
setWindowFlags(Qt::FramelessWindowHint);
resize(250, 500);
m_nDesktopWidth = QApplication::desktop()->width();
init();
}
AutoHideWidget::~AutoHideWidget()
{
}
void AutoHideWidget::init()
{
m_pTitleWidget = new QWidget(this);
m_pTitleWidget->setFixedHeight(36);
m_pTitleWidget->setStyleSheet("background-color: #25A7E9;");
m_pTitleWidget->installEventFilter(this);
QPushButton *pCloseButton = new QPushButton(this);
pCloseButton->setFixedSize(25, 25);
pCloseButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton));
pCloseButton->setFlat(true);
pCloseButton->setStyleSheet("QPushButton"
"{"
"border: none;"
"}"
"QPushButton:hover"
"{"
"background: #30B7EA;"
"}");
connect(pCloseButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *pTitleLayout = new QHBoxLayout;
pTitleLayout->setContentsMargins(0,0,10,0);
pTitleLayout->addStretch();
pTitleLayout->addWidget(pCloseButton);
m_pTitleWidget->setLayout(pTitleLayout);
m_pContentWidget = new QWidget(this);
QVBoxLayout *pMainlayout = new QVBoxLayout;
pMainlayout->setMargin(0);
pMainlayout->setSpacing(0);
pMainlayout->addWidget(m_pTitleWidget);
pMainlayout->addWidget(m_pContentWidget);
this->setLayout(pMainlayout);
}
void AutoHideWidget::isAutoHide()
{
QPoint pos = this->pos();
m_bIsAutoHide = true;
if (pos.x() < 2)
{
m_enDriection = Left;
}
else if (pos.y() < 2)
{
m_enDriection = Up;
}
else if (this->pos().x() + this->width() > m_nDesktopWidth - 2)
{
m_enDriection = Right;
}
else
{
m_enDriection = None;
m_bIsAutoHide = false;
}
}
void AutoHideWidget::hideWidget()
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(100);
animation->setStartValue(QRect(this->pos(), this->size()));
QRect rcEnd;
if (m_enDriection & Up)
{
rcEnd = QRect(this->x(), -this->height() + 2, this->size().width(), this->rect().height());
}
else if (m_enDriection & Left)
{
rcEnd = QRect(-this->width() + 2, this->y(), this->size().width(), this->rect().height());
}
else if (m_enDriection & Right)
{
rcEnd = QRect(m_nDesktopWidth - 2, this->y(), this->size().width(), this->rect().height());
}
animation->setEndValue(rcEnd);
animation->start();
}
void AutoHideWidget::showWidget()
{
QPoint pos = this->pos();
QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
animation->setDuration(100);
animation->setStartValue(QRect(pos, this->size()));
QRect rcEnd;
if (m_enDriection & Up)
{
rcEnd = QRect(this->x(), 0, this->size().width(), this->rect().height());
}
else if (m_enDriection & Left)
{
rcEnd = QRect(0, this->y(), this->size().width(), this->rect().height());
}
else if (m_enDriection & Right)
{
rcEnd = QRect(m_nDesktopWidth - this->width(), this->y(), this->size().width(), this->rect().height());
}
animation->setEndValue(rcEnd);
animation->start();
}
bool AutoHideWidget::eventFilter(QObject *watched, QEvent *event)
{
if (m_pTitleWidget == watched)
{
QMouseEvent *pMouseEvent = dynamic_cast<QMouseEvent*>(event);
if (pMouseEvent != NULL)
{
if (pMouseEvent->type() == QEvent::MouseButtonPress)
{
if (pMouseEvent->button() == Qt::LeftButton)
{
m_bMousePressed = true;
m_mouseMovePoint = pMouseEvent->pos();
}
}
else if (m_bMousePressed && pMouseEvent->type() == QEvent::MouseMove)
{
if (!this->isMaximized())
{
QPoint movePoint = pMouseEvent->globalPos();
this->move(movePoint - m_mouseMovePoint);
}
}
else if (pMouseEvent->type() == QEvent::MouseButtonRelease)
{
m_bMousePressed = false;
}
}
}
return QWidget::eventFilter(watched, event);
}
bool AutoHideWidget::event(QEvent *event)
{
if (m_bMousePressed && event->type() == QEvent::Move)
{
QMoveEvent *pMoveEvent = dynamic_cast<QMoveEvent *>(event);
if (pMoveEvent != NULL)
{
QPoint pos = pMoveEvent->pos();
if (pos.x() < 0)
{
pos.setX(0);
}
if (pos.y() < 0)
{
pos.setY(0);
}
if (pos.x() + this->width() > m_nDesktopWidth)
{
pos.setX(m_nDesktopWidth - this->width());
}
event->ignore();
this->move(pos);
}
}
return QWidget::event(event);
}
void AutoHideWidget::leaveEvent(QEvent *event)
{
isAutoHide();
if (m_bIsAutoHide)
{
hideWidget();
}
}
void AutoHideWidget::enterEvent(QEvent *event)
{
if (m_bIsAutoHide)
{
showWidget();
}
}
- 1
- 2
- 3
前往页