项目:https://gitee.com/czyt1988/SARibbon
#include <QApplication>
#include <QDebug>
#include <QtWidgets>
#include <QStyle>
#include <QRandomGenerator>
#include "SARibbonBar.h"
#include "SARibbonWidget.h"
#include "SARibbonApplicationButton.h"
#include "SARibbonContextCategory.h"
#include "SARibbonQuickAccessBar.h"
#include "SARibbonButtonGroupWidget.h"
#include "SARibbonMainWindow.h"
QIcon getRandomStyleIcon()
{
QIcon icon;
do
{
icon = QApplication::style()->standardIcon(static_cast<QStyle::StandardPixmap>(QRandomGenerator::global()->bounded(QStyle::SP_TitleBarMenuButton, QStyle::SP_RestoreDefaultsButton)));
}while (icon.isNull());
return icon;
}
SARibbonBar * getABar()
{
auto ribbonBar = new SARibbonBar;
ribbonBar->setRibbonStyle(SARibbonBar::RibbonStyleLooseThreeRow);
ribbonBar->setEnableWordWrap(true);
auto appButton = new SARibbonApplicationButton(getRandomStyleIcon(),"我是应用按钮");
ribbonBar->setApplicationButton(appButton);
auto category01 = new SARibbonCategory("标签页01");
auto pannel01 = new SARibbonPannel("复制粘贴面板");
category01->addPannel(pannel01);
pannel01->addAction(new QAction(getRandomStyleIcon(),"复制"));
pannel01->addAction(new QAction(getRandomStyleIcon(),"粘贴"));
auto pannel02 = new SARibbonPannel("撤销回撤面板");
category01->addPannel(pannel02);
pannel02->addAction(new QAction(getRandomStyleIcon(),"撤销"));
pannel02->addAction(new QAction(getRandomStyleIcon(),"回撤"));
auto category02 = new SARibbonCategory("标签页02");
auto pannel03 = new SARibbonPannel("撤销回撤面板");
category02->addPannel(pannel03);
pannel03->addAction(new QAction(getRandomStyleIcon(),"撤销"));
pannel03->addAction(new QAction(getRandomStyleIcon(),"回撤"));
ribbonBar->addCategoryPage(category01);
ribbonBar->addCategoryPage(category02);
{
//SARibbonContextCategory的内容是默认不显示的
auto contextCategory = new SARibbonContextCategory;
contextCategory->setContextTitle("上下文标签");
contextCategory->setContextColor(Qt::red);
ribbonBar->addContextCategory(contextCategory);//必须先添加到Bar再往上面添加内容才会正确显示
auto category03 = new SARibbonCategory("标签页03");
auto pannel04 = new SARibbonPannel("上下文标签上的面板01");
category03->addPannel(pannel04);
pannel04->addAction(new QAction(getRandomStyleIcon(),"测试Action01"));
pannel04->addAction(new QAction(getRandomStyleIcon(),"测试Action02"));
contextCategory->addCategoryPage(category03);
auto category04 = new SARibbonCategory("标签页04");
auto pannel05 = new SARibbonPannel("上下文标签上的面板02");
category04->addPannel(pannel05);
pannel05->addAction(new QAction(getRandomStyleIcon(),"测试Action03"));
pannel05->addAction(new QAction(getRandomStyleIcon(),"测试Action04"));
contextCategory->addCategoryPage(category04);
ribbonBar->showContextCategory(contextCategory);//让它显示出来
}
SARibbonQuickAccessBar* quickAccessBar = ribbonBar->quickAccessBar();
quickAccessBar->addAction(new QAction(getRandomStyleIcon(),"测试Action03"));
quickAccessBar->addSeparator();
quickAccessBar->addAction(new QAction(getRandomStyleIcon(),"测试Action03"));
quickAccessBar->addAction(new QAction(getRandomStyleIcon(),"测试Action03"));
quickAccessBar->addSeparator();
SARibbonButtonGroupWidget* rightBar = ribbonBar->rightButtonGroup();
rightBar->addAction(new QAction(getRandomStyleIcon(),"测试Action03"));
rightBar->addSeparator();
rightBar->addAction(new QAction(getRandomStyleIcon(),"测试Action03"));
rightBar->addAction(new QAction(getRandomStyleIcon(),"测试Action03"));
rightBar->addSeparator();
return ribbonBar;
}
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
SARibbonMainWindow w2;
w2.setWindowTitle("我是窗口标题");
w2.setRibbonBar(getABar());
w2.resize(800,600);
w2.show();
auto w = new QWidget;
auto vb = new QVBoxLayout(w);
auto font = w->font();
font.setPixelSize(18);
w->setFont(font);
auto hb = new QHBoxLayout;
hb->addWidget(new QLabel("风格样式:"));
vb->addLayout(hb);
QButtonGroup buttonGroup;
const QMetaObject &mo = SARibbonBar::staticMetaObject;
QMetaEnum metaEnum = mo.enumerator(mo.indexOfEnumerator("RibbonStyleFlag"));
for (int i = 0; i < metaEnum.keyCount(); ++i)
{
auto btn = new QRadioButton(metaEnum.key(i));
hb->addWidget(btn);
buttonGroup.addButton(btn,metaEnum.value(i));
}
buttonGroup.button(SARibbonBar::RibbonStyleLoose)->setChecked(true);
buttonGroup.connect(&buttonGroup,&QButtonGroup::idClicked,[&](int id)
{
w2.ribbonBar()->setRibbonStyle(static_cast<SARibbonBar::RibbonStyles>(id));
});
hb = new QHBoxLayout;
hb->addWidget(new QLabel("主题:"));
vb->addLayout(hb);
QButtonGroup buttonGroup2;
QList<std::pair<QString,int>> list;
list << std::make_pair("RibbonThemeOffice2013",static_cast<int>(SARibbonTheme::RibbonThemeOffice2013));
list << std::make_pair("office2016-蓝色主题",static_cast<int>(SARibbonTheme::RibbonThemeOffice2016Blue));
list << std::make_pair("office2021-蓝色主题",static_cast<int>(SARibbonTheme::RibbonThemeOffice2021Blue));
list << std::make_pair("win7主题",static_cast<int>(SARibbonTheme::RibbonThemeWindows7));
list << std::make_pair("暗色主题",static_cast<int>(SARibbonTheme::RibbonThemeDark));
list << std::make_pair("暗色主题2",static_cast<int>(SARibbonTheme::RibbonThemeDark2));
for(int i = 0;i < list.size();++i)
{
auto btn = new QRadioButton(list.at(i).first);
hb->addWidget(btn);
buttonGroup2.addButton(btn,list.at(i).second);
}
buttonGroup2.button(static_cast<int>(SARibbonTheme::RibbonThemeOffice2013))->setChecked(true);
buttonGroup2.connect(&buttonGroup2,&QButtonGroup::idClicked,[&](int id)
{
w2.setRibbonTheme(static_cast<SARibbonTheme>(id));
});
w2.setCentralWidget(w);
return a.exec();
}
文章评论