Skip to content

Commit

Permalink
一些细节调整
Browse files Browse the repository at this point in the history
修正quickaccessbar的尺寸在刚启动时刷新的问题
调整了一个接口名字paintBackground->paintTabbarBaseLine
  • Loading branch information
czyt1988 committed Dec 11, 2023
1 parent bdf672c commit 1c45a01
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 59 deletions.
74 changes: 31 additions & 43 deletions src/SARibbonBar/SARibbonBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class SARibbonBar::PrivateData
SARibbonBar::SARibbonBar(QWidget* parent) : QMenuBar(parent), d_ptr(new SARibbonBar::PrivateData(this))
{
d_ptr->init();
ensurePolished();
setNativeMenuBar(false);
// #ifdef Q_OS_MACOS
// setNativeMenuBar(false);
Expand Down Expand Up @@ -1395,6 +1396,13 @@ bool SARibbonBar::isTitleVisible() const
void SARibbonBar::setEnableUserDefineAccessBarIconSize(bool on)
{
d_ptr->mEnableUserDefineAccessBarIconSize = on;
if (!(d_ptr->mEnableUserDefineAccessBarIconSize)) { //允许用户自定义AccessBar的IconSize就不进入此条件重置大小
// 变更iconsize
QSize btnIconSize = PrivateData::calcIconSizeByHeight(titleBarHeight());
if (btnIconSize != d_ptr->mQuickAccessBar->iconSize()) {
d_ptr->mQuickAccessBar->setIconSize(btnIconSize);
}
}
}

/**
Expand All @@ -1418,6 +1426,13 @@ bool SARibbonBar::isEnableUserDefineAccessBarIconSize() const
void SARibbonBar::setEnableUserDefineRightBarIconSize(bool on)
{
d_ptr->mEnableUserDefineRightBarIconSize = on;
// 变更iconsize
if (!(d_ptr->mEnableUserDefineRightBarIconSize)) {
QSize btnIconSize = PrivateData::calcIconSizeByHeight(titleBarHeight());
if (btnIconSize != d_ptr->mRightButtonGroup->iconSize()) {
d_ptr->mRightButtonGroup->setIconSize(btnIconSize);
}
}
}

/**
Expand Down Expand Up @@ -1561,6 +1576,7 @@ void SARibbonBar::updateCategoryTitleToTabName()
}
}
}
repaint();
}

void SARibbonBar::paintEvent(QPaintEvent* e)
Expand All @@ -1584,11 +1600,11 @@ void SARibbonBar::paintInNormalStyle()
QPainter p(this);

//!
paintBackground(p);
paintTabbarBaseLine(p);
//! 显示上下文标签
p.save();
QList< _SAContextCategoryManagerData > contextCategoryDataList = d_ptr->mCurrentShowingContextCategory;
bool isCurrentSelectContextCategoryPage = false;
// bool isCurrentSelectContextCategoryPage = false;

QPoint contextCategoryRegion(width(), -1);
QMargins border = contentsMargins();
Expand All @@ -1615,16 +1631,16 @@ void SARibbonBar::paintInNormalStyle()
contextCategoryRegion.setY(contextTitleRect.right());
}
}
isCurrentSelectContextCategoryPage = indexs.contains(d_ptr->mRibbonTabBar->currentIndex());
if (isCurrentSelectContextCategoryPage) {
QPen pen;
pen.setColor(clr);
pen.setWidth(1);
p.setPen(pen);
p.setBrush(Qt::NoBrush);
p.drawRect(d_ptr->mStackedContainerWidget->geometry());
isCurrentSelectContextCategoryPage = false;
}
// isCurrentSelectContextCategoryPage = indexs.contains(d_ptr->mRibbonTabBar->currentIndex());
// if (isCurrentSelectContextCategoryPage) {
// QPen pen;
// pen.setColor(clr);
// pen.setWidth(1);
// p.setPen(pen);
// p.setBrush(Qt::NoBrush);
// p.drawRect(d_ptr->mStackedContainerWidget->geometry());
// isCurrentSelectContextCategoryPage = false;
// }
}
p.restore();
//! 显示标题等
Expand Down Expand Up @@ -1667,7 +1683,7 @@ void SARibbonBar::paintInWpsLiteStyle()
{
QPainter p(this);
//!
paintBackground(p);
paintTabbarBaseLine(p);
//! 显示上下文标签
p.save();
QList< _SAContextCategoryManagerData > contextCategoryDataList = d_ptr->mCurrentShowingContextCategory;
Expand Down Expand Up @@ -1885,14 +1901,7 @@ void SARibbonBar::resizeInOfficeStyle()
}
QSize quickAccessBarSize = d_ptr->mQuickAccessBar->sizeHint();
// 上下留1px的边线
d_ptr->mQuickAccessBar->setGeometry(x, y + 1, quickAccessBarSize.width(), validTitleBarHeight - 2);
if (!(d_ptr->mEnableUserDefineAccessBarIconSize)) { //允许用户自定义AccessBar的IconSize就不进入此条件重置大小
// 变更iconsize
QSize btnIconSize = PrivateData::calcIconSizeByHeight(validTitleBarHeight - 2);
if (btnIconSize != d_ptr->mQuickAccessBar->iconSize()) {
d_ptr->mQuickAccessBar->setIconSize(btnIconSize);
}
}
d_ptr->mQuickAccessBar->setGeometry(x, y + 1, quickAccessBarSize.width(), validTitleBarHeight);
}
}
// 第二行,开始布局applicationButton,tabbar,tabBarRightSizeButtonGroupWidget,TopRightCorner
Expand Down Expand Up @@ -1933,13 +1942,6 @@ void SARibbonBar::resizeInOfficeStyle()
endX -= wSize.width();
// 上下留1px的边线
d_ptr->mRightButtonGroup->setGeometry(endX, y + 1, wSize.width(), tabH - 2);
// 变更iconsize
if (!(d_ptr->mEnableUserDefineRightBarIconSize)) {
QSize btnIconSize = PrivateData::calcIconSizeByHeight(tabH - 2);
if (btnIconSize != d_ptr->mRightButtonGroup->iconSize()) {
d_ptr->mRightButtonGroup->setIconSize(btnIconSize);
}
}
}
// 最后确定tabbar宽度
int tabBarAllowedWidth = endX - x;
Expand Down Expand Up @@ -1994,13 +1996,6 @@ void SARibbonBar::resizeInWpsLiteStyle()
endX -= wSize.width();
// 上下留1px的边线
d_ptr->mRightButtonGroup->setGeometry(endX, y + 1, wSize.width(), validTitleBarHeight - 2);
// 变更iconsize
if (!(d_ptr->mEnableUserDefineRightBarIconSize)) {
QSize btnIconSize = PrivateData::calcIconSizeByHeight(validTitleBarHeight - 2);
if (btnIconSize != d_ptr->mRightButtonGroup->iconSize()) {
d_ptr->mRightButtonGroup->setIconSize(btnIconSize);
}
}
}
// quick access bar定位
if (d_ptr->mQuickAccessBar) {
Expand All @@ -2009,13 +2004,6 @@ void SARibbonBar::resizeInWpsLiteStyle()
endX -= quickAccessBarSize.width();
// 上下留1px的边线
d_ptr->mQuickAccessBar->setGeometry(endX, y + 1, quickAccessBarSize.width(), validTitleBarHeight - 2);
// 变更iconsize
if (!(d_ptr->mEnableUserDefineAccessBarIconSize)) { //允许用户自定义AccessBar的IconSize就不进入此条件重置大小
QSize btnIconSize = PrivateData::calcIconSizeByHeight(validTitleBarHeight - 2);
if (btnIconSize != d_ptr->mQuickAccessBar->iconSize()) {
d_ptr->mQuickAccessBar->setIconSize(btnIconSize);
}
}
}
}
// cornerWidget - TopLeftCorner
Expand Down Expand Up @@ -2077,7 +2065,7 @@ void SARibbonBar::resizeInWpsLiteStyle()
resizeStackedContainerWidget();
}

void SARibbonBar::paintBackground(QPainter& painter)
void SARibbonBar::paintTabbarBaseLine(QPainter& painter)
{
painter.save();
// 在tabbar下绘制一条线
Expand Down
2 changes: 1 addition & 1 deletion src/SARibbonBar/SARibbonBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ protected slots:
virtual void resizeEvent(QResizeEvent* e) Q_DECL_OVERRIDE;
virtual void moveEvent(QMoveEvent* e) Q_DECL_OVERRIDE;
virtual void changeEvent(QEvent* e) Q_DECL_OVERRIDE;
virtual void paintBackground(QPainter& painter);
virtual void paintTabbarBaseLine(QPainter& painter);
virtual void paintWindowTitle(QPainter& painter, const QString& title, const QRect& titleRegion);
virtual void paintContextCategoryTab(QPainter& painter, const QString& title, QRect contextRect, const QColor& color);
};
Expand Down
11 changes: 8 additions & 3 deletions src/SARibbonBar/SARibbonCtrlContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <QStylePainter>
#include <QDebug>
#include <QLabel>
#include "SARibbonDrawHelper.h"

/**
* @brief The SARibbonCtrlContainerPrivate class
Expand Down Expand Up @@ -117,8 +116,14 @@ bool SARibbonCtrlContainer::hasContainerWidget() const
*/
void SARibbonCtrlContainer::setIcon(const QIcon& i)
{
d_ptr->icon = i;
d_ptr->labelPixmap->setPixmap(i.pixmap(d_ptr->iconSize));
d_ptr->icon = i;
QPixmap pixmap = i.pixmap(d_ptr->iconSize);
d_ptr->labelPixmap->setPixmap(pixmap);
}

void SARibbonCtrlContainer::setIcon(const QPixmap& pixmap)
{
d_ptr->labelPixmap->setPixmap(pixmap);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/SARibbonBar/SARibbonCtrlContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SA_RIBBON_EXPORT SARibbonCtrlContainer : public QWidget
bool hasContainerWidget() const;
//图标
void setIcon(const QIcon& i);
void setIcon(const QPixmap& pixmap);
QIcon getIcon() const;
//图标
void setText(const QString& t);
Expand Down
2 changes: 1 addition & 1 deletion src/SARibbonBar/SARibbonPannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ void SARibbonPannel::resizeEvent(QResizeEvent* event)
if (ThreeRowMode == pannelLayoutMode()) {
d_ptr->m_optionActionButton->move(width() - d_ptr->m_optionActionButton->width() - 2,
height() - titleHeight()
+ (titleHeight() - d_ptr->m_optionActionButton->height()) / 2);
+ (titleHeight() - d_ptr->m_optionActionButton->height()) / 2);
} else {
d_ptr->m_optionActionButton->move(width() - d_ptr->m_optionActionButton->width(),
height() - d_ptr->m_optionActionButton->height());
Expand Down
4 changes: 1 addition & 3 deletions src/SARibbonBar/SARibbonToolButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ QSize SARibbonToolButton::PrivateData::calcLargeButtonSizeHint(const QStyleOptio
int h = opt.fontMetrics.lineSpacing() * 4.5; // 3*1.5
int minW = h * 0.75; //最小宽度,在pannel里面的按钮,最小宽度要和icon适应
if (mDrawIconRect.isValid()) {
minW = mDrawIconRect.height();
minW = mDrawIconRect.width();
}
if (SARibbonPannel* pannel = qobject_cast< SARibbonPannel* >(q_ptr->parent())) {
//对于建立在SARibbonPannel的基础上的大按钮,把高度设置为SARibbonPannel计算的大按钮高度
Expand Down Expand Up @@ -539,7 +539,6 @@ int SARibbonToolButton::PrivateData::estimateLargeButtonTextWidth(int buttonHeig
float widthHeightRatio,
int maxTrycount)
{

QSize textSize;
int space = SA_FONTMETRICS_WIDTH(fm, (QLatin1Char(' '))) * 2;
int hintMaxWidth = buttonHeight * widthHeightRatio; ///< 建议的宽度
Expand Down Expand Up @@ -1064,7 +1063,6 @@ void SARibbonToolButton::updateRect()
{
QStyleOptionToolButton opt;
initStyleOption(&opt);
// d_ptr->updateSizeHint(opt);
d_ptr->updateDrawRect(opt);
}

Expand Down
14 changes: 7 additions & 7 deletions src/example/MainWindowExample/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void MainWindow::createRibbonApplicationButton()
btn = new SARibbonApplicationButton(this);
ribbon->setApplicationButton(btn);
}
ribbon->applicationButton()->setText((" File ")); // 文字两边留有间距,好看一点
ribbon->applicationButton()->setText((" File ")); // 文字两边留有间距,好看一点
// cn: SARibbonMenu和QMenu的操作是一样的
// en: The operations of SARibbonMenu and QMenu are the same
if (!mMenuApplicationBtn) {
Expand Down Expand Up @@ -217,14 +217,14 @@ void MainWindow::onStyleClicked(int id)
switch (ribbonStyle) {
case SARibbonBar::RibbonStyleLooseThreeRow:
mTextedit->append(
tr("\nchange ribbon style to office style,The standard office style text display is line wrapped, "
"and you can also control whether it wrap through SARibbonToolButton::setEnableWordWrap")); // cn:标准的office样式的文字显示是换行的,你也可以通过SARibbonToolButton::setEnableWordWrap来控制它是否换行
tr("\nchange ribbon style to office style,The standard office style text display is line wrapped, "
"and you can also control whether it wrap through SARibbonToolButton::setEnableWordWrap")); // cn:标准的office样式的文字显示是换行的,你也可以通过SARibbonToolButton::setEnableWordWrap来控制它是否换行
mTextedit->append(tr("ribbonBar()->setRibbonStyle(SARibbonBar::OfficeStyle);"));
break;
case SARibbonBar::RibbonStyleLooseTwoRow:
mTextedit->append(
tr("\nchange ribbon style to office style 2 row,All text in 2-line mode does not wrap, and you "
"can also control whether it wraps through SARibbonToolButton: setEnableWordWrap")); // cn:所有2行模式的文字都是不换行的,你也可以通过SARibbonToolButton::setEnableWordWrap来控制它是否换行
tr("\nchange ribbon style to office style 2 row,All text in 2-line mode does not wrap, and you "
"can also control whether it wraps through SARibbonToolButton: setEnableWordWrap")); // cn:所有2行模式的文字都是不换行的,你也可以通过SARibbonToolButton::setEnableWordWrap来控制它是否换行
mTextedit->append(tr("ribbonBar()->setRibbonStyle(SARibbonBar::OfficeStyleTwoRow);"));
break;
case SARibbonBar::RibbonStyleCompactThreeRow:
Expand Down Expand Up @@ -298,7 +298,7 @@ void MainWindow::onActionHelpTriggered()
"\n Author:czy"
"\n Email:czy.t@163.com"
"\n ===============")
.arg(SARibbonBar::versionString()));
.arg(SARibbonBar::versionString()));
}

