ToolBarControlDemo Example

#include <QFontComboBox>
#include <QScreen>

#include "aboutdialog.h"
#include "mainwindow.h"

/* MainWindow */
MainWindow::MainWindow(QWidget* parent)
    : DemoRibbonWindow(parent)
{
    setWindowTitle(QObject::tr("QtitanRibbon ToolBarControl"));

    ribbonBar()->setFrameThemeEnabled();
    RibbonPage* page = ribbonBar()->addPage(tr("&Home"));
    createLargeButtons(page);
    createSmallButtons(page);
    createLargePlusSmallButtons(page);

#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    const int factor = 5;
    const QRect availableGeometry = screen()->availableGeometry();
    resize(2 * availableGeometry.width() / factor, 2 * availableGeometry.height() / (factor * 3));
    move((availableGeometry.width() - width()) / 2, (availableGeometry.height() - height()) / 2);
#else
    resize(1000, 300); move(200, 200);
#endif
}

MainWindow::~MainWindow()
{
}

void MainWindow::createLargeButtons(RibbonPage* page)
{
    RibbonGroup* group = page->addGroup(tr("Large Buttons"));
    RibbonToolBarControl* toolBar = new RibbonToolBarControl(group);
    toolBar->addAction(QIcon(QStringLiteral(":/res/largeNewFile.png")),   tr("New"), Qt::ToolButtonTextUnderIcon);
    toolBar->addAction(QIcon(QStringLiteral(":/res/largeOpenFile.png")),  tr("Open"), Qt::ToolButtonTextUnderIcon);
    toolBar->addAction(QIcon(QStringLiteral(":/res/largePrint.png")),     tr("Print"), Qt::ToolButtonTextUnderIcon);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(QStringLiteral(":/res/largezoom.png")),      tr("Zoom"), Qt::ToolButtonTextUnderIcon);
    toolBar->addAction(QIcon(QStringLiteral(":/res/largeWatermark.png")), tr("Watermark"), Qt::ToolButtonTextUnderIcon);
    group->addControl(toolBar);
}

void MainWindow::createSmallButtons(RibbonPage* page)
{
    RibbonGroup* group = page->addGroup(tr("Small Buttons"));
    RibbonToolBarControl* toolBar = new RibbonToolBarControl(group);

    QFontComboBox* fontComboBox = new QFontComboBox();
    toolBar->addWidget(fontComboBox);

    QComboBox* sizeComboBox = new QComboBox();
    sizeComboBox->setEditable(true);
    QFontDatabase db;
    foreach(int size, db.standardSizes())
        sizeComboBox->addItem(QString::number(size));
    toolBar->addWidget(sizeComboBox);
    toolBar->addSeparator();

    toolBar->addAction(QIcon(QStringLiteral(":/res/smallnew.png")), tr("New"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallOpen.png")), tr("Open"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallPrint.png")), tr("Print"));
    toolBar->addSeparator();
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallRedo.png")), tr("Redo"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallUndo.png")), tr("Undo"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallreplace.png")), tr("Replace"));
    toolBar->addSeparator();
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallfontshrink.png")), tr("Shrink"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallfontgrow.png")), tr("Grow"));

    group->addControl(toolBar);
}

void MainWindow::createLargePlusSmallButtons(RibbonPage* page)
{
    RibbonGroup* group = page->addGroup(tr("Large+Small Buttons"));
    RibbonToolBarControl* toolBar = new RibbonToolBarControl(group);
#if 0
    toolBar->addAction(QIcon(QStringLiteral(":/res/largeNewFile.png")), tr("New"), Qt::ToolButtonTextUnderIcon);
    toolBar->addAction(QIcon(QStringLiteral(":/res/largeOpenFile.png")), tr("Open"), Qt::ToolButtonTextUnderIcon);
    toolBar->addAction(QIcon(QStringLiteral(":/res/largePrint.png")), tr("Print"), Qt::ToolButtonTextUnderIcon);
    toolBar->addSeparator();
#endif
    toolBar->addAction(QIcon(QStringLiteral(":/res/largezoom.png")), tr("Zoom"), Qt::ToolButtonTextUnderIcon);
    toolBar->addAction(QIcon(QStringLiteral(":/res/largeWatermark.png")), tr("Watermark"), Qt::ToolButtonTextUnderIcon);
    toolBar->addSeparator();

    toolBar->addAction(QIcon(QStringLiteral(":/res/smallnew.png")), tr("New"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallOpen.png")), tr("Open"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallPrint.png")), tr("Print"));
    toolBar->addSeparator();
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallRedo.png")), tr("Redo"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallUndo.png")), tr("Undo"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallreplace.png")), tr("Replace"));
    toolBar->addSeparator();
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallfontshrink.png")), tr("Shrink"));
    toolBar->addAction(QIcon(QStringLiteral(":/res/smallfontgrow.png")), tr("Grow"));
    toolBar->addSeparator();

    group->addControl(toolBar);

    group->addSeparator();
    QAction* action = group->addAction(QIcon(QStringLiteral(":/res/largeNewFile.png")), tr("New"), Qt::ToolButtonTextUnderIcon);
    RibbonControl* control = group->controlByAction(action);
    control->sizeDefinition(RibbonControlSizeDefinition::GroupSimplified)->setLabelVisible(false);

    action = group->addAction(QIcon(QStringLiteral(":/res/largeOpenFile.png")), tr("Open"), Qt::ToolButtonTextUnderIcon);
    control = group->controlByAction(action);
    control->sizeDefinition(RibbonControlSizeDefinition::GroupSimplified)->setLabelVisible(false);

    action = group->addAction(QIcon(QStringLiteral(":/res/largePrint.png")), tr("Print"), Qt::ToolButtonTextUnderIcon);
    control = group->controlByAction(action);
    control->sizeDefinition(RibbonControlSizeDefinition::GroupSimplified)->setLabelVisible(false);
}