Skip to content

Commit

Permalink
!13 SARibbonQuickAccessBar和SARibbonButtonGroupWidget优化
Browse files Browse the repository at this point in the history
Merge pull request !13 from xinchangli/master
  • Loading branch information
czyt1988 authored and gitee-org committed Dec 29, 2023
2 parents b1854ef + c2e5a9f commit d8ae756
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 49 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project(SARibbon VERSION ${SARIBBON_VERSION} LANGUAGES CXX)

# option(BUILD_SHARED_LIBS "build the SARibbonBar in shared lib mode" ON)
option(SARIBBON_BUILD_EXAMPLES "build the examples" ON)
option(SARIBBON_USE_FRAMELESS_LIB "use frameless lib" OFF)

# load Qt library, minimum version required is 5.8
# cn:Qt库加载,最低版本要求为5.8
Expand All @@ -25,20 +26,19 @@ find_package(Qt${QT_VERSION_MAJOR} ${SARIBBON_MIN_QT_VERSION} COMPONENTS
)
message(STATUS "current Qt version is Qt${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH}")


# 根据qt版本选择是否使用frameless库,目前frameless库支持qt5.14,qt5.15,qt6.4+,除了上诉版本,都使用不了
if(${QT_VERSION_MAJOR} EQUAL 5)
# qt版本为5,判断是否小版本大于14
if(${QT_VERSION_MINOR} GREATER_EQUAL 14)
#5.14和5.15可以使用frameless
set(SARIBBON_USE_FRAMELESS_LIB ON)
#set(SARIBBON_USE_FRAMELESS_LIB ON)
else()
set(SARIBBON_USE_FRAMELESS_LIB OFF)
endif()
elseif(${QT_VERSION_MAJOR} EQUAL 6)
# qt版本为6,判断是否小版本大于6.1
if(${QT_VERSION_MINOR} GREATER_EQUAL 1)
set(SARIBBON_USE_FRAMELESS_LIB ON)
#set(SARIBBON_USE_FRAMELESS_LIB ON)
else()
#6.0及bug多多无法使用frameless
set(SARIBBON_USE_FRAMELESS_LIB OFF)
Expand Down
60 changes: 43 additions & 17 deletions src/SARibbonBar/SARibbonButtonGroupWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#include "SARibbonControlButton.h"
#include "SARibbonElementManager.h"
#include "SARibbonSeparatorWidget.h"
#include "SARibbonPannel.h"

//===================================================
// SAPrivateRibbonButtonGroupWidgetItem
//===================================================

class SAPrivateRibbonButtonGroupWidgetItem
{
public:
Expand Down Expand Up @@ -48,9 +50,12 @@ 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
mIconSize = QSize(24, 24);
}

//===================================================
Expand Down Expand Up @@ -98,8 +103,10 @@ SARibbonButtonGroupWidget::~SARibbonButtonGroupWidget()
}
}

QAction* SARibbonButtonGroupWidget::addAction(QAction* a)
QAction* SARibbonButtonGroupWidget::addAction(QAction* a, Qt::ToolButtonStyle buttonStyle, QToolButton::ToolButtonPopupMode popMode)
{
SARibbonPannel::setActionToolButtonStyleProperty(a, buttonStyle);
SARibbonPannel::setActionToolButtonPopupModeProperty(a, popMode);
QWidget::addAction(a);
return (a);
}
Expand All @@ -112,23 +119,17 @@ QAction* SARibbonButtonGroupWidget::addAction(QAction* a)
* @param popMode
* @return
*/
QAction* SARibbonButtonGroupWidget::addAction(const QString& text, const QIcon& icon, 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);
ButtonTyle* btn = qobject_cast< ButtonTyle* >(d_ptr->mItems.back().widget);
btn->setPopupMode(popMode);
addAction(a, buttonStyle, popMode);
return (a);
}

QAction* SARibbonButtonGroupWidget::addMenu(QMenu* menu, QToolButton::ToolButtonPopupMode popMode)
QAction* SARibbonButtonGroupWidget::addMenu(QMenu* menu, Qt::ToolButtonStyle buttonStyle, QToolButton::ToolButtonPopupMode popMode)
{
QAction* a = menu->menuAction();

addAction(a);
ButtonTyle* btn = qobject_cast< ButtonTyle* >(d_ptr->mItems.back().widget);
btn->setPopupMode(popMode);
QAction* a = menu->menuAction();
addAction(a, buttonStyle, popMode);
return (a);
}

Expand All @@ -151,6 +152,20 @@ QAction* SARibbonButtonGroupWidget::addWidget(QWidget* w)
return (a);
}

