#include "customBorderContainer.h"
#include <QPainter>
#include <QDialog>
#include <QPropertyAnimation>

CustomBorderContainer::CustomBorder::CustomBorder(QWidget* parent, borderType type, CustomBorderContainer* container) : QLabel(parent)
{
    //setParent(parent);
    m_type = type;
    m_pContainer = container;
    m_bMousIsPress = false;
    m_bShowPainterDraw = false;

    if(m_type == border_left)
    {
        setCursor(Qt::SizeHorCursor);
        m_strOperation = "resizeByLeft";
    }
    else if(m_type == border_right)
    {
        setCursor(Qt::SizeHorCursor);
        m_strOperation = "resizeByRight";
    }
    else if(m_type == border_top)
    {
        setCursor(Qt::SizeVerCursor);
        m_strOperation = "resizeByTop";
    }
    else if(m_type == border_bottom)
    {
        setCursor(Qt::SizeVerCursor);
        m_strOperation = "resizeByBottom";
    }
    else if(m_type == border_leftTop)
    {
        setCursor(Qt::SizeFDiagCursor);
        m_strOperation = "resizeByLeftTop";
    }
    else if(m_type == border_rightBottom)
    {
        setCursor(Qt::SizeFDiagCursor);
        m_strOperation = "resizeByRightBottom";
    }
    else if(m_type == border_leftBottom)
    {
        setCursor(Qt::SizeBDiagCursor);
        m_strOperation = "resizeByLeftBottom";
    }
    else if(m_type == border_rightTop)
    {
        setCursor(Qt::SizeBDiagCursor);
        m_strOperation = "resizeByRightTop";
    }
    else if(m_type == border_move)
    {
        setCursor(Qt::ArrowCursor);
        m_strOperation = "move";
    }
}

void CustomBorderContainer::CustomBorder::paintEvent(QPaintEvent* e)
{
    if(m_type == border_move && m_bShowPainterDraw)
    {
        QPainter painter(this);
        QBrush brush;
        brush.setColor(QColor(250, 250, 250));
        brush.setStyle(Qt::Dense7Pattern);
        painter.setBrush(brush);
        painter.setPen(QPen(Qt::transparent));
        painter.drawRect(QRect((width() - 30)*0.5, 5, 30, 8));
    }
    /*else if(m_type == border_right)
    {
        QPainter painter(this);
        QBrush brush;
        brush.setColor(QColor(250, 250, 250));
        brush.setStyle(Qt::SolidPattern);
        painter.setBrush(brush);
        painter.setPen(QPen(Qt::transparent));
        painter.drawRect(QRect(0, 0, width(), height()));
    }*/
}

void CustomBorderContainer::CustomBorder::mousePressEvent(QMouseEvent* event)
{
    if(m_pContainer->m_opOptions == OperationOption::WidgetNoOperation)
    {
        //setCursor(Qt::ArrowCursor);
        return;
    }

    if(event->button() != Qt::LeftButton)
        return;

    m_bMousIsPress = true;
    m_ptMouseLast = event->globalPosition().toPoint();

    QLabel::mousePressEvent(event);
}
void CustomBorderContainer::CustomBorder::mouseMoveEvent(QMouseEvent* event)
{
    if(m_bMousIsPress)
    {
        QPoint moveLength = event->globalPosition().toPoint() - m_ptMouseLast;
        m_mouseMoveLengt = moveLength;
        switch(m_type)
        {
        case border_left:
            m_pContainer->scaleByLeftBorder(moveLength);
            break;
        case border_right:
            m_pContainer->scaleByRightBorder(moveLength);
            break;
        case border_top:
            m_pContainer->scaleByTopBorder(moveLength);
            break;
        case border_bottom:
            m_pContainer->scaleByBottomBorder(moveLength);
            break;
        case border_leftTop:
            m_pContainer->scaleByLeftTopBorder(moveLength);
            break;
        case border_leftBottom:
            m_pContainer->scaleByLeftBottomBorder(moveLength);
            break;
        case border_rightTop:
            m_pContainer->scaleByRightTopBorder(moveLength);
            break;
        case border_rightBottom:
            m_pContainer->scaleByRightBottomBorder(moveLength);
            break;
        case border_move:
            m_pContainer->moveByMoveBorder(moveLength);
            break;
        default:
            break;
        }
        m_ptMouseLast = event->globalPosition().toPoint();
    }

    // if(!m_repaintTimer.isActive())
    // {
    //     m_repaintTimer.start(16, this);
    // }

    QLabel::mouseMoveEvent(event);
}
void CustomBorderContainer::CustomBorder::mouseReleaseEvent(QMouseEvent* event)
{
    Q_UNUSED(event)
    m_bMousIsPress = false;
    //if(container->m_opOptions & OperationOption::WidgetAutoAdjustGeometry)
    if(m_pContainer->m_opOptions.testFlag(OperationOption::WidgetAutoAdjustGeometry))
        m_pContainer->autoAdjustGeometry(m_strOperation);
    else
        m_pContainer->updateBorder();

    QLabel::mouseReleaseEvent(event);
}

