2019-08-25 16:28:08 +08:00
|
|
|
|
#include "qqtbodyresizer_p.h"
|
|
|
|
|
#include "qqtbodyresizer.h"
|
2019-08-25 11:14:05 +08:00
|
|
|
|
|
2019-08-25 16:28:08 +08:00
|
|
|
|
#include <QObject>
|
2019-08-25 11:14:05 +08:00
|
|
|
|
#include <QWidget>
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
//#pragma comment(lib, "user32.lib")
|
|
|
|
|
#include <qt_windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <qqtcore.h>
|
|
|
|
|
|
2019-08-25 16:28:08 +08:00
|
|
|
|
QQtBodyResizerPrivate::QQtBodyResizerPrivate ( QQtBodyResizer* q ) :
|
|
|
|
|
q_ptr ( q )
|
2019-08-25 11:14:05 +08:00
|
|
|
|
{
|
2019-08-25 16:28:08 +08:00
|
|
|
|
bMousePressed = false;
|
|
|
|
|
margins = QMargins ( 10, 10, 10, 10 );
|
2019-08-25 11:14:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 16:28:08 +08:00
|
|
|
|
QQtBodyResizerPrivate::~QQtBodyResizerPrivate()
|
2019-08-25 11:14:05 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 16:28:08 +08:00
|
|
|
|
void QQtBodyResizerPrivate::mousePressEvent ( QMouseEvent* event, QWidget* target )
|
2019-08-25 11:14:05 +08:00
|
|
|
|
{
|
|
|
|
|
Q_ASSERT ( target );
|
|
|
|
|
|
|
|
|
|
if ( event->button() == Qt::LeftButton )
|
|
|
|
|
{
|
|
|
|
|
bMousePressed = true;
|
|
|
|
|
pressedPoint = event->globalPos();
|
|
|
|
|
}
|
2019-08-25 16:28:08 +08:00
|
|
|
|
|
|
|
|
|
checkDirection ( event, target );
|
|
|
|
|
|
2019-08-25 11:14:05 +08:00
|
|
|
|
event->accept();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 16:28:08 +08:00
|
|
|
|
void QQtBodyResizerPrivate::mouseReleaseEvent ( QMouseEvent* event, QWidget* target )
|
2019-08-25 11:14:05 +08:00
|
|
|
|
{
|
|
|
|
|
Q_ASSERT ( target );
|
|
|
|
|
bMousePressed = false;
|
2019-08-25 16:28:08 +08:00
|
|
|
|
direction = None;
|
2019-08-25 11:14:05 +08:00
|
|
|
|
event->accept();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 16:28:08 +08:00
|
|
|
|
void QQtBodyResizerPrivate::mouseMoveEvent ( QMouseEvent* event, QWidget* target )
|
2019-08-25 11:14:05 +08:00
|
|
|
|
{
|
|
|
|
|
Q_ASSERT ( target );
|
2019-08-25 16:28:08 +08:00
|
|
|
|
|
2019-08-25 11:14:05 +08:00
|
|
|
|
QWidget* win = target->window();
|
2019-08-25 16:28:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//如果鼠标没有press,遭遇到窗体边缘,变换鼠标样式。
|
|
|
|
|
if ( !bMousePressed && !win->isMaximized() )
|
|
|
|
|
{
|
|
|
|
|
checkDirection ( event, target );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//如果鼠标pressed,那么移动。
|
2019-08-25 11:14:05 +08:00
|
|
|
|
if ( bMousePressed && !win->isMaximized() )
|
|
|
|
|
{
|
2019-08-25 16:28:08 +08:00
|
|
|
|
QRect oldGeometry = target->geometry();
|
|
|
|
|
QPoint widgetPos = oldGeometry.topLeft();
|
|
|
|
|
|
|
|
|
|
int rx, ry;
|
|
|
|
|
int x, y, width = 0, height = 0;
|
|
|
|
|
|
|
|
|
|
QRect newGeometry = oldGeometry;
|
|
|
|
|
|
|
|
|
|
switch ( direction )
|
|
|
|
|
{
|
|
|
|
|
case Left: // left
|
|
|
|
|
//cursor
|
|
|
|
|
rx = event->globalPos().x() - pressedPoint.x();
|
|
|
|
|
ry = 0;
|
|
|
|
|
|
|
|
|
|
//widget
|
|
|
|
|
x = widgetPos.x() + rx;
|
|
|
|
|
y = widgetPos.y();
|
|
|
|
|
width = oldGeometry.width() - rx;
|
|
|
|
|
height = oldGeometry.height();
|
|
|
|
|
|
|
|
|
|
newGeometry = QRect ( x, y, width, height );
|
|
|
|
|
|
|
|
|
|
//pline() << event->globalPos().x() << pressedPoint.x() << rx << ry;
|
|
|
|
|
//pline() << oldGeometry << newGeometry;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Right: //right
|
|
|
|
|
//cursor
|
|
|
|
|
rx = event->globalPos().x() - pressedPoint.x();
|
|
|
|
|
ry = 0;
|
|
|
|
|
|
|
|
|
|
//widget
|
|
|
|
|
x = widgetPos.x();
|
|
|
|
|
y = widgetPos.y();
|
|
|
|
|
width = oldGeometry.width() + rx;
|
|
|
|
|
height = oldGeometry.height();
|
|
|
|
|
|
|
|
|
|
newGeometry = QRect ( x, y, width, height );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LeftTop: //left top
|
|
|
|
|
//cursor
|
|
|
|
|
rx = event->globalPos().x() - pressedPoint.x();
|
|
|
|
|
ry = event->globalPos().y() - pressedPoint.y();
|
|
|
|
|
|
|
|
|
|
//widget
|
|
|
|
|
x = widgetPos.x() + rx;
|
|
|
|
|
y = widgetPos.y() + ry;
|
|
|
|
|
width = oldGeometry.width() - rx;
|
|
|
|
|
height = oldGeometry.height() - ry;
|
|
|
|
|
|
|
|
|
|
newGeometry = QRect ( x, y, width, height );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RightTop: //right top
|
|
|
|
|
//cursor
|
|
|
|
|
rx = event->globalPos().x() - pressedPoint.x();
|
|
|
|
|
ry = event->globalPos().y() - pressedPoint.y();
|
|
|
|
|
|
|
|
|
|
//widget
|
|
|
|
|
x = widgetPos.x();
|
|
|
|
|
y = widgetPos.y() + ry;
|
|
|
|
|
width = oldGeometry.width() + rx;
|
|
|
|
|
height = oldGeometry.height() - ry;
|
|
|
|
|
|
|
|
|
|
newGeometry = QRect ( x, y, width, height );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LeftBottom: //left bottom
|
|
|
|
|
//cursor
|
|
|
|
|
rx = event->globalPos().x() - pressedPoint.x();
|
|
|
|
|
ry = event->globalPos().y() - pressedPoint.y();
|
|
|
|
|
|
|
|
|
|
//widget
|
|
|
|
|
x = widgetPos.x() + rx;
|
|
|
|
|
y = widgetPos.y();
|
|
|
|
|
width = oldGeometry.width() - rx;
|
|
|
|
|
height = oldGeometry.height() + ry;
|
|
|
|
|
|
|
|
|
|
newGeometry = QRect ( x, y, width, height );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case RightBottom: //right bottom
|
|
|
|
|
//cursor
|
|
|
|
|
rx = event->globalPos().x() - pressedPoint.x();
|
|
|
|
|
ry = event->globalPos().y() - pressedPoint.y();
|
|
|
|
|
|
|
|
|
|
//widget
|
|
|
|
|
x = widgetPos.x();
|
|
|
|
|
y = widgetPos.y();
|
|
|
|
|
width = oldGeometry.width() + rx;
|
|
|
|
|
height = oldGeometry.height() + ry;
|
|
|
|
|
|
|
|
|
|
newGeometry = QRect ( x, y, width, height );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Top: //top
|
|
|
|
|
//cursor
|
|
|
|
|
rx = 0;
|
|
|
|
|
ry = event->globalPos().y() - pressedPoint.y();
|
|
|
|
|
|
|
|
|
|
//widget
|
|
|
|
|
x = widgetPos.x();
|
|
|
|
|
y = widgetPos.y() + ry;
|
|
|
|
|
width = oldGeometry.width();
|
|
|
|
|
height = oldGeometry.height() - ry;
|
|
|
|
|
|
|
|
|
|
newGeometry = QRect ( x, y, width, height );
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Bottom: //bottom
|
|
|
|
|
//cursor
|
|
|
|
|
rx = 0;
|
|
|
|
|
ry = event->globalPos().y() - pressedPoint.y();
|
|
|
|
|
|
|
|
|
|
//widget
|
|
|
|
|
x = widgetPos.x();
|
|
|
|
|
y = widgetPos.y();
|
|
|
|
|
width = oldGeometry.width();
|
|
|
|
|
height = oldGeometry.height() + ry;
|
|
|
|
|
|
|
|
|
|
newGeometry = QRect ( x, y, width, height );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 11:14:05 +08:00
|
|
|
|
pressedPoint = event->globalPos();
|
2019-08-25 16:28:08 +08:00
|
|
|
|
|
|
|
|
|
if ( width > ( margins.left() + margins.right() ) &&
|
|
|
|
|
height > ( margins.top() + margins.bottom() ) )
|
|
|
|
|
{
|
|
|
|
|
//target->move ( x, y );
|
|
|
|
|
//target->resize ( width, height );
|
|
|
|
|
target->setGeometry ( newGeometry );
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 11:14:05 +08:00
|
|
|
|
}
|
2019-08-25 16:28:08 +08:00
|
|
|
|
|
2019-08-25 11:14:05 +08:00
|
|
|
|
event->accept();
|
2019-08-25 16:28:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QQtBodyResizerPrivate::checkDirection ( QMouseEvent* event, QWidget* target )
|
|
|
|
|
{
|
|
|
|
|
QRect rectMustIn = target->geometry();//target->frameGeometry();
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
|
|
|
|
|
QRect rectMustNotIn = rectMustIn.marginsRemoved ( margins );
|
|
|
|
|
#else
|
|
|
|
|
QRect rectMustNotIn = rectMustIn.adjusted ( margins.left(), margins.top(), margins.right(), margins.bottom() );
|
2019-08-25 11:14:05 +08:00
|
|
|
|
#endif
|
2019-08-25 16:28:08 +08:00
|
|
|
|
QPoint cursorPos = event->globalPos();//QCursor::pos();
|
|
|
|
|
|
|
|
|
|
if ( target->isMaximized() ||
|
|
|
|
|
!target->isActiveWindow() ||
|
|
|
|
|
!rectMustIn.contains ( cursorPos ) ||
|
|
|
|
|
rectMustNotIn.contains ( cursorPos ) )
|
|
|
|
|
{
|
|
|
|
|
direction = None;
|
|
|
|
|
target->unsetCursor();
|
|
|
|
|
event->ignore();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int baseLeftCoord, baseTopCoord, baseRightCoord, baseBottomCoord;
|
|
|
|
|
rectMustNotIn.getCoords ( &baseLeftCoord, &baseTopCoord, &baseRightCoord, &baseBottomCoord );
|
|
|
|
|
|
|
|
|
|
int x, y;
|
|
|
|
|
x = cursorPos.x();
|
|
|
|
|
y = cursorPos.y();
|
|
|
|
|
|
|
|
|
|
direction = None;
|
|
|
|
|
|
|
|
|
|
if ( x < baseLeftCoord )
|
|
|
|
|
{
|
|
|
|
|
if ( y < baseTopCoord )
|
|
|
|
|
{
|
|
|
|
|
direction = LeftTop;
|
|
|
|
|
}
|
|
|
|
|
else if ( y > baseBottomCoord )
|
|
|
|
|
{
|
|
|
|
|
direction = LeftBottom;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
direction = Left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if ( x > baseRightCoord )
|
|
|
|
|
{
|
|
|
|
|
if ( y < baseTopCoord )
|
|
|
|
|
{
|
|
|
|
|
direction = RightTop;
|
|
|
|
|
}
|
|
|
|
|
else if ( y > baseBottomCoord )
|
|
|
|
|
{
|
|
|
|
|
direction = RightBottom;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
direction = Right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( direction == None )
|
|
|
|
|
{
|
|
|
|
|
if ( y < baseTopCoord )
|
|
|
|
|
{
|
|
|
|
|
direction = Top;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
direction = Bottom;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch ( direction )
|
|
|
|
|
{
|
|
|
|
|
case Left:
|
|
|
|
|
case Right:
|
|
|
|
|
target->setCursor ( Qt::SizeHorCursor );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Top:
|
|
|
|
|
case Bottom:
|
|
|
|
|
target->setCursor ( Qt::SizeVerCursor );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LeftTop:
|
|
|
|
|
case RightBottom:
|
|
|
|
|
target->setCursor ( Qt::SizeFDiagCursor );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case LeftBottom:
|
|
|
|
|
case RightTop:
|
|
|
|
|
target->setCursor ( Qt::SizeBDiagCursor );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 11:14:05 +08:00
|
|
|
|
}
|