From ad8188624ee305c764ecaa14b81056183158e2c4 Mon Sep 17 00:00:00 2001
From: "Anders H. Rebner" <anderhre@stud.ntnu.no>
Date: Tue, 31 Mar 2020 19:11:30 +0200
Subject: [PATCH] #86 Kommentarfiksing

---
 .../modules/module_authentification_board.py  | 53 ++++++++++---------
 soitool/modules/module_base.py                | 11 +---
 soitool/modules/module_table.py               |  2 +-
 soitool/serialize_export_import_soi.py        |  1 -
 4 files changed, 32 insertions(+), 35 deletions(-)

diff --git a/soitool/modules/module_authentification_board.py b/soitool/modules/module_authentification_board.py
index 11e6091..3f463ad 100644
--- a/soitool/modules/module_authentification_board.py
+++ b/soitool/modules/module_authentification_board.py
@@ -1,4 +1,4 @@
-"""Module containing for SOI-module 'Autentifiseringstavle'."""
+"""Module containing SOI-module 'Autentifiseringstavle'."""
 import string
 from random import choices
 from PySide2.QtWidgets import QTableWidget, QTableWidgetItem
@@ -38,8 +38,8 @@ class AuthentificationBoardModule(ModuleBase, QTableWidget, metaclass=Meta):
 
     If parameters are given, the widget initializes accordingly:
     'size' is a dict: {"width": int, "height": int},
-    'data' is a 2D list where data[x][y] represents row x, column y, and
-    data[0][0] is the headline.
+    'data' is a 2D list where data[0] is the headline,
+    and data[x][y] represents row x, column y.
 
     The widget does not use more room than needed, and resizes dynamically.
     It has shortcuts for adding and removing rows.
@@ -61,10 +61,10 @@ class AuthentificationBoardModule(ModuleBase, QTableWidget, metaclass=Meta):
 
         # If parameters are None, generate new table
         if size is None and data is None:
-            self.generate_table_data()
+            self.generate_table()
             self.resizeColumnsToContents()
-
             self.insert_headline()
+
             # Resize height of rows and set size of window
             resize_table(self, resize_column=False)
         else:
@@ -84,28 +84,28 @@ class AuthentificationBoardModule(ModuleBase, QTableWidget, metaclass=Meta):
 
             self.insert_headline(data[0])
 
-    def generate_table_data(self):
-        """Insert authentification codes and row identifiers."""
+    def generate_table(self):
+        """Insert row identifiers and authentification codes."""
         # Set number of rows and columns
         self.setRowCount(START_NO_OF_AUTHENTICATION_CODES)
         self.setColumnCount(3)
 
-        # Get generated codes
+        # Generate codes
         codes = generate_authentification_codes()
 
         # Insert table data
         for i in range(START_NO_OF_AUTHENTICATION_CODES):
-            # Insert row identifier in first column
+            # Insert non-editable row identifier in first column
             item_first = QTableWidgetItem(ROW_IDENTIFIERS[i])
             item_first.setFlags(item_first.flags() ^ Qt.ItemIsEditable)
             self.setItem(i, 0, item_first)
 
-            # Insert row identifier (int) in second column
+            # Insert non-editable row identifier (int) in second column
             item_second = QTableWidgetItem(str(i))
             item_second.setFlags(item_second.flags() ^ Qt.ItemIsEditable)
             self.setItem(i, 1, item_second)
 
-            # Insert code in third column
+            # Insert non-editable code in third column
             item_third = QTableWidgetItem(codes[i])
             item_third.setFlags(item_third.flags() ^ Qt.ItemIsEditable)
             self.setItem(i, 2, item_third)
@@ -124,10 +124,16 @@ class AuthentificationBoardModule(ModuleBase, QTableWidget, metaclass=Meta):
 
         self.insertRow(0)
         self.setItem(0, 0, item_headline)
-        self.setSpan(0, 0, 1, self.columnCount())  # Make item span all columns
+        self.setSpan(0, 0, 1, self.columnCount())  # Make cell span all columns
 
     def generate_unique_authentification_code(self):
-        """Generate a authentification-code that does not already exist."""
+        """Generate authentification-code that does not already exist.
+
+        Returns
+        -------
+        string
+            Generated, unique authentification-code
+        """
         # Get existing codes
         existing_codes = self.get_codes()
 
@@ -175,7 +181,7 @@ class AuthentificationBoardModule(ModuleBase, QTableWidget, metaclass=Meta):
             super(AuthentificationBoardModule, self).keyPressEvent(event)
 
     def add_row(self):
-        """Add row with cell-data below selected row."""
+        """Insert row below the selected row and add data."""
         row_index = self.currentRow()
         # If maximum amount of rows not reached and a row is selected
         # (+ 1 to skip row containing headline)
@@ -202,15 +208,15 @@ class AuthentificationBoardModule(ModuleBase, QTableWidget, metaclass=Meta):
             self.setItem(row_index + 1, 2, item_third)
 
             # Resize code-column in case it got wider
-            # Example: Letters BGD is wider than I (depending on font)
+            # Example: 'BGD' is wider than 'III' (depending on font)
             self.resizeColumnToContents(2)
-            # Resize widget size
             resize_table(self, resize_column=False)
 
     def remove_row(self):
         """Remove selected row."""
         row_index = self.currentRow()
-        # If at least one row exists (+ headline) and a row is selected
+        # If at least one row (+ headline-row) exists and a row other than
+        # headline-row is selected
         if self.rowCount() > 2 and row_index != 0 and row_index != -1:
             # Remove row
             self.removeRow(row_index)
@@ -219,7 +225,6 @@ class AuthentificationBoardModule(ModuleBase, QTableWidget, metaclass=Meta):
             for i in range(row_index, self.rowCount()):
                 self.item(i, 0).setText(CODE_CHARACTERS[i - 1])
                 self.item(i, 1).setText(str(i - 1))
-        # Resize widget size
         resize_table(self, resize_column=False)
 
     def get_codes(self):
@@ -232,14 +237,14 @@ class AuthentificationBoardModule(ModuleBase, QTableWidget, metaclass=Meta):
         """
         codes = []
 
-        # Start with row 1 to skip headline
+        # Start with row 1 to skip headline-row
         for i in range(1, self.rowCount()):
             codes.append(self.item(i, 2).text())
 
         return codes
 
     def get_size(self):
-        """Get size of widget."""
+        """Return size of widget."""
         return get_table_size(self)
 
     def get_data(self):
@@ -291,7 +296,7 @@ def generate_authentification_codes(amount=START_NO_OF_AUTHENTICATION_CODES):
     """
     codes = []
     for _ in range(amount):
-        sequence = choices(CODE_CHARACTERS, k=CODE_LENGTH)
+        code = choices(CODE_CHARACTERS, k=CODE_LENGTH)
         # Choose starting point for separating code with spaces, depending on
         # SEPARATE_INTERVAL and CODE_LENGTH being even or odd numbers.
         # If code is 1234567 and interval is 3, code will be 1234 567 instead
@@ -306,9 +311,9 @@ def generate_authentification_codes(amount=START_NO_OF_AUTHENTICATION_CODES):
         else:
             start = SEPARATE_INTERVAL
         for i in range(
-            start, len(sequence) - SEPARATE_INTERVAL, SEPARATE_INTERVAL
+            start, len(code) - SEPARATE_INTERVAL, SEPARATE_INTERVAL
         ):
-            sequence[i] += " "
-        codes.append("".join(sequence))
+            code[i] += " "
+        codes.append("".join(code))
 
     return codes
diff --git a/soitool/modules/module_base.py b/soitool/modules/module_base.py
index 5e4535d..0fc14b8 100644
--- a/soitool/modules/module_base.py
+++ b/soitool/modules/module_base.py
@@ -54,21 +54,14 @@ def resize_table(widget, resize_row=True, resize_column=True):
     if resize_column:
         widget.resizeColumnsToContents()
 
-    width = 0
-    height = 0
-
-    for x in range(widget.columnCount()):
-        width += widget.columnWidth(x) + 0.5
-
-    for y in range(widget.rowCount()):
-        height += widget.rowHeight(y) + 0.5
+    width, height = get_table_size(widget)
 
     widget.setFixedWidth(width)
     widget.setFixedHeight(height)
 
 
 def get_table_size(widget):
-    """Calculate and return the size of a QTableWidget.
+    """Calculate and return total width and height of a QTableWidget.
 
     Parameters
     ----------
diff --git a/soitool/modules/module_table.py b/soitool/modules/module_table.py
index 66c5d09..433587a 100644
--- a/soitool/modules/module_table.py
+++ b/soitool/modules/module_table.py
@@ -149,7 +149,7 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta):
             resize_table(self)
 
     def get_size(self):
-        """Get size of widget."""
+        """Return size of widget."""
         return get_table_size(self)
 
     def get_data(self):
diff --git a/soitool/serialize_export_import_soi.py b/soitool/serialize_export_import_soi.py
index c2e9229..07ce5cd 100644
--- a/soitool/serialize_export_import_soi.py
+++ b/soitool/serialize_export_import_soi.py
@@ -222,7 +222,6 @@ def import_soi(file_path):
                 {"widget": TableModule(size, data), "meta": module["meta"]}
             )
         elif module_type == "AuthentificationBoardModule":
-            print(module["data"])
             size = module["size"]
             data = module["data"]
             modules.append(
-- 
GitLab