From 8aa39a2ec10407574cb96297afa01e6fb5ab29d8 Mon Sep 17 00:00:00 2001
From: "Anders H. Rebner" <anderhre@stud.ntnu.no>
Date: Fri, 28 Feb 2020 14:44:14 +0100
Subject: [PATCH] =?UTF-8?q?#23=20Widgetst=C3=B8rrelse=20endrer=20seg=20n?=
 =?UTF-8?q?=C3=A5=20dynamisk=20etter=20innhold?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 soitool/modules/module_table.py | 68 +++++++++++++++++++++++++--------
 1 file changed, 53 insertions(+), 15 deletions(-)

diff --git a/soitool/modules/module_table.py b/soitool/modules/module_table.py
index 11f368f..a945597 100644
--- a/soitool/modules/module_table.py
+++ b/soitool/modules/module_table.py
@@ -1,6 +1,6 @@
 """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
 
-- 
GitLab