FileBrowser 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
from DevMachines.QtitanGrid import (getGridVersion, TreeGrid, GridColumn, GridEditor,
                                    ContextMenuEventArgs, EditorValidationEventArgs)

if __pyside2__:
    from PySide2 import QtCore
    from PySide2.QtCore import Qt
    from PySide2.QtWidgets import (QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton,
                                   QSlider, QLabel, QCheckBox, QComboBox, QMessageBox, QFileDialog, QFileSystemModel)
if __pyside6__:
    from PySide6 import QtCore
    from PySide6.QtCore import Qt
    from PySide6.QtWidgets import (QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton,
                                   QSlider, QLabel, QCheckBox, QComboBox, QMessageBox, QFileDialog, QFileSystemModel)

from DemoMainWindow import DemoMainWindow

class Window(DemoMainWindow):
    def __init__(self):
        DemoMainWindow.__init__(self, "QtitanDataGrid", getGridVersion())
        self.setWindowTitle(self.tr("Qtitan::TreeGrid File Browser"))
        self.setGeometry(150, 150, 1000, 800)

        TreeGrid.loadTranslation()
        self.tree = TreeGrid(self)

        # Configure grid view
        self.tree.setViewType(TreeGrid.BandedTreeView)
        view = self.tree.view()
        view.beginUpdate()
        view.options().setGestureEnabled(True)
        catalogBand = view.addBand("Catalog")
        detailsBand = view.addBand("Details")

        # Connect Grid's context menu handler.
        self.connect(view, QtCore.SIGNAL("contextMenu(ContextMenuEventArgs*)"), self, QtCore.SLOT("contextMenu(ContextMenuEventArgs*)"))
        self.connect(view, QtCore.SIGNAL("focusRowChanged(int,int)"), self, QtCore.SLOT("focusRowChanged(int,int)"))

        fileSystem = QFileSystemModel()
        fileSystem.setRootPath("C:/");
        view.setModel(fileSystem)

        column = view.getColumn(0)
        column.setBandIndex(catalogBand.index())
        column.setMenuButtonVisible(True)
        column.setFixedKind(Qtitan.AtBeginning)

        column = view.getColumn(1)
        column.setBandIndex(detailsBand.index())
        column = view.getColumn(2)
        column.setBandIndex(detailsBand.index())
        column = view.getColumn(3)
        column.setBandIndex(detailsBand.index())

        view.expand(fileSystem.index(0, 0)) #Expand root node.
        view.endUpdate();

        self.setDemoWidget(self.tree, self.createSettingsWidget())

        view.bestFit(Qtitan.FitToHeaderAndContent)

    def createSettingsWidget(self):
        # Create settings widget
        settings = QWidget(self)
        l = QVBoxLayout(settings)
        autoWidthCheck = QCheckBox(settings)
        autoWidthCheck.setText("Column auto width")
        self.connect(autoWidthCheck, QtCore.SIGNAL("stateChanged(int)"), self, QtCore.SLOT("autoWidthStateChanged(int)"))
        l.addWidget(autoWidthCheck)
        autoWidthCheck.setChecked(True)

        fastScrollCheck = QCheckBox(settings)
        fastScrollCheck.setText("Fast scroll effect")
        self.connect(fastScrollCheck, QtCore.SIGNAL("stateChanged(int)"), self, QtCore.SLOT("fastScrollChanged(int)"))
        l.addWidget(fastScrollCheck)
        fastScrollCheck.setChecked(True)

        dottedLineCheck = QCheckBox(settings)
        dottedLineCheck.setText("Dotted grid line")
        self.connect(dottedLineCheck, QtCore.SIGNAL("stateChanged(int)"), self, QtCore.SLOT("dottedLineChanged(int)"))
        l.addWidget(dottedLineCheck)
        dottedLineCheck.setChecked(True)

        label = QLabel(self)
        hl = QHBoxLayout()
        label.setText("Grid line style:")
        lineStylesSelect = QComboBox(settings)
        lineStylesSelect.addItem("None")
        lineStylesSelect.addItem("Both")
        lineStylesSelect.addItem("Both2D")
        lineStylesSelect.addItem("Horizontal")
        lineStylesSelect.addItem("Horizontal2D")
        lineStylesSelect.addItem("Vertical")
        lineStylesSelect.addItem("Vertical2D")
        self.connect(lineStylesSelect, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("selectGridLineStyles(int)"))

        l2 = QHBoxLayout()
        button1 = QPushButton(settings)
        button1.setText(self.tr("Save Layout"))
        self.connect(button1, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("saveLayout()"))
        l2.addWidget(button1)
        button2 = QPushButton(settings)
        button2.setText(self.tr("Load Layout"))
        self.connect(button2, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("loadLayout()"))
        l2.addWidget(button2)

        hl.addWidget(label)
        hl.addWidget(lineStylesSelect)
        l.addLayout(hl)
        l.addLayout(l2)
        lineStylesSelect.setCurrentIndex(2)

        zoomEnable = QCheckBox(settings)
        zoomEnable.setText(self.tr("Zoom enabled"))
        zoomEnable.setChecked(True)
        self.connect(zoomEnable, QtCore.SIGNAL("stateChanged(int)"), self, QtCore.SLOT("zoomEnabledChanged(int)"))
        l.addWidget(zoomEnable)

        zoomIndicator = QCheckBox(settings)
        zoomIndicator.setText(self.tr("Show zoom indicator"))
        zoomIndicator.setChecked(True)
        self.connect(zoomIndicator, QtCore.SIGNAL("stateChanged(int)"), self, QtCore.SLOT("zoomIndicatorChanged(int)"))
        l.addWidget(zoomIndicator)

        zoomSlider = QSlider(settings)
        zoomSlider.setOrientation(Qt.Horizontal)
        zoomSlider.setTickPosition(QSlider.TicksBothSides)
        zoomSlider.setMinimum(25)
        zoomSlider.setMaximum(300)
        zoomSlider.setTickInterval(25)
        zoomSlider.setSingleStep(25)
        zoomSlider.setValue(100)
        self.connect(zoomSlider, QtCore.SIGNAL("sliderMoved(int)"), self, QtCore.SLOT("zoomValueChanged(int)"))
        self.connect(self.tree.view(), QtCore.SIGNAL("zoomChanged(int)"), zoomSlider, QtCore.SLOT("setValue(int)"))
        l.addWidget(zoomSlider)

        cellAutoRaise = QCheckBox(settings)
        cellAutoRaise.setText(self.tr("Auto raise cell button"))
        self.connect(cellAutoRaise, QtCore.SIGNAL("stateChanged(int)"), self, QtCore.SLOT("cellButtonAutoRaiseEnabled(int)"))
        cellAutoRaise.setChecked(True)
        l.addWidget(cellAutoRaise)

        frozenRowsBox = QCheckBox(settings)
        frozenRowsBox.setText(self.tr("Frozen Rows"))
        self.connect(frozenRowsBox, QtCore.SIGNAL("stateChanged(int)"), self, QtCore.SLOT("frozenRowsEnabled(int)"))
        frozenRowsBox.setChecked(True)
        l.addWidget(frozenRowsBox)

        transparentBox = QCheckBox(settings)
        transparentBox.setText(self.tr("Transparent Background"))
        self.connect(transparentBox, QtCore.SIGNAL("stateChanged(int)"), self, QtCore.SLOT("transparentBackgroundEnabled(int)"))
        transparentBox.setChecked(False)
        l.addWidget(transparentBox)

        printButton = QPushButton(settings)
        printButton.setText(self.tr("Print Preview"))
        self.connect(printButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("printPreview()"))
        l.addWidget(printButton)

        return settings

    @QtCore.Slot(int)
    def autoWidthStateChanged(self, state):
        view = self.tree.view()
        view.tableOptions().setColumnAutoWidth(Qt.CheckState(state) == Qt.Checked)

    @QtCore.Slot(int)
    def fastScrollChanged(self, state):
        view = self.tree.view()
        view.options().setFastScrollEffect(Qt.CheckState(state) == Qt.Checked)

    @QtCore.Slot(int)
    def dottedLineChanged(self, state):
        view = self.tree.view()
        pen = view.options().gridLinePen()
        if Qt.CheckState(state) == Qt.Checked:
            pen.setStyle(Qt.DotLine)
        else:
            pen.setStyle(Qt.SolidLine)
        view.options().setGridLinePen(pen)

    @QtCore.Slot(int)
    def selectGridLineStyles(self, index):
        view = self.tree.view()
        if index == 0:
            view.options().setGridLines(Qtitan.LinesNone)
        elif index == 1:
            view.options().setGridLines(Qtitan.LinesBoth)
        elif index == 2:
            view.options().setGridLines(Qtitan.LinesBoth2D)
        elif index == 3:
            view.options().setGridLines(Qtitan.LinesHorizontal)
        elif index == 4:
            view.options().setGridLines(Qtitan.LinesHorizontal2D)
        elif index == 5:
            view.options().setGridLines(Qtitan.LinesVertical)
        elif index == 6:
            view.options().setGridLines(Qtitan.LinesVertical2D)
        else:
            view.options().setGridLines(Qtitan.LinesBoth)

    @QtCore.Slot()
    def saveLayout(self):
        fileName = QFileDialog.getSaveFileName(self, self.tr("Save QtitanDataGrid Layout to File"),
            "qtitan-layout.xml", self.tr("Layout-XML (*.xml)"))[0]
        if fileName == "":
            return
        self.tree.saveLayoutToFile(fileName)

    @QtCore.Slot()
    def loadLayout(self):
        fileName = QFileDialog.getOpenFileName(self, self.tr("Open QtitanDataGrid Layout File"), "",
                                               self.tr("Layout-XML (*.xml)"))[0]
        if not fileName:
            return
        self.tree.loadLayoutFromFile(fileName)

    @QtCore.Slot(int)
    def zoomEnabledChanged(self, state):
        view = self.tree.view()
        view.options().setZoomEnabled(Qt.CheckState(state) == Qt.Checked)

    @QtCore.Slot(int)
    def zoomIndicatorChanged(self, state):
        view = self.tree.view()
        view.options().setZoomIndicatorActive(Qt.CheckState(state) == Qt.Checked)

    @QtCore.Slot(int)
    def zoomValueChanged(self, value):
        factor = (float(value) / 25) * 25
        view = self.tree.view()
        view.options().setZoomFactor(factor / 100)

    @QtCore.Slot(int)
    def focusRowChanged(self, oldRowIndex, rowIndex):
        #oldRowIndex;
        #rowIndex;
        print("oldRowIndex: " + str(oldRowIndex) + ", rowIndex: " + str(rowIndex))

    @QtCore.Slot(int)
    def cellButtonAutoRaiseEnabled(self, state):
        view = self.tree.view()
        view.options().setCellButtonAutoRaise(Qt.CheckState(state) == Qt.Checked)

    @QtCore.Slot(int)
    def frozenRowsEnabled(self, state):
        view = self.tree.view()
        view.tableOptions().setRowFrozenButtonVisible(Qt.CheckState(state) == Qt.Checked)
        view.tableOptions().setFrozenPlaceQuickSelection(Qt.CheckState(state) == Qt.Checked)

    @QtCore.Slot(int)
    def transparentBackgroundEnabled(self, state):
        view = self.tree.view()
        view.options().setTransparentBackground(Qt.CheckState(state) == Qt.Checked)
        view.options().setAlternatingRowColors(not view.options().alternatingRowColors())

    @QtCore.Slot(ContextMenuEventArgs)
    def contextMenu(self, args):
        args.contextMenu().addAction("Print Preview", self, QtCore.SLOT("printPreview()"))
        args.contextMenu().addSeparator()
        args.contextMenu().addAction("Developer Machines on the Web", self, QtCore.SLOT("showCompanyWebSite()"))

    @QtCore.Slot()
    def printPreview(self):
        self.tree.view().printPreview()

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

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