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

#86 Kommentarfiksing

parent d626e9d9
No related branches found
No related tags found
1 merge request!50#86 Modul: Autentiseringstavle
Pipeline #77985 failed
"""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
......@@ -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
----------
......
......@@ -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):
......
......@@ -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(
......
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