SystemMenuDemo Example

#include <QFileDialog>
#include <QPrinter>
#include <QPrintDialog>
#include <QPainter>
#include <QStyleOption>
#include <QPaintEvent>
#include "QtnRibbonDef.h"

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

/* MainWindow */
MainWindow::MainWindow(QWidget* parent)
    : DemoRibbonWindow(parent)
{
    setWindowTitle(tr("Ribbon System Menu"));
    setCentralWidget(new RibbonWorkspace);
    createMenuFile();
    addActionsToQuickAccessBar();
    createRibbon();
    statusBar();
    ribbonBar()->setFrameThemeEnabled();
    setDefaultWidgetGeometry(this);
}

MainWindow::~MainWindow()
{
}

void MainWindow::createMenuFile()
{
    QIcon iconLogo;
    iconLogo.addPixmap(QPixmap(QStringLiteral(":/shared/res/qtitan.png")));
    iconLogo.addPixmap(QPixmap(QStringLiteral(":/shared/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"));

    RibbonSystemMenu* systemMenu = qobject_cast<RibbonSystemMenu *>(actionFile->menu());
    QAction* newFile = systemMenu->addAction(QIcon(QStringLiteral(":/res/new.png")), tr("&New"));
    newFile->setShortcut(tr("Ctrl+N"));
    newFile->setStatusTip(tr("Create a new document"));
    newFile->setToolTip(tr("New"));
    newFile->setEnabled(false);

    QAction* openFile = systemMenu->addAction(QIcon(QStringLiteral(":/res/open.png")), 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()));

    QAction* saveFile = systemMenu->addAction(QIcon(QStringLiteral(":/res/save.png")), 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 = systemMenu->addAction(QIcon(QStringLiteral(":/res/save_as.png")), 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()));

    systemMenu->addSeparator();

    QAction* actionPrint = new QAction(QIcon(QStringLiteral(":/res/print.png")), tr("&Print..."), this);
    actionPrint->setToolTip(tr("Select a printer, number of copies, and other printing options before printing"));

    QAction* actionPagePrint = new QAction(actionPrint->icon(), actionPrint->text(), this);
    systemMenu->addAction(actionPagePrint);

    RibbonPageSystemPopup* pageSystemPopup = systemMenu->addPageSystemPopup(tr("Preview and print the document"), actionPagePrint, true);
    connect(actionPagePrint, SIGNAL(triggered()), actionPrint, SIGNAL(triggered()));
    pageSystemPopup->addAction(actionPrint);

    QAction* actionPrintSetup = new QAction(QIcon(QStringLiteral(":/res/printSetup.png")), tr("P&rint Setup"), this);
    actionPrintSetup->setToolTip(tr("Change the printer and printing options"));

    pageSystemPopup->addAction(actionPrintSetup);

    QAction* actionPrintPreview = new QAction(QIcon(QStringLiteral(":/res/printPreview.png")), tr("Print Pre&view..."), this);
    actionPrintPreview->setToolTip(tr("Preview and make changes to pages before printing"));
    pageSystemPopup->addAction(actionPrintPreview);

    QAction* actionPrepare = systemMenu->addAction(QIcon(QStringLiteral(":/res/prepare.png")), tr("Pr&epare"));
    systemMenu->addAction(actionPrepare);

    QAction* actionSend = systemMenu->addAction(QIcon(QStringLiteral(":/res/send.png")), tr("Sen&d"));
    systemMenu->addAction(actionSend);
    pageSystemPopup = systemMenu->addPageSystemPopup(tr("Preview and print the document"), actionSend, false);
    QAction* actionMail = pageSystemPopup->addAction(QIcon(QStringLiteral(":/res/mail.png")), tr("E-Mail"));
    actionMail->setToolTip(tr("Send the active document by e-mail"));
    pageSystemPopup->addAction(actionMail);

    QAction* actionIntenetFax = pageSystemPopup->addAction(QIcon(QStringLiteral(":/res/internetfix.png")), tr("Intenet Fax"));
    actionIntenetFax->setToolTip(tr("Use an Internet fax service to fax the document"));
    pageSystemPopup->addAction(actionIntenetFax);

    QAction* actionPublish = systemMenu->addAction(QIcon(QStringLiteral(":/res/publish.png")), tr("&Publish"));
    systemMenu->addAction(actionPublish);

    systemMenu->addSeparator();

    QIcon iconClose;
    iconClose.addPixmap(QPixmap(QStringLiteral(":/res/close.png")));
    iconClose.addPixmap(QPixmap(QStringLiteral(":/res/smallclose.png")));
    QAction* actClose = systemMenu->addAction(iconClose, tr("&Close"));
    actClose->setShortcut(tr("Ctrl+C"));
    actClose->setStatusTip(tr("Exit"));
    connect(actClose, SIGNAL(triggered()), this, SLOT(close()));
    systemMenu->addPopupBarAction(actClose, Qt::ToolButtonTextBesideIcon);

    QAction* option = new QAction(QPixmap(QStringLiteral(":/res/smalloption.png")), tr("Opt&ion"), this);
    systemMenu->addPopupBarAction(option, Qt::ToolButtonTextBesideIcon);
    option->setEnabled(false);

    RibbonPageSystemRecentFileList* pageRecentFile = systemMenu->addPageRecentFile(tr("Recent Documents"));
    pageRecentFile->setSize(9);
    QStringList list;
    list.append(tr("Document1.txt"));
    list.append(tr("Document2.txt"));
    list.append(tr("Document3.txt"));
    list.append(tr("Document4.txt"));
    list.append(tr("Document5.txt"));
    list.append(tr("Document6.txt"));
    pageRecentFile->updateRecentFileActions(list);
}

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

    QAction* smallButton = quickAccessBar->addAction(QIcon(QStringLiteral(":/res/smallNew.png")), tr("New"));
    smallButton->setToolTip(tr("Create a new document"));
    connect(smallButton, SIGNAL(triggered()), this, SLOT(pressButton()));
    quickAccessBar->setActionVisible(smallButton, false);

    smallButton = quickAccessBar->addAction(QIcon(QStringLiteral(":/res/smallOpen.png")), tr("Open"));
    smallButton->setToolTip(tr("Open an existing document"));
    connect(smallButton, SIGNAL(triggered()), this, SLOT(pressButton()));
    quickAccessBar->setActionVisible(smallButton, false);

    smallButton = quickAccessBar->addAction(QIcon(QStringLiteral(":/res/smallSave.png")), 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::createRibbon()
{
    RibbonPage* pageButtons = ribbonBar()->addPage(tr("&Home"));
    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/smallformat.png")), tr("F&ormat Pointer"), Qt::ToolButtonTextBesideIcon);
}

void MainWindow::open()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open"));
    if (!fileName.isEmpty())
        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())
    {
        statusBar()->showMessage(tr("File saved"), 2000);
        return true;
    }
    return false;
}

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

void MainWindow::printSetup()
{
}

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