ToolBars Example

#include <QApplication>
#include <QScreen>
#include <QAction>
#include <QMenuBar>
#include <QStatusBar>
#include <QSpinBox>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <qevent.h>

#include "mainwindow.h"

/* MainWindow */
MainWindow::MainWindow()
    : m_mdiArea(new QMdiArea)
{
    m_mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    setCentralWidget(m_mdiArea);

    createActions();
    createMenus();
    createToolBars();
    createStatusBar();

    QWidget* child = createMdiChild();
    child->showMaximized();
    child->setFocus();

#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    const QRect availableGeometry = screen()->availableGeometry();
#else
    const QRect availableGeometry = QGuiApplication::primaryScreen()->availableGeometry();
#endif
    resize(availableGeometry.width() / 2, availableGeometry.height() / 2);
    move((availableGeometry.width() - width()) / 2, (availableGeometry.height() - height()) / 2);
    readSettings();
}

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

void MainWindow::createActions()
{
    m_fileNewAction = new QAction(QIcon(QStringLiteral(":/res/new16x16.png")), tr("&New"), this);
    m_fileNewAction->setShortcuts(QKeySequence::New);
    m_fileNewAction->setStatusTip(tr("Create a new document"));

    m_fileOpenAction = new QAction(QIcon(QStringLiteral(":/res/open16x16.png")), tr("&Open"), this);
    m_fileOpenAction->setShortcuts(QKeySequence::Open);
    m_fileOpenAction->setStatusTip(tr("Open an existing document"));

    m_fileSaveAction = new QAction(QIcon(QStringLiteral(":/res/save16x16.png")), tr("&Save..."), this);
    m_fileSaveAction->setShortcuts(QKeySequence::Save);
    m_fileSaveAction->setStatusTip(tr("Save the active document"));

    m_fileSaveAllAction = new QAction(QIcon(QStringLiteral(":/res/save_all16x16.png")), tr("&Sa&ve All"), this);
    m_fileSaveAllAction->setShortcuts(QKeySequence::Save);
    m_fileSaveAllAction->setStatusTip(tr("Save All"));

    m_filePrintAction = new QAction(QIcon(QStringLiteral(":/res/print16x16.png")), tr("Print"), this);
    m_filePrintAction->setStatusTip(tr("Print the active document"));

    m_editCutAction = new QAction(QIcon(QStringLiteral(":/res/cut16x16.png")), tr("&Cut"), this);
    m_editCutAction->setShortcuts(QKeySequence::Cut);
    m_editCutAction->setStatusTip(tr("Cut the selection and put it on the Clipboard"));

    m_editCopyAction = new QAction(QIcon(QStringLiteral(":/res/copy16x16.png")), tr("&Copy"), this);
    m_editCopyAction->setShortcuts(QKeySequence::Copy);
    m_editCopyAction->setStatusTip(tr("Copy the selection and put it on the Clipboard"));

    m_editUndoAction = new QAction(QIcon(QStringLiteral(":/res/undo16x16.png")), tr("Undo"), this);
    m_editUndoAction->setStatusTip(tr("Undo the last action"));

    m_editRedoAction = new QAction(QIcon(QStringLiteral(":/res/redo16x16.png")), tr("Redo"), this);
    m_editRedoAction->setStatusTip(tr("Redo the previously undone action"));

    m_editTogglebookmarkAction = new QAction(QIcon(QStringLiteral(":/res/togglebookmark16x16.png")), tr("&Toggle Bookmark"), this);
    m_editTogglebookmarkAction->setStatusTip(tr("Inserts or removes a BookMark"));

    m_editNextbookmarkAction = new QAction(QIcon(QStringLiteral(":/res/nextbookmark16x16.png")), tr("&Next Bookmark"), this);
    m_editNextbookmarkAction->setStatusTip(tr("Go to the next temporary bookmark"));

    m_editPreviousbookmarkAction = new QAction(QIcon(QStringLiteral(":/res/previousbookmark16x16.png")), tr("&Previous Bookmark"), this);
    m_editPreviousbookmarkAction->setStatusTip(tr("Go to the previous temporary bookmark"));

    m_editClearallbookmarkAction = new QAction(QIcon(QStringLiteral(":/res/clearallbookmark16x16.png")), tr("&Clear All Bookmark"), this);
    m_editClearallbookmarkAction->setStatusTip(tr("Clear all Bookmarks in the windows"));

    m_editFindAction = new QAction(QIcon(QStringLiteral(":/res/find16x16.png")), tr("&Find"), this);
    m_editFindAction->setStatusTip(tr("Find the specified text"));

    m_editIncreaseIdentAction = new QAction(QIcon(QStringLiteral(":/res/increaseident16x16.png")), tr("&Increase Ident"), this);
    m_editIncreaseIdentAction->setStatusTip(tr("Idents the selection text right one tab stop"));

    m_editDecreaseIdentAction = new QAction(QIcon(QStringLiteral(":/res/decreaseident16x16.png")), tr("&Decrease Ident"), this);
    m_editDecreaseIdentAction->setStatusTip(tr("Idents the selection text left one tab stop"));

    m_buildActiveConfigurationAction = new QAction(QIcon(QStringLiteral(":/res/activeconfiguration16x16.png")), tr("Select Active Configuration"), this);
    m_buildActiveConfigurationAction->setStatusTip(tr("Selects the active configuration"));

    m_buildDummyCompileAction = new QAction(QIcon(QStringLiteral(":/res/dummycompile16x16.png")), tr("Compile"), this);
    m_buildDummyCompileAction->setStatusTip(tr("Compile the file"));

    m_buildDummyBuildAction = new QAction(QIcon(QStringLiteral(":/res/dummybuild16x16.png")), tr("Build"), this);
    m_buildDummyBuildAction->setStatusTip(tr("Build the project"));

    m_buildDummyStopBuildAction = new QAction(QIcon(QStringLiteral(":/res/dummystopbuild16x16.png")), tr("Stop Build"), this);
    m_buildDummyStopBuildAction->setStatusTip(tr("Stop the build"));

    m_buildDummyExecuteAction = new QAction(QIcon(QStringLiteral(":/res/dummyexecute16x16.png")), tr("Execute Program"), this);
    m_buildDummyExecuteAction->setStatusTip(tr("Execute the program"));

    m_buildDummyGoAction = new QAction(QIcon(QStringLiteral(":/res/dummygo16x16.png")), tr("Go"), this);
    m_buildDummyGoAction->setStatusTip(tr("Starts or continues the program"));

    m_editTogglebreakPointAction = new QAction(QIcon(QStringLiteral(":/res/togglebreakpoint16x16.png")), tr("Insert/Remove Breakpoint"), this);
    m_editTogglebreakPointAction->setStatusTip(tr("Inserts or removes a breakpoint"));

    m_editRemoveAllBreakpointsAction = new QAction(QIcon(QStringLiteral(":/res/removeallbreakpoints16x16.png")), tr("Remove All Breakpoints"), this);
    m_editRemoveAllBreakpointsAction->setStatusTip(tr("Removes all breakpoints"));
}

