From 6f74dd38d7efe1a628be63c88d56135df6730b5a Mon Sep 17 00:00:00 2001 From: czyt1988 Date: Tue, 26 Dec 2023 10:27:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=8F=AF=E8=A7=81=E6=80=A7?= =?UTF-8?q?=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SARibbonBar/SARibbonApplicationButton.cpp | 4 --- src/SARibbonBar/SARibbonBar.cpp | 28 +++++++++---------- src/SARibbonBar/SARibbonMainWindow.cpp | 9 ++++++ src/SARibbonBar/SARibbonMainWindow.h | 3 ++ src/example/MainWindowExample/mainwindow.cpp | 26 ++++++++--------- src/example/StaticExample/MainWindow.cpp | 19 ++++++++++++- src/example/WidgetWithRibbon/CMakeLists.txt | 1 + src/example/WidgetWithRibbon/Widget.ui | 15 ++++++++++ 8 files changed, 73 insertions(+), 32 deletions(-) diff --git a/src/SARibbonBar/SARibbonApplicationButton.cpp b/src/SARibbonBar/SARibbonApplicationButton.cpp index 4a6f0045..44efb8e6 100644 --- a/src/SARibbonBar/SARibbonApplicationButton.cpp +++ b/src/SARibbonBar/SARibbonApplicationButton.cpp @@ -1,13 +1,11 @@ #include "SARibbonApplicationButton.h" -#define SARIBBON_APPLICATION_BUTTON_MINIMUM_WIDTH 40 SARibbonApplicationButton::SARibbonApplicationButton(QWidget* parent) : QToolButton(parent) { setFocusPolicy(Qt::NoFocus); setAutoRaise(true); setPopupMode(QToolButton::InstantPopup); setToolButtonStyle(Qt::ToolButtonTextBesideIcon); - setMinimumWidth(SARIBBON_APPLICATION_BUTTON_MINIMUM_WIDTH); } SARibbonApplicationButton::SARibbonApplicationButton(const QString& text, QWidget* parent) : QToolButton(parent) @@ -17,7 +15,6 @@ SARibbonApplicationButton::SARibbonApplicationButton(const QString& text, QWidge setPopupMode(QToolButton::InstantPopup); setToolButtonStyle(Qt::ToolButtonTextBesideIcon); setText(text); - setMinimumWidth(SARIBBON_APPLICATION_BUTTON_MINIMUM_WIDTH); } SARibbonApplicationButton::SARibbonApplicationButton(const QIcon& icon, const QString& text, QWidget* parent) @@ -29,5 +26,4 @@ SARibbonApplicationButton::SARibbonApplicationButton(const QIcon& icon, const QS setToolButtonStyle(Qt::ToolButtonTextBesideIcon); setIcon(icon); setText(text); - setMinimumWidth(SARIBBON_APPLICATION_BUTTON_MINIMUM_WIDTH); } diff --git a/src/SARibbonBar/SARibbonBar.cpp b/src/SARibbonBar/SARibbonBar.cpp index fb4d19c7..ac9ead62 100644 --- a/src/SARibbonBar/SARibbonBar.cpp +++ b/src/SARibbonBar/SARibbonBar.cpp @@ -410,7 +410,7 @@ QList< QColor > SARibbonBar::getDefaultContextCategoryColorList() << QColor(14, 81, 167) // 蓝 << QColor(228, 0, 69) // 红 << QColor(67, 148, 0) // 绿 - ; + ; return res; } @@ -967,11 +967,11 @@ void SARibbonBar::showMinimumModeButton(bool isShow) d_ptr->mMinimumCategoryButtonAction = new QAction(this); d_ptr->mMinimumCategoryButtonAction->setIcon( - style()->standardIcon(isMinimumMode() ? QStyle::SP_TitleBarUnshadeButton : QStyle::SP_TitleBarShadeButton, nullptr)); + style()->standardIcon(isMinimumMode() ? QStyle::SP_TitleBarUnshadeButton : QStyle::SP_TitleBarShadeButton, nullptr)); connect(d_ptr->mMinimumCategoryButtonAction, &QAction::triggered, this, [ this ]() { this->setMinimumMode(!isMinimumMode()); this->d_ptr->mMinimumCategoryButtonAction->setIcon( - style()->standardIcon(isMinimumMode() ? QStyle::SP_TitleBarUnshadeButton : QStyle::SP_TitleBarShadeButton, nullptr)); + style()->standardIcon(isMinimumMode() ? QStyle::SP_TitleBarUnshadeButton : QStyle::SP_TitleBarShadeButton, nullptr)); }); d_ptr->mRightButtonGroup->addAction(d_ptr->mMinimumCategoryButtonAction); @@ -1821,7 +1821,7 @@ void SARibbonBar::paintInNormalStyle() titleRegion.setRect(d_ptr->mQuickAccessBar->geometry().right() + 1, border.top(), width() - d_ptr->mIconRightBorderPosition - border.right() - - d_ptr->mWindowButtonSize.width() - d_ptr->mQuickAccessBar->geometry().right() - 1, + - d_ptr->mWindowButtonSize.width() - d_ptr->mQuickAccessBar->geometry().right() - 1, titleBarHeight()); } else { int leftwidth = contextCategoryRegion.x() - d_ptr->mQuickAccessBar->geometry().right() - d_ptr->mIconRightBorderPosition; @@ -2054,7 +2054,7 @@ void SARibbonBar::resizeInOfficeStyle() x += d_ptr->mIconRightBorderPosition + 5; if (QWidget* connerL = cornerWidget(Qt::TopLeftCorner)) { - if (connerL->isVisible()) { + if (connerL->isVisibleTo(this)) { QSize connerSize = connerL->sizeHint(); if (connerSize.height() < validTitleBarHeight) { int detal = (validTitleBarHeight - connerSize.height()) / 2; @@ -2067,7 +2067,7 @@ void SARibbonBar::resizeInOfficeStyle() } // quick access bar定位 if (d_ptr->mQuickAccessBar) { - if (d_ptr->mQuickAccessBar->isVisible()) { + if (d_ptr->mQuickAccessBar->isVisibleTo(this)) { if (d_ptr->mQuickAccessBar->height() != validTitleBarHeight) { d_ptr->mQuickAccessBar->setFixedHeight(validTitleBarHeight); } @@ -2081,8 +2081,8 @@ void SARibbonBar::resizeInOfficeStyle() y += validTitleBarHeight; // applicationButton 定位 if (d_ptr->mApplicationButton) { - if (d_ptr->mApplicationButton->isVisible()) { - d_ptr->mApplicationButton->setGeometry(x, y, d_ptr->mApplicationButton->size().width(), tabH); + if (d_ptr->mApplicationButton->isVisibleTo(this)) { + d_ptr->mApplicationButton->setGeometry(x, y, d_ptr->mApplicationButton->sizeHint().width(), tabH); x = d_ptr->mApplicationButton->geometry().right(); } } @@ -2093,7 +2093,7 @@ void SARibbonBar::resizeInOfficeStyle() int endX = width() - border.right(); if (QWidget* connerW = cornerWidget(Qt::TopRightCorner)) { - if (connerW->isVisible()) { + if (connerW->isVisibleTo(this)) { QSize connerSize = connerW->sizeHint(); endX -= connerSize.width(); if (connerSize.height() < tabH) { @@ -2149,7 +2149,7 @@ void SARibbonBar::resizeInWpsLiteStyle() int endX = width() - border.right() - d_ptr->mWindowButtonSize.width(); if (QWidget* connerW = cornerWidget(Qt::TopRightCorner)) { - if (connerW->isVisible()) { + if (connerW->isVisibleTo(this)) { QSize connerSize = connerW->sizeHint(); endX -= connerSize.width(); if (connerSize.height() < validTitleBarHeight) { @@ -2171,7 +2171,7 @@ void SARibbonBar::resizeInWpsLiteStyle() } // quick access bar定位 if (d_ptr->mQuickAccessBar) { - if (d_ptr->mQuickAccessBar->isVisible()) { + if (d_ptr->mQuickAccessBar->isVisibleTo(this)) { QSize quickAccessBarSize = d_ptr->mQuickAccessBar->sizeHint(); endX -= quickAccessBarSize.width(); // 上下留1px的边线 @@ -2180,7 +2180,7 @@ void SARibbonBar::resizeInWpsLiteStyle() } // cornerWidget - TopLeftCorner if (QWidget* connerL = cornerWidget(Qt::TopLeftCorner)) { - if (connerL->isVisible()) { + if (connerL->isVisibleTo(this)) { QSize connerSize = connerL->sizeHint(); endX -= connerSize.width(); if (connerSize.height() < validTitleBarHeight) { @@ -2203,8 +2203,8 @@ void SARibbonBar::resizeInWpsLiteStyle() // applicationButton 定位,与TabBar同高 if (d_ptr->mApplicationButton) { - if (d_ptr->mApplicationButton->isVisible()) { - d_ptr->mApplicationButton->setGeometry(x, y, d_ptr->mApplicationButton->size().width(), tabH); + if (d_ptr->mApplicationButton->isVisibleTo(this)) { + d_ptr->mApplicationButton->setGeometry(x, y, d_ptr->mApplicationButton->sizeHint().width(), tabH); x = d_ptr->mApplicationButton->geometry().right() + 2; } } diff --git a/src/SARibbonBar/SARibbonMainWindow.cpp b/src/SARibbonBar/SARibbonMainWindow.cpp index 36f0be81..cb8c88cb 100644 --- a/src/SARibbonBar/SARibbonMainWindow.cpp +++ b/src/SARibbonBar/SARibbonMainWindow.cpp @@ -140,6 +140,15 @@ Qt::WindowFlags SARibbonMainWindow::windowButtonFlags() const return (windowFlags()); } +/** + * @brief SARibbonMainWindow::setRibbonTheme + * + * 注意主题在构造函数设置主题会不完全生效,使用QTimer投放到队列最后执行即可 + * @code + * QTimer::singleShot(0, this, [ this ]() { this->setRibbonTheme(SARibbonMainWindow::RibbonThemeDark); }); + * @endcode + * @param theme + */ void SARibbonMainWindow::setRibbonTheme(SARibbonMainWindow::RibbonTheme theme) { sa_set_ribbon_theme(this, theme); diff --git a/src/SARibbonBar/SARibbonMainWindow.h b/src/SARibbonBar/SARibbonMainWindow.h index aa62554a..6a01acf9 100644 --- a/src/SARibbonBar/SARibbonMainWindow.h +++ b/src/SARibbonBar/SARibbonMainWindow.h @@ -82,6 +82,9 @@ class SA_RIBBON_EXPORT SARibbonMainWindow : public QMainWindow void updateWindowFlag(Qt::WindowFlags flags); // 获取系统按钮的状态 Qt::WindowFlags windowButtonFlags() const; + + // 注意主题在构造函数设置主题会不完全生效,使用QTimer投放到队列最后执行即可 + // QTimer::singleShot(0, this, [ this ]() { this->setRibbonTheme(SARibbonMainWindow::RibbonThemeDark); }); void setRibbonTheme(RibbonTheme theme); RibbonTheme ribbonTheme() const; // 判断当前是否使用ribbon模式 diff --git a/src/example/MainWindowExample/mainwindow.cpp b/src/example/MainWindowExample/mainwindow.cpp index e5d2a4a5..acf7806c 100644 --- a/src/example/MainWindowExample/mainwindow.cpp +++ b/src/example/MainWindowExample/mainwindow.cpp @@ -89,7 +89,7 @@ MainWindow::MainWindow(QWidget* par) //! 添加主标签页,这里演示通过SARibbonBar::addCategoryPage函数添加一个标签页 //! en: //! Add the main tab. Here we show how to add a tab through the SARibbonBar::addCategoryPage function - SARibbonCategory* categoryMain = ribbon->addCategoryPage(tr("Main")); + SARibbonCategory* categoryMain = ribbon->addCategoryPage(tr("&Main")); //! cn: SARibbonBar的Category和Pannel,以及对应的Action都应该设置ObjectName,因为如果要自定义action,这些ObjectName是必不可少的 //! en: The category , pannel and actions of SARibbonBar, should be set with Object Names, as these Object Names are essential for customizing actions @@ -176,7 +176,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) { @@ -214,14 +214,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: @@ -295,7 +295,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) @@ -304,7 +304,7 @@ void MainWindow::onActionRemoveAppBtnTriggered(bool b) ribbonBar()->setApplicationButton(nullptr); } else { SARibbonApplicationButton* actionRemoveAppBtn = new SARibbonApplicationButton(); - actionRemoveAppBtn->setText(tr(" File ")); + actionRemoveAppBtn->setText(tr(" &File ")); this->ribbonBar()->setApplicationButton(actionRemoveAppBtn); createRibbonApplicationButton(); } @@ -427,7 +427,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); } @@ -511,11 +511,11 @@ void MainWindow::createCategoryMain(SARibbonCategory* page) SARibbonPannel* pannelStyle = page->addPannel(("ribbon style")); QAction* actSave = createAction(tr("Save"), ":/icon/icon/save.svg"); - //这样设置快捷键 + // 这样设置快捷键 QShortcut* shortCut = new QShortcut(QKeySequence(QLatin1String("Ctrl+S")), this); connect(shortCut, &QShortcut::activated, this, [ actSave ]() { actSave->trigger(); }); - //这样设置是无效的 - // actSave->setShortcut(QKeySequence(QLatin1String("Ctrl+S"))); + // 这样设置是无效的 + // actSave->setShortcut(QKeySequence(QLatin1String("Ctrl+S"))); connect(actSave, &QAction::triggered, this, [ this ](bool b) { Q_UNUSED(b); @@ -1096,7 +1096,7 @@ void MainWindow::createContextCategoryPage1(SARibbonCategory* page) pannel->addLargeAction(mActionShowTest); mPannelVisbileExample = page->addPannel(tr("show/hide")); - //重复添加 + // 重复添加 mPannelVisbileExample->addLargeAction(mActionSetTextTest); connect(mActionShowTest, &QAction::toggled, this, [ this ](bool b) { diff --git a/src/example/StaticExample/MainWindow.cpp b/src/example/StaticExample/MainWindow.cpp index 8fd27971..9c002d80 100644 --- a/src/example/StaticExample/MainWindow.cpp +++ b/src/example/StaticExample/MainWindow.cpp @@ -1,5 +1,7 @@ #include "MainWindow.h" #include "SARibbon.h" +#include +#include MainWindow::MainWindow(QWidget* parent) : SARibbonMainWindow(parent) { setWindowIcon(QIcon(":/app/icon/SA.svg")); @@ -14,8 +16,23 @@ MainWindow::MainWindow(QWidget* parent) : SARibbonMainWindow(parent) pannel->addAction(tr("action5"), QIcon(":/app/icon/folder-star.svg"), QToolButton::InstantPopup, SARibbonPannelItem::Small); pannel->addAction(tr("action6"), QIcon(":/app/icon/test1.svg"), QToolButton::InstantPopup, SARibbonPannelItem::Small); SARibbonCategory* otherCate = bar->addCategoryPage(tr("Other")); - Q_UNUSED(otherCate); + SARibbonPannel* pannel2 = otherCate->addPannel(tr("other")); + pannel2->addAction(tr("action1"), QIcon(":/app/icon/action.svg"), QToolButton::InstantPopup); + QComboBox* combo = new QComboBox(this); + pannel2->addMediumWidget(combo); + combo->addItem("RibbonThemeWindows7", static_cast< int >(SARibbonMainWindow::RibbonThemeWindows7)); + combo->addItem("RibbonThemeOffice2013", static_cast< int >(SARibbonMainWindow::RibbonThemeOffice2013)); + combo->addItem("RibbonThemeOffice2016Blue", static_cast< int >(SARibbonMainWindow::RibbonThemeOffice2016Blue)); + combo->addItem("RibbonThemeOffice2021Blue", static_cast< int >(SARibbonMainWindow::RibbonThemeOffice2021Blue)); + combo->addItem("RibbonThemeDark", static_cast< int >(SARibbonMainWindow::RibbonThemeDark)); + connect(combo, QOverload< int >::of(&QComboBox::currentIndexChanged), [ this, combo ](int index) { + SARibbonMainWindow::RibbonTheme th = static_cast< SARibbonMainWindow::RibbonTheme >(combo->itemData(index).toInt()); + this->setRibbonTheme(th); + }); resize(800, 600); + // 在构造函数设置主题会不完全生效 + QTimer::singleShot(0, this, [ this ]() { this->setRibbonTheme(SARibbonMainWindow::RibbonThemeDark); }); + // more example see MainWindowExample } diff --git a/src/example/WidgetWithRibbon/CMakeLists.txt b/src/example/WidgetWithRibbon/CMakeLists.txt index 56369766..bdfe7f06 100644 --- a/src/example/WidgetWithRibbon/CMakeLists.txt +++ b/src/example/WidgetWithRibbon/CMakeLists.txt @@ -57,6 +57,7 @@ add_executable(${SARIBBON_EXPAMPLE_NAME} Widget.cpp main.cpp icon.qrc + Widget.ui ) target_include_directories(${SARIBBON_EXPAMPLE_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../../SARibbonBar") diff --git a/src/example/WidgetWithRibbon/Widget.ui b/src/example/WidgetWithRibbon/Widget.ui index 1544fa8e..cd8595ff 100644 --- a/src/example/WidgetWithRibbon/Widget.ui +++ b/src/example/WidgetWithRibbon/Widget.ui @@ -31,9 +31,24 @@ + + QTabWidget::Triangular + 0 + + + 22 + 22 + + + + true + + + true + Tab 1