Skip to content

Commit

Permalink
Footer class in TableViewWithFooter was unified with TreeViewWithFooter.
Browse files Browse the repository at this point in the history
  • Loading branch information
titov-vv committed Mar 6, 2024
1 parent dd17245 commit 8b3090d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions jal/db/balances_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ def data(self, index, role=Qt.DisplayRole):

def footerData(self, section, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
total_data = self._root.details()
if section == self.fieldIndex('account_name'):
return self.tr("Total:")
elif section == self.fieldIndex('value_common'):
return localize_decimal(total_data[self._columns[section]['field']], precision=2)
totals_data = self._root.details()
return localize_decimal(totals_data[self._columns[section]['field']], precision=2)
if role == Qt.FontRole:
return self.bold_font
if role == Qt.TextAlignmentRole:
Expand Down
30 changes: 15 additions & 15 deletions jal/widgets/custom/tableview_with_footer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from PySide6.QtCore import Qt, QAbstractItemModel, QRect
from PySide6.QtWidgets import QTableView, QHeaderView
from PySide6.QtGui import QResizeEvent, QPainter
from PySide6.QtWidgets import QTableView, QHeaderView, QStyle, QStyleOptionHeaderV2
from PySide6.QtGui import QResizeEvent, QPainter, QIcon, QFontMetrics

# ----------------------------------------------------------------------------------------------------------------------
# File implements TableViewWithFooter class that is a descendant of QTableView class with footer.
# Underlying model should support footerData(section, role) method in order to provide data for the footer.
# The footer is implemented as FooterView class that is derived from QHeaderView.
# ----------------------------------------------------------------------------------------------------------------------
class FooterView(QHeaderView):
class FooterView(QHeaderView): # The same class as in treeview_with_footer.py (to keep everything in one module)
def __init__(self, parent: QTableView):
super().__init__(Qt.Horizontal, parent)
self._model = None
Expand All @@ -19,24 +19,24 @@ def setModel(self, model: QAbstractItemModel) -> None:
super().setModel(model)

def paintSection(self, painter: QPainter, rect: QRect, idx: int) -> None:
# First make standard painting by parent QHeaderView class method
painter.save()
super().paintSection(painter, rect, idx)
painter.restore()
# Clean footer content (by default QHeaderView puts sections names there
inner_rect = rect.adjusted(self.lineWidth(), self.lineWidth(), -self.lineWidth(), -self.lineWidth())
bg_color = self.palette().color(self.backgroundRole())
painter.fillRect(inner_rect, bg_color)
# Get data from model and write text with given font and position
opt = QStyleOptionHeaderV2()
self.initStyleOption(opt)
self.initStyleOptionForIndex(opt, idx)
opt.rect = rect
text = self._model.footerData(idx, role=Qt.DisplayRole)
if text is None:
return
opt.text = '' if text is None else text
font = self._model.footerData(idx, role=Qt.FontRole)
if font is not None:
opt.fontMetrics = QFontMetrics(font)
painter.setFont(font)
icon = self._model.footerData(idx, role=Qt.DecorationRole)
opt.iconAlignment = Qt.AlignVCenter
opt.icon = QIcon() if icon is None else icon
alignment = self._model.footerData(idx, role=Qt.TextAlignmentRole)
alignment = Qt.AlignCenter | Qt.AlignVCenter if alignment is None else alignment
painter.drawText(inner_rect, alignment, f" {text} ")
opt.textAlignment = Qt.AlignCenter | Qt.AlignVCenter if alignment is None else alignment
self.style().drawControl(QStyle.CE_Header, opt, painter, self)
painter.restore()

def on_header_resize(self, section: int, _old_size: int, new_size: int) -> None:
self.resizeSection(section, new_size)
Expand Down
2 changes: 1 addition & 1 deletion jal/widgets/custom/treeview_with_footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Underlying model should support footerData(section, role) method in order to provide data for the footer.
# The footer is implemented as FooterView class that is derived from QHeaderView.
# ----------------------------------------------------------------------------------------------------------------------
class FooterView(QHeaderView):
class FooterView(QHeaderView): # The same class as in tableview_with_footer.py (to keep everything in one module)
def __init__(self, parent: QTreeView):
super().__init__(Qt.Horizontal, parent)
self._model = None
Expand Down

0 comments on commit 8b3090d

Please sign in to comment.