1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-11-16 12:19:03 +00:00
dupeguru/qt/column.py

111 lines
4.4 KiB
Python
Raw Normal View History

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
2019-09-10 00:54:28 +00:00
class Column:
def __init__(
self,
attrname,
default_width,
editor=None,
alignment=Qt.AlignLeft,
cant_truncate=False,
painter=None,
resize_to_fit=False,
):
2019-09-10 00:54:28 +00:00
self.attrname = attrname
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.
self.cant_truncate = cant_truncate
self.resize_to_fit = resize_to_fit
2019-09-10 00:54:28 +00:00
class Columns:
def __init__(self, model, columns, header_view):
2019-09-10 00:54:28 +00:00
self.model = model
self._header_view = header_view
self._header_view.setDefaultAlignment(Qt.AlignLeft)
2019-09-10 00:54:28 +00:00
def setspecs(col, modelcol):
modelcol.default_width = col.default_width
2019-09-10 00:54:28 +00:00
modelcol.editor = col.editor
modelcol.painter = col.painter
modelcol.resize_to_fit = col.resize_to_fit
2019-09-10 00:54:28 +00:00
modelcol.alignment = col.alignment
modelcol.cant_truncate = col.cant_truncate
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:
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
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:
if column.resize_to_fit:
self._header_view.setSectionResizeMode(column.logical_index, QHeaderView.ResizeToContents)
2019-09-10 00:54:28 +00:00
# --- Public
def set_columns_width(self, widths):
# `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):
if width == 0: # column was hidden before.
2019-09-10 00:54:28 +00:00
width = column.default_width
self._header_view.resizeSection(column.logical_index, width)
2019-09-10 00:54:28 +00:00
def set_columns_order(self, column_indexes):
if not column_indexes:
2019-09-10 00:54:28 +00:00
return
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
visual_index = self._header_view.visualIndex(column_index)
self._header_view.moveSection(visual_index, dest_index)
2019-09-10 00:54:28 +00:00
# --- Events
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
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
# --- 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]
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
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)
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)
self._header_view.setSectionHidden(column.logical_index, not visible)