2024-10-10 16:59:51 +08:00
|
|
|
|
/**
|
|
|
|
|
|
*\brief 自定义边框容器类,可以挂载给任意采用无系统边框的widget上,进而实现widget的缩放、移动等功能
|
|
|
|
|
|
*
|
|
|
|
|
|
*\author dsc
|
|
|
|
|
|
*/
|
|
|
|
|
|
#ifndef CUSTOMBORDERCONTAINER_H
|
|
|
|
|
|
#define CUSTOMBORDERCONTAINER_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <QWidget>
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
|
#include <QMouseEvent>
|
2025-06-13 11:21:16 +08:00
|
|
|
|
#include <QBasicTimer>
|
2024-10-10 16:59:51 +08:00
|
|
|
|
|
|
|
|
|
|
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); //设置边框尺寸
|
2025-06-13 11:21:16 +08:00
|
|
|
|
void updateBorder(); //重绘border
|
2024-10-10 16:59:51 +08:00
|
|
|
|
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*);
|
2025-06-13 11:21:16 +08:00
|
|
|
|
void timerEvent(QTimerEvent*);
|
2024-10-10 16:59:51 +08:00
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
borderType m_type;
|
|
|
|
|
|
QPoint m_ptMouseLast;
|
2025-06-13 11:21:16 +08:00
|
|
|
|
QPoint m_mouseMoveLengt;
|
2024-10-10 16:59:51 +08:00
|
|
|
|
bool m_bMousIsPress;
|
|
|
|
|
|
bool m_bShowPainterDraw;
|
|
|
|
|
|
QString m_strOperation;
|
|
|
|
|
|
CustomBorderContainer* m_pContainer;
|
2025-06-13 11:21:16 +08:00
|
|
|
|
|
|
|
|
|
|
QBasicTimer m_repaintTimer;
|
2024-10-10 16:59:51 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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&);
|
2025-06-13 11:21:16 +08:00
|
|
|
|
void moveByMoveBorder(const QPoint&);
|
2024-10-10 16:59:51 +08:00
|
|
|
|
void autoAdjustGeometry(const QString&);
|
2025-06-13 11:21:16 +08:00
|
|
|
|
void updateContentWidget();
|
2024-10-10 16:59:51 +08:00
|
|
|
|
|
|
|
|
|
|
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
|