SARibbonControlToolButton *SARibbonButtonGroupWidget::actionToRibbonControlToolButton(QAction *action)
{
for (auto obj : qAsConst(children())) {
if (obj->isWidgetType()) {
if (SARibbonControlToolButton* btn = qobject_cast< SARibbonControlToolButton* >(obj)) {
if (btn->defaultAction() == action) {
return btn;
}
}
}
}
return (nullptr);
}

QSize SARibbonButtonGroupWidget::sizeHint() const
{
return (layout()->sizeHint());
Expand All @@ -171,8 +186,9 @@ void SARibbonButtonGroupWidget::setIconSize(const QSize& s)
// 迭代已经保存的button
const QObjectList& objlist = children();
for (QObject* obj : objlist) {
if (ButtonTyle* btn = qobject_cast< ButtonTyle* >(obj)) {
if (SARibbonControlToolButton* btn = qobject_cast< SARibbonControlToolButton* >(obj)) {
btn->setIconSize(s);
btn->setFixedHeight(s.height());
}
}
}
Expand Down Expand Up @@ -203,6 +219,8 @@ void SARibbonButtonGroupWidget::actionEvent(QActionEvent* e)
if (item.widget != nullptr) {
item.widget->setAttribute(Qt::WA_LayoutUsesWidgetRect);
item.widget->show();
//widget高度保持一致
item.widget->setFixedHeight(d_ptr->mIconSize.height());
item.customWidget = true;
}
} else if (item.action->isSeparator()) {
Expand All @@ -212,16 +230,24 @@ void SARibbonButtonGroupWidget::actionEvent(QActionEvent* e)
}
// 不是widget,自动生成ButtonTyle
if (!item.widget) {
ButtonTyle* button = RibbonSubElementDelegate->createRibbonControlButton(this);
//只使用small
SARibbonToolButton::RibbonButtonType buttonType = SARibbonToolButton::SmallButton;
SARibbonControlToolButton* button = RibbonSubElementDelegate->createRibbonControlToolButton(this);
button->setAutoRaise(true);
button->setIconSize(d_ptr->mIconSize);
button->setFixedHeight(d_ptr->mIconSize.height());
button->setFocusPolicy(Qt::NoFocus);
button->setToolButtonStyle(Qt::ToolButtonIconOnly);
button->setButtonType(buttonType);
button->setDefaultAction(item.action);
// 属性设置
QToolButton::ToolButtonPopupMode popMode = SARibbonPannel::getActionToolButtonPopupModeProperty(item.action);
button->setPopupMode(popMode);
Qt::ToolButtonStyle buttonStyle = SARibbonPannel::getActionToolButtonStyleProperty(item.action);
button->setToolButtonStyle(buttonStyle);
// 根据QAction的属性设置按钮的大小

QObject::connect(button, &ButtonTyle::triggered, this, &SARibbonButtonGroupWidget::actionTriggered);
item.widget = button;
QObject::connect(button, &SARibbonToolButton::triggered, this, &SARibbonButtonGroupWidget::actionTriggered);
item.widget = button;
}
layout()->addWidget(item.widget);
d_ptr->mItems.append(item);
Expand Down
18 changes: 13 additions & 5 deletions src/SARibbonBar/SARibbonButtonGroupWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,34 @@
#include <QToolButton>
#include <QMenu>
#include <QFrame>
class SARibbonControlButton;
class SARibbonControlToolButton;
/**
* @brief 用于管理一组Action,类似于QToolBar
*/
class SA_RIBBON_EXPORT SARibbonButtonGroupWidget : public QFrame
{
Q_OBJECT
SA_RIBBON_DECLARE_PRIVATE(SARibbonButtonGroupWidget)
using ButtonTyle = SARibbonControlButton;

public:
SARibbonButtonGroupWidget(QWidget* parent = Q_NULLPTR);
~SARibbonButtonGroupWidget() Q_DECL_OVERRIDE;

//生成并添加一个action
QAction* addAction(QAction* a);
QAction* addAction(const QString& text, const QIcon& icon, QToolButton::ToolButtonPopupMode popMode = QToolButton::InstantPopup);
QAction* addMenu(QMenu* menu, QToolButton::ToolButtonPopupMode popMode = QToolButton::InstantPopup);
QAction* addAction(QAction* a,
Qt::ToolButtonStyle buttonStyle = Qt::ToolButtonIconOnly,
QToolButton::ToolButtonPopupMode popMode = QToolButton::DelayedPopup);
QAction* addAction(const QString& text, const QIcon& icon,
Qt::ToolButtonStyle buttonStyle = Qt::ToolButtonIconOnly,
QToolButton::ToolButtonPopupMode popMode = QToolButton::DelayedPopup);
QAction* addMenu(QMenu* menu,
Qt::ToolButtonStyle buttonStyle = Qt::ToolButtonIconOnly,
QToolButton::ToolButtonPopupMode popMode = QToolButton::InstantPopup);
QAction* addSeparator();
QAction* addWidget(QWidget* w);
// 从ButtonGroupWidget中把action对应的button提取出来,如果action没有对应的button,就返回nullptr
SARibbonControlToolButton* actionToRibbonControlToolButton(QAction* action);

QSize sizeHint() const Q_DECL_OVERRIDE;
QSize minimumSizeHint() const Q_DECL_OVERRIDE;
//设置icon尺寸
Expand Down
6 changes: 6 additions & 0 deletions src/SARibbonBar/SARibbonControlButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ SARibbonControlButton::SARibbonControlButton(QWidget *parent)
: QToolButton(parent)
{
}

SARibbonControlToolButton::SARibbonControlToolButton(QWidget *parent)
: SARibbonToolButton(parent)
{

}
16 changes: 14 additions & 2 deletions src/SARibbonBar/SARibbonControlButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#define SARIBBONCONTROLBUTTON_H
#include <QToolButton>
#include "SARibbonGlobal.h"

#include "SARibbonToolButton.h"

/**
* @brief 用于SARibbonPannel的control button
* @brief 用于SARibbonGallery的control button
*
* 为了防止外部qss的影响,单独继承一个类
*/
Expand All @@ -16,4 +16,16 @@ class SA_RIBBON_EXPORT SARibbonControlButton : public QToolButton
SARibbonControlButton(QWidget *parent = 0);
};

