MDIDemo Example

#include <QApplication>
#include <QScreen>
#include <QDesktopServices>
#include <QLabel>
#include <QTextEdit>
#include <QSettings>
#include <QPainter>
#include <QStyleOption>
#include <QPaintEvent>
#include "window.h"

/* MdiArea */
MdiArea::MdiArea(QWidget* parent)
    : QMdiArea(parent)
{
    setTabsClosable(true);
}

MdiArea::~MdiArea()
{
}

void MdiArea::paintEvent(QPaintEvent* paintEvent)
{
    Q_UNUSED(paintEvent);
    QPainter painter(viewport());
    QStyleOption opt;
    opt.initFrom(this);
    painter.fillRect(opt.rect, WindowsColor(WindowsColor::MetroUI_DarkCerulean));
}

/* Window */
Window::Window()
    : NavigationMainWindow(), m_stateWindow(Qt::WindowStates())
{
    setWindowTitle(tr("Navigation MDI Application"));

    QAction* action = navigationBar()->addSystemButton();
    //connect(action, SIGNAL(triggered()), this, SLOT(systemButtonClicked()));

    action = navigationBar()->addLogoButton(QIcon(QStringLiteral(":res/logo_50.png")));
    NavigationToolButton* button = navigationBar()->buttonByAction(action);
    button->setDescription(tr("Logo"), tr("CRM - Navigation menu example."));
    connect(action, SIGNAL(triggered()), this, SLOT(showCompanyWebSite()));

    NavigationMainMenu* mainMenu = new NavigationMainMenu(navigationBar());
    mainMenu->setAutoClose(true);
    action = navigationBar()->addMainMenuButton(mainMenu);
    button = navigationBar()->buttonByAction(action);
    button->setDescription(tr("Main"), tr("Show work areas."));
    NavigationMainMenuItem* tileItem = mainMenu->addItem(QIcon(QStringLiteral(":res/tile_icon_service_85x71.png")), tr("Add MDI Window"));
    connect(tileItem->defaultAction(), SIGNAL(triggered()), this, SLOT(addMDIWindow()));

    action = navigationBar()->addAction(NavigationBar::New);
    connect(action, SIGNAL(triggered()), this, SLOT(addMDIWindow()));
    action = navigationBar()->addAction(NavigationBar::Delete);
    connect(action, SIGNAL(triggered()), this, SLOT(removeMDIWindow()));

    m_actionFullScreen = navigationBar()->addAction(NavigationBar::Shelf);
    button = navigationBar()->buttonByAction(m_actionFullScreen);
    button->setAlignment(Qt::AlignRight);
    m_actionFullScreen->setCheckable(true);
    connect(m_actionFullScreen, SIGNAL(toggled(bool)), this, SLOT(fullScreen(bool)));

    NavigationSidePane* helpPane = new NavigationSidePane(navigationBar());
    helpPane->setWidget(createHelpWidget());
    action = navigationBar()->addHelpButton(helpPane);
    button = navigationBar()->buttonByAction(action);
    button->setDescription(tr("Help"), tr("Find help and training."));

    m_mdiArea = new MdiArea(this);
    m_mdiArea->setLineWidth(0);
    m_mdiArea->setViewMode(QMdiArea::SubWindowView);
    m_mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    setCentralWidget(m_mdiArea);

    connect(m_mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(subWindowActivated(QMdiSubWindow*)));

    //setUnifiedTitleAndToolBarOnMac(true);

    m_stateWindow = windowState();

    readSettings();
}

Window::~Window()
{
}

static QString create_link(const QString& link, const QString& text)
{

    QString ret = QStringLiteral("<a style=\"text-decoration:underline;\" href=\"%1\">%2</a>").arg(link).arg(text);
    return ret;
}

QWidget* Window::createHelpWidget()
{
    QLabel* label = new QLabel();
    QString html = create_link(QStringLiteral("#"), QStringLiteral("Help Topics"));
    html += QStringLiteral("<br><br>");
    html += create_link(QStringLiteral("#"), QStringLiteral("Feedback"));
    html += QStringLiteral("<br><br>");
    html += create_link(QStringLiteral("#"), QStringLiteral("Community"));
    html += QStringLiteral("<br><br>");
    html += create_link(QStringLiteral("#"), QStringLiteral("Legal Information"));
    html += QStringLiteral("<br><br>");
    html += create_link(QStringLiteral("#"), QStringLiteral("Privacy and cookie"));
    html += QStringLiteral("<br><br>");
    label->setText(html);
    return label;
}

void Window::closeEvent(QCloseEvent* event)
{
    m_mdiArea->closeAllSubWindows();
    if (m_mdiArea->currentSubWindow())
    {
        event->ignore();
    }
    else
    {
        writeSettings();
        event->accept();
    }
}

void Window::showCompanyWebSite()
{
    QDesktopServices::openUrl(QUrl(QStringLiteral("http://www.devmachines.com")));
}

void Window::addMDIWindow()
{
    QTextEdit* edit = new QTextEdit();
    QMdiSubWindow* w = m_mdiArea->addSubWindow(edit);
    w->setVisible(true);
}

void Window::removeMDIWindow()
{
    QMdiSubWindow* w = m_mdiArea->currentSubWindow();
    if (w == Q_NULL)
        return;
    delete w;
}

void Window::readSettings()
{
    QSettings settings(this);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    const QRect availableGeometry = screen()->availableGeometry();
#else
    QRect availableGeometry = QGuiApplication::primaryScreen()->availableGeometry();
#endif
    QRect rect = QRect(QPoint((availableGeometry.width() - width()) / 2, (availableGeometry.height() - height()) / 2),
        QSize(2 * availableGeometry.width() / 3, 2 * availableGeometry.height() / 3));
    QPoint pos = settings.value(QStringLiteral("pos"), rect.topLeft()).toPoint();
    QSize size = settings.value(QStringLiteral("size"), rect.size()).toSize();
    move(pos);
    resize(size);
}

void Window::writeSettings()
{
    QSettings settings(this);
    settings.setValue(QStringLiteral("pos"), pos());
    settings.setValue(QStringLiteral("size"), size());
}

void Window::setActiveSubWindow(QWidget* window)
{
    if (!window)
        return;
    m_mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow*>(window));
}

void Window::switchViewMode(int state)
{
    m_mdiArea->setLineWidth(state == Qt::Checked ? 3 : 0);
    m_mdiArea->setViewMode(state == Qt::Checked ? QMdiArea::TabbedView : QMdiArea::SubWindowView);
}

void Window::subWindowActivated(QMdiSubWindow* subWindow)
{
    Q_UNUSED(subWindow);
}

void Window::fullScreen(bool checked)
{
    if (checked)
    {
        m_stateWindow = windowState();
        setWindowState(Qt::WindowFullScreen);
    }
    else
    {
        setWindowState(m_stateWindow);
    }
}

void Window::keyPressEvent(QKeyEvent* event)
{
    Qtitan::NavigationMainWindow::keyPressEvent(event);

    if (event->key() == Qt::Key_Escape && m_actionFullScreen->isChecked())
    {
        m_actionFullScreen->setChecked(false);
    }
}