Skip to content
Snippets Groups Projects
Commit 8aa39a2e authored by Anders H. Rebner's avatar Anders H. Rebner
Browse files

#23 Widgetstørrelse endrer seg nå dynamisk etter innhold

parent 786f6144
No related branches found
No related tags found
1 merge request!8ModuleTable og ModuleBase
Pipeline #71228 failed
"""Module containing subclassed SOIModule (QTableWidget, ModuleBase)."""
from PySide2.QtWidgets import QTableWidget, QTableWidgetItem, QShortcut
from PySide2 import QtGui
from PySide2 import QtGui, QtCore
from module_base import ModuleBase
HEADER_FONT = QtGui.QFont()
......@@ -8,6 +8,9 @@ HEADER_FONT.setFamily('Arial')
HEADER_FONT.setPointSize(12)
HEADER_FONT.setWeight(100)
START_COLUMNS = 2
START_ROWS = 2
class Meta(type(ModuleBase), type(QTableWidget)):
"""Used as a metaclass to enable multiple inheritance."""
......@@ -19,6 +22,9 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
Has shortcuts for adding and removing rows and columns.
Inherits from ModuleBase and QTableWidget.
Functions inherited from ModuleBase are overridden by this class.
Has header-styled first row.
Functionality for adding and removing columns and rows is implemented.
"""
def __init__(self):
......@@ -26,19 +32,28 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
QTableWidget.__init__(self)
super(QTableWidget)
# Remove headers
self.horizontalHeader().hide()
self.verticalHeader().hide()
self.setColumnCount(2)
self.setRowCount(2)
# Set number of columns and rows
self.setColumnCount(START_COLUMNS)
self.setRowCount(START_ROWS)
self.set_header_item(0, "")
self.set_header_item(1, "")
# Resize width and height of columns and rows, and set size of window
self.resize()
self.setFixedWidth(START_COLUMNS * self.columnWidth(0) + 2)
self.setFixedHeight(START_ROWS * self.rowHeight(0) + 5)
self.resizeRowsToContents()
self.resizeColumnsToContents()
# Remove scrollbars
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.cellChanged.connect(self.cell_changed)
# Set headers
for i in range(self.columnCount()):
self.set_header_item(i, "")
self.cellChanged.connect(self.resize)
self.set_shortcuts()
......@@ -61,11 +76,13 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
def set_shortcuts(self):
"""Set shortcuts for adding and removing rows and columns."""
# Create shortcuts
shortcut_add_col = QShortcut(QtGui.QKeySequence("Shift++"), self)
shortcut_rem_col = QShortcut(QtGui.QKeySequence("Shift+-"), self)
shortcut_add_row = QShortcut(QtGui.QKeySequence("Ctrl++"), self)
shortcut_rem_row = QShortcut(QtGui.QKeySequence("Ctrl+-"), self)
# Connect shortcuts to functions
shortcut_add_col.activated.connect(self.add_column)
shortcut_rem_col.activated.connect(self.remove_column)
shortcut_add_row.activated.connect(self.add_row)
......@@ -75,28 +92,48 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
"""Add column to the right of selected column."""
self.insertColumn(self.currentColumn() + 1)
self.set_header_item(self.currentColumn() + 1, "")
self.resizeColumnsToContents()
self.resize()
def remove_column(self):
"""Remove selected column if two or more columns exist."""
if self.columnCount() > 1:
self.removeColumn(self.currentColumn())
self.resize()
def add_row(self):
"""Add row below selected row."""
self.insertRow(self.currentRow() + 1)
self.resizeRowsToContents()
self.resize()
def remove_row(self):
"""Remove selected row if two or more rows exist (including header)."""
if self.rowCount() > 2:
if self.rowCount() > 2 and self.currentRow() != 0:
self.removeRow(self.currentRow())
self.resize()
def cell_changed(self):
"""Resize rows and columns to contents when a cell changes."""
def resize(self):
"""Resize widget, rows and columns.
Resize widget size to total width and height of rows and columns.
Resize rows and columns to contents.
"""
self.resizeColumnsToContents()
self.resizeRowsToContents()
# Calculate total width and height of columns and rows
width = 0
height = 0
for x in range(self.columnCount()):
width += self.columnWidth(x) + 0.5
for y in range(self.rowCount()):
height += self.rowHeight(y) + 0.5
# Set total width and height
self.setFixedWidth(width)
self.setFixedHeight(height)
def get_size(self):
"""Get size of widget.
......@@ -105,14 +142,15 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
Tuple
(width, height) (total)
"""
# Calculate total width and height of columns and rows
width = 0
height = 0
for i in range(self.columnCount()):
width += self.columnWidth(i)
width += self.columnWidth(i) + 0.5
for i in range(self.columnCount()):
height += self.rowHeight(i)
height += self.rowHeight(i) + 0.5
return width, height
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment