BackstageView Example

#include <QPrinter>
#include <QPrintDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QSettings>
#include <QDockWidget>
#include <QListWidget>
#include <QGroupBox>

#include "mainwindow.h"
#include "backstagepages.h"

/* MainWindow */
MainWindow::MainWindow(QWidget* parent)
    : DemoRibbonWindow(parent)
{
//    setStyleByName(QStringLiteral("AdobePhotoshopLightGray"));

    setStyleByName(QStringLiteral("Office2016Colorful"));

    setWindowTitle(tr("QtitanRibbon BackstageView"));
    setCentralWidget(new RibbonWorkspace);
    createBackstageView();
    addActionsToQuickAccessBar();
    createRibbonBar();
    createDockPanels();
    createStatusBar();

    ribbonBar()->setTitleBackground(QPixmap(QStringLiteral(":/res/background.png")));

    setDefaultWidgetGeometry(this);
    QSettings settings;
    QStringList files = settings.value(QStringLiteral("recentFileList")).toStringList();
    settings.setValue(QStringLiteral("recentFileList"), files);
    emit updateRecentFileActions(files);

    RibbonCustomizeManager* customizeManager = ribbonBar()->customizeManager();
    customizeManager->setDefaultState(); //Make a default state-point for ribbon-ui elements.

    readSettings();
}

MainWindow::~MainWindow()
{
    writeSettings();
}

void MainWindow::createBackstageView()
{
    QIcon iconLogo;
    iconLogo.addPixmap(QPixmap(QStringLiteral(":/res/file.png")));
    iconLogo.addPixmap(QPixmap(QStringLiteral(":/res/qtitanlogo32x32.png")));
    QAction* actionFile = ribbonBar()->addSystemButton(iconLogo, tr("&File"));
    actionFile->setToolTip(tr("Click here to see everything<br />you can do with your<br />document"));

    RibbonBackstageView* backstageView = new RibbonBackstageView(this);

    connect(backstageView, SIGNAL(aboutToShow()), this, SLOT(showBackstage()));
    connect(backstageView, SIGNAL(aboutToHide()), this, SLOT(hideBackstage()));

    QAction* saveFile = backstageView->addAction(DemoRibbonWindow::createIcon(Image_Save), tr("&Save"));
    saveFile->setShortcut(tr("Ctrl+S"));
    saveFile->setToolTip(tr("Save"));
    saveFile->setStatusTip(tr("Save the active document"));
    connect(saveFile, SIGNAL(triggered()), this, SLOT(save()));

    QAction* saveAsFile = backstageView->addAction(DemoRibbonWindow::createIcon(Image_SaveAs), tr("Save &As"));
    saveAsFile->setToolTip(tr("Save As"));
    saveAsFile->setStatusTip(tr("Save the active document with a new name"));
    connect(saveAsFile, SIGNAL(triggered()), this, SLOT(save()));

    QAction* openFile = backstageView->addAction(DemoRibbonWindow::createIcon(Image_Open), tr("&Open"));
    openFile->setShortcut(tr("Ctrl+O"));
    openFile->setToolTip(tr("Open"));
    openFile->setStatusTip(tr("Open an existing document"));
    connect(openFile, SIGNAL(triggered()), this, SLOT(open()));

    BackstagePageInfo* infoPage = new BackstagePageInfo(this);
    backstageView->addPage(infoPage);

    BackstagePageSave* savePage = new BackstagePageSave(this);
    backstageView->addPage(savePage);

    BackstagePageAbout* aboutPage = new BackstagePageAbout(this);
    backstageView->addPage(aboutPage);

    QAction* actOptions = backstageView->addAction(QIcon(QStringLiteral(":/res/smallOptions.png")), tr("Options"));
    connect(actOptions, SIGNAL(triggered()), this, SLOT(pressButton()));

    backstageView->addSeparator();

    QAction* actExit = backstageView->addAction(QIcon(QStringLiteral(":/res/smallClose.png")), tr("Exit"));
    connect(actExit, SIGNAL(triggered()), this, SLOT(close()));

    if (RibbonSystemButton* sysButton = ribbonBar()->systemButton())
        sysButton->setBackstage(backstageView);
    backstageView->open();
}

