chore: 格式化ElaWidgetTools代码

This commit is contained in:
sleepwithoutbz
2025-09-20 01:39:00 +08:00
parent d2fded145d
commit 4ab391f8a1
394 changed files with 10913 additions and 15860 deletions

View File

@@ -9,28 +9,19 @@
#include "ElaTheme.h"
#include "ElaToolBar.h"
ElaToolBarStyle::ElaToolBarStyle(QStyle* style)
{
ElaToolBarStyle::ElaToolBarStyle(QStyle *style) {
_themeMode = eTheme->getThemeMode();
connect(eTheme, &ElaTheme::themeModeChanged, this, [=](ElaThemeType::ThemeMode themeMode) {
_themeMode = themeMode;
});
connect(eTheme, &ElaTheme::themeModeChanged, this, [=](ElaThemeType::ThemeMode themeMode) { _themeMode = themeMode; });
}
ElaToolBarStyle::~ElaToolBarStyle()
{
}
ElaToolBarStyle::~ElaToolBarStyle() {}
void ElaToolBarStyle::drawPrimitive(PrimitiveElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
{
switch (element)
{
case QStyle::PE_PanelButtonTool:
{
void ElaToolBarStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const {
switch (element) {
case QStyle::PE_PanelButtonTool: {
return;
}
case QStyle::PE_FrameMenu:
{
case QStyle::PE_FrameMenu: {
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(ElaThemeColor(_themeMode, PopupBorder));
@@ -39,9 +30,8 @@ void ElaToolBarStyle::drawPrimitive(PrimitiveElement element, const QStyleOption
painter->restore();
return;
}
case QStyle::PE_IndicatorToolBarHandle:
{
//拖动虚线
case QStyle::PE_IndicatorToolBarHandle: {
// 拖动虚线
QRect handleRect = option->rect;
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
@@ -49,106 +39,87 @@ void ElaToolBarStyle::drawPrimitive(PrimitiveElement element, const QStyleOption
handlePen.setColor(ElaThemeColor(_themeMode, PrimaryNormal));
handlePen.setWidthF(1.8);
painter->setPen(handlePen);
const ElaToolBar* toolBar = dynamic_cast<const ElaToolBar*>(widget);
if (toolBar->orientation() == Qt::Vertical)
{
const ElaToolBar *toolBar = dynamic_cast<const ElaToolBar *>(widget);
if (toolBar->orientation() == Qt::Vertical) {
painter->drawLine(handleRect.x(), handleRect.center().y(), handleRect.right(), handleRect.center().y());
}
else
{
} else {
painter->drawLine(handleRect.center().x(), handleRect.y(), handleRect.center().x(), handleRect.bottom());
}
painter->restore();
return;
}
case QStyle::PE_IndicatorToolBarSeparator:
{
//间隔符绘制
case QStyle::PE_IndicatorToolBarSeparator: {
// 间隔符绘制
QRect separatorRect = option->rect;
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(Qt::NoPen);
painter->setBrush(ElaThemeColor(_themeMode, PrimaryNormal));
const ElaToolBar* toolBar = dynamic_cast<const ElaToolBar*>(widget);
if (toolBar->orientation() == Qt::Vertical)
{
painter->drawRect(QRectF(separatorRect.x() + separatorRect.width() * 0.1, separatorRect.center().y() - 0.9, separatorRect.width() * 0.8, 1.8));
}
else
{
painter->drawRect(QRectF(separatorRect.center().x() - 0.9, separatorRect.y() + separatorRect.height() * 0.1, 1.8, separatorRect.height() * 0.8));
const ElaToolBar *toolBar = dynamic_cast<const ElaToolBar *>(widget);
if (toolBar->orientation() == Qt::Vertical) {
painter->drawRect(
QRectF(separatorRect.x() + separatorRect.width() * 0.1, separatorRect.center().y() - 0.9, separatorRect.width() * 0.8, 1.8));
} else {
painter->drawRect(
QRectF(separatorRect.center().x() - 0.9, separatorRect.y() + separatorRect.height() * 0.1, 1.8, separatorRect.height() * 0.8));
}
painter->restore();
return;
}
default:
{
default: {
break;
}
}
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
void ElaToolBarStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
{
switch (element)
{
case QStyle::CE_ToolButtonLabel:
{
//展开按钮
if (const QStyleOptionToolButton* bopt = qstyleoption_cast<const QStyleOptionToolButton*>(option))
{
void ElaToolBarStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const {
switch (element) {
case QStyle::CE_ToolButtonLabel: {
// 展开按钮
if (const QStyleOptionToolButton *bopt = qstyleoption_cast<const QStyleOptionToolButton *>(option)) {
painter->save();
painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
painter->setPen(Qt::NoPen);
if (widget->objectName() == "qt_toolbar_ext_button")
{
if (bopt->state.testFlag(QStyle::State_Enabled) && bopt->state.testFlag(QStyle::State_MouseOver))
{
if (widget->objectName() == "qt_toolbar_ext_button") {
if (bopt->state.testFlag(QStyle::State_Enabled) && bopt->state.testFlag(QStyle::State_MouseOver)) {
painter->setBrush(ElaThemeColor(_themeMode, BasicHoverAlpha));
painter->drawRect(bopt->rect);
}
//展开图标
painter->setPen(!bopt->state.testFlag(QStyle::State_Enabled) ? ElaThemeColor(_themeMode, BasicTextDisable) : ElaThemeColor(_themeMode, BasicText));
// 展开图标
painter->setPen(!bopt->state.testFlag(QStyle::State_Enabled) ? ElaThemeColor(_themeMode, BasicTextDisable)
: ElaThemeColor(_themeMode, BasicText));
QFont iconFont = QFont("ElaAwesome");
iconFont.setPixelSize(18);
painter->setFont(iconFont);
painter->drawText(bopt->rect, Qt::AlignCenter, QChar((unsigned short)ElaIconType::AngleRight));
}
else
{
if (bopt->arrowType != Qt::NoArrow)
{
} else {
if (bopt->arrowType != Qt::NoArrow) {
break;
}
//背景绘制
if (bopt->state.testFlag(QStyle::State_Enabled))
{
if (bopt->state.testFlag(QStyle::State_Sunken))
{
// 背景绘制
if (bopt->state.testFlag(QStyle::State_Enabled)) {
if (bopt->state.testFlag(QStyle::State_Sunken)) {
painter->setBrush(ElaThemeColor(_themeMode, BasicPressAlpha));
painter->drawRoundedRect(bopt->rect, 4, 4);
}
else
{
if (bopt->state.testFlag(QStyle::State_MouseOver) || bopt->state.testFlag(QStyle::State_On))
{
} else {
if (bopt->state.testFlag(QStyle::State_MouseOver) || bopt->state.testFlag(QStyle::State_On)) {
painter->setBrush(ElaThemeColor(_themeMode, BasicHoverAlpha));
painter->drawRoundedRect(bopt->rect, 4, 4);
}
}
}
//指示器绘制
// 指示器绘制
_drawIndicator(painter, bopt, widget);
//图标绘制
QRect contentRect = subControlRect(QStyle::CC_ToolButton, bopt, QStyle::SC_ScrollBarAddLine, widget);
QRect iconRect = contentRect;
int heightOffset = iconRect.height() * 0.1;
// 图标绘制
QRect contentRect = subControlRect(QStyle::CC_ToolButton, bopt, QStyle::SC_ScrollBarAddLine, widget);
QRect iconRect = contentRect;
int heightOffset = iconRect.height() * 0.1;
iconRect.adjust(3, heightOffset, -3, -heightOffset);
_drawIcon(painter, iconRect, bopt, widget);
//文字绘制
// 文字绘制
contentRect.adjust(0, heightOffset, 0, -heightOffset);
_drawText(painter, contentRect, bopt);
}
@@ -156,42 +127,33 @@ void ElaToolBarStyle::drawControl(ControlElement element, const QStyleOption* op
}
return;
}
case QStyle::CE_ToolBar:
{
case QStyle::CE_ToolBar: {
return;
}
default:
{
default: {
break;
}
}
QProxyStyle::drawControl(element, option, painter, widget);
}
int ElaToolBarStyle::pixelMetric(PixelMetric metric, const QStyleOption* option, const QWidget* widget) const
{
switch (metric)
{
case QStyle::PM_ToolBarExtensionExtent:
{
int ElaToolBarStyle::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const {
switch (metric) {
case QStyle::PM_ToolBarExtensionExtent: {
return 16;
}
default:
{
default: {
break;
}
}
return QProxyStyle::pixelMetric(metric, option, widget);
}
void ElaToolBarStyle::_drawIndicator(QPainter* painter, const QStyleOptionToolButton* bopt, const QWidget* widget) const
{
if (bopt->features.testFlag(QStyleOptionToolButton::MenuButtonPopup))
{
void ElaToolBarStyle::_drawIndicator(QPainter *painter, const QStyleOptionToolButton *bopt, const QWidget *widget) const {
if (bopt->features.testFlag(QStyleOptionToolButton::MenuButtonPopup)) {
QRect indicatorRect = subControlRect(QStyle::CC_ToolButton, bopt, QStyle::SC_ScrollBarSubLine, widget);
//指示器区域
if (bopt->state.testFlag(QStyle::State_Enabled) && bopt->activeSubControls.testFlag(QStyle::SC_ScrollBarSubLine))
{
// 指示器区域
if (bopt->state.testFlag(QStyle::State_Enabled) && bopt->activeSubControls.testFlag(QStyle::SC_ScrollBarSubLine)) {
painter->setBrush(ElaThemeColor(_themeMode, BasicIndicator));
QPainterPath path;
path.moveTo(indicatorRect.topLeft());
@@ -203,10 +165,11 @@ void ElaToolBarStyle::_drawIndicator(QPainter* painter, const QStyleOptionToolBu
path.closeSubpath();
painter->drawPath(path);
}
//指示器
painter->setBrush(bopt->state.testFlag(QStyle::State_Enabled) ? ElaThemeColor(_themeMode, BasicText) : ElaThemeColor(_themeMode, BasicTextDisable));
// 指示器
painter->setBrush(bopt->state.testFlag(QStyle::State_Enabled) ? ElaThemeColor(_themeMode, BasicText)
: ElaThemeColor(_themeMode, BasicTextDisable));
QPainterPath indicatorPath;
qreal indicatorHeight = qCos(30 * M_PI / 180.0) * indicatorRect.width() * 0.85;
qreal indicatorHeight = qCos(30 * M_PI / 180.0) * indicatorRect.width() * 0.85;
indicatorPath.moveTo(indicatorRect.x() + indicatorRect.width() * 0.15, indicatorRect.center().y());
indicatorPath.lineTo(indicatorRect.right() - indicatorRect.width() * 0.15, indicatorRect.center().y());
indicatorPath.lineTo(indicatorRect.center().x(), indicatorRect.center().y() + indicatorHeight / 2);
@@ -215,81 +178,62 @@ void ElaToolBarStyle::_drawIndicator(QPainter* painter, const QStyleOptionToolBu
}
}
void ElaToolBarStyle::_drawIcon(QPainter* painter, QRectF iconRect, const QStyleOptionToolButton* bopt, const QWidget* widget) const
{
if (bopt->toolButtonStyle != Qt::ToolButtonTextOnly)
{
QSize iconSize = bopt->iconSize;
const QToolButton* toolButton = dynamic_cast<const QToolButton*>(widget);
QAction* action = toolButton->defaultAction();
if (!action)
{
void ElaToolBarStyle::_drawIcon(QPainter *painter, QRectF iconRect, const QStyleOptionToolButton *bopt, const QWidget *widget) const {
if (bopt->toolButtonStyle != Qt::ToolButtonTextOnly) {
QSize iconSize = bopt->iconSize;
const QToolButton *toolButton = dynamic_cast<const QToolButton *>(widget);
QAction *action = toolButton->defaultAction();
if (!action) {
return;
}
if (action->property("ElaIconType").toString().isEmpty())
{
//绘制QIcon
if (action->property("ElaIconType").toString().isEmpty()) {
// 绘制QIcon
QIcon icon = bopt->icon;
if (!icon.isNull())
{
QPixmap iconPix = icon.pixmap(iconSize,
(bopt->state & State_Enabled) ? QIcon::Normal
: QIcon::Disabled,
(bopt->state & State_Selected) ? QIcon::On
: QIcon::Off);
switch (bopt->toolButtonStyle)
{
if (!icon.isNull()) {
QPixmap iconPix = icon.pixmap(iconSize, (bopt->state & State_Enabled) ? QIcon::Normal : QIcon::Disabled,
(bopt->state & State_Selected) ? QIcon::On : QIcon::Off);
switch (bopt->toolButtonStyle) {
case Qt::ToolButtonIconOnly:
case Qt::ToolButtonTextBesideIcon:
{
case Qt::ToolButtonTextBesideIcon: {
painter->drawPixmap(QRect(QPoint(iconRect.x(), iconRect.center().y() - iconSize.height() / 2), iconSize), iconPix);
break;
}
case Qt::ToolButtonTextUnderIcon:
{
case Qt::ToolButtonTextUnderIcon: {
painter->drawPixmap(QRect(QPoint(iconRect.center().x() - iconSize.width() / 2, iconRect.y()), iconSize), iconPix);
break;
}
case Qt::ToolButtonFollowStyle:
{
case Qt::ToolButtonFollowStyle: {
painter->drawPixmap(iconRect.x(), iconRect.y() - iconSize.height() / 2, iconPix);
break;
}
default:
{
default: {
break;
}
}
}
}
else
{
//绘制ElaIcon
} else {
// 绘制ElaIcon
painter->save();
painter->setPen(ElaThemeColor(_themeMode, BasicText));
QFont iconFont = QFont("ElaAwesome");
switch (bopt->toolButtonStyle)
{
switch (bopt->toolButtonStyle) {
case Qt::ToolButtonIconOnly:
case Qt::ToolButtonTextBesideIcon:
case Qt::ToolButtonFollowStyle:
{
case Qt::ToolButtonFollowStyle: {
QRect adjustIconRect(iconRect.x(), iconRect.y(), iconSize.width(), iconRect.height());
iconFont.setPixelSize(0.8 * std::min(adjustIconRect.width(), adjustIconRect.height()));
painter->setFont(iconFont);
painter->drawText(adjustIconRect, Qt::AlignCenter, action->property("ElaIconType").toString());
break;
}
case Qt::ToolButtonTextUnderIcon:
{
case Qt::ToolButtonTextUnderIcon: {
QRect adjustIconRect(iconRect.center().x() - iconSize.width() / 2, iconRect.y(), iconSize.width(), iconSize.height());
iconFont.setPixelSize(0.8 * std::min(iconSize.width(), iconSize.height()));
painter->setFont(iconFont);
painter->drawText(adjustIconRect, Qt::AlignHCenter, action->property("ElaIconType").toString());
break;
}
default:
{
default: {
break;
}
}
@@ -298,34 +242,28 @@ void ElaToolBarStyle::_drawIcon(QPainter* painter, QRectF iconRect, const QStyle
}
}
void ElaToolBarStyle::_drawText(QPainter* painter, QRect contentRect, const QStyleOptionToolButton* bopt) const
{
if (!bopt->text.isEmpty())
{
void ElaToolBarStyle::_drawText(QPainter *painter, QRect contentRect, const QStyleOptionToolButton *bopt) const {
if (!bopt->text.isEmpty()) {
painter->setPen(ElaThemeColor(_themeMode, BasicText));
switch (bopt->toolButtonStyle)
{
case Qt::ToolButtonTextOnly:
{
switch (bopt->toolButtonStyle) {
case Qt::ToolButtonTextOnly: {
painter->drawText(contentRect, Qt::AlignCenter, bopt->text);
break;
}
case Qt::ToolButtonTextBesideIcon:
{
painter->drawText(QRect(contentRect.x() + bopt->iconSize.width() + 12, contentRect.y(), contentRect.width() - bopt->iconSize.width(), contentRect.height()), Qt::AlignLeft | Qt::AlignVCenter, bopt->text);
case Qt::ToolButtonTextBesideIcon: {
painter->drawText(QRect(contentRect.x() + bopt->iconSize.width() + 12, contentRect.y(), contentRect.width() - bopt->iconSize.width(),
contentRect.height()),
Qt::AlignLeft | Qt::AlignVCenter, bopt->text);
break;
}
case Qt::ToolButtonTextUnderIcon:
{
case Qt::ToolButtonTextUnderIcon: {
painter->drawText(contentRect, Qt::AlignBottom | Qt::AlignHCenter, bopt->text);
break;
}
case Qt::ToolButtonFollowStyle:
{
case Qt::ToolButtonFollowStyle: {
break;
}
default:
{
default: {
break;
}
}