MDIApplication Example

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

from PySide2 import QtCore
from PySide2.QtCore import Qt, QDate, QTime, QAbstractItemModel, QModelIndex, SIGNAL, SLOT
from PySide2.QtGui import QPixmap, QIcon
from PySide2.QtWidgets import (QWidget, QApplication, QMdiArea, QVBoxLayout, QHBoxLayout, QPushButton,
                               QSlider, QLabel, QCheckBox, QComboBox, QMessageBox)

from DevMachines import QtitanBase
from DevMachines.QtitanBase import Qtitan
from DevMachines.QtitanGrid import (getGridVersion, Grid, GridViewOptions, GridColumn, GridEditor)

from DemoMainWindow import DemoMainWindow

class DataItem:
    v0 = None
    v1 = None
    v2 = None
    v3 = None

class CustomDataModel(QAbstractItemModel):
    def __init__(self, parent):
        QAbstractItemModel.__init__(self, parent)

        self.values = list(range(0, 100))
        y = 2012
        m = 1
        d = 1
        for i in self.values:
            item = DataItem()
            item.v0 = i
            item.v1 = "String: " + str(i)
            item.v3 = QTime(12, 0, 0)

            if d > 28:
                d = 1
                m = m + 1
                if m > 12:
                    m = 1
                    y = y + 1

            item.v2 = QDate(y, m, d)
            if not ((i + 1) % 10):
                d = d + 1
            self.values[i] = item

    def headerData(self, section, orientation, role):
        if section == 0:
            return "Integer"
        elif section == 1:
            return "String"
        elif section == 2:
            return "Date"
        elif section == 3:
            return "Time"
        return None

    def parent(self, child):
        return QModelIndex()

    def hasChildren(self, parent):
        if parent.model() == self or not parent.isValid():
            return self.rowCount(parent) > 0 and self.columnCount(parent) > 0
        return False

    def rowCount(self, parent):
        if parent.isValid():
            return 0
        return len(self.values)

    def columnCount(self, parent):
        if parent.isValid():
            return 0
        return 4

    def index(self, row, column, parent):
        if parent.isValid():
            return QModelIndex()

        if row < 0 or row >= self.rowCount(parent):
            return QModelIndex()

        if column < 0 or column >= self.columnCount(parent):
            return QModelIndex()

        return self.createIndex(row, column, parent)

    def data(self, index, role):
        if not index.isValid():
            return None

        if index.row() < 0 or index.row() >= self.rowCount(index.parent()):
            return None

        if index.column() < 0 or index.column() >= self.columnCount(index.parent()):
            return None

        if role == Qt.DisplayRole or role == Qt.EditRole:
            if index.column() == 0:
                return self.values[index.row()].v0
            elif index.column() == 1:
                return self.values[index.row()].v1
            elif index.column() == 2:
                return self.values[index.row()].v2
            elif index.column() == 3:
                return self.values[index.row()].v3
        elif role == Qt.CheckStateRole:
            if index.column() == 0:
                return self.values[index.row()].v0
        return None

    def setData(self, index, value, role):
        if not index.isValid():
            return False

        if index.row() < 0 or index.row() >= self.rowCount(index.parent()):
            return False

        if index.column() < 0 or index.column() >= self.columnCount(index.parent()):
            return False

        if role != Qt.EditRole:
            return False

        if index.column() == 0:
            self.values[index.row()].v0 = int(value)
        elif index.column() == 1:
            self.values[index.row()].v1 = str(value)
        elif index.column() == 2:
            self.values[index.row()].v2 = value
        elif index.column() == 3:
            self.values[index.row()].v3 = value

        self.dataChanged.emit(index, index)
        return True

    def flags(self, index):
        if not index.isValid():
            return Qt.ItemFlags()
        return Qt.ItemIsEnabled | Qt.ItemIsEditable

class Window(DemoMainWindow):
    def __init__(self):
        DemoMainWindow.__init__(self, "QtitanDataGrid", getGridVersion())

        self.setWindowTitle("QtitanDataGrid - MDI Application")
        self.setGeometry(50, 50, 800, 500)

        self.mdiArea = QMdiArea(self)
        self.mdiArea.setViewMode(QMdiArea.TabbedView)

        self.mdiArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.mdiArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)

        self.setDemoWidget(self.mdiArea, self.createSettingsWidget())

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

    def createSettingsWidget(self):
        settings = QWidget(self)
        l = QVBoxLayout(settings)

        hl = QHBoxLayout()
        label = QLabel(settings)
        label.setText("MDI view mode:")
        mdiViewModeComboBox = QComboBox(settings)
        mdiViewModeComboBox.addItem("TabbedView")
        mdiViewModeComboBox.addItem("SubWindowView")
        self.connect(mdiViewModeComboBox, SIGNAL("currentIndexChanged(int)"), self, SLOT("setMDIViewMode(int)"))
        hl.addWidget(label)
        hl.addWidget(mdiViewModeComboBox)
        l.addLayout(hl)
        mdiViewModeComboBox.setCurrentIndex(0)

        addGridButton = QPushButton(settings)
        addGridButton.setText(self.tr("Add grid"))
        self.connect(addGridButton, SIGNAL("clicked()"), self, SLOT("addGrid()"))
        l.addWidget(addGridButton)

        removeGridButton = QPushButton(settings)
        removeGridButton.setText(self.tr("Remove grid"))
        self.connect(removeGridButton, SIGNAL("clicked()"), self, SLOT("removeGrid()"))
        l.addWidget(removeGridButton)
        return settings

    def setShadeColor(self, color):
        Grid.themeManager().setShadeColor(color)

    @QtCore.Slot()
    def addGrid(self):
        grid = Grid()

        # Configure grid view
        grid.setViewType(Grid.TableView)
        view = grid.view()
        view.options().setGridLineWidth(0)
        view.options().setSelectionPolicy(GridViewOptions.MultiRowSelection)
        view.tableOptions().setColumnAutoWidth(True)
        view.tableOptions().setRowsQuickSelection(True)

        model = CustomDataModel(grid)
        view.setModel(model)
        view.setFocusedRowIndex(0)
        view.setFocusedColumnIndex(0)
        view.deselectAll()

        title = "Window " + str(len(self.mdiArea.subWindowList()))
        subWindow = self.mdiArea.addSubWindow(grid)
        subWindow.setWindowTitle(title)
        subWindow.show()

    @QtCore.Slot()
    def removeGrid(self):
        windowList = self.mdiArea.subWindowList()
        if len(windowList) == 0:
            return
        subWindow = windowList[-1] # Last element in list
        subWindow.close()
        subWindow.deleteLater()

    def setMDIViewMode(self, index):
        if index == 0:
            self.mdiArea.setViewMode(QMdiArea.TabbedView)
        else:
            self.mdiArea.setViewMode(QMdiArea.SubWindowView)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())