void MainWindow::addActionsToQuickAccessBar()
{
    RibbonQuickAccessBar* quickAccessBar = ribbonBar()->quickAccessBar();
    quickAccessBar->addAction(DemoRibbonWindow::autoSaveAction());

    QAction* smallButton = quickAccessBar->addAction(DemoRibbonWindow::createIcon(Image_New), tr("New"));
    smallButton->setToolTip(tr("Create a new document"));
    connect(smallButton, SIGNAL(triggered()), this, SLOT(pressButton()));
    quickAccessBar->setActionVisible(smallButton, false);

    smallButton = quickAccessBar->addAction(DemoRibbonWindow::createIcon(Image_Open), tr("Open"));
    smallButton->setToolTip(tr("Open an existing document"));
    connect(smallButton, SIGNAL(triggered()), this, SLOT(pressButton()));
    quickAccessBar->setActionVisible(smallButton, false);

    smallButton = quickAccessBar->addAction(DemoRibbonWindow::createIcon(Image_Save), tr("Save"));
    smallButton->setToolTip(tr("Save the active document"));
    connect(smallButton, SIGNAL(triggered()), this, SLOT(pressButton()));

    smallButton = quickAccessBar->addAction(QIcon(QStringLiteral(":/res/smallUndo.png")), tr("&Undo"));
    smallButton->setShortcut(QKeySequence::Undo);
    smallButton->setEnabled(false);

    smallButton = quickAccessBar->addAction(QIcon(QStringLiteral(":/res/smallRedo.png")), tr("&Redo"));
    smallButton->setShortcut(QKeySequence::Redo);
    smallButton->setEnabled(false);
    quickAccessBar->setVisible(true);
}

void MainWindow::createRibbonBar()
{
    RibbonPage* pageButtons = ribbonBar()->addPage(tr("&Buttons"));
    QMenu* splitPopup = new QMenu(this);
    splitPopup->addAction(tr("Option 1"));
    splitPopup->addAction(tr("Option 2"));
    splitPopup->addAction(tr("Option 3"));

    RibbonGroup* groupClipboard = pageButtons->addGroup(tr("Clipboard"));
    QMenu* editPaste = new QMenu(this);
    QAction* actionPaste1 = editPaste->addAction(QIcon(QStringLiteral(":/res/smallClipboard.png")), tr("Paste"));
    actionPaste1->setPriority(QAction::LowPriority);
    actionPaste1->setShortcut(QKeySequence::Paste);

    QAction* actionPaste2 = groupClipboard->addAction(QIcon(QStringLiteral(":/res/clipboard.png")),
        tr("&Paste"), Qt::ToolButtonTextUnderIcon, editPaste);
    actionPaste2->setPriority(QAction::LowPriority);
    actionPaste2->setShortcut(QKeySequence::Paste);

    QAction* actionCut = groupClipboard->addAction(QIcon(QStringLiteral(":/res/smallCut.png")),
        tr("&Cut"), Qt::ToolButtonTextBesideIcon);
    actionCut->setShortcut(QKeySequence::Cut);

    QAction* actionCopy = groupClipboard->addAction(QIcon(QStringLiteral(":/res/smallCopy.png")),
        tr("&Copy"), Qt::ToolButtonTextBesideIcon);
    actionCopy->setShortcut(QKeySequence::Copy);

    groupClipboard->addAction(QIcon(QStringLiteral(":/res/smallFormatPainter.png")),
        tr("F&ormat Pointer"), Qt::ToolButtonTextBesideIcon);
    groupClipboard->setEnabled(false);

    RibbonGroup* groupView = pageButtons->addGroup(tr("View"));
    QCheckBox* checkStatusBar = new QCheckBox(tr("Status Bar"));
    checkStatusBar->setToolTip(tr("CheckBox 1"));
    checkStatusBar->setCheckState(Qt::Checked);
    groupView->addWidget(checkStatusBar);
    connect(checkStatusBar, SIGNAL(stateChanged(int)), this, SLOT(hideStatusBar(int)));

    ribbonBar()->addPage(tr("&Window"));
    ribbonBar()->addPage(tr("&Context Tab"))->setContextColor(ContextColorGreen);
    ribbonBar()->setFrameThemeEnabled();
}

