Ejemplo n.º 1
0
def load_state(window: QMainWindow, settings: QSettings):
    settings.beginGroup(window.objectName())
    geometry = settings.value("geometry")
    state = settings.value("windowState")
    if geometry is not None:
        window.restoreGeometry(geometry)
    if state is not None:
        window.restoreState(state)
    settings.endGroup()

    ## store geometry of all widgets
    widgets = window.findChildren(QtWidgets.QWidget)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        geometry = settings.value("geometry")
        if geometry is not None:
            w.restoreGeometry(geometry)
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QSplitter)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        state = settings.value("widgetState")
        if state is not None:
            w.restoreState(state)
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QCheckBox)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        state = settings.value("checkState")
        if state is not None:
            w.setCheckState(int(state))
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QSpinBox)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        state = settings.value("value")
        if state is not None:
            w.setValue(int(state))
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QTabWidget)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        state = settings.value("currentIndex")
        if state is not None:
            currIndex = int(state)
            w.setCurrentIndex(currIndex)
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QTableView)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        colsNum = 0
        wmodel = w.model()
        if wmodel is not None:
            colsNum = wmodel.columnCount()
        for c in range(0, colsNum):
            state = settings.value("column" + str(c))
            if state is not None:
                currWidth = int(state)
                w.setColumnWidth(c, currWidth)
        sortColumn = settings.value("sortColumn")
        sortOrder = settings.value("sortOrder")
        if sortColumn is not None and sortOrder is not None:
            w.sortByColumn(int(sortColumn), int(sortOrder))
        stretchLast = settings.value("stretchLast")
        if stretchLast is not None:
            stretchLastValue = bool(stretchLast)
            header = w.horizontalHeader()
            header.setStretchLastSection(stretchLastValue)
            if stretchLastValue:
                colsNum = header.count()
                w.resizeColumnToContents(colsNum - 1)
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QTableWidget)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        colsNum = w.columnCount()
        for c in range(0, colsNum):
            state = settings.value("column" + str(c))
            if state is not None:
                currWidth = int(state)
                w.setColumnWidth(c, currWidth)
        sortColumn = settings.value("sortColumn")
        sortOrder = settings.value("sortOrder")
        if sortColumn is not None and sortOrder is not None:
            w.sortByColumn(int(sortColumn), int(sortOrder))
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QTreeView)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        colsNum = w.header().count()
        for c in range(0, colsNum):
            state = settings.value("column" + str(c))
            if state is not None:
                currWidth = int(state)
                w.setColumnWidth(c, currWidth)
        sortColumn = settings.value("sortColumn")
        sortOrder = settings.value("sortOrder")
        if sortColumn is not None and sortOrder is not None:
            w.sortByColumn(int(sortColumn), int(sortOrder))
        settings.endGroup()
Ejemplo n.º 2
0
def set_workspace(window: QMainWindow, file: str):
    tree = ET.parse(file)
    root = tree.getroot()

    if root.tag == "Workspace":
        # Hide all dock widgets
        for dock in window.findChildren(QDockWidget):
            dock.hide()

        for child in root.iter("Dock"):
            dock_name = child.attrib["name"]
            dock = window.findChild(QDockWidget, dock_name)
            if dock:
                # Change where the area which the dock widget is placed
                try:
                    dock_area = child.attrib["area"]
                    if dock_area:
                        try:
                            window.removeDockWidget(dock)
                        except:
                            pass

                        if dock_area == "Left":
                            window.addDockWidget(Qt.LeftDockWidgetArea, dock)
                        elif dock_area == "Right":
                            window.addDockWidget(Qt.RightDockWidgetArea, dock)
                        elif dock_area == "Top":
                            window.addDockWidget(Qt.TopDockWidgetArea, dock)
                        elif dock_area == "Bottom":
                            window.addDockWidget(Qt.BottomDockWidgetArea, dock)
                        else:
                            window.addDockWidget(Qt.LeftDockWidgetArea, dock)
                except KeyError:
                    pass

                # Show or hide the dock widget
                try:
                    dock_show = child.attrib["show"]
                    if dock_show == "True":
                        dock.show()
                    else:
                        dock.hide()
                except KeyError:
                    pass

        for child in root.iter("SplitDocks"):
            dock1_name = child.attrib["dock1"]
            dock2_name = child.attrib["dock2"]
            dock1 = window.findChild(QDockWidget, dock1_name)
            dock2 = window.findChild(QDockWidget, dock2_name)
            split_orientation = child.attrib["orientation"]
            if split_orientation == "Horizontal":
                window.splitDockWidget(dock1, dock2, Qt.Horizontal)
            elif split_orientation == "Vertical":
                window.splitDockWidget(dock1, dock2, Qt.Vertical)
            else:
                pass

        for child in root.iter("TabbifyDocks"):
            dock1_name = child.attrib["dock1"]
            dock2_name = child.attrib["dock2"]
            dock1 = window.findChild(QDockWidget, dock1_name)
            dock2 = window.findChild(QDockWidget, dock2_name)
            window.tabifyDockWidget(dock1, dock2)

        # Focus the dock widget
        for child in root.iter("Dock"):
            dock_name = child.attrib["name"]
            dock = window.findChild(QDockWidget, dock_name)
            try:
                dock_focus = child.attrib["focus"]
                if dock_focus == "True":
                    dock.raise_()
            except KeyError:
                pass
Ejemplo n.º 3
0
def save_state(window: QMainWindow, settings: QSettings):
    settings.beginGroup(window.objectName())
    settings.setValue("geometry", window.saveGeometry())
    settings.setValue("windowState", window.saveState())
    settings.endGroup()

    ## store geometry of all widgets
    widgets = window.findChildren(QtWidgets.QWidget)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        settings.setValue("geometry", w.saveGeometry())
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QSplitter)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        settings.setValue("widgetState", w.saveState())
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QCheckBox)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        settings.setValue("checkState", w.checkState())
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QSpinBox)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        settings.setValue("value", w.value())
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QTabWidget)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        settings.setValue("currentIndex", w.currentIndex())
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QTableView)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        colsNum = w.model().columnCount()
        for c in range(0, colsNum):
            settings.setValue("column" + str(c), w.columnWidth(c))
        header = w.horizontalHeader()
        sortColumn = header.sortIndicatorSection()
        settings.setValue("sortColumn", sortColumn)
        sortOrder = header.sortIndicatorOrder()
        settings.setValue("sortOrder", sortOrder)
        stretchLast = header.stretchLastSection()
        settings.setValue("stretchLast", stretchLast)
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QTableWidget)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        colsNum = w.columnCount()
        for c in range(0, colsNum):
            settings.setValue("column" + str(c), w.columnWidth(c))
        header = w.horizontalHeader()
        sortColumn = header.sortIndicatorSection()
        settings.setValue("sortColumn", sortColumn)
        sortOrder = header.sortIndicatorOrder()
        settings.setValue("sortOrder", sortOrder)
        settings.endGroup()

    widgets = window.findChildren(QtWidgets.QTreeView)
    widgetsList = sort_widgets(widgets)
    for w, wKey in widgetsList:
        settings.beginGroup(wKey)
        header = w.header()
        colsNum = header.count()
        for c in range(0, colsNum):
            settings.setValue("column" + str(c), w.columnWidth(c))
        sortColumn = header.sortIndicatorSection()
        settings.setValue("sortColumn", sortColumn)
        sortOrder = header.sortIndicatorOrder()
        settings.setValue("sortOrder", sortOrder)
        settings.endGroup()