void MainWindow::onActionRemoveAppBtnTriggered(bool b)
Expand Down Expand Up @@ -430,7 +430,7 @@ void MainWindow::onColorButtonColorClicked(const QColor& c, bool on)
void MainWindow::onRibbonThemeComboBoxCurrentIndexChanged(int index)
{
SARibbonMainWindow::RibbonTheme t = static_cast< SARibbonMainWindow::RibbonTheme >(
mComboboxRibbonTheme->itemData(index).toInt());
mComboboxRibbonTheme->itemData(index).toInt());
setRibbonTheme(t);
}

Expand Down
5 changes: 4 additions & 1 deletion src/example/WidgetWithRibbon/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ void Widget::buildRibbon(SARibbonBar* bar)
page1->setCategoryName("page1");
SARibbonPannel* pannel1 = new SARibbonPannel("pannel1", page1);
page1->addPannel(pannel1);
pannel1->addLargeAction(createAction("save", ":/icon/icon/save.svg"));
QAction* act = createAction(" save ", ":/icon/icon/save.svg");
act->setIconText(" save ");
pannel1->addLargeAction(act);

pannel1->addLargeAction(createAction("open", ":/icon/icon/folder-star.svg"));
pannel1->addSmallAction(createAction("action1", ":/icon/icon/action.svg"));
pannel1->addSmallAction(createAction("action2", ":/icon/icon/action2.svg"));
Expand Down

0 comments on commit 1c45a01

Please sign in to comment.