MSOfficeMode 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,
CellButtonClickEventArgs, CellButtonEventArgs, ContextMenuEventArgs,
EditorValidationEventArgs)
if __pyside2__:
from PySide2 import QtCore
from PySide2.QtCore import Qt, QTime, QDateTime, QDate
from PySide2.QtGui import QPixmap, QStandardItemModel
from PySide2.QtWidgets import (QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton,
QSlider, QLabel, QCheckBox, QComboBox, QMessageBox)
if __pyside6__:
from PySide6 import QtCore
from PySide6.QtCore import Qt, QTime, QDateTime, QDate
from PySide6.QtGui import QPixmap, QStandardItemModel
from PySide6.QtWidgets import (QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton,
QSlider, QLabel, QCheckBox, QComboBox, QMessageBox)
import msofficemode_rc
from DemoMainWindow import DemoMainWindow
def addMail(model, subject, _from, date, attachment, read, status):
model.insertRow(0)
pm = QPixmap()
model.setData(model.index(0, 0), pm)
if attachment:
pm = QPixmap(":res/attachmentcell.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
model.setData(model.index(0, 1), pm)
pm = QPixmap()
model.setData(model.index(0, 2), pm)
if read:
pm = QPixmap(":res/mail.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
else:
pm = QPixmap(":res/mailandarrow.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
model.setData(model.index(0, 3), pm)
model.setData(model.index(0, 4), date)
model.setData(model.index(0, 5), subject)
model.setData(model.index(0, 6), _from)
if status == 0:
pm = QPixmap(":res/flag.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
else:
pm = QPixmap(":res/flag_red.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
model.setData(model.index(0, 7), pm)
def createMailModel(parent):
model = QStandardItemModel(0, 8, parent)
model.setHeaderData(0, Qt.Horizontal, model.tr("Importance"))
model.setHeaderData(1, Qt.Horizontal, model.tr("Attachment"))
model.setHeaderData(2, Qt.Horizontal, model.tr("Reminder"))
model.setHeaderData(3, Qt.Horizontal, model.tr("Read"))
model.setHeaderData(4, Qt.Horizontal, model.tr("Created"))
model.setHeaderData(5, Qt.Horizontal, model.tr("Subject"))
model.setHeaderData(6, Qt.Horizontal, model.tr("From"))
model.setHeaderData(7, Qt.Horizontal, model.tr("Status"))
addMail(model, "Happy Year!", "Grace K. <[email protected]>",
QDateTime(QDate(2015, 12, 31), QTime(17, 3)), False, True, 0)
addMail(model, "Radically concept", "Grace K. <[email protected]>",
QDateTime(QDate(2014, 12, 22), QTime(9, 44)), True, True, 0)
addMail(model, "Accounts", "[email protected]",
QDateTime(QDate(2014, 12, 31), QTime(12, 50)), False, True, 0)
addMail(model, "Expenses", "Joe Bloggs <[email protected]>",
QDateTime(QDate(2014, 12, 25), QTime(11, 39)), False, False, 0)
addMail(model, "Re: Expenses", "Andy <[email protected]>",
QDateTime(QDate(2015, 1, 2), QTime(16, 5)), False, True, 1)
addMail(model, "Re: Accounts", "Joe Bloggs <[email protected]>",
QDateTime(QDate(2015, 1, 3), QTime(14, 18)), False, False, 0)
addMail(model, "Re: Accounts", "Andy <[email protected]>",
QDateTime(QDate(2015, 1, 3), QTime(14, 26)), True, False, 0)
addMail(model, "Sports", "Linda Smith <[email protected]>",
QDateTime(QDate(2013, 1, 5), QTime(11, 33)), False, True, 1)
addMail(model, "AW: Sports", "Rolf Newschweinstein <[email protected]>",
QDateTime(QDate(2014, 1, 5), QTime(12, 0)), True, True, 0)
addMail(model, "RE: Sports", "Petra Schmidt <[email protected]>",
QDateTime(QDate(2015, 1, 5), QTime(12, 1)), False, True, 0)
return model
class Window(DemoMainWindow):
def __init__(self):
DemoMainWindow.__init__(self, "QtitanDataGrid", getGridVersion())
self.setWindowTitle(self.tr("QtitanDataGrid Demo (MS-Office row grouping)"))
self.setGeometry(150, 150, 1000, 600)
Grid.loadTranslation()
self.grid = Grid()
model = createMailModel(self.grid)
# Configure grid view
self.grid.setViewType(Grid.TableView)
view = self.grid.view()
view.options().setModelDecoration(False)
view.options().setGroupRowStyle(GridViewOptions.GroupOfficeOutlook)
view.options().setShowFocusDecoration(True)
# Connect Grid's context menu handler.
self.connect(view, QtCore.SIGNAL("contextMenu(ContextMenuEventArgs*)"), self, QtCore.SLOT("contextMenu(ContextMenuEventArgs* )"))
view.setModel(model)
column = view.getColumnByModelColumnName("Subject")
#Add cell button to the column.
column.addButton(GridColumn.TextButtonIcon, Qtitan.AtBeginning)
self.connect(column, QtCore.SIGNAL("buttonClicked(CellButtonClickEventArgs*)"), self, QtCore.SLOT("cellButtonClicked(CellButtonClickEventArgs*)"))
pm = QPixmap(":res/attachment.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
column = view.getColumnByModelColumnName("Attachment")
column.setIcon(pm)
column.setIconApperance(Qtitan.IconOnly)
column.setMaxWidth(20)
column.setMinWidth(20)
column.setTextAlignment(Qt.AlignCenter)
column.setHorSizingEnabled(False)
column.setFilterButtonVisible(False)
column.setEditorType(GridEditor.Picture)
column.editorRepository().setEditable(False)
column.editorRepository().setAlignment(Qt.AlignCenter)
column.editorRepository().setScaled(False)
pm = QPixmap(":res/importance.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
column = view.getColumnByModelColumnName("Importance")
column.setIcon(pm)
column.setIconApperance(Qtitan.IconOnly)
column.setMaxWidth(20)
column.setMinWidth(20)
column.setFilterButtonVisible(False)
column.setMenuButtonVisible(False)
column.setTextAlignment(Qt.AlignCenter)
column.setHorSizingEnabled(False)
column.setEditorType(GridEditor.Picture)
column.editorRepository().setEditable(False)
column.editorRepository().setAlignment(Qt.AlignCenter)
column.editorRepository().setScaled(False)
pm = QPixmap(":res/reminder.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
column = view.getColumnByModelColumnName("Reminder")
column.setIcon(pm)
column.setIconApperance(Qtitan.IconOnly)
column.setMaxWidth(20)
column.setMinWidth(20)
column.setFilterButtonVisible(False)
column.setMenuButtonVisible(False)
column.setTextAlignment(Qt.AlignCenter)
column.setHorSizingEnabled(False)
column.setEditorType(GridEditor.Picture)
column.editorRepository().setEditable(False)
column.editorRepository().setAlignment(Qt.AlignCenter)
column.editorRepository().setScaled(False)
pm = QPixmap(":res/read.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
column = view.getColumnByModelColumnName("Read")
column.setIcon(pm)
column.setIconApperance(Qtitan.IconOnly)
column.setMaxWidth(20)
column.setMinWidth(20)
column.setFilterButtonVisible(False)
column.setMenuButtonVisible(False)
column.setTextAlignment(Qt.AlignCenter)
column.setHorSizingEnabled(False)
column.setEditorType(GridEditor.Picture)
column.editorRepository().setEditable(False)
column.editorRepository().setAlignment(Qt.AlignCenter)
column.editorRepository().setScaled(False)
pm = QPixmap(":res/status.png")
pm.setMask(pm.createMaskFromColor(Qt.magenta))
column = view.getColumnByModelColumnName("Status")
column.setIcon(pm)
column.setIconApperance(Qtitan.IconOnly)
column.setMaxWidth(20)
column.setMinWidth(20)
column.setFilterButtonVisible(False)
column.setMenuButtonVisible(False)
column.setHorSizingEnabled(False)
column.setEditorType(GridEditor.Picture)
column.editorRepository().setEditable(False)
column.editorRepository().setAlignment(Qt.AlignCenter)
column.editorRepository().setScaled(False)
self.setDemoWidget(self.grid, self.createSettingsWidget())
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(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(4)
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.grid.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.grid.view()
view.tableOptions().setColumnAutoWidth(Qt.CheckState(state) == Qt.Checked)
@QtCore.Slot(int)
def fastScrollChanged(self, state):
view = self.grid.view()
view.options().setFastScrollEffect(Qt.CheckState(state) == Qt.Checked)
@QtCore.Slot(int)
def dottedLineChanged(self, state):
view = self.grid.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.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)
@QtCore.Slot(int)
def zoomEnabledChanged(self, state):
view = self.grid.view()
view.options().setZoomEnabled(Qt.CheckState(state) == Qt.Checked)
@QtCore.Slot(int)
def zoomIndicatorChanged(self, state):
view = self.grid.view()
view.options().setZoomIndicatorActive(Qt.CheckState(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(Qt.CheckState(state) == Qt.Checked)
@QtCore.Slot(int)
def frozenRowsEnabled(self, state):
view = self.grid.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.grid.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(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.grid.view().printPreview()
def setShadeColor(self, color):
Grid.themeManager().setShadeColor(color)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())