/**
* @brief 用于SARibbonButtonGroupWidget的control button
*
* 为了防止SARibbonToolButton qss的影响,单独继承一个类
*/
class SA_RIBBON_EXPORT SARibbonControlToolButton : public SARibbonToolButton
{
Q_OBJECT
public:
SARibbonControlToolButton(QWidget *parent = 0);
};

#endif // SARIBBONPANNELTOOLBUTTON_H
5 changes: 5 additions & 0 deletions src/SARibbonBar/SARibbonElementFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ SARibbonControlButton* SARibbonElementFactory::createRibbonControlButton(QWidget
return (new SARibbonControlButton(parent));
}

SARibbonControlToolButton *SARibbonElementFactory::createRibbonControlToolButton(QWidget *parent)
{
return (new SARibbonControlToolButton(parent));
}

SARibbonStackedWidget* SARibbonElementFactory::createRibbonStackedWidget(SARibbonBar* parent)
{
return (new SARibbonStackedWidget(parent));
Expand Down
2 changes: 2 additions & 0 deletions src/SARibbonBar/SARibbonElementFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SARibbonGallery;
class SARibbonGalleryGroup;
class SARibbonToolButton;
class SARibbonControlButton;
class SARibbonControlToolButton;
class SARibbonButtonGroupWidget;
class SARibbonStackedWidget;
class SARibbonQuickAccessBar;
Expand Down Expand Up @@ -46,6 +47,7 @@ class SA_RIBBON_EXPORT SARibbonElementFactory
virtual SARibbonGalleryGroup* createRibbonGalleryGroup(QWidget* parent);
virtual SARibbonToolButton* createRibbonToolButton(QWidget* parent);
virtual SARibbonControlButton* createRibbonControlButton(QWidget* parent);
virtual SARibbonControlToolButton* createRibbonControlToolButton(QWidget* parent);
virtual SARibbonStackedWidget* createRibbonStackedWidget(SARibbonBar* parent);
virtual SARibbonButtonGroupWidget* craeteButtonGroupWidget(QWidget* parent);
virtual SARibbonQuickAccessBar* createQuickAccessBar(QWidget* parent);
Expand Down
27 changes: 27 additions & 0 deletions src/SARibbonBar/SARibbonPannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,33 @@ QToolButton::ToolButtonPopupMode SARibbonPannel::getActionToolButtonPopupModePro
return (QToolButton::InstantPopup);
}

/**
* @brief 设置action的ToolButtonStyle属性
* @param action
* @param buttonStyle
*/
void SARibbonPannel::setActionToolButtonStyleProperty(QAction *action, Qt::ToolButtonStyle buttonStyle)
{
Q_CHECK_PTR(action);
action->setProperty(SA_ActionPropertyName_ToolButtonStyle, static_cast< int >(buttonStyle));
}

