DocumentDemo Example

import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../shared")

from DevMachines import __pyside2__, __pyside6__
from DevMachines import QtitanBase
from DevMachines.QtitanBase import Qtitan, WindowTitleBar
from DevMachines.QtitanDocking import DockMainWindow, DockWidgetPanel, DockDocumentPanel, DockLayoutPanel

if __pyside2__:
    from PySide2 import QtCore
    from PySide2.QtCore import Qt, QFile, QFileInfo
    from PySide2.QtGui import QIcon, QKeySequence, QFontMetrics
    from PySide2.QtWidgets import QAction, QActionGroup, QApplication, QLabel, QComboBox, QFileDialog

if __pyside6__:
    from PySide6 import QtCore
    from PySide6.QtCore import Qt, QFile, QFileInfo
    from PySide6.QtGui import QIcon, QKeySequence, QFontMetrics, QAction, QActionGroup
    from PySide6.QtWidgets import QApplication, QLabel, QComboBox, QFileDialog

from DemoDockWindow import DemoDockWindow
from MdiChild import MdiChild

import DocumentDemo_rc

class TitleBarCaption(QLabel):
    def __init__(self):

        QLabel.__init__(self)
        self.link = ""
        self.setAttribute(Qt.WA_NoSystemBackground, True)
        self.setOpenExternalLinks(True)
        self.setText("<table border=\"0\"><tr>"
                "<td><img src=\":res/logo.png\"></td><td style=\"vertical-align: middle;\"> {0} by "
                "<a href=\"http://www.devmachines.com\">Developer Machines</a></td></tr></table>".format(qApp.applicationName()))
        self.connect(self, QtCore.SIGNAL("linkHovered(const QString &)"), self, QtCore.SLOT("setLink(const QString &)"))

    def mousePressEvent(self, event):
        QLabel.mousePressEvent(self, event)
        if self.link != "":
            event.accept()

    def setLink(self, link):
        self.link = link

