2019-09-10 00:54:28 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2009-11-25
|
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
|
|
|
#
|
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
|
|
|
from PyQt5.QtCore import Qt
|
|
|
|
from PyQt5.QtWidgets import QHeaderView
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
class Column:
|
2020-01-01 02:16:27 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
attrname,
|
2021-08-25 05:46:33 +00:00
|
|
|
default_width,
|
2020-01-01 02:16:27 +00:00
|
|
|
editor=None,
|
|
|
|
alignment=Qt.AlignLeft,
|
2021-08-25 05:46:33 +00:00
|
|
|
cant_truncate=False,
|
2020-01-01 02:16:27 +00:00
|
|
|
painter=None,
|
2021-08-25 05:46:33 +00:00
|
|
|
resize_to_fit=False,
|
2020-01-01 02:16:27 +00:00
|
|
|
):
|
2019-09-10 00:54:28 +00:00
|
|
|
self.attrname = attrname
|
2021-08-25 05:46:33 +00:00
|
|
|
self.default_width = default_width
|
2019-09-10 00:54:28 +00:00
|
|
|
self.editor = editor
|
|
|
|
# See moneyguru #15. Painter attribute was added to allow custom painting of amount value and
|
|
|
|
# currency information. Can be used as a pattern for custom painting of any column.
|
|
|
|
self.painter = painter
|
|
|
|
self.alignment = alignment
|
|
|
|
# This is to indicate, during printing, that a column can't have its data truncated.
|
2021-08-25 05:46:33 +00:00
|
|
|
self.cant_truncate = cant_truncate
|
|
|
|
self.resize_to_fit = resize_to_fit
|
2019-09-10 00:54:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Columns:
|
2021-08-25 05:46:33 +00:00
|
|
|
def __init__(self, model, columns, header_view):
|
2019-09-10 00:54:28 +00:00
|
|
|
self.model = model
|
2021-08-25 05:46:33 +00:00
|
|
|
self._header_view = header_view
|
|
|
|
self._header_view.setDefaultAlignment(Qt.AlignLeft)
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def setspecs(col, modelcol):
|
2021-08-25 05:46:33 +00:00
|
|
|
modelcol.default_width = col.default_width
|
2019-09-10 00:54:28 +00:00
|
|
|
modelcol.editor = col.editor
|
|
|
|
modelcol.painter = col.painter
|
2021-08-25 05:46:33 +00:00
|
|
|
modelcol.resize_to_fit = col.resize_to_fit
|
2019-09-10 00:54:28 +00:00
|
|
|
modelcol.alignment = col.alignment
|
2021-08-25 05:46:33 +00:00
|
|
|
modelcol.cant_truncate = col.cant_truncate
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
if columns:
|
|
|
|
for col in columns:
|
|
|
|
modelcol = self.model.column_by_name(col.attrname)
|
|
|
|
setspecs(col, modelcol)
|
|
|
|
else:
|
2020-01-01 02:16:27 +00:00
|
|
|
col = Column("", 100)
|
2019-09-10 00:54:28 +00:00
|
|
|
for modelcol in self.model.column_list:
|
|
|
|
setspecs(col, modelcol)
|
|
|
|
self.model.view = self
|
2021-08-25 05:46:33 +00:00
|
|
|
self._header_view.sectionMoved.connect(self.header_section_moved)
|
|
|
|
self._header_view.sectionResized.connect(self.header_section_resized)
|
2019-09-10 00:54:28 +00:00
|
|
|
|
|
|
|
# See moneyguru #14 and #15. This was added in order to allow automatic resizing of columns.
|
|
|
|
for column in self.model.column_list:
|
2021-08-25 05:46:33 +00:00
|
|
|
if column.resize_to_fit:
|
|
|
|
self._header_view.setSectionResizeMode(column.logical_index, QHeaderView.ResizeToContents)
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
# --- Public
|
2021-08-25 05:46:33 +00:00
|
|
|
def set_columns_width(self, widths):
|
2020-01-01 02:16:27 +00:00
|
|
|
# `widths` can be None. If it is, then default widths are set.
|
2019-09-10 00:54:28 +00:00
|
|
|
columns = self.model.column_list
|
|
|
|
if not widths:
|
|
|
|
widths = [column.default_width for column in columns]
|
|
|
|
for column, width in zip(columns, widths):
|
2020-01-01 02:16:27 +00:00
|
|
|
if width == 0: # column was hidden before.
|
2019-09-10 00:54:28 +00:00
|
|
|
width = column.default_width
|
2021-08-25 05:46:33 +00:00
|
|
|
self._header_view.resizeSection(column.logical_index, width)
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2021-08-25 05:46:33 +00:00
|
|
|
def set_columns_order(self, column_indexes):
|
|
|
|
if not column_indexes:
|
2019-09-10 00:54:28 +00:00
|
|
|
return
|
2021-08-25 05:46:33 +00:00
|
|
|
for dest_index, column_index in enumerate(column_indexes):
|
2019-09-10 00:54:28 +00:00
|
|
|
# moveSection takes 2 visual index arguments, so we have to get our visual index first
|
2021-08-25 05:46:33 +00:00
|
|
|
visual_index = self._header_view.visualIndex(column_index)
|
|
|
|
self._header_view.moveSection(visual_index, dest_index)
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
# --- Events
|
2021-08-25 05:46:33 +00:00
|
|
|
def header_section_moved(self, logical_index, old_visual_index, new_visual_index):
|
|
|
|
attrname = self.model.column_by_index(logical_index).name
|
|
|
|
self.model.move_column(attrname, new_visual_index)
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2021-08-25 05:46:33 +00:00
|
|
|
def header_section_resized(self, logical_index, old_size, new_size):
|
|
|
|
attrname = self.model.column_by_index(logical_index).name
|
|
|
|
self.model.resize_column(attrname, new_size)
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
# --- model --> view
|
2019-09-10 00:54:28 +00:00
|
|
|
def restore_columns(self):
|
|
|
|
columns = self.model.ordered_columns
|
|
|
|
indexes = [col.logical_index for col in columns]
|
2021-08-25 05:46:33 +00:00
|
|
|
self.set_columns_order(indexes)
|
2019-09-10 00:54:28 +00:00
|
|
|
widths = [col.width for col in self.model.column_list]
|
|
|
|
if not any(widths):
|
|
|
|
widths = None
|
2021-08-25 05:46:33 +00:00
|
|
|
self.set_columns_width(widths)
|
2019-09-10 00:54:28 +00:00
|
|
|
for column in self.model.column_list:
|
|
|
|
visible = self.model.column_is_visible(column.name)
|
2021-08-25 05:46:33 +00:00
|
|
|
self._header_view.setSectionHidden(column.logical_index, not visible)
|
2019-09-10 00:54:28 +00:00
|
|
|
|
|
|
|
def set_column_visible(self, colname, visible):
|
|
|
|
column = self.model.column_by_name(colname)
|
2021-08-25 05:46:33 +00:00
|
|
|
self._header_view.setSectionHidden(column.logical_index, not visible)
|