DragDropFeature 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, Grid,
                                    GridColumn, GridEditor,
                                    GridViewOptions,
                                    ContextMenuEventArgs, CellButtonClickEventArgs)

if __pyside2__:
    from PySide2 import QtCore
    from PySide2.QtCore import Qt
    from PySide2.QtGui import QStandardItemModel, QStandardItem
    from PySide2.QtWidgets import (QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton,
                                   QSlider, QLabel, QCheckBox, QComboBox, QMessageBox,
                                   QAbstractItemView, QTreeView, QSplitter)

if __pyside6__:
    from PySide6 import QtCore
    from PySide6.QtCore import Qt
    from PySide6.QtGui import QStandardItemModel, QStandardItem
    from PySide6.QtWidgets import (QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton,
                                   QSlider, QLabel, QCheckBox, QComboBox, QMessageBox,
                                   QAbstractItemView, QTreeView, QSplitter)

from DemoMainWindow import DemoMainWindow

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

        self.setWindowTitle("Drag & Drop Demo")
        self.setGeometry(200, 250, 1200, 800)

        Grid.loadTranslation()

        self.grid1 = Grid()
        self.grid1.setViewType(Grid.TableView)
        view1 = self.grid1.view()
        view1.options().setScrollRowStyle(Qtitan.ScrollByPixel)
        # Connect Grid's context menu handler.
        self.connect(view1, QtCore.SIGNAL("contextMenu(ContextMenuEventArgs*)"), self,
            QtCore.SLOT("contextMenu(ContextMenuEventArgs* )"))

        model1 =  QStandardItemModel(4, 4)
        for row in range(0, 40):
            for column in range(0, 4):
                item = QStandardItem("row " + str(row) + ", column " + str(column))
                model1.setItem(row, column, item)

        view1.setModel(model1)
        view1.getColumn(0).setMenuButtonVisible(True)
        view1.getColumn(1).setMenuButtonVisible(True)

        self.grid2 = Grid()
        self.grid2.setViewType(Grid.TableView)
        view2 = self.grid2.view()

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

        model2 =  QStandardItemModel(4, 4)
        for row in range(0, 40):
            for column in range(0, 4):
                item = QStandardItem("row " + str(row) + ", column " + str(column))
                model2.setItem(row, column, item)

        view2.setModel(model2)

        self.grid3 = QTreeView()
        model3 = QStandardItemModel(4, 4)
        for row in range(0, 40):
            for column in range(0, 4):
                item = QStandardItem("row " + str(row) + ", column " + str(column))
                model3.setItem(row, column, item)

        self.grid3.setModel(model3)
        self.grid3.setDropIndicatorShown(True)
        self.grid3.setDragDropOverwriteMode(False)

        s1 = QSplitter(Qt.Horizontal, self)
        s2 = QSplitter(Qt.Vertical, self)
        s1.addWidget(self.grid1)
        s1.addWidget(self.grid2)
        s2.addWidget(s1)
        s2.addWidget(self.grid3)

        # Create settings widget
        settings = QWidget(self)
        l = QVBoxLayout(settings)

        hl = QHBoxLayout()
        label = QLabel(self)
        label.setText("Multi-select mode:")
        policyBox = QComboBox(self)
        policyBox.addItem("Single Row")
        policyBox.addItem("Single Cell")
        policyBox.addItem("Multi Row")
        policyBox.addItem("Multi Row (Using RubberBand)")
        policyBox.addItem("Multi Cell")
        policyBox.addItem("Multi Cell (Using RubberBand)")
        self.connect(policyBox, QtCore.SIGNAL("currentIndexChanged(int)"), self,
            QtCore.SLOT("newPolicyActivated(int)"))
        hl.addWidget(label)
        hl.addWidget(policyBox)
        l.addLayout(hl)
        policyBox.setCurrentIndex(3)

        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(False)

        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)"))
        hl.addWidget(label)
        hl.addWidget(lineStylesSelect)
        l.addLayout(hl)
        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.grid1.view(), QtCore.SIGNAL("zoomChanged(int)"), zoomSlider, QtCore.SLOT("setValue(int)"))
        self.connect(self.grid2.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)

        moveCheckBox = QCheckBox(settings)
        moveCheckBox.setText(self.tr("Internal Move (new)"))
        self.connect(moveCheckBox, QtCore.SIGNAL("stateChanged(int)"), self,
            QtCore.SLOT("moveCheckBoxEnabled(int)"))
        moveCheckBox.setChecked(False)
        l.addWidget(moveCheckBox)
        self.moveCheckBoxEnabled(Qt.Unchecked)

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

        self.setDemoWidget(s2, settings)

    @QtCore.Slot(int)
    def newPolicyActivated(self, index):
        if self.grid1 == None or self.grid2 == None:
            return

        view1 = self.grid1.view()
        view2 = self.grid2.view()

        if index == 0:
            view1.options().setSelectionPolicy(GridViewOptions.RowSelection)
            view2.options().setSelectionPolicy(GridViewOptions.RowSelection)
        elif index == 1:
            view1.options().setSelectionPolicy(GridViewOptions.CellSelection)
            view2.options().setSelectionPolicy(GridViewOptions.CellSelection)
        elif index == 2:
            view1.options().setSelectionPolicy(GridViewOptions.MultiRowSelection)
            view1.options().setRubberBandSelection(False)
            view2.options().setSelectionPolicy(GridViewOptions.MultiRowSelection)
            view2.options().setRubberBandSelection(False)
        elif index == 3:
            view1.options().setSelectionPolicy(GridViewOptions.MultiRowSelection)
            view1.options().setRubberBandSelection(True)
            view2.options().setSelectionPolicy(GridViewOptions.MultiRowSelection)
            view2.options().setRubberBandSelection(True)
        elif index == 4:
            view1.options().setSelectionPolicy(GridViewOptions.MultiCellSelection)
            view1.options().setRubberBandSelection(False)
            view2.options().setSelectionPolicy(GridViewOptions.MultiCellSelection)
            view2.options().setRubberBandSelection(False)
        elif index == 5:
            view1.options().setSelectionPolicy(GridViewOptions.MultiCellSelection)
            view1.options().setRubberBandSelection(True)
            view2.options().setSelectionPolicy(GridViewOptions.MultiCellSelection)
            view2.options().setRubberBandSelection(True)
        else:
            view1.options().setSelectionPolicy(GridViewOptions.RowSelection)
            view2.options().setSelectionPolicy(GridViewOptions.RowSelection)

    @QtCore.Slot(int)
    def fastScrollChanged(self, state):
        if self.grid1 == None or self.grid2 == None:
            return
        view1 = self.grid1.view()
        view2 = self.grid2.view()
        view1.options().setFastScrollEffect(Qt.CheckState(state) == Qt.Checked)
        view2.options().setFastScrollEffect(Qt.CheckState(state) == Qt.Checked)

    @QtCore.Slot(int)
    def dottedLineChanged(self, state):
        if self.grid1 == None or self.grid2 == None:
            return
        view1 = self.grid1.view()
        view2 = self.grid2.view()
        pen = view1.options().gridLinePen()
        if Qt.CheckState(state) == Qt.Checked:
            pen.setStyle(Qt.DotLine)
        else:
            pen.setStyle(Qt.SolidLine)
        view1.options().setGridLinePen(pen)
        view2.options().setGridLinePen(pen)

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

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

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

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

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

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

    @QtCore.Slot(int)
    def moveCheckBoxEnabled(self, state):
        view1 = self.grid1.view()
        view2 = self.grid2.view()
        if Qt.CheckState(state) == Qt.Checked:
            view1.options().setDragDropMode(QAbstractItemView.InternalMove)
            view2.options().setDragDropMode(QAbstractItemView.InternalMove)
            self.grid3.setDragDropMode(QAbstractItemView.InternalMove)
        else:
            view1.options().setDragDropMode(QAbstractItemView.DragDrop)
            view2.options().setDragDropMode(QAbstractItemView.DragOnly)
            self.grid3.setDragDropMode(QAbstractItemView.DragDrop)

    @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(CellButtonClickEventArgs)
    def cellButtonClicked(self, args):
        QMessageBox.information(self, "Cell button clicked",
            "Clicked: Button - " + str(args.buttonIndex()) + ", Column Title - " + args.column().caption() + ", RowIndex - " + str(args.row().rowIndex()))

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

    def setShadeColor(self, color):
        self.grid1.themeManager().setShadeColor(color)
        self.grid2.themeManager().setShadeColor(color)

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