class MainWindow(DemoDockWindow):
    def __init__(self, parent = None):
        DemoDockWindow.__init__(self, parent)
        self.defaultStyle = "windowsvista"
        qApp.style().setObjectName(self.defaultStyle)

        self.dockPanelManager().createDocumentLayout()

        self.createActions()
        self.createMainMenu()
        self.createToolBars()
        self.createDockPanels()
        self.createStatusBar()
        self.updateActionStyles()
        self.openFile(":/res/glwidget.h")
        self.openFile(":/res/glwidget.cpp")

        geom = self.screen().availableGeometry()
        self.resize(2 * geom.width() / 3, 2 * geom.height() / 3)

        self.titleBar().setWidget(TitleBarCaption(), WindowTitleBar.AlignClient)
        self.titleBar().show()
        self.readSettings()

    def createActions(self):
        self.navigateBackwardAction = QAction(QIcon(":/res/backward16x16.png"), self.tr("Navigate Backward"), self);
        self.navigateBackwardAction.setStatusTip(self.tr("Navigate Backward"));
        self.navigateBackwardAction.setEnabled(False);
        self.navigateForwardAction = QAction(QIcon(":/res/forward16x16.png"), self.tr("Navigate Forward"), self);
        self.navigateForwardAction.setStatusTip(self.tr("Navigate Forward"));
        self.navigateForwardAction.setEnabled(False);

        self.newFileAction = QAction(QIcon(":/res/newfile16x16.png"), self.tr("New"), self)
        self.newFileAction.setPriority(QAction.LowPriority)
        self.newFileAction.setShortcut(QKeySequence.New)
        self.newFileAction.setStatusTip(self.tr("Create a document"))
        self.newFileAction.setToolTip(self.tr("New"))
        self.connect(self.newFileAction, QtCore.SIGNAL("triggered()"), self, QtCore.SLOT("newFile()"))

        self.openFileAction = QAction(QIcon(":/res/openfolder16x16.png"), self.tr("Open..."), self)
        self.openFileAction.setShortcut(QKeySequence.Open)
        self.openFileAction.setToolTip(self.tr("Open"))
        self.openFileAction.setStatusTip(self.tr("Open an existing document"))
        self.connect(self.openFileAction, QtCore.SIGNAL("triggered()"), self, QtCore.SLOT("open()"))

        self.closeDocFileAction = QAction(self.tr("Close"), self)

        self.saveFileAction = QAction(QIcon(":/res/save16x16.png"), self.tr("Save"), self)
        self.saveFileAction.setShortcut(QKeySequence.Save)
        self.saveFileAction.setToolTip(self.tr("Save"))
        self.saveFileAction.setStatusTip(self.tr("Save the active document"))
        self.saveFileAction.setEnabled(False)

        self.saveAllFileAction = QAction(QIcon(":/res/saveall16x16.png"), self.tr("Save All"), self)
        self.saveAllFileAction.setShortcut(self.tr("Ctrl+Shift+S"))
        self.saveAllFileAction.setToolTip(self.tr("Save All"))
        self.saveAllFileAction.setStatusTip(self.tr("Save All"))

        self.undoAction = QAction(QIcon(":/res/undo16x16.png"), self.tr("Undo"), self)
        self.undoAction.setShortcut(self.tr("Ctrl+Z"))

        self.redoAction = QAction(QIcon(":/res/redo16x16.png"), self.tr("Redo"), self)
        self.redoAction.setShortcut(self.tr("Ctrl+Y"))

    def createMainMenu(self):
        self.fileMenu = self.menuBar().addMenu(self.tr("&File"))
        self.createFileMenu(self.fileMenu)

        self.editMenu = self.menuBar().addMenu(self.tr("&Edit"))

        self.viewMenu = self.menuBar().addMenu(self.tr("&View"))
        self.createViewMenu(self.viewMenu)

        self.buildMenu = self.menuBar().addMenu(self.tr("&Build"))

        self.toolsMenu = self.menuBar().addMenu(self.tr("&Tools"))
        self.createToolMenu(self.toolsMenu)

        self.windowMenu = self.menuBar().addMenu(self.tr("&Window"))
        self.helpMenu = self.menuBar().addMenu(self.tr("&Help"))

    def createFileMenu(self, menu):
        menu.addAction(self.newFileAction)
        menu.addAction(self.openFileAction)
        menu.addAction(self.saveFileAction)
        menu.addSeparator()
        menu.addAction(self.closeDocFileAction)

    def createViewMenu(self, menuView):
        actionFullScreen = menuView.addAction(self.tr("F&ull Screen"))
        actionFullScreen.setEnabled(False)
        menuView.addSeparator()
        # panels...
        self.solutionExplorerAction = menuView.addAction(self.tr("Solution &Explorer"))
        self.solutionExplorerAction.setCheckable(True)
        self.solutionExplorerAction.setChecked(True)

        actionClassView = menuView.addAction(self.tr("&Class View"))
        actionClassView.setEnabled(False)

        actionResourceView = menuView.addAction(self.tr("&Resource View"))
        actionResourceView.setEnabled(False)

        menuView.addSeparator()
        # styles...
        self.createViewMenuStyle(menuView);

    def createToolMenu(self, menu):
        menu.addAction(self.saveStateAction)
        menu.addAction(self.loadStateAction)

    def createToolBars(self):
        toolBar = self.addToolBar(self.tr("Standard"), Qtitan.DockBarTop)
        toolBar.addAction(self.navigateBackwardAction)
        toolBar.addAction(self.navigateForwardAction)
        toolBar.addSeparator()
        toolBar.addAction(self.newFileAction)
        toolBar.addAction(self.openFileAction)
        toolBar.addAction(self.saveFileAction)
        toolBar.addAction(self.saveAllFileAction)
        toolBar.addSeparator()
        toolBar.addAction(self.undoAction)
        toolBar.addAction(self.redoAction)
        configurationsBox = QComboBox(self)
        configurationsBox.addItem(self.tr("Debug"))
        configurationsBox.addItem(self.tr("Release"))
        configurationsBox.addItem(self.tr("Configuration Manager..."))
        fm = QFontMetrics(configurationsBox.font())
        configurationsBox.setMaximumWidth(fm.horizontalAdvance("XXXXXXXXXXXXX", -1))
        configurationsAction = toolBar.addWidget(configurationsBox)
        configurationsAction.setToolTip(self.tr("Solution Configurations"))
        configurationsAction.setStatusTip(self.tr("Solution Configurations"))

        platformsBox = QComboBox(self)
        platformsBox.addItem(self.tr("x64"))
        platformsBox.addItem(self.tr("Configuration Manager..."))
        platformsBox.setMinimumWidth(100)
        platformsAction = toolBar.addWidget(platformsBox)
        platformsAction.setToolTip(self.tr("Solution Platforms"))
        platformsAction.setStatusTip(self.tr("Solution Platforms"))
        platformsAction.setEnabled(False)

        toolBar.addWidget(QLabel(self.tr("   Style:")));
        self.createComboBoxStyle()
        stylesAction = toolBar.addWidget(self.styleSwitcher)
        stylesAction.setToolTip(self.tr("Styles switcher"))

    def createDockPanels(self):
        #self.panelSolutionExplorer = FileViewPanel(self.tr("Solution Explorer"), self.dockPanelManager())
        self.panelSolutionExplorer = DockWidgetPanel(self.tr("Solution Explorer"), self.dockPanelManager())
        self.dockPanelManager().insertDockPanel(self.panelSolutionExplorer, Qtitan.LeftDockPanelArea)
        self.connect(self.solutionExplorerAction, QtCore.SIGNAL("triggered(bool)"), self.panelSolutionExplorer.visibleAction(), QtCore.SIGNAL("triggered(bool)"))
        self.connect(self.panelSolutionExplorer.visibleAction(), QtCore.SIGNAL("toggled(bool)"), self.solutionExplorerAction, QtCore.SLOT("setChecked(bool)"))

        #self.panelOutput = OutputListPanel(self.tr("Output"), self.dockPanelManager())
        self.panelOutput = DockWidgetPanel(self.tr("Output"), self.dockPanelManager())
        self.dockPanelManager().insertDockPanel(self.panelOutput, Qtitan.BottomDockPanelArea)
        self.panelOutput.setAutoHide(True)

        #self.panelWatch = WatchPanel(self.tr("Watch"), self.dockPanelManager())
        self.panelWatch = DockWidgetPanel(self.tr("Watch"), self.dockPanelManager())
        self.dockPanelManager().insertDockPanel(self.panelWatch, Qtitan.BottomDockPanelArea, self.panelOutput)
        self.panelWatch.setAutoHide(True)

        #self.panelToolBox = ToolBoxPanel(self.tr("ToolBox"), self.dockPanelManager())
        self.panelToolBox = DockWidgetPanel(self.tr("ToolBox"), self.dockPanelManager())
        self.dockPanelManager().insertDockPanel(self.panelToolBox, Qtitan.RightDockPanelArea)

    def createStatusBar(self):
        self.statusBar().showMessage(self.tr("Ready"))

    def updateActionStyles(self):
        strStyle = qApp.style().objectName()
        if strStyle == self.defaultStyle:
            self.defaultStyleAction.setChecked(True)
        elif strStyle == "Fusion":
            self.fusionStyleAction.setChecked(True)

    def openFile(self, fileName):
        panel = self.findMdiChild(fileName)
        if panel != None:
            panel.activate()
            return True

        succeeded = self.loadFile(fileName)
        if succeeded:
            self.statusBar().showMessage(self.tr("File loaded"), 2000)
        else:
            self.statusBar().showMessage(self.tr("Cant load file {0}").format(fileName), 2000)

        return succeeded

    def newFile(self):
        child = self.createMdiChild()
        child.newFile()
        documentPanel = self.dockPanelManager().addDocumentPanel(child.windowTitle())
        documentPanel.setWidget(child)
        self.connect(child, QtCore.SIGNAL("windowTitleChanged(const QString&)"), documentPanel, QtCore.SIGNAL("windowTitleChanged(const QString&)"))

    def open(self):
        fileName = QFileDialog.getOpenFileName(self, self.tr("Open File"), "", "C++ Files (*.cpp *.h)")[0]
        if not fileName:
            return
        self.openFile(fileName)

    def createMdiChild(self):
        child = MdiChild()
        child.setVisible(False)
        return child

    def changeStyle(self, action):
        if action == self.defaultStyleAction:
            qApp.setStyle(self.defaultStyle)
            qApp.style().setObjectName(self.defaultStyle)
        elif action == self.fusionStyleAction:
            qApp.setStyle("fusion")
        QApplication.setPalette(QApplication.style().standardPalette())
        self.updateActionStyles()

    def loadFile(self, fileName):
        child = self.createMdiChild()
        succeeded = child.loadFile(fileName)
        if not succeeded:
            child.close()

        documentPanel = self.dockPanelManager().addDocumentPanel(child.windowTitle())
        documentPanel.setWidget(child)
        self.connect(child, QtCore.SIGNAL("windowTitleChanged(const QString&)"), documentPanel, QtCore.SIGNAL("windowTitleChanged(const QString&)"))
        return succeeded

    def findMdiChild(self, fileName):
        lstPanel = self.findChildren(DockDocumentPanel)
        canonicalFilePath = QFileInfo(fileName).canonicalFilePath()
        for panel in lstPanel:
            widget = panel.widget()
            if widget.inherits("MdiChild"):
                mdiChild = widget
                if mdiChild.currentFileName == canonicalFilePath:
                    return panel
        return None

    def closeEvent(self, event):
        event.accept()
        self.writeSettings()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setApplicationName("PyQtitanDocking - Document Demo")
    app.setOrganizationName("Developer Machines")
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())