void CustomBorderContainer::CustomBorder::timerEvent(QTimerEvent* event)
{
    if(event->timerId() == m_repaintTimer.timerId())
    {
        m_repaintTimer.stop();
        m_pContainer->updateContentWidget();
    }
}

void CustomBorderContainer::CustomBorder::showPainterDraw(bool bShow)
{
    m_bShowPainterDraw = bShow;
}

CustomBorderContainer::CustomBorderContainer(QWidget *parent, int minWindowWidth, int minWindowHeight, int borderSize, int autoAdjustDistance)
    : QObject(parent)
{
    m_pWidget = parent;
    //setParent(parent);
    m_nMinWindowSize_width = minWindowWidth;
    m_nMinWindowSize_height = minWindowHeight;
    m_nBorderSize = borderSize;
    m_nAutoAdjustDistance = autoAdjustDistance;
    m_opOptions = DefaultOperationOption;
    m_pAnimation_pos = nullptr;
    m_pAnimation_geometry = nullptr;
    iniBorder();
}

CustomBorderContainer::~CustomBorderContainer()
{}

void CustomBorderContainer::iniBorder()
{
    m_pBorder_Left = new CustomBorder(m_pWidget, border_left, this);
    m_pBorder_Left->setStyleSheet("background-color:transparent");
    m_pBorder_Left->raise();

    m_pBorder_Right = new CustomBorder(m_pWidget, border_right, this);
    m_pBorder_Right->setStyleSheet("background-color:transparent");
    m_pBorder_Right->raise();

    m_pBorder_Top = new CustomBorder(m_pWidget, border_top, this);
    m_pBorder_Top->setStyleSheet("background-color:transparent");
    m_pBorder_Top->raise();

    m_pBorder_Bottom = new CustomBorder(m_pWidget, border_bottom, this);
    m_pBorder_Bottom->setStyleSheet("background-color:transparent");
    m_pBorder_Bottom->raise();

    m_pBorder_LeftTop = new CustomBorder(m_pWidget, border_leftTop, this);
    m_pBorder_LeftTop->setStyleSheet("background-color:transparent");
    m_pBorder_LeftTop->raise();

    m_pBorder_LeftBottom = new CustomBorder(m_pWidget, border_leftBottom, this);
    m_pBorder_LeftBottom->setStyleSheet("background-color:transparent");
    m_pBorder_LeftBottom->raise();

    m_pBorder_RightTop = new CustomBorder(m_pWidget, border_rightTop, this);
    m_pBorder_RightTop->setStyleSheet("background-color:transparent");
    m_pBorder_RightTop->raise();

    m_pBorder_RightBottom = new CustomBorder(m_pWidget, border_rightBottom, this);
    m_pBorder_RightBottom->setStyleSheet("background-color:transparent");
    m_pBorder_RightBottom->raise();

    m_pBorder_Move = new CustomBorder(m_pWidget, border_move, this);
    m_pBorder_Move->setStyleSheet("background-color:transparent");
    m_pBorder_Move->raise();

    updateBorder();
}

void CustomBorderContainer::setMinWindowSize(int nWidth, int nHeight)
{
    m_nMinWindowSize_width = nWidth;
    m_nMinWindowSize_height = nHeight;
}

void CustomBorderContainer::setBorderSize(int nSize)
{
    m_nBorderSize = nSize;
}

