feat: Improve MyStyle, MyStyleHelp, MyStylePainter (mostly written, but not over)

This commit is contained in:
muli 2020-02-22 12:43:23 +08:00
parent 77115da29d
commit 787a6061a8
4 changed files with 148 additions and 3 deletions

View File

@ -30,6 +30,28 @@ MyStyle::MyStyle()
}
//******************************************************************
void MyStyle::drawPrimitive(const QStyle *style, MyStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w)
{
}
void MyStyle::drawControl(const QStyle *style, MyStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w)
{
}
QRect MyStyle::subElementRect(const QStyle *style, MyStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget)
{
return QRect();
}
int MyStyle::pixelMetric(const QStyle *style, MyStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget)
{
return 0;
}
//******************************************************************
void MyStyle::drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const
{
proxy()->drawPrimitive(static_cast<QStyle::PrimitiveElement>(pe), opt, p, w);
@ -98,7 +120,13 @@ void MyStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt
default:
break;
}
QCommonStyle::drawPrimitive(pe, opt, p, w);
//后续分离开为模块的判断
// if (Q_UNLIKELY(pe < QStyle::PE_CustomBase))
// else
// drawPrimitive(this, static_cast<MyStyle::PrimitiveElement>(pe), opt, p, w);
}
void MyStyle::drawControl(QStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const
@ -216,6 +244,70 @@ int MyStyle::styleHint(QStyle::StyleHint stylehint, const QStyleOption *opt, con
return QCommonStyle::styleHint(stylehint, opt, widget, returnData);
}
//******************************************************************
MyStyleHelp::MyStyleHelp(const QStyle* style)
{
setStyle(style);
}
void MyStyleHelp::setStyle(const QStyle* style)
{
m_qStyle = style;
m_myStyle = qobject_cast<const MyStyle *>(style);
}
const QStyle *MyStyleHelp::qStyle() const
{
return m_qStyle;
}
const MyStyle *MyStyleHelp::myStyle() const
{
return m_myStyle;
}
void MyStyleHelp::drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const
{
m_myStyle ? m_myStyle->drawPrimitive(pe, opt, p, w)
: MyStyle::drawPrimitive(m_qStyle, pe, opt, p, w);
}
void MyStyleHelp::drawControl(MyStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const
{
m_myStyle ? m_myStyle->drawControl(element, opt, p, w)
: MyStyle::drawControl(m_qStyle, element, opt, p, w);
}
QRect MyStyleHelp::subElementRect(MyStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const
{
return m_myStyle ? m_myStyle->subElementRect(subElement, option, widget)
: MyStyle::subElementRect(m_qStyle, subElement, option, widget);
}
int MyStyleHelp::pixelMetric(MyStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
{
return m_myStyle ? m_myStyle->pixelMetric(metric, option, widget)
: MyStyle::pixelMetric(m_qStyle, metric, option, widget);
}
//******************************************************************
void MyStylePainter::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt)
{
m_qStyle->drawPrimitive(pe, opt, this, m_widget);
}
void MyStylePainter::drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt)
{
m_myStyleHelp.drawPrimitive(pe, opt, this, m_widget);
}
void MyStylePainter::drawControl(QStyle::ControlElement element, const QStyleOption *opt)
{
m_qStyle->drawControl(element, opt, this, m_widget);
}
void MyStylePainter::drawControl(MyStyle::ControlElement element, const QStyleOption *opt)
{
m_myStyleHelp.drawControl(element, opt, this, m_widget);
}

View File

@ -23,10 +23,11 @@
#define MYSTYLE_H
#include <QCommonStyle>
#include <QPainter>
class MyStyle : public QCommonStyle
{
// Q_OBJECT
Q_OBJECT //connect qobject_cast
public:
//这里新增加的枚举,是属于 MyStyle:: , 而非 QStyle:: 范围
@ -59,7 +60,18 @@ public:
// QStyle interface
public:
//static 函数,供 MyStylrHelp 调用 [用来绘画自增加的控件枚举]
static void drawPrimitive(const QStyle *style, MyStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w = nullptr);
static void drawControl(const QStyle *style, MyStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w);
static QRect subElementRect(const QStyle *style, MyStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget);
// static void drawComplexControl(const QStyle *style, MyStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const;
// static QRect subControlRect(const QStyle *style, MyStyle::ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const;
static int pixelMetric(const QStyle *style, MyStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget);
// static QSize sizeFromContents(const QStyle *style, MyStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const;
// static int styleHint(const QStyle *style, MyStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const;
//新增加的枚举属 MyStyle:: , 之能够在此内敛函数里面调用
//[主要用来绘画 自定义新增 的控件枚举 --> 实际调用在 下面的 virtual 里面绘画]
inline void drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w = nullptr) const;
inline void drawControl(MyStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const;
inline QRect subElementRect(MyStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const;
@ -71,6 +83,7 @@ public:
//这里的快捷方式创建的枚举,都是不带QStyle:: ; 但是快捷方式的定义是带是QStyle:: , 此处声明的地方必须加上 QStyle:: /*后面改写更复杂的得写上MyStyle:: 因添加自定义的枚举*/
//这里 override 的虚函数,只能够调用旧有的 QStyle:: 的函数
//[主要用来绘画 Qt 、 自定义新增 的控件枚举]
virtual void drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const override;
virtual void drawControl(QStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const override;
virtual QRect subElementRect(QStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const override;
@ -96,4 +109,40 @@ public:
};
class MyStyleHelp
{
public:
inline MyStyleHelp (const QStyle* style = nullptr);
inline void setStyle(const QStyle* style);
inline const QStyle* qStyle() const;
inline const MyStyle* myStyle() const;
inline void drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w = nullptr) const;
inline void drawControl(MyStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const;
inline QRect subElementRect(MyStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const;
inline int pixelMetric(MyStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const;
private:
const QStyle* m_qStyle;
const MyStyle* m_myStyle;
};
class MyStylePainter : public QPainter
{
public:
MyStylePainter() {}
~MyStylePainter() {}
inline void drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt);
inline void drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt);
inline void drawControl(MyStyle::ControlElement element, const QStyleOption *opt);
inline void drawControl(QStyle::ControlElement element, const QStyleOption *opt);
private:
QWidget* m_widget;
QStyle* m_qStyle;
MyStyleHelp m_myStyleHelp;
};
#endif // MYSTYLE_H

View File

@ -3,7 +3,7 @@
#include "mystyle.h"
#include <QStylePainter>
#include <QStyleOptionButton>
#include <QStyleOption>
MySwitchButton::MySwitchButton(QWidget *parent)
: QAbstractButton(parent)
@ -30,7 +30,10 @@ void MySwitchButton::paintEvent(QPaintEvent *event)
QStyleOptionButton opt;
initStyleOption(&opt);
QStylePainter pa(this);
// MyStylePainter painter(this);
// MyStylePainter painter;
// painter.drawControl(MyStyle::CE_SwitchButton, &opt);//static_cast<QStyleOption>(opt));
// pa.drawControl(MyStyle::CE_SwitchButton, opt);
}

View File

@ -2,6 +2,7 @@
#define MYSWITCHBUTTON_H
#include <QAbstractButton>
#include <QStyleOption>
class QStyleOptionButton;
class MySwitchButtonPrivate;