qsswraper/Qss.h

440 lines
11 KiB
C
Raw Normal View History

2021-05-17 01:54:55 +00:00

2020-10-26 15:44:45 +00:00
#ifndef QSS_H
#define QSS_H
#include <QWidget>
#include <QMainWindow>
#include <QDialog>
#include <QMessageBox>
#include <QFrame>
#include <QDockWidget>
#include <QPushButton>
2020-12-06 16:00:45 +00:00
#include <QAbstractNativeEventFilter>
#include <QDebug>
2021-07-08 05:45:41 +00:00
#include <QThread>
2020-12-07 16:11:45 +00:00
#include "ui_qsstoast.h"
2021-04-30 17:32:54 +00:00
#include "ui_process.h"
2022-01-18 16:26:45 +00:00
#include <QScreen>
2021-04-30 17:32:54 +00:00
2020-12-07 16:11:45 +00:00
#ifdef __MINGW32__
2020-12-06 16:00:45 +00:00
#include "windows.h"
#include "winuser.h"
2020-12-07 16:11:45 +00:00
#pragma comment(lib, "libuser32.a")
#endif
2020-10-26 15:44:45 +00:00
class QPushButton;
class QLabel;
class QMouseEvent;
2022-01-19 02:38:35 +00:00
#define STANDARD_DPI 96.0
2022-01-19 02:38:35 +00:00
2021-10-05 05:50:16 +00:00
#ifndef WM_DPICHANGED
2020-12-06 16:00:45 +00:00
#define WM_DPICHANGED 0x02e0
2021-10-05 05:50:16 +00:00
#endif
2020-12-06 16:00:45 +00:00
2022-01-18 16:26:45 +00:00
2020-10-26 15:44:45 +00:00
class QssPushButton : public QPushButton{
public:
QssPushButton(QWidget *parent,QString objName);
};
2022-01-18 09:12:01 +00:00
// 获取当前dpi
uint16_t CurrentDPI(QWidget* widget,int monitor);
2020-10-26 15:44:45 +00:00
class QssTtitleBar : public QWidget
{
Q_OBJECT
signals:
void OnMaxOrRestore(bool );
2020-10-26 15:44:45 +00:00
public:
enum QTitleBar_Button
{
QTitleBar_Button_Min = 0x00000001,
QTitleBar_Button_Max = 0x00000002,
QTitleBar_Button_Restore = 0x00000004,
QTitleBar_Button_Close = 0x00000008
};
enum QTitleBar_Type
{
2020-12-04 13:12:58 +00:00
QTitleBar_Type_MainWindow = QTitleBar_Button_Min |
QTitleBar_Button_Max |QTitleBar_Button_Restore | QTitleBar_Button_Close,
2020-10-26 15:44:45 +00:00
QTitleBar_Type_Dialog = QTitleBar_Button_Close,
QTitleBar_Type_MessageBox = QTitleBar_Button_Close
};
QssTtitleBar(QWidget *parent, QTitleBar_Type type = QTitleBar_Type_MainWindow);
~QssTtitleBar();
void setTitle(QString title);
void setIcon( QIcon icon);
void setMaxOrRestore(bool val);
bool maxOrRestore(){return m_maxOrRestore;}
void SetMainWindow(QMainWindow*);
QRect& normalRect(){return m_rcNormal;}
private slots:
void onMaxOrRestore();
protected:
void paintEvent(QPaintEvent *);
void mouseMoveEvent(QMouseEvent * ev);
void mousePressEvent(QMouseEvent * ev);
void mouseReleaseEvent(QMouseEvent * ev);
bool eventFilter(QObject * obj, QEvent * ev);
private:
QPushButton* m_closeBtn;
QPushButton* m_maxBtn;
QPushButton* m_restoreBtn;
QPushButton* m_minBtn;
QLabel* m_titlebarTitle;
QMainWindow *m_Main;
bool m_maxOrRestore;
bool m_pressed;
QPoint m_pressedPos;
QRect m_rcValid;//桌面最大可用尺寸
QRect m_rcNormal;//还原后窗口尺寸
QTitleBar_Type m_type;
};
2020-12-06 16:00:45 +00:00
class QssEventFilter
: public QAbstractNativeEventFilter
{
public:
virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE
2022-01-19 02:38:35 +00:00
{
2020-12-06 16:00:45 +00:00
return false;
}
};
2020-10-26 15:44:45 +00:00
2022-01-18 16:26:45 +00:00
class QssMainWindow : public QMainWindow
2020-10-26 15:44:45 +00:00
{
Q_OBJECT
public slots:
void OnMaxOrRestore(bool max);
2022-01-19 02:38:35 +00:00
void PhisycalDpiChange(qreal);
void LogicalDpiChange(qreal);
2020-10-26 15:44:45 +00:00
public:
2021-05-05 14:59:44 +00:00
typedef enum{
EVENT_MOVE = 523,
} EVENT_CUSTOM;
QssMainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0,float scale = 1);
2020-10-26 15:44:45 +00:00
virtual ~QssMainWindow();
2022-04-09 13:02:25 +00:00
void AdapteDpi();
2021-05-05 14:59:44 +00:00
QWidget *TitleBar();
2022-01-19 02:38:35 +00:00
void RangeObject();
2022-01-18 16:26:45 +00:00
void DetectDpiChange();
2021-07-12 15:31:30 +00:00
void ShowMask();
void HideMask();
2020-10-26 15:44:45 +00:00
void show();
void showMinimized();
void showMaximized();
void showFullScreen();
void showNormal();
void setCursorShape(int CalPos) ;
void setWindowTitle( QString title );
void setWindowIcon( QIcon icon );
int CalCursorPos(QPoint pt, int colPos);
2020-12-04 13:12:58 +00:00
void SetTitleHeight(uint32_t height);
2020-10-26 15:44:45 +00:00
inline QssTtitleBar* titleBar(){return m_titleBar;}
inline QFrame* frame(){return m_frame;}
int CalCursorCol(QPoint pt); //计算鼠标X的位置
virtual bool eventFilter(QObject * obj, QEvent * ev);
private:
void onMouseMoveEvent(QMouseEvent * ev);
void onMousePressEvent(QMouseEvent * ev);
void onMouseReleaseEvent(QMouseEvent * ev);
2022-01-18 16:26:45 +00:00
void dpiScaleChildren();
2022-04-09 13:02:25 +00:00
void detectDpi();
void rangeObjectList(QObject*obj,int indent);
protected:
2020-10-26 15:44:45 +00:00
QFrame* m_frame;
QRect mFrameRect;
QRect m_CentralRect;
2020-10-26 15:44:45 +00:00
QssTtitleBar* m_titleBar;
void showEvent(QShowEvent *ev);
2020-12-04 13:12:58 +00:00
virtual void ScaleChanged(float scale) ;
virtual WId GetWID() const;
virtual void SetScale(float scale) ;
2021-08-04 01:15:30 +00:00
virtual void paintEvent(QPaintEvent *event);
private:
2020-10-26 15:44:45 +00:00
QRect m_rcValid;//桌面最大可用尺寸
QRect m_rcNormal;//还原后窗口尺寸
QRect m_rcNormalCentral;//还原后central widget的尺寸
2020-10-26 15:44:45 +00:00
/** 边框调整大小相关 */
QPoint m_pos;
bool m_mousePressedInBoundy;
bool m_left,m_right,m_top,m_bottom;
int m_iCalCursorPos;
bool m_bLeftPress;
QRect m_rtPreGeometry;
QPoint m_ptViewMousePos;
2022-01-18 16:26:45 +00:00
float m_dpi_ratio;
2021-07-12 15:31:30 +00:00
QWidget *mShadowMask;
2022-01-18 16:26:45 +00:00
QScreen *mCurrentScreen;
2021-07-12 15:31:30 +00:00
2020-10-26 15:44:45 +00:00
};
class QssDialog : public QDialog
{
Q_OBJECT
public:
QssDialog(QWidget *parent);
virtual ~QssDialog();
2021-07-12 15:31:30 +00:00
void ShowMask();
void HideMask();
2020-10-26 15:44:45 +00:00
void show();
void raise();
void activateWindow();
int exec();
void setWindowTitle( QString title );
void setWindowIcon( QIcon icon );
inline QssTtitleBar* titleBar(){return m_titleBar;}
inline QFrame* frame(){return m_frame;}
virtual bool eventFilter(QObject * obj, QEvent * ev);
private:
void onMouseMoveEvent(QMouseEvent * ev);
void onMousePressEvent(QMouseEvent * ev);
void onMouseReleaseEvent(QMouseEvent * ev);
private:
2021-04-01 16:46:23 +00:00
QWidget* m_parent;
2020-10-26 15:44:45 +00:00
QFrame* m_frame;
QssTtitleBar* m_titleBar;
QRect m_rcValid;
/** 边框调整大小相关 */
QRect mFrameRect;
2020-10-26 15:44:45 +00:00
QPoint m_pos;
bool m_mousePressedInBorder;
bool m_left,m_right,m_top,m_bottom;
2021-07-12 15:31:30 +00:00
QWidget *mShadowMask;
2020-10-26 15:44:45 +00:00
};
// 可停靠widget
class QssDockWidget : public QDockWidget
{
Q_OBJECT
public:
QssDockWidget(QWidget *parent);
2021-07-12 15:31:30 +00:00
void ShowMask();
void HideMask();
2020-10-26 15:44:45 +00:00
virtual ~QssDockWidget();
void show();
void raise();
void activateWindow();
int exec();
void setWindowTitle( QString title );
void setWindowIcon( QIcon icon );
void paintEvent(QPaintEvent *);
inline QssTtitleBar* titleBar(){return m_titleBar;}
inline QFrame* frame(){return m_frame;}
virtual bool eventFilter(QObject * obj, QEvent * ev);
private:
void onMouseMoveEvent(QMouseEvent * ev);
void onMousePressEvent(QMouseEvent * ev);
void onMouseReleaseEvent(QMouseEvent * ev);
private:
QFrame* m_frame;
QssTtitleBar* m_titleBar;
QRect m_rcValid;
QWidget* m_parent;
/** 边框调整大小相关 */
QRect mFrameRect;
2020-10-26 15:44:45 +00:00
QPoint m_pos;
bool m_mousePressedInBorder;
bool m_left,m_right,m_top,m_bottom;
2021-07-12 15:31:30 +00:00
QWidget *mShadowMask;
2020-10-26 15:44:45 +00:00
};
class QssMessageBox : public QMessageBox
{
Q_OBJECT
public:
explicit QssMessageBox(QWidget *parent = 0);
QssMessageBox(Icon icon, const QString &title, const QString &text,
StandardButtons buttons = NoButton, QWidget *parent = 0,
Qt::WindowFlags flags = Qt::Widget | Qt::FramelessWindowHint);
~QssMessageBox();
inline QssTtitleBar* titleBar(){return m_titleBar;}
inline QFrame* frame(){return m_frame;}
static QMessageBox::StandardButton tips(const QString & text, QWidget* parent = 0,
const QString & title = QString::fromLocal8Bit("提示"), StandardButtons btn = QMessageBox::Ok);
static QMessageBox::StandardButton warn(const QString & text, QWidget* parent = 0,
const QString & title = QString::fromLocal8Bit("警告"), StandardButtons btn = QMessageBox::Ok);
static QMessageBox::StandardButton error(const QString & text, QWidget* parent = 0,
const QString & title = QString::fromLocal8Bit("错误"), StandardButtons btn = QMessageBox::Ok);
static QMessageBox::StandardButton ask(const QString & text, QWidget* parent = 0,
const QString & title = QString::fromLocal8Bit("询问"), StandardButtons btn = QMessageBox::Yes | QMessageBox::No);
/** 覆盖定义适配原有QMessageBox */
static StandardButton information(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton);
static StandardButton question(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = QMessageBox::Ok,
StandardButton defaultButton = QMessageBox::NoButton);
static StandardButton warning(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = QMessageBox::Ok,
StandardButton defaultButton = QMessageBox::NoButton);
static StandardButton critical(QWidget *parent, const QString &title,
const QString &text, StandardButtons buttons = QMessageBox::Ok,
StandardButton defaultButton = QMessageBox::NoButton);
static QMessageBox::StandardButton regard(const QString & text, QWidget* parent = 0,
const QString & title = QString::fromLocal8Bit("关于"));
static QMessageBox::StandardButton regard(const QString & text, QIcon icon, QWidget* parent = 0,
const QString & title = QString::fromLocal8Bit("关于"));
static QMessageBox::StandardButton regardQt(QWidget* parent = 0,
const QString & title = QString::fromLocal8Bit("关于Qt"));
static void about(QWidget *parent, const QString &title, const QString &text);
static void about(QWidget *parent, const QString &title, const QString &text, QIcon icon);
static void aboutQt(QWidget *parent, const QString &title = QString());
bool eventFilter(QObject * obj, QEvent * ev);
private:
QFrame* m_frame;
QssTtitleBar* m_titleBar;
QWidget* m_parent;
QRect m_rcValid;
};
2020-12-06 17:05:32 +00:00
2021-05-05 14:59:44 +00:00
class QssMaskWidget : public QWidget
{
Q_OBJECT
public:
QssMaskWidget(QWidget *parent = Q_NULLPTR);
2021-05-17 01:54:55 +00:00
virtual ~QssMaskWidget();
2021-05-05 14:59:44 +00:00
virtual bool eventFilter(QObject * obj, QEvent * ev);
public:
protected:
virtual void paintEvent(QPaintEvent *event);
private:
QWidget *mParent;
Ui::Toast ui;
};
2020-12-06 17:05:32 +00:00
class QssToastWidget : public QWidget
{
Q_OBJECT
public:
QssToastWidget(QWidget *parent = Q_NULLPTR);
~QssToastWidget();
void setText(const QString& text);
void showAnimation(int timeout = 2000);// 动画方式show出默认2秒后消失
public:
// 静态调用
static void showTip(const QString& text, QWidget* parent = nullptr);
protected:
virtual void paintEvent(QPaintEvent *event);
private:
Ui::Toast ui;
};
2021-04-30 17:32:54 +00:00
class QSSProcessBar : public QDialog
{
Q_OBJECT
public:
explicit QSSProcessBar(QWidget *parent = nullptr);
~QSSProcessBar();
2021-05-17 01:54:55 +00:00
void showEvent(QShowEvent *) override;
2021-05-05 14:59:44 +00:00
2021-04-30 17:32:54 +00:00
public slots:
void on_percent(int);
void on_done_close();
private:
Ui::Process *ui;
2021-05-17 01:54:55 +00:00
QWidget *mParent;
2021-04-30 17:32:54 +00:00
};
2021-07-08 05:45:41 +00:00
// 异步执行任务不会阻塞ui线程
2021-08-04 01:15:30 +00:00
class QSSASyncProcess : public QObject{
2021-07-08 05:45:41 +00:00
Q_OBJECT
public:
2021-07-08 06:49:46 +00:00
QSSASyncProcess(QWidget *parent = nullptr);
~QSSASyncProcess();
2021-08-04 01:15:30 +00:00
int Start(void*);
2022-01-18 09:12:01 +00:00
QThread &Thread();
2021-07-08 05:45:41 +00:00
signals:
void DonePercent(int);
void Done();
2021-08-04 01:15:30 +00:00
void StartRun(void *);
public slots:
virtual void Run(void *) ;
2021-07-08 06:49:46 +00:00
protected:
2021-07-08 05:45:41 +00:00
QWidget *mParent;
QThread mThread;
};
2021-04-30 17:32:54 +00:00
2020-10-26 15:44:45 +00:00
#define tipBox(text) QssMessageBox::tips(text)
#define warnBox(text) QssMessageBox::warn(text)
#define errBox(text) QssMessageBox::error(text)
#define askBox(text) QssMessageBox::ask(text)
#define aboutBox(title, text) QssMessageBox::regard(text, 0,title)
#define aboutQtBox() QssMessageBox::regardQt()
#endif // QSS_H