void CustomBorderContainer::showBorderDraw()
{
    m_pBorder_Move->showPainterDraw(true);
    m_pBorder_Move->update();
}
void CustomBorderContainer::hideBorderDraw()
{
    m_pBorder_Move->showPainterDraw(false);
    m_pBorder_Move->update();
}

void CustomBorderContainer::setAutoAdjustDistance(int nDistance)
{
    m_nAutoAdjustDistance = nDistance;
}

void CustomBorderContainer::setOperationOption(OperationOptions options)
{
    m_opOptions = options;
}

void CustomBorderContainer::scaleByLeftBorder(const QPoint& moveLength)
{
    //不能采用宽度判断:if(m_widget->width()<minWindowWidth)，因为一旦进入这个判断条件，就无法再更新宽度，无法重新缩放，因此采用鼠标点位置的实时计算来判断
    if((m_pWidget->pos().x() + moveLength.x()) > (m_pWidget->pos().x() + m_pWidget->width() - m_nMinWindowSize_width)) //左边界动态调整x坐标和width，判断这两个值的实时关系
        return;

    m_pWidget->setGeometry(m_pWidget->pos().x() + moveLength.x(), m_pWidget->pos().y(), m_pWidget->width() - moveLength.x(), m_pWidget->height());
    //updateBorder();因为moveBorder会显示出来，所以只对它实时刷新
    m_pBorder_Move->setGeometry(m_nBorderSize, m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize * 10);
}
void CustomBorderContainer::scaleByRightBorder(const QPoint& moveLength)
{
    if((m_pWidget->pos().x() + m_pWidget->width() + moveLength.x()) < (m_pWidget->pos().x() + m_nMinWindowSize_width)) //有边界动态调整width，判断width和x坐标的关系
        return;

    m_pWidget->setGeometry(m_pWidget->pos().x(), m_pWidget->pos().y(), m_pWidget->width() + moveLength.x(), m_pWidget->height());
    //updateBorder();
    m_pBorder_Move->setGeometry(m_nBorderSize, m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize * 10);
}
void CustomBorderContainer::scaleByTopBorder(const QPoint& moveLength)
{
    if((m_pWidget->pos().y() + moveLength.y()) > (m_pWidget->pos().y() + m_pWidget->height() - m_nMinWindowSize_height))
        return;

    m_pWidget->setGeometry(m_pWidget->pos().x(), m_pWidget->pos().y() + moveLength.y(), m_pWidget->width(), m_pWidget->height() - moveLength.y());
    //updateBorder();
    m_pBorder_Move->setGeometry(m_nBorderSize, m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize * 10);
}
void CustomBorderContainer::scaleByBottomBorder(const QPoint& moveLength)
{
    if((m_pWidget->pos().y() + m_pWidget->height() + moveLength.y()) < (m_pWidget->pos().y() + m_nMinWindowSize_height))
        return;

    m_pWidget->setGeometry(m_pWidget->pos().x(), m_pWidget->pos().y(), m_pWidget->width(), m_pWidget->height() + moveLength.y());
    //updateBorder();
    m_pBorder_Move->setGeometry(m_nBorderSize, m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize * 10);
}
void CustomBorderContainer::scaleByLeftTopBorder(const QPoint& moveLength)
{
    if((m_pWidget->pos().x() + moveLength.x()) > (m_pWidget->pos().x() + m_pWidget->width() - m_nMinWindowSize_width)
        || (m_pWidget->pos().y() + moveLength.y()) > (m_pWidget->pos().y() + m_pWidget->height() - m_nMinWindowSize_height))
        return;

    m_pWidget->setGeometry(m_pWidget->pos().x() + moveLength.x(), m_pWidget->pos().y() + moveLength.y(), m_pWidget->width() - moveLength.x(), m_pWidget->height() - moveLength.y());
    //updateBorder();
    m_pBorder_Move->setGeometry(m_nBorderSize, m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize * 10);
}
void CustomBorderContainer::scaleByLeftBottomBorder(const QPoint& moveLength)
{
    if((m_pWidget->pos().x() + moveLength.x()) > (m_pWidget->pos().x() + m_pWidget->width() - m_nMinWindowSize_width)
        || (m_pWidget->pos().y() + m_pWidget->height() + moveLength.y()) < (m_pWidget->pos().y() + m_nMinWindowSize_height))
        return;

    m_pWidget->setGeometry(m_pWidget->pos().x() + moveLength.x(), m_pWidget->pos().y(), m_pWidget->width() - moveLength.x(), m_pWidget->height() + moveLength.y());
    //updateBorder();
    m_pBorder_Move->setGeometry(m_nBorderSize, m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize * 10);
}
void CustomBorderContainer::scaleByRightTopBorder(const QPoint& moveLength)
{
    if((m_pWidget->pos().x() + m_pWidget->width() + moveLength.x()) < (m_pWidget->pos().x() + m_nMinWindowSize_width)
        || (m_pWidget->pos().y() + moveLength.y()) > (m_pWidget->pos().y() + m_pWidget->height() - m_nMinWindowSize_height))
        return;

    m_pWidget->setGeometry(m_pWidget->pos().x(), m_pWidget->pos().y() + moveLength.y(), m_pWidget->width() + moveLength.x(), m_pWidget->height() - moveLength.y());
    //updateBorder();
    m_pBorder_Move->setGeometry(m_nBorderSize, m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize * 10);
}
void CustomBorderContainer::scaleByRightBottomBorder(const QPoint& moveLength)
{
    if((m_pWidget->pos().x() + m_pWidget->width() + moveLength.x()) < (m_pWidget->pos().x() + m_nMinWindowSize_width)
        || (m_pWidget->pos().y() + m_pWidget->height() + moveLength.y()) < (m_pWidget->pos().y() + m_nMinWindowSize_height))
        return;

    m_pWidget->setGeometry(m_pWidget->pos().x(), m_pWidget->pos().y(), m_pWidget->width() + moveLength.x(), m_pWidget->height() + moveLength.y());
    //updateBorder();
    m_pBorder_Move->setGeometry(m_nBorderSize, m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize * 10);
}

