/**
     *\brief 自定义边框容器类，可以挂载给任意采用无系统边框的widget上，进而实现widget的缩放、移动等功能
     *
     *\author dsc
     */
#ifndef CUSTOMBORDERCONTAINER_H
#define CUSTOMBORDERCONTAINER_H

#include <QWidget>
#include <QObject>
#include <QLabel>
#include <QMouseEvent>
#include <QBasicTimer>

class QPropertyAnimation;
class CustomBorderContainer : public QObject
{
    Q_OBJECT

public:
    enum borderType
    {
        border_left = 0,
        border_right,
        border_top,
        border_bottom,
        border_leftTop,
        border_leftBottom,
        border_rightTop,
        border_rightBottom,
        border_move
    };

    enum OperationOption
    {
        WidgetNoOperation = 0x000,
        WidgetMovable = 0x001,
        WidgetResizable = 0x002,
        WidgetAutoAdjustGeometry = 0x004, //根据兄弟widget和parent的border相对位置进行自动调整
        DefaultOperationOption = WidgetMovable | WidgetResizable
    };
    Q_DECLARE_FLAGS(OperationOptions, OperationOption)

    CustomBorderContainer(QWidget *parent, int minWindowWidth = 50, int minWindowHeight = 50, int borderSize = 3, int autoAdjustDistance = 45);
    ~CustomBorderContainer();

    void setMinWindowSize(int, int); //设置窗口能缩放的最小尺寸
    void setBorderSize(int); //设置边框尺寸
    void updateBorder(); //重绘border
    void showBorderDraw();
    void hideBorderDraw();
    void setAutoAdjustDistance(int);
    void setOperationOption(OperationOptions);

private:
    //采用内部类
    class CustomBorder : public QLabel
    {
    public:
        CustomBorder(QWidget* parent, borderType type, CustomBorderContainer* container);

        void showPainterDraw(bool);

    protected:
        void mousePressEvent(QMouseEvent*);
        void mouseMoveEvent(QMouseEvent*);
        void mouseReleaseEvent(QMouseEvent*);
        void paintEvent(QPaintEvent*);
        void timerEvent(QTimerEvent*);

    private:
        borderType m_type;
        QPoint     m_ptMouseLast;
        QPoint     m_mouseMoveLengt;
        bool       m_bMousIsPress;
        bool       m_bShowPainterDraw;
        QString    m_strOperation;
        CustomBorderContainer*  m_pContainer;

        QBasicTimer m_repaintTimer;
    };

    void iniBorder();
    void scaleByLeftBorder(const QPoint&);
    void scaleByRightBorder(const QPoint&);
    void scaleByTopBorder(const QPoint&);
    void scaleByBottomBorder(const QPoint&);
    void scaleByLeftTopBorder(const QPoint&);
    void scaleByLeftBottomBorder(const QPoint&);
    void scaleByRightTopBorder(const QPoint&);
    void scaleByRightBottomBorder(const QPoint&);
    void moveByMoveBorder(const QPoint&);
    void autoAdjustGeometry(const QString&);
    void updateContentWidget();

    QWidget*    m_pWidget;
    int         m_nMinWindowSize_width;
    int         m_nMinWindowSize_height;
    int         m_nBorderSize;
    int         m_nAutoAdjustDistance; //自动调整位置的判断距离
    //border
    CustomBorder*  m_pBorder_Left;
    CustomBorder*  m_pBorder_Right;
    CustomBorder*  m_pBorder_Top;
    CustomBorder*  m_pBorder_Bottom;
    CustomBorder*  m_pBorder_LeftTop;
    CustomBorder*  m_pBorder_LeftBottom;
    CustomBorder*  m_pBorder_RightTop;
    CustomBorder*  m_pBorder_RightBottom;
    CustomBorder*  m_pBorder_Move; //用于移动窗体的border

    OperationOptions m_opOptions;

    QPropertyAnimation* m_pAnimation_pos;
    QPropertyAnimation* m_pAnimation_geometry;

private slots:
    void onAnimationFinished();
};

Q_DECLARE_OPERATORS_FOR_FLAGS(CustomBorderContainer::OperationOptions)

#endif
