Skip to content

Commit

Permalink
调整了sperate的实现方式,修正了各个主题的对于分割线的定义
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt1988 committed Jan 7, 2024
1 parent 210a005 commit ec8a637
Show file tree
Hide file tree
Showing 21 changed files with 634 additions and 129 deletions.
20 changes: 14 additions & 6 deletions src/SARibbonBar/SARibbonBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void SARibbonBar::PrivateData::init()
*/
int SARibbonBar::PrivateData::calcTabBarHeight(const QFontMetrics& fm)
{
int r = fm.height() * 1.5;
int r = fm.height() * 1.6;
if (r < 20) {
r = 20;
}
Expand All @@ -194,6 +194,7 @@ int SARibbonBar::PrivateData::calcTitleBarHeight(const QFontMetrics& fm)

/**
* @brief 估算category的高度
* @note 经过对照,1.6行高和office的高度比较接近
* @param fm
* @param s
* @return
Expand All @@ -202,13 +203,13 @@ int SARibbonBar::PrivateData::calcCategoryHeight(const QFontMetrics& fm, SARibbo
{
int textH = fm.lineSpacing(); // 这里用linespace,因为在换行的情况下,行距是不可忽略的,ribbon的大按钮默认是2行
if (SARibbonPannel::ThreeRowMode == lm) {
// 5.5=(3*1.5+1) (三行),1是给panneltitle预留的
return textH * 4.5 + pannelTitleHeight;
// 5.5=(3*1.6+1) (三行),1是给panneltitle预留的
return textH * 4.8 + pannelTitleHeight;
} else {
// 3=2*1.5
return textH * 3 + pannelTitleHeight;
// 3=2*1.6
return textH * 3.2 + pannelTitleHeight;
}
return (textH * 4.5 + pannelTitleHeight);
return (textH * 4.8 + pannelTitleHeight);
}

/**
Expand Down Expand Up @@ -2478,8 +2479,15 @@ void SARibbonBar::resizeInCompactStyle()
resizeStackedContainerWidget();
}

/**
* @brief 绘制tabbar下的基准线,这个方法仅仅在office2013模式下需要
* @param painter
*/
void SARibbonBar::paintTabbarBaseLine(QPainter& painter)
{
if (!d_ptr->mTabBarBaseLineColor.isValid()) {
return;
}
painter.save();
// 在tabbar下绘制一条线
const int lineY = d_ptr->mRibbonTabBar->geometry().bottom();
Expand Down
22 changes: 13 additions & 9 deletions src/SARibbonBar/SARibbonButtonGroupWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ SARibbonButtonGroupWidget::PrivateData::PrivateData(SARibbonButtonGroupWidget* p
void SARibbonButtonGroupWidget::PrivateData::init()
{
QHBoxLayout* layout = new QHBoxLayout(q_ptr);
//上下保留一点间隙
// 上下保留一点间隙
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
q_ptr->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
//默认尺寸24
// 默认尺寸24
mIconSize = QSize(24, 24);
}

Expand Down Expand Up @@ -103,7 +103,9 @@ SARibbonButtonGroupWidget::~SARibbonButtonGroupWidget()
}
}

QAction* SARibbonButtonGroupWidget::addAction(QAction* a, Qt::ToolButtonStyle buttonStyle, QToolButton::ToolButtonPopupMode popMode)
QAction* SARibbonButtonGroupWidget::addAction(QAction* a,
Qt::ToolButtonStyle buttonStyle,
QToolButton::ToolButtonPopupMode popMode)
{
SARibbonPannel::setActionToolButtonStyleProperty(a, buttonStyle);
SARibbonPannel::setActionToolButtonPopupModeProperty(a, popMode);
Expand All @@ -119,7 +121,10 @@ QAction* SARibbonButtonGroupWidget::addAction(QAction* a, Qt::ToolButtonStyle bu
* @param popMode
* @return
*/
QAction* SARibbonButtonGroupWidget::addAction(const QString& text, const QIcon& icon, Qt::ToolButtonStyle buttonStyle, QToolButton::ToolButtonPopupMode popMode)
QAction* SARibbonButtonGroupWidget::addAction(const QString& text,
const QIcon& icon,
Qt::ToolButtonStyle buttonStyle,
QToolButton::ToolButtonPopupMode popMode)
{
QAction* a = new QAction(icon, text, this);
addAction(a, buttonStyle, popMode);
Expand Down Expand Up @@ -152,7 +157,7 @@ QAction* SARibbonButtonGroupWidget::addWidget(QWidget* w)
return (a);
}

SARibbonControlToolButton *SARibbonButtonGroupWidget::actionToRibbonControlToolButton(QAction *action)
SARibbonControlToolButton* SARibbonButtonGroupWidget::actionToRibbonControlToolButton(QAction* action)
{
for (auto obj : qAsConst(children())) {
if (obj->isWidgetType()) {
Expand Down Expand Up @@ -219,18 +224,17 @@ void SARibbonButtonGroupWidget::actionEvent(QActionEvent* e)
if (item.widget != nullptr) {
item.widget->setAttribute(Qt::WA_LayoutUsesWidgetRect);
item.widget->show();
//widget高度保持一致
// widget高度保持一致
item.widget->setFixedHeight(d_ptr->mIconSize.height());
item.customWidget = true;
}
} else if (item.action->isSeparator()) {
SARibbonSeparatorWidget* sp = RibbonSubElementDelegate->createRibbonSeparatorWidget(this);
sp->setTopBottomMargins(3, 3);
item.widget = sp;
item.widget = sp;
}
// 不是widget,自动生成ButtonTyle
if (!item.widget) {
//只使用small
// 只使用small
SARibbonToolButton::RibbonButtonType buttonType = SARibbonToolButton::SmallButton;
SARibbonControlToolButton* button = RibbonSubElementDelegate->createRibbonControlToolButton(this);
button->setAutoRaise(true);
Expand Down
5 changes: 0 additions & 5 deletions src/SARibbonBar/SARibbonElementFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ SARibbonPannel* SARibbonElementFactory::createRibbonPannel(QWidget* parent)
return (new SARibbonPannel(parent));
}

SARibbonSeparatorWidget* SARibbonElementFactory::createRibbonSeparatorWidget(int value, QWidget* parent)
{
return (new SARibbonSeparatorWidget(value, parent));
}

SARibbonSeparatorWidget* SARibbonElementFactory::createRibbonSeparatorWidget(QWidget* parent)
{
return (new SARibbonSeparatorWidget(parent));
Expand Down
1 change: 0 additions & 1 deletion src/SARibbonBar/SARibbonElementFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class SA_RIBBON_EXPORT SARibbonElementFactory
virtual SARibbonCategory* createRibbonCategory(QWidget* parent);
virtual SARibbonContextCategory* createRibbonContextCategory(QWidget* parent);
virtual SARibbonPannel* createRibbonPannel(QWidget* parent);
virtual SARibbonSeparatorWidget* createRibbonSeparatorWidget(int value, QWidget* parent);
virtual SARibbonSeparatorWidget* createRibbonSeparatorWidget(QWidget* parent);
virtual SARibbonGallery* createRibbonGallery(QWidget* parent);
virtual SARibbonGalleryGroup* createRibbonGalleryGroup(QWidget* parent);
Expand Down
50 changes: 35 additions & 15 deletions src/SARibbonBar/SARibbonMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SARibbonMainWindow::PrivateData
void installFrameless(SARibbonMainWindow* p);

public:
SARibbonMainWindow::RibbonTheme mCurrentRibbonTheme { SARibbonMainWindow::RibbonThemeOffice2013 };
SARibbonMainWindow::RibbonTheme mCurrentRibbonTheme { SARibbonMainWindow::RibbonThemeOffice2021Blue };
SARibbonBar* mRibbonBar { nullptr };
SAWindowButtonGroup* mWindowButtonGroup { nullptr };
#if SARIBBON_USE_3RDPARTY_FRAMELESSHELPER
Expand Down Expand Up @@ -205,33 +205,44 @@ void SARibbonMainWindow::setRibbonTheme(SARibbonMainWindow::RibbonTheme theme)
sa_set_ribbon_theme(this, theme);
d_ptr->mCurrentRibbonTheme = theme;
if (SARibbonBar* bar = ribbonBar()) {
auto theme = ribbonTheme();
// 尺寸修正
switch (ribbonTheme()) {
switch (theme) {
case RibbonThemeWindows7:
break;
case RibbonThemeOffice2013:
case RibbonThemeOffice2016Blue:
case RibbonThemeDark: {
case RibbonThemeDark:
case RibbonThemeDark2: {
//! 在设置qss后需要针对margin信息重新设置进SARibbonTabBar中
//! office2013.qss的margin信息如下设置,em是字符M所对应的宽度的长度
//! margin-top: 0em;
//! margin-right: 0em;
//! margin-left: 0.2em;
//! margin-bottom: 0em;

//! office2013.qss的margin信息如下设置
//! margin-top: 0px;
//! margin-right: 0px;
//! margin-left: 5px;
//! margin-bottom: 0px;
SARibbonTabBar* tab = bar->ribbonTabBar();
if (!tab) {
break;
}
QFontMetrics fm = tab->fontMetrics();
int emWidth = SA_FONTMETRICS_WIDTH(fm, "M");
tab->setTabMargin(QMargins(0.2 * emWidth, 0, 0, 0));
tab->setTabMargin(QMargins(5, 0, 0, 0));
} break;
case RibbonThemeOffice2021Blue: {
SARibbonTabBar* tab = bar->ribbonTabBar();
if (!tab) {
break;
}
//! 在设置qss后需要针对margin信息重新设置进SARibbonTabBar中
//! office2021.qss的margin信息如下设置
//! margin-top: 0px;
//! margin-right: 5px;
//! margin-left: 5px;
//! margin-bottom: 0px;
tab->setTabMargin(QMargins(5, 0, 5, 0));
}
default:
break;
}
// 上下文标签颜色设置
switch (ribbonTheme()) {
// 上下文标签颜色设置,以及基线颜色设置
switch (theme) {
case RibbonThemeWindows7:
case RibbonThemeOffice2013:
case RibbonThemeDark:
Expand All @@ -246,6 +257,12 @@ void SARibbonMainWindow::setRibbonTheme(SARibbonMainWindow::RibbonTheme theme)
default:
break;
}
// 基线颜色设置
if (RibbonThemeOffice2013 == theme) {
bar->setTabBarBaseLineColor(QColor(186, 201, 219));
} else {
bar->setTabBarBaseLineColor(QColor());
}
}
}

Expand Down Expand Up @@ -361,6 +378,9 @@ void sa_set_ribbon_theme(QWidget* w, SARibbonMainWindow::RibbonTheme theme)
case SARibbonMainWindow::RibbonThemeDark:
file.setFileName(":/theme/resource/theme-dark.qss");
break;
case SARibbonMainWindow::RibbonThemeDark2:
file.setFileName(":/theme/resource/theme-dark2.qss");
break;
default:
file.setFileName(":/theme/resource/theme-office2013.qss");
break;
Expand Down
3 changes: 2 additions & 1 deletion src/SARibbonBar/SARibbonMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class SA_RIBBON_EXPORT SARibbonMainWindow : public QMainWindow
RibbonThemeOffice2016Blue, ///< office2016-蓝色主题
RibbonThemeOffice2021Blue, ///< office2021-蓝色主题
RibbonThemeWindows7, ///< win7主题
RibbonThemeDark ///< 暗色主题
RibbonThemeDark, ///< 暗色主题
RibbonThemeDark2
};
Q_ENUM(RibbonTheme)
public:
Expand Down
14 changes: 6 additions & 8 deletions src/SARibbonBar/SARibbonPannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,12 @@ SARibbonGallery* SARibbonPannel::addGallery(bool expanding)
* @param top 上边距 @default 6
* @param bottom 下边距 @default 6
*/
QAction* SARibbonPannel::addSeparator(int top, int bottom)
QAction* SARibbonPannel::addSeparator()
{
QAction* action = new QAction(this);

action->setSeparator(true);
setActionRowProportionProperty(action, SARibbonPannelItem::Large);
action->setProperty(SA_ActionPropertyName_SeparatorTop, top);
action->setProperty(SA_ActionPropertyName_SeparatorBottom, bottom);
addAction(action);
return (action);
}
Expand Down Expand Up @@ -759,18 +757,18 @@ int SARibbonPannel::pannelHeightHint(const QFontMetrics& fm, PannelLayoutMode la
int textH = fm.lineSpacing(); // 这里用linespace,因为在换行的情况下,行距是不可忽略的,ribbon的大按钮默认是2行
switch (layMode) {
case SARibbonPannel::ThreeRowMode: {
// 5.5=(3*1.5+1) (三行),1是给panneltitle预留的
return textH * 4.5 + pannelTitleHeight;
// 5.5=(3*1.6+1) (三行),1是给panneltitle预留的
return textH * 4.8 + pannelTitleHeight;
} break;
case SARibbonPannel::TwoRowMode: {
// 3=2*1.5
return textH * 3 + pannelTitleHeight;
// 3=2*1.6
return textH * 3.2 + pannelTitleHeight;
} break;
default: {
qWarning() << "unknow SARibbonPannel::PannelLayoutMode:" << layMode;
}
}
return (textH * 4.5 + pannelTitleHeight);
return (textH * 4.8 + pannelTitleHeight);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/SARibbonBar/SARibbonPannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class SA_RIBBON_EXPORT SARibbonPannel : public QFrame
SARibbonGallery* addGallery(bool expanding = true);

// 添加分割线
QAction* addSeparator(int top = 6, int bottom = 6);
QAction* addSeparator();

// 从pannel中把action对应的button提取出来,如果action没有对应的button,就返回nullptr
SARibbonToolButton* actionToRibbonToolButton(QAction* action);
Expand Down
6 changes: 0 additions & 6 deletions src/SARibbonBar/SARibbonPannelItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,4 @@ class SA_RIBBON_EXPORT SARibbonPannelItem : public QWidgetItem
#ifndef SA_ActionPropertyName_ToolButtonStyle
#define SA_ActionPropertyName_ToolButtonStyle "_sa_ToolButtonStyle"
#endif
#ifndef SA_ActionPropertyName_SeparatorTop
#define SA_ActionPropertyName_SeparatorTop "_sa_SeparatorTop"
#endif
#ifndef SA_ActionPropertyName_SeparatorBottom
#define SA_ActionPropertyName_SeparatorBottom "_sa_SeparatorBottom"
#endif
#endif // SARIBBONPANNELITEM_H
3 changes: 0 additions & 3 deletions src/SARibbonBar/SARibbonPannelLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,6 @@ SARibbonPannelItem* SARibbonPannelLayout::createItem(QAction* action, SARibbonPa
} else if (action->isSeparator()) {
SARibbonSeparatorWidget* sep = RibbonSubElementDelegate->createRibbonSeparatorWidget(pannel);
widget = sep;
auto t = action->property(SA_ActionPropertyName_SeparatorTop).toInt();
auto b = action->property(SA_ActionPropertyName_SeparatorBottom).toInt();
sep->setTopBottomMargins(t, b);
}
// 不是widget,自动生成SARibbonToolbutton
if (!widget) {
Expand Down
49 changes: 8 additions & 41 deletions src/SARibbonBar/SARibbonSeparatorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,17 @@
#include <QPainter>
#include <QDebug>

SARibbonSeparatorWidget::SARibbonSeparatorWidget(int height, QWidget *parent)
: QWidget(parent)
, m_topMargins(4)
, m_bottomMargins(4)
SARibbonSeparatorWidget::SARibbonSeparatorWidget(QWidget* parent) : QFrame(parent)
{
setFixedSize(6, height);
setFrameShape(QFrame::VLine);
setFrameShadow(QFrame::Plain);
setLineWidth(1);
setMidLineWidth(1);
}


SARibbonSeparatorWidget::SARibbonSeparatorWidget(QWidget *parent)
: QWidget(parent)
, m_topMargins(4)
, m_bottomMargins(4)
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
setFixedWidth(6);
}


QSize SARibbonSeparatorWidget::sizeHint() const
{
return (QSize(6, height()));
}


/**
* @brief 设置分割线的上下距离
* @param top 上边界,默认为4
* @param bottom 下边界,默认为4
*/
void SARibbonSeparatorWidget::setTopBottomMargins(int top, int bottom)
{
m_topMargins = top;
m_bottomMargins = bottom;
}


void SARibbonSeparatorWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);

painter.setPen(palette().window().color().darker(114));
int x1 = rect().center().x();

painter.drawLine(QPoint(x1, rect().top() + m_topMargins), QPoint(x1, rect().bottom() - m_bottomMargins));
QSize sh = QFrame::sizeHint();
sh.setWidth(1);
return sh;
}
Loading

0 comments on commit ec8a637

Please sign in to comment.