void CustomBorderContainer::moveByMoveBorder(const QPoint& moveLength)
{
    //m_pWidget->setUpdatesEnabled(false);
    m_pWidget->move(moveLength + m_pWidget->pos());
    //m_pWidget->setUpdatesEnabled(true);
}

void CustomBorderContainer::updateBorder()
{
    m_pBorder_Left->setGeometry(0, m_nBorderSize, m_nBorderSize, m_pWidget->height() - m_nBorderSize * 2);
    m_pBorder_Top->setGeometry(m_nBorderSize, 0, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize);
    m_pBorder_Right->setGeometry(m_pWidget->width() - m_nBorderSize, m_nBorderSize, m_nBorderSize, m_pWidget->height() - m_nBorderSize * 2);
    m_pBorder_Bottom->setGeometry(m_nBorderSize, m_pWidget->height() - m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize);
    m_pBorder_LeftTop->setGeometry(0, 0, m_nBorderSize, m_nBorderSize);
    m_pBorder_LeftBottom->setGeometry(0, m_pWidget->height() - m_nBorderSize, m_nBorderSize, m_nBorderSize);
    m_pBorder_RightTop->setGeometry(m_pWidget->width() - m_nBorderSize, 0, m_nBorderSize, m_nBorderSize);
    m_pBorder_RightBottom->setGeometry(m_pWidget->width() - m_nBorderSize, m_pWidget->height() - m_nBorderSize, m_nBorderSize, m_nBorderSize);
    m_pBorder_Move->setGeometry(m_nBorderSize, m_nBorderSize, m_pWidget->width() - m_nBorderSize * 2, m_nBorderSize * 10);
}

void CustomBorderContainer::updateContentWidget()
{
    m_pWidget->update();
}

