MDIDemo Example

import sys
import MDIDemo_rc

from PySide2 import QtCore
from PySide2.QtCore import Qt, QUrl, QSettings
from PySide2.QtGui import QPainter, QPixmap, QIcon, QColor, QDesktopServices
from PySide2.QtWidgets import QApplication, QTextEdit, QMdiArea, QStyleOption, QLabel, QMessageBox

from DevMachines import QtitanBase
from DevMachines.QtitanStyle import ColorizedStyle
from DevMachines.QtitanNavigationDesignUI import (NavigationMainWindow, NavigationBar, NavigationMainMenu, NavigationSidePane, WindowsColor)

class MdiArea(QMdiArea):
    def __init__(self, parent = None):
        QMdiArea.__init__(self, parent)
        self.setTabsClosable(True)

    def paintEvent(self, event):
        QMdiArea.paintEvent(self, event)
        #painter = QPainter(self)
        #painter.setClipRegion(event.region())
        #opt = QStyleOption()
        #opt.init(self)
        #painter.fillRect(opt.rect, WindowsColor(WindowsColor.MetroUI_DarkCerulean))

class Window(NavigationMainWindow):
    def __init__(self):
        NavigationMainWindow.__init__(self)
        self.setWindowTitle(self.tr("Navigation MDI Application"))
        action = self.navigationBar().addSystemButton()
        self.connect(action, QtCore.SIGNAL("triggered()"), self.systemButtonClicked)

        action = self.navigationBar().addLogoButton(QIcon(":res/logo_50.png"))
        button = self.navigationBar().buttonByAction(action);
        button.setDescription("Logo", self.tr("CRM - Navigation menu example."))
        self.connect(action, QtCore.SIGNAL("triggered()"), self.showCompanyWebSite)

        mainMenu = NavigationMainMenu(self.navigationBar())
        mainMenu.setAutoClose(True)
        action = self.navigationBar().addMainMenuButton(mainMenu)
        button = self.navigationBar().buttonByAction(action)
        button.setDescription("Main", self.tr("Show work areas."))
        tileItem = mainMenu.addItem(QIcon(":res/tile_icon_service_85x71.png"), self.tr("Add MDI Window"))
        self.connect(tileItem.defaultAction(), QtCore.SIGNAL("triggered()"), self.addMDIWindow)

        action = self.navigationBar().addAction(NavigationBar.New)
        self.connect(action, QtCore.SIGNAL("triggered()"), self.addMDIWindow)
        action = self.navigationBar().addAction(NavigationBar.Delete)
        self.connect(action, QtCore.SIGNAL("triggered()"), self.removeMDIWindow)

        self.actionFullScreen = self.navigationBar().addAction(NavigationBar.Shelf)
        button = self.navigationBar().buttonByAction(self.actionFullScreen)
        button.setAlignment(Qt.AlignRight)
        self.actionFullScreen.setCheckable(True)
        self.connect(self.actionFullScreen, QtCore.SIGNAL("toggled(bool)"), self.fullScreen)

        helpPane = NavigationSidePane(self.navigationBar())
        helpPane.setWidget(self.createHelpWidget())
        action = self.navigationBar().addHelpButton(helpPane)
        button = self.navigationBar().buttonByAction(action)
        button.setDescription("Help", self.tr("Find help and training."))

        self.mdiArea = MdiArea()
        self.mdiArea.setLineWidth(0)
        self.mdiArea.setViewMode(QMdiArea.SubWindowView)
        self.mdiArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.mdiArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.setCentralWidget(self.mdiArea)

        self.connect(self.mdiArea, QtCore.SIGNAL("subWindowActivated(QMdiSubWindow*)"), self.subWindowActivated)
        #setUnifiedTitleAndToolBarOnMac(true);

        self.stateWindow = self.windowState()
        geom = QApplication.desktop().availableGeometry(self)
        self.resize( 2 * geom.width() / 3, 2 * geom.height() / 3 )
        self.readSettings()

    def closeEvent(self, event):
        self.mdiArea.closeAllSubWindows()
        if self.mdiArea.currentSubWindow() != None:
            event.ignore()
        else:
            self.writeSettings()
            event.accept()

    def create_link(self, link, text):
        ret = "<a style=\"text-decoration:underline;\" href=\"" + link + "\">" + text + "</a>"
        return ret

    def createHelpWidget(self):
        label = QLabel()
        html = self.create_link("#", "Help Topics")
        html += "<br><br>"
        html += self.create_link("#", "Feedback")
        html += "<br><br>"
        html += self.create_link("#", "Community")
        html += "<br><br>"
        html += self.create_link("#", "Legal Information")
        html += "<br><br>"
        html += self.create_link("#", "Privacy and cookie")
        html += "<br><br>"
        label.setText(html)
        return label

    @QtCore.Slot()
    def systemButtonClicked(self):
        QMessageBox.information(self, self.tr("MDI"), self.tr("System Button clicked"))

    @QtCore.Slot()
    def showCompanyWebSite(self):
        QDesktopServices.openUrl(QUrl("https://www.devmachines.com"))

    @QtCore.Slot()
    def addMDIWindow(self):
        edit = QTextEdit()
        w = self.mdiArea.addSubWindow(edit)
        w.setVisible(True)

    @QtCore.Slot()
    def removeMDIWindow(self):
        w = self.mdiArea.currentSubWindow()
        if w:
            self.mdiArea.removeSubWindow(w)

    def readSettings(self):
        settings = QSettings(QSettings.UserScope, "Developer Machines")
        geom = QApplication.desktop().availableGeometry(self)
        pos = settings.value("pos", QtCore.QPoint(200, 200))
        size = settings.value("size", QtCore.QSize(2 * geom.width() / 3, 2 * geom.height() / 3))
        self.move(pos)
        self.resize(size)

    def writeSettings(self):
        settings = QSettings(QSettings.UserScope, "Developer Machines")
        settings.setValue("pos", self.pos())
        settings.setValue("size", self.size())

    def setActiveSubWindow(self, window):
        if  window:
            self.mdiArea.setActiveSubWindow(window)

    def switchViewMode(self, state):
        if state == Qt.Checked:
            self.mdiArea.setLineWidth(3)
            self.mdiArea.setViewMode(QMdiArea.TabbedView)
        else:
            self.mdiArea.setLineWidth(0)
            self.mdiArea.setViewMode(QMdiArea.SubWindowView)

    def subWindowActivated(self, subWindow):
        subWindow = subWindow

    def fullScreen(self, checked):
        if checked:
            self.stateWindow = self.windowState()
            self.setWindowState(Qt.WindowFullScreen)
        else:
            self.setWindowState(self.stateWindow)

    def keyPressEvent(self, event):
        NavigationMainWindow.keyPressEvent(event)
        if event.key() == Qt.Key_Escape and self.actionFullScreen.isChecked():
            self.actionFullScreen.setChecked(False)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    style = ColorizedStyle()
    app.setStyle(style)

    w = Window()
    w.show()
    sys.exit(app.exec_())