void MainWindow::createMenus()
{
    m_fileMenu = menuBar()->addMenu(tr("&File"));
    m_fileMenu->addAction(m_fileNewAction);
    m_fileMenu->addAction(m_fileSaveAction);
    m_fileMenu->addAction(m_filePrintAction);

    m_editMenu = menuBar()->addMenu(tr("&Edit"));
    m_viewMenu = menuBar()->addMenu(tr("&View"));
    addSaveLoadMenu(m_viewMenu);
    menuBar()->addSeparator();
    m_helpMenu = menuBar()->addMenu(tr("&Help"));
    m_helpMenu->addAction(m_aboutAction);
}

void MainWindow::createToolBars()
{
    m_fileToolBar = addToolBar(tr("Standard"), Qtitan::DockBarTop);
    m_fileToolBar->addAction(m_fileNewAction);
    m_fileToolBar->addAction(m_fileOpenAction);
    m_fileToolBar->addAction(m_fileSaveAction);
    m_fileToolBar->addAction(m_fileSaveAllAction);
    m_fileToolBar->addSeparator();
    m_fileToolBar->addAction(m_editCutAction);
    m_fileToolBar->addAction(m_editCopyAction);
    m_fileToolBar->addSeparator();
    m_fileToolBar->addAction(m_editUndoAction);
    m_fileToolBar->addAction(m_editRedoAction);
    m_fileToolBar->addSeparator();
    m_fileToolBar->addAction(m_editFindAction);
    m_fileToolBar->addSeparator();
    m_fileToolBar->addAction(m_filePrintAction);

    m_fileToolBar->addWidget(new QLabel(tr("   Style:")));
    QAction* stylesAction = m_fileToolBar->addWidget(createComboBoxStyle());
    stylesAction->setToolTip(tr("Styles switcher"));

    m_fileToolBar->addSeparator();
    m_fileToolBar->addAction(m_aboutAction);

    m_editToolBar = addToolBar(tr("Edit"), Qtitan::DockBarTop);
    m_editToolBar->addAction(m_editTogglebookmarkAction);
    m_editToolBar->addAction(m_editNextbookmarkAction);
    m_editToolBar->addAction(m_editPreviousbookmarkAction);
    m_editToolBar->addAction(m_editClearallbookmarkAction);
    m_editToolBar->addSeparator();
    m_editToolBar->addAction(m_editFindAction);
    m_editToolBar->addSeparator();
    m_editToolBar->addAction(m_editIncreaseIdentAction);
    m_editToolBar->addAction(m_editDecreaseIdentAction);
    moveDockBar(m_fileToolBar, m_editToolBar);

    m_buildToolBar = addToolBar(tr("Build"), Qtitan::DockBarTop);
    m_buildToolBar->addAction(m_buildActiveConfigurationAction);
    m_buildToolBar->addSeparator();
    m_buildToolBar->addAction(m_buildDummyCompileAction);
    m_buildToolBar->addAction(m_buildDummyBuildAction);
    m_buildToolBar->addAction(m_buildDummyStopBuildAction);
    m_buildToolBar->addSeparator();
    m_buildToolBar->addAction(m_buildDummyExecuteAction);
    m_buildToolBar->addAction(m_buildDummyGoAction);
    m_buildToolBar->addAction(m_editTogglebreakPointAction);
    m_buildToolBar->addAction(m_editRemoveAllBreakpointsAction);

    m_editorsToolBar = addToolBar(tr("Editors 1"), Qtitan::DockBarTop);
    m_editorsToolBar->addWidget(new QLabel(QStringLiteral("Editor:")));
    QLineEdit* edit1 = new QLineEdit(QStringLiteral("LineEdit 1"));
    m_editorsToolBar->addWidget(edit1);
    QLineEdit* edit2 = new QLineEdit(QStringLiteral("LineEdit 2"));
    m_editorsToolBar->addWidget(edit2);
    moveDockBar(m_buildToolBar, m_editorsToolBar);

    m_comboBoxToolBar = addToolBar(tr("ComboBox 1"), Qtitan::DockBarTop);
    m_comboBoxToolBar->addWidget(new QLabel(QStringLiteral("ComboBox:")));
    QComboBox* comboBox1 = new QComboBox();
    comboBox1->addItem(QStringLiteral("ComboBox 1"));
    m_comboBoxToolBar->addWidget(comboBox1);
    QComboBox* comboBox2 = new QComboBox();
    comboBox2->addItem(QStringLiteral("ComboBox 2"));
    m_comboBoxToolBar->addWidget(comboBox2);
    moveDockBar(m_editToolBar, m_comboBoxToolBar);

    m_spinBoxToolBar = addToolBar(tr("SpinBox"), Qtitan::DockBarTop);
    m_spinBoxToolBar->addWidget(new QLabel(QStringLiteral("SpinBox:")));
    QSpinBox* spinBox1 = new QSpinBox();
    spinBox1->setMinimumWidth(90);
    m_spinBoxToolBar->addWidget(spinBox1);
    QSpinBox* spinBox2 = new QSpinBox();
    spinBox2->setMinimumWidth(90);
    m_spinBoxToolBar->addWidget(spinBox2);
    moveDockBar(m_editorsToolBar, m_spinBoxToolBar);

    m_editorsToolBar2 = addToolBar(tr("Editors 2"), Qtitan::DockBarLeft);
    m_editorsToolBar2->addWidget(new QLabel(QStringLiteral("Editor:")));
    QLineEdit* edit11 = new QLineEdit(QStringLiteral("LineEdit 1"));
    edit11->setMinimumWidth(135);
    m_editorsToolBar2->addWidget(edit11);
    QLineEdit* edit12 = new QLineEdit(QStringLiteral("LineEdit 2"));
    edit12->setMinimumWidth(135);
    m_editorsToolBar2->addWidget(edit12);

    m_comboBoxToolBar2 = addToolBar(tr("ComboBox 2"), Qtitan::DockBarLeft);
    m_comboBoxToolBar2->addWidget(new QLabel(QStringLiteral("ComboBox:")));
    QComboBox* comboBox21 = new QComboBox();
    comboBox21->setMinimumWidth(135);
    comboBox21->addItem(QStringLiteral("ComboBox 1"));
    m_comboBoxToolBar2->addWidget(comboBox21);
    QComboBox* comboBox22 = new QComboBox();
    comboBox22->setMinimumWidth(135);
    comboBox22->addItem(QStringLiteral("ComboBox 2"));
    m_comboBoxToolBar2->addWidget(comboBox22);
    moveDockBar(m_comboBoxToolBar2, m_editorsToolBar2);
}

void MainWindow::createStatusBar()
{
    statusBar()->showMessage(tr("Ready"));
}

QWidget* MainWindow::createMdiChild()
{
    QTextEdit* child = new QTextEdit;
    m_mdiArea->addSubWindow(child);
    return child;
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    event->accept();
}