/**
* @brief 获取action的ToolButtonStyle属性
* @param action
* @return
*/
Qt::ToolButtonStyle SARibbonPannel::getActionToolButtonStyleProperty(QAction *action)
{
bool isok = false;
int r = action->property(SA_ActionPropertyName_ToolButtonStyle).toInt(&isok);

if (isok) {
return (static_cast< Qt::ToolButtonStyle >(r));
}
return (Qt::ToolButtonIconOnly);
}

/**
* @brief 添加action
*
Expand Down
8 changes: 6 additions & 2 deletions src/SARibbonBar/SARibbonPannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,14 @@ class SA_RIBBON_EXPORT SARibbonPannel : public QWidget
static void setActionRowProportionProperty(QAction* action, SARibbonPannelItem::RowProportion rp);
// 获取action的行属性
static SARibbonPannelItem::RowProportion getActionRowProportionProperty(QAction* action);
// 把action的行属性设置进action中,action自身携带了行属性
// 把action的PopupMode属性设置进action中,action自身携带了PopupMode属性
static void setActionToolButtonPopupModeProperty(QAction* action, QToolButton::ToolButtonPopupMode popMode);
// 获取action的行属性
// 获取action的PopupMode属性
static QToolButton::ToolButtonPopupMode getActionToolButtonPopupModeProperty(QAction* action);
// 把action的ToolButtonStyle属性设置进action中,action自身携带了ToolButtonStyle属性
static void setActionToolButtonStyleProperty(QAction* action, Qt::ToolButtonStyle buttonStyle);
// 获取action的ToolButtonStyle属性
static Qt::ToolButtonStyle getActionToolButtonStyleProperty(QAction* action);

protected:
// 设置PannelLayoutMode,此函数设置为protect避免误调用
Expand Down
3 changes: 3 additions & 0 deletions src/SARibbonBar/SARibbonPannelItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class SA_RIBBON_EXPORT SARibbonPannelItem : public QWidgetItem
#ifndef SA_ActionPropertyName_ToolButtonPopupMode
#define SA_ActionPropertyName_ToolButtonPopupMode "_sa_ToolButtonPopupMode"
#endif
#ifndef SA_ActionPropertyName_ToolButtonStyle
#define SA_ActionPropertyName_ToolButtonStyle "_sa_ToolButtonStyle"
#endif
#ifndef SA_ActionPropertyName_SeparatorTop
#define SA_ActionPropertyName_SeparatorTop "_sa_SeparatorTop"
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/SARibbonBar/SARibbonQuickAccessBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ void SARibbonQuickAccessBar::addSeparator()
d_ptr->groupWidget->addSeparator();
}

void SARibbonQuickAccessBar::addAction(QAction* act)
void SARibbonQuickAccessBar::addAction(QAction* act, Qt::ToolButtonStyle buttonStyle, QToolButton::ToolButtonPopupMode popMode)
{
d_ptr->groupWidget->addAction(act);
d_ptr->groupWidget->addAction(act, buttonStyle, popMode);
}

void SARibbonQuickAccessBar::addWidget(QWidget* w)
{
d_ptr->groupWidget->addWidget(w);
}

void SARibbonQuickAccessBar::addMenu(QMenu* m, QToolButton::ToolButtonPopupMode popMode)
void SARibbonQuickAccessBar::addMenu(QMenu* m, Qt::ToolButtonStyle buttonStyle, QToolButton::ToolButtonPopupMode popMode)
{
d_ptr->groupWidget->addMenu(m, popMode);
d_ptr->groupWidget->addMenu(m, buttonStyle, popMode);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/SARibbonBar/SARibbonQuickAccessBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class SA_RIBBON_EXPORT SARibbonQuickAccessBar : public SARibbonCtrlContainer
SARibbonQuickAccessBar(QWidget* parent = 0);
~SARibbonQuickAccessBar();
void addSeparator();
void addAction(QAction* act);
void addAction(QAction* act, Qt::ToolButtonStyle buttonStyle = Qt::ToolButtonIconOnly, QToolButton::ToolButtonPopupMode popMode = QToolButton::DelayedPopup);
void addWidget(QWidget* w);
void addMenu(QMenu* m, QToolButton::ToolButtonPopupMode popMode = QToolButton::InstantPopup);
void addMenu(QMenu* m, Qt::ToolButtonStyle buttonStyle = Qt::ToolButtonIconOnly, QToolButton::ToolButtonPopupMode popMode = QToolButton::InstantPopup);
//获取内部管理的ButtonGroupWidget
SARibbonButtonGroupWidget* buttonGroupWidget();
const SARibbonButtonGroupWidget* buttonGroupWidget() const;
Expand Down
Loading

0 comments on commit d8ae756

Please sign in to comment.