void CustomBorderContainer::autoAdjustGeometry(const QString& strOperation)
{
    QWidget* parentWiget = m_pWidget->parentWidget();
    if(!parentWiget)
        return;

    QList<QDialog*> siblingDialogs = parentWiget->findChildren<QDialog*>(Qt::FindDirectChildrenOnly);
    int nMinDistance_X = m_nAutoAdjustDistance + 1;
    int nMinDistance_Y = m_nAutoAdjustDistance + 1;

    //和父对象边界的距离
    {
        int nDistance_X_Left2Left = m_pWidget->geometry().left();
        int nDistance_X_Right2Right = m_pWidget->geometry().right() - parentWiget->width();
        int nDistance_X;
        if(nDistance_X_Left2Left == 0)
            nDistance_X = nDistance_X_Right2Right;
        else if(nDistance_X_Right2Right == 0)
            nDistance_X = nDistance_X_Left2Left;
        else
            nDistance_X= (qAbs(nDistance_X_Left2Left) < qAbs(nDistance_X_Right2Right)) ? nDistance_X_Left2Left : nDistance_X_Right2Right;
        if(qAbs(nDistance_X) < qAbs(nMinDistance_X))
            nMinDistance_X = nDistance_X;

        int nDistance_Y_Top2Top = m_pWidget->geometry().top();
        int nDistance_Y_Bottom2Bottom = m_pWidget->geometry().bottom() - parentWiget->height();
        int nDistance_Y;
        if(nDistance_Y_Top2Top == 0)
            nDistance_Y = nDistance_Y_Bottom2Bottom;
        else if(nDistance_Y_Bottom2Bottom == 0)
            nDistance_Y = nDistance_Y_Top2Top;
        else
            nDistance_Y= (qAbs(nDistance_Y_Top2Top) < qAbs(nDistance_Y_Bottom2Bottom)) ? nDistance_Y_Top2Top : nDistance_Y_Bottom2Bottom;
        if(qAbs(nDistance_Y) < qAbs(nMinDistance_Y))
            nMinDistance_Y = nDistance_Y;
    }

    //和兄弟对话框的
    for(int n = 0; n < siblingDialogs.count(); n++)
    {
        QDialog* dialog = siblingDialogs.at(n);
        if(dialog == m_pWidget)
            continue;
        //水平方向
          //leftBorder
        int nDistance_X_Left2Left = m_pWidget->geometry().left() - dialog->geometry().left();
        int nDistance_X_Left2Right = m_pWidget->geometry().left() - dialog->geometry().right();
        int nDistance_X_Left/* = m_nAutoAdjustDistance + 1*/;
        if(nDistance_X_Left2Left == 0)
            nDistance_X_Left = nDistance_X_Left2Right;
        else if(nDistance_X_Left2Right == 0)
            nDistance_X_Left = nDistance_X_Left2Left;
        else
            nDistance_X_Left = (qAbs(nDistance_X_Left2Left) < qAbs(nDistance_X_Left2Right)) ? nDistance_X_Left2Left : nDistance_X_Left2Right;
          //rightBorder
        int nDistance_X_Right2Left = m_pWidget->geometry().right() - dialog->geometry().left();
        int nDistance_X_Right2Right = m_pWidget->geometry().right() - dialog->geometry().right();
        int nDistance_X_Right/* = m_nAutoAdjustDistance + 1*/;
        if(nDistance_X_Right2Left == 0)
            nDistance_X_Right = nDistance_X_Right2Right;
        else if(nDistance_X_Right2Right == 0)
            nDistance_X_Right = nDistance_X_Right2Left;
        else
            nDistance_X_Right = (qAbs(nDistance_X_Right2Left) < qAbs(nDistance_X_Right2Right)) ? nDistance_X_Right2Left : nDistance_X_Right2Right;

        int nDistance_X = (qAbs(nDistance_X_Left) < qAbs(nDistance_X_Right)) ? nDistance_X_Left : nDistance_X_Right;
        if(qAbs(nDistance_X) < qAbs(nMinDistance_X))
            nMinDistance_X = nDistance_X;
        //垂直方向
          //topBorder
        int nDistance_Y_Top2Top = m_pWidget->geometry().top() - dialog->geometry().top();
        int nDistance_Y_Top2Bottom = m_pWidget->geometry().top() - dialog->geometry().bottom();
        int nDistance_Top/* = m_nAutoAdjustDistance + 1*/;
        if(nDistance_Y_Top2Top == 0)
            nDistance_Top = nDistance_Y_Top2Bottom;
        else if(nDistance_Y_Top2Bottom == 0)
            nDistance_Top = nDistance_Y_Top2Top;
        else
            nDistance_Top = (qAbs(nDistance_Y_Top2Top) < qAbs(nDistance_Y_Top2Bottom)) ? nDistance_Y_Top2Top : nDistance_Y_Top2Bottom;
          //bottomBorder
        int nDistance_Y_Bottom2Top = m_pWidget->geometry().bottom() - dialog->geometry().top();
        int nDistance_Y_Bottom2Bottom = m_pWidget->geometry().bottom() - dialog->geometry().bottom();
        int nDistance_Bottom/* = m_nAutoAdjustDistance + 1*/;
        if(nDistance_Y_Bottom2Top == 0)
            nDistance_Bottom = nDistance_Y_Bottom2Bottom;
        else if(nDistance_Y_Bottom2Bottom == 0)
            nDistance_Bottom = nDistance_Y_Bottom2Top;
        else
            nDistance_Bottom = (qAbs(nDistance_Y_Bottom2Top) < qAbs(nDistance_Y_Bottom2Bottom)) ? nDistance_Y_Bottom2Top : nDistance_Y_Bottom2Bottom;

        int nDistance_Y = (qAbs(nDistance_Top) < qAbs(nDistance_Bottom)) ? nDistance_Top : nDistance_Bottom;
        if(qAbs(nDistance_Y) < qAbs(nMinDistance_Y))
            nMinDistance_Y = nDistance_Y;
    }

    //qDebug() << "nMinDistance_X: " << nMinDistance_X << " nMinDistance_Y:" << nMinDistance_Y;

    if(strOperation == "move")
    {
        int nMoveX = m_pWidget->geometry().x();
        int nMoveY = m_pWidget->geometry().y();
        //出界
        if(m_pWidget->geometry().left() < 0 || m_pWidget->geometry().top() < 0 || m_pWidget->geometry().right() > parentWiget->width() || m_pWidget->geometry().bottom() > parentWiget->height())
        {
            if(m_pWidget->geometry().left() < 0)
                nMoveX = 0;
            if(m_pWidget->geometry().top() < 0)
                nMoveY = 0;
            if(m_pWidget->geometry().right() > parentWiget->width())
                nMoveX = nMoveX - (m_pWidget->geometry().right() - parentWiget->width());
            if(m_pWidget->geometry().bottom() > parentWiget->height())
                nMoveY = nMoveY - (m_pWidget->geometry().bottom() - parentWiget->height());

            //左上边界不能出界(自身大小大于父组件移动时会出现)，不然无法移动(移动热点在上方)
            if(nMoveX < 0)
                nMoveX = 0;
            if(nMoveY < 0)
                nMoveY = 0;
        }
        else
        {
            if(qAbs(nMinDistance_X) < m_nAutoAdjustDistance)
                nMoveX = nMoveX - nMinDistance_X;
            if(qAbs(nMinDistance_Y) < m_nAutoAdjustDistance)
                nMoveY = nMoveY - nMinDistance_Y;
        }

        if(nMoveX != m_pWidget->geometry().x() || nMoveY != m_pWidget->geometry().y())
        {
            // m_pWidget->move(nMoveX, nMoveY);
            // updateBorder();
            if(!m_pAnimation_pos)
            {
                m_pAnimation_pos = new QPropertyAnimation(this);
                m_pAnimation_pos->setPropertyName("pos");
                m_pAnimation_pos->setDuration(150);
                connect(m_pAnimation_pos, SIGNAL(finished()), this, SLOT(onAnimationFinished()));
            }
            m_pAnimation_pos->setTargetObject(m_pWidget);
            m_pAnimation_pos->setStartValue(QPoint(m_pWidget->geometry().x(), m_pWidget->geometry().y()));
            m_pAnimation_pos->setEndValue(QPoint(nMoveX, nMoveY));
            m_pAnimation_pos->start();
        }
    }
    else if(strOperation.contains("resize") /*&& (qAbs(nMinDistance_X) < m_nAutoAdjustDistance || qAbs(nMinDistance_Y) < m_nAutoAdjustDistance)*/)
    {
        int nX = m_pWidget->geometry().x();
        int nY = m_pWidget->geometry().y();
        int nWidth = m_pWidget->geometry().width();
        int nHeight = m_pWidget->geometry().height();

        int nDistanceX = (qAbs(nMinDistance_X) < m_nAutoAdjustDistance) ? nMinDistance_X : 0;
        int nDistanceY = (qAbs(nMinDistance_Y) < m_nAutoAdjustDistance) ? nMinDistance_Y : 0;

        if(strOperation == "resizeByLeft")
        {
            if(nX < 0) //出界
            {
                nX = 0;
                nWidth = nWidth + nX;
            }
            else
            {
                nX = nX - nDistanceX;
                nWidth = nWidth + nDistanceX;
            }
        }
        else if(strOperation == "resizeByRight")
        {
            if(m_pWidget->geometry().right() > parentWiget->width()) //出界
                nWidth = nWidth - (m_pWidget->geometry().right() - parentWiget->width());
            else
                nWidth = nWidth - nDistanceX;
        }
        else if(strOperation == "resizeByTop")
        {
            if(nY < 0) //出界
            {
                nY = 0;
                nHeight = nHeight + nY;
            }
            else
            {
                nY = nY - nDistanceY;
                nHeight = nHeight + nDistanceY;
            }
        }
        else if(strOperation == "resizeByBottom")
        {
            if(m_pWidget->geometry().bottom() > parentWiget->height()) //出界
                nHeight = nHeight - (m_pWidget->geometry().bottom() - parentWiget->height());
            else
                nHeight = nHeight - nDistanceY;
        }
        else if(strOperation == "resizeByLeftTop")
        {
            if(nX < 0) //出界
            {
                nX = 0;
                nWidth = nWidth + nX;
            }
            else
            {
                nX = nX - nDistanceX;
                nWidth = nWidth + nDistanceX;
            }

            if(nY < 0) //出界
            {
                nY = 0;
                nHeight = nHeight + nY;
            }
            else
            {
                nY = nY - nDistanceY;
                nHeight = nHeight + nDistanceY;
            }
        }
        else if(strOperation == "resizeByLeftBottom")
        {
            if(nX < 0) //出界
            {
                nX = 0;
                nWidth = nWidth + nX;
            }
            else
            {
                nX = nX - nDistanceX;
                nWidth = nWidth + nDistanceX;
            }

            if(m_pWidget->geometry().bottom() > parentWiget->height()) //出界
                nHeight = nHeight - (m_pWidget->geometry().bottom() - parentWiget->height());
            else
                nHeight = nHeight - nDistanceY;
        }
        else if(strOperation == "resizeByRightTop")
        {
            if(m_pWidget->geometry().right() > parentWiget->width()) //出界
                nWidth = nWidth - (m_pWidget->geometry().right() - parentWiget->width());
            else
                nWidth = nWidth - nDistanceX;

            if(nY < 0) //出界
            {
                nY = 0;
                nHeight = nHeight + nY;
            }
            else
            {
                nY = nY - nDistanceY;
                nHeight = nHeight + nDistanceY;
            }
        }
        else if(strOperation == "resizeByRightBottom")
        {
            if(m_pWidget->geometry().right() > parentWiget->width()) //出界
                nWidth = nWidth - (m_pWidget->geometry().right() - parentWiget->width());
            else
                nWidth = nWidth - nDistanceX;

            if(m_pWidget->geometry().bottom() > parentWiget->height()) //出界
                nHeight = nHeight - (m_pWidget->geometry().bottom() - parentWiget->height());
            else
                nHeight = nHeight - nDistanceY;
        }

        // m_pWidget->setGeometry(nX, nY, nWidth, nHeight);
        // updateBorder();
        if(!m_pAnimation_geometry)
        {
            m_pAnimation_geometry = new QPropertyAnimation(this);
            m_pAnimation_geometry->setPropertyName("geometry");
            m_pAnimation_geometry->setDuration(150);
            connect(m_pAnimation_geometry, SIGNAL(finished()), this, SLOT(onAnimationFinished()));
        }
        m_pAnimation_geometry->setTargetObject(m_pWidget);
        m_pAnimation_geometry->setStartValue(QRect(m_pWidget->x(), m_pWidget->y(), m_pWidget->width(), m_pWidget->height()));
        m_pAnimation_geometry->setEndValue(QRect(nX, nY, nWidth, nHeight));
        m_pAnimation_geometry->start();
    }

}

void CustomBorderContainer::onAnimationFinished()
{
    updateBorder();
}





