MDIWindowDemo Example

#include <QApplication>
#include <QScreen>
#include <QAction>
#include <QMenuBar>
#include <QStatusBar>
#include <QLabel>
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
#include <QTextCodec>
#endif
#include <QMdiArea>
#include <QBuffer>
#include <QTextEdit>
#include <QMdiSubWindow>
#include <QTreeWidget>
#include <QFile>

#include "mainwindow.h"

/* MyDockSubWindow */
class MyDockSubWindow : public DockMainWindow
{
public:
    MyDockSubWindow(QWidget* parent = Q_NULL);
public:
    void createDockWidget();
    bool load(const QString& f);
public:
    virtual QSize minimumSizeHint() const { return centralWidget()->minimumSizeHint().expandedTo(QSize(240, 200)); }
protected:
    QTextEdit* m_textEdit;
private:
    Q_DISABLE_COPY(MyDockSubWindow)
};

MyDockSubWindow::MyDockSubWindow(QWidget* parent)
    : DockMainWindow(parent)
{
    m_textEdit = new QTextEdit;
    setCentralWidget(m_textEdit);
    QString initialFile = QStringLiteral(":/res/example.html");
    load(initialFile);
    createDockWidget();
}

void MyDockSubWindow::createDockWidget()
{
    if (DockWidgetPanel* panel = addDockPanel(QStringLiteral("Child DockWidgetPanel"), QSize(200, 120), LeftDockPanelArea))
    {
        QTreeWidget* treeWidget = new QTreeWidget;
        treeWidget->setAlternatingRowColors(true);
        treeWidget->setMinimumWidth(190);
        treeWidget->setHeaderHidden(true);

        QStringList strings;
        strings.append(tr("MainItem"));
        QTreeWidgetItem* mainItem = new QTreeWidgetItem(strings);
        mainItem->setCheckState(0, Qt::Checked);
        mainItem->setExpanded(false);
        mainItem->setIcon(0, QIcon(QStringLiteral(":/res/open16x16.png")));
        treeWidget->insertTopLevelItem(0, mainItem);

        for (int i = 0; 10 > i; ++i)
        {
            strings.clear();
            strings.append(tr("Item %1").arg(i + 1));
            QTreeWidgetItem* item = new QTreeWidgetItem(mainItem, strings);
            item->setCheckState(0, Qt::Checked);
            item->setIcon(0, QIcon(QStringLiteral(":/res/open16x16.png")));
            item->setExpanded(true);
            treeWidget->insertTopLevelItem(1, item);
        }
        treeWidget->expandAll();

        panel->setWidget(treeWidget);

        DockWidgetPanel::DockPanelFeatures features = panel->features();
        features &= ~(DockWidgetPanel::DockPanelMenuButton);
        panel->setFeatures(features);
    }
}

bool MyDockSubWindow::load(const QString& f)
{
    if (!QFile::exists(f))
        return false;

    QFile file(f);
    if (!file.open(QFile::ReadOnly))
        return false;

    QString shownName = QStringLiteral("untitled.txt");
    setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Qtitan Docking")));

    QByteArray data = file.readAll();
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
    QString str = QString::fromLocal8Bit(data);
    m_textEdit->setHtml(str);
#else
    QTextCodec* codec = Qt::codecForHtml(data);
    QString str = codec->toUnicode(data);
    if (Qt::mightBeRichText(str))
    {
        m_textEdit->setHtml(str);
    }
    else
    {
        str = QString::fromLocal8Bit(data);
        m_textEdit->setPlainText(str);
    }
#endif
    return true;
}

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

    createActions();
    createMenu();
    createToolBar();
    createStatusBar();
    createMdiChild();

#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"));
    //    connect(m_fileNewAction, SIGNAL(triggered()), this, SLOT(newLetter()));

    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"));
    //    connect(m_fileOpenAction, SIGNAL(triggered()), this, SLOT(newLetter()));

    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"));
    //    connect(m_fileSaveAction, SIGNAL(triggered()), this, SLOT(save()));

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

    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_editCutAction->setDisabled(true);

    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_editCopyAction->setDisabled(true);

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

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

void MainWindow::createMenu()
{
    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"));
    addStyleMenu(m_viewMenu);
    menuBar()->addSeparator();
    m_helpMenu = menuBar()->addMenu(tr("&Help"));
    m_helpMenu->addAction(m_aboutAction);
}

void MainWindow::createToolBar()
{
    m_fileToolBar = addToolBar(tr("Standard"), Qtitan::DockBarTop);
    m_fileToolBar->setIconSize(QSize(16, 16));
    m_fileToolBar->addAction(m_fileNewAction);
    m_fileToolBar->addAction(m_fileOpenAction);
    m_fileToolBar->addAction(m_fileSaveAction);
    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_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);
}

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

void MainWindow::createMdiChild()
{
    QMdiSubWindow* subWindow = m_mdiArea->addSubWindow(new MyDockSubWindow());
    subWindow->resize(640, 480);
}