CardViewVertical Example

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

from PySide2 import QtCore
from PySide2.QtCore import Qt, SIGNAL, SLOT, QFileInfo
from PySide2.QtGui import QPixmap, QIcon
from PySide2.QtWidgets import (QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton,
                               QSlider, QLabel, QCheckBox, QComboBox, QMessageBox, QFileDialog)
from PySide2.QtSql import QSqlDatabase, QSqlError, QSqlTableModel

from DevMachines import QtitanBase
from DevMachines.QtitanBase import Qtitan
from DevMachines.QtitanGrid import (getGridVersion, CardGrid, GridColumn, GridEditor,
                                    CellButtonClickEventArgs, CellButtonEventArgs, ContextMenuEventArgs,
                                    EditorValidationEventArgs)
from DevMachines.QtitanFastInfoset import getFastInfosetVersion, FastInfosetStreamWriter, FastInfosetStreamReader

from DemoMainWindow import DemoMainWindow

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

        self.setWindowTitle(self.tr("QtitanDataGrid - CardViewVertical view"))
        self.setGeometry(150, 150, 1200, 700)

        CardGrid.loadTranslation()

        self.grid = CardGrid(self)

        prefix = os.path.dirname(os.path.realpath(__file__))
        prefix += "/../../SQLFiles/assets"

        db = QSqlDatabase.addDatabase("QSQLITE", "database_demo")
        db.setDatabaseName(prefix + "/database_yachts.sqlite")
        db.setHostName("")
        db.setPort(-1)
        if not db.open("", ""):
            err = db.lastError()
            QSqlDatabase.removeDatabase("database_demo")
            QMessageBox.critical(self, "Demo Error", "Error: Can't open database " +  db.databaseName() + " , error - " + err.text())
            QApplication.exit(1)
            return

        model = QSqlTableModel(self.grid, db)
        model.setTable("data")
        model.select()
        if model.lastError().type() != QSqlError.NoError:
            QMessageBox.critical(0, "Demo Error", "Error: SQL data base is not valid.")
            QApplication.exit(1)
            return

        #  Configure grid view
        self.grid.setViewType(CardGrid.CardViewVertical)
        view = self.grid.view()
        view.options().setScrollRowStyle(Qtitan.ScrollByPixel)
        view.options().setCellWidth(250)

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

        view.setModel(model)

        column = view.getColumnByModelColumnName("Photo")
        column.setRowSpan(5)
        column.setEditorType(GridEditor.Picture)

        column = view.getColumnByModelColumnName("Description")
        column.setEditorType(GridEditor.Memo)
        column.setRowSpan(4)
        # Add cell button to the column.
        column.addButton(GridColumn.ChoiceButtonIcon, Qtitan.AtEnd)
        self.connect(column, SIGNAL("buttonClicked(CellButtonClickEventArgs*)"), self,
            SLOT("cellButtonClicked(CellButtonClickEventArgs*)"))

        column = view.getColumnByModelColumnName("Built Year")
        column.setEditorType(GridEditor.Date)
        column.editorRepository().setDisplayFormat("yyyy")

        column = view.getColumnByModelColumnName("id")
        view.removeColumn(column.index())

       # Show button menu for all column headers.
        for i in range(0, view.getColumnCount()):
            view.getColumn(i).setMenuButtonVisible(True)

        self.setDemoWidget(self.grid, self.createSettingsWidget())

        column = view.getColumnByModelColumnName("Registry")
        column.setGroupIndex(0)

        view.bestFit()
        view.expandAll()

    def createSettingsWidget(self):
        # Create settings widget
        settings = QWidget(self)
        l = QVBoxLayout(settings)

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

        dottedLineCheck = QCheckBox(settings)
        dottedLineCheck.setText("Dotted grid line")
        self.connect(dottedLineCheck, SIGNAL("stateChanged(int)"), self,
            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, SIGNAL("currentIndexChanged(int)"), self,
            SLOT("selectGridLineStyles(int)"))
        hl.addWidget(label)
        hl.addWidget(lineStylesSelect)
        l.addLayout(hl)
        lineStylesSelect.setCurrentIndex(2)

        alignLabel = QLabel(self)
        alignLayout = QHBoxLayout()
        alignLabel.setText("Items alignement:")
        alignSelect = QComboBox(settings)

        alignSelect.addItem("Top")
        alignSelect.addItem("Center")
        alignSelect.addItem("Bottom")
        self.connect(alignSelect, SIGNAL("currentIndexChanged(int)"), self,
        SLOT("alignSelectChanged(int)"))
        alignLayout.addWidget(alignLabel)
        alignLayout.addWidget(alignSelect)
        l.addLayout(alignLayout)
        alignSelect.setCurrentIndex(0)

        l2 = QHBoxLayout()
        button1 = QPushButton(settings)
        button1.setText(self.tr("Save to XML/FastInfoset"))
        self.connect(button1, SIGNAL("clicked()"), self, SLOT("saveLayout()"))
        l2.addWidget(button1)
        button2 = QPushButton(settings)
        button2.setText(self.tr("Load from XML/FastInfoset"))
        self.connect(button2, SIGNAL("clicked()"), self, SLOT("loadLayout()"))
        l2.addWidget(button2)
        l.addLayout(l2)

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

        zoomIndicator = QCheckBox(settings)
        zoomIndicator.setText(self.tr("Show zoom indicator"))
        zoomIndicator.setChecked(True)
        self.connect(zoomIndicator, SIGNAL("stateChanged(int)"), self,
            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, SIGNAL("sliderMoved(int)"), self, SLOT("zoomValueChanged(int)"))
        self.connect(self.grid.view(), SIGNAL("zoomChanged(int)"), zoomSlider, SLOT("setValue(int)"))
        l.addWidget(zoomSlider)

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

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

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

        return settings

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

    @QtCore.Slot(int)
    def dottedLineChanged(self, state):
        view = self.grid.view()
        pen = view.options().gridLinePen()
        if 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.grid.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)

    def alignSelectChanged(self, index):
        view = self.grid.view()
        if index == 0:
            view.options().setItemAlignment(Qt.AlignLeft)
        elif index == 1:
            view.options().setItemAlignment(Qt.AlignCenter)
        elif index == 2:
            view.options().setItemAlignment(Qt.AlignRight)
        else:
            view.options().setItemAlignment(Qt.Alignment())

    @QtCore.Slot()
    def saveLayout(self):
        fileName = QFileDialog.getSaveFileName(self, self.tr("Save QtitanDataGrid Layout to File"),
            "qtitan-layout", "XML (*.xml);;FastInfoset (*.fi)")[0]
        if fileName == "":
            return
        if "xml" in QFileInfo(fileName).completeSuffix():
            self.grid.saveLayoutToFile(fileName)
        else:
            writer = FastInfosetStreamWriter(fileName)
            self.grid.saveLayoutToXML(writer)

    @QtCore.Slot()
    def loadLayout(self):
        fileName = QFileDialog.getOpenFileName(self, self.tr("Open QtitanDataGrid Layout File"),
        "", "XML (*.xml);;FastInfoset (*.fi)")[0]
        if fileName == "":
            return
        if "xml" in QFileInfo(fileName).completeSuffix():
            self.grid.loadLayoutFromFile(fileName)
        else:
            reader = FastInfosetStreamReader(fileName)
            self.grid.loadLayoutFromXML(reader)

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

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

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

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

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

    @QtCore.Slot(ContextMenuEventArgs)
    def contextMenu(self, args):
        args.contextMenu().addAction("Print Preview", self, SLOT("printPreview()"))
        args.contextMenu().addSeparator()
        args.contextMenu().addAction("Developer Machines on the Web", self, 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(CellButtonEventArgs)
    def cellButtonStateChanged(self, args):
        if args.row().rowIndex() == 1 or args.row().rowIndex() == 2:
            args.setEnabled(False)

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

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

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