void MainWindow::createDockPanels()
{
    QDockWidget* dock = new QDockWidget(tr("Styles"), this);
    dock->setAllowedAreas(Qt::AllDockWidgetAreas);

    QWidget* styleWidget = new QWidget();

    QWidget* styleGroup = new QWidget();
    QVBoxLayout* listLayout = new QVBoxLayout();
    listLayout->setContentsMargins(0, 0, 0, 0);

    QListWidget* customerList = new QListWidget();
    for (int i = 0; 10 > i; i++)
        customerList->addItems(QStringList(QObject::tr("Item %1").arg(i+1)));

    listLayout->addWidget(customerList);
    styleGroup->setLayout(listLayout);

    QGroupBox* optionsGroup = new QGroupBox(tr("Preview"));
    QVBoxLayout* optionsLayout = new QVBoxLayout();
    QCheckBox* showPreviewBox = new QCheckBox(tr("Show Preview"));
    showPreviewBox->setEnabled(false);
    QCheckBox* linkedStylesBox = new QCheckBox(tr("Disable Linked Styles"));
    optionsLayout->addWidget(showPreviewBox);
    optionsLayout->addWidget(linkedStylesBox);
    optionsGroup->setLayout(optionsLayout);

    QVBoxLayout* mainLayout = new QVBoxLayout;
    mainLayout->addWidget(styleGroup);
    mainLayout->addWidget(optionsGroup);
    styleWidget->setLayout(mainLayout);

    dock->setWidget(styleWidget);
    addDockWidget(Qt::LeftDockWidgetArea, dock);

#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
    QList<QDockWidget*> docks; docks << dock;
    QList<int> sizes; sizes << 120;
    resizeDocks(docks, sizes, Qt::Vertical);
#endif
}

void MainWindow::createStatusBar()
{
    setStatusBar(new RibbonStatusBar());
}

void MainWindow::open()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open"));
    if (!fileName.isEmpty())
    {
        setCurrentFile(fileName);
        statusBar()->showMessage(tr("File loaded"), 2000);
    }
}

bool MainWindow::save()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),tr("Document"), QLatin1Char('*') + QStringLiteral(".txt"));
    if (!fileName.isEmpty())
    {
        setCurrentFile(fileName);
        statusBar()->showMessage(tr("File saved"), 2000);
        return true;
    }
    return false;
}

void MainWindow::openRecentFile(const QString& file)
{
    QMessageBox messageBox(QMessageBox::Information, windowTitle(),
        QString(), QMessageBox::Ok, this);
    messageBox.setInformativeText(QStringLiteral("Open Recent File.") + QStringLiteral(" ") + file);
    messageBox.exec();
}

void MainWindow::print()
{
    QPrinter printer;
    QPrintDialog dialog(&printer, this);
    if (dialog.exec() == QDialog::Accepted)
    {
    }
}

void MainWindow::printSetup()
{
}

void MainWindow::hideStatusBar(int state)
{
    statusBar()->setVisible(state == Qt::Checked);
}

void MainWindow::pressButton()
{
    QMessageBox messageBox(QMessageBox::Information, windowTitle(),
        QString(), QMessageBox::Ok, this);
    messageBox.setInformativeText(QStringLiteral("Press button."));
    messageBox.exec();
}

void MainWindow::showBackstage()
{
    ribbonBar()->quickAccessBar()->setEnabled(false);
    m_minimizeAction->setEnabled(false);
    m_optionsMenu->setEnabled(false);
}

void MainWindow::hideBackstage()
{
    ribbonBar()->quickAccessBar()->setEnabled(true);
    m_minimizeAction->setEnabled(true);
    m_optionsMenu->setEnabled(true);
}

void MainWindow::setCurrentFile(const QString& fileName)
{
    QString curFile = fileName;
    setWindowFilePath(curFile);
    QSettings settings;
    QStringList files = settings.value(QStringLiteral("recentFileList")).toStringList();
    files.removeAll(fileName);
    files.prepend(fileName);
    while (files.size() > 9)
        files.removeLast();
    settings.setValue(QStringLiteral("recentFileList"), files);
    emit updateRecentFileActions(files);
}