diff --git a/soitool/modules/module_base.py b/soitool/modules/module_base.py index 060df85f8ea041bf4e4768692d93ff546840e926..1db6de94f98dc1bd251f02d67ddeb6db5062c204 100644 --- a/soitool/modules/module_base.py +++ b/soitool/modules/module_base.py @@ -23,6 +23,6 @@ class ModuleBase(ABC): """Abstract method, should be implemented by derived class.""" raise NotImplementedError - def get_as_dict(self): + def get_data(self): """Abstract method, should be implemented by derived class.""" raise NotImplementedError diff --git a/soitool/modules/module_table.py b/soitool/modules/module_table.py index 666a4ff9e9c5e8d3ac4dd62533b1a76e71854755..558e9f28ebb451249efb094626542713e9b8cb54 100644 --- a/soitool/modules/module_table.py +++ b/soitool/modules/module_table.py @@ -29,10 +29,10 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta): By default, the widget initializes as an empty START_ROWS * START_COLUMNS table. If parameters are given, the table initializes accordingly: 'size' is a dict: {"width": int, "height": int}, - 'content' is a 2D list where content[x][y] represents row x, column y. + 'data' is a 2D list where content[x][y] represents row x, column y. """ - def __init__(self, size=None, content=None): + def __init__(self, size=None, data=None): self.type = "TableModule" QTableWidget.__init__(self) ModuleBase.__init__(self) @@ -44,7 +44,7 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta): self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) # If parameters are None, start as empty table. - if size is None and content is None: + if size is None and data is None: # Set number of columns and rows self.setColumnCount(START_COLUMNS) self.setRowCount(START_ROWS) @@ -58,17 +58,17 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta): for i in range(self.columnCount()): self.set_header_item(i, "") else: - self.setColumnCount(len(content[0])) - self.setRowCount(len(content)) + self.setColumnCount(len(data[0])) + self.setRowCount(len(data)) # Set header-items for i in range(self.columnCount()): - self.set_header_item(i, content[0][i]) + self.set_header_item(i, data[0][i]) # Set cell-items for i in range(1, self.rowCount()): for j in range(self.columnCount()): - item = QTableWidgetItem(content[i][j]) + item = QTableWidgetItem(data[i][j]) self.setItem(i, j, item) self.resizeColumnsToContents() @@ -206,13 +206,13 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta): def render_onto_pdf(self): """Render onto pdf.""" - def get_as_dict(self): - """Return a dict containing size and content of module. + def get_data(self): + """Return list containing module data. Returns ------- - Dictionary - Size and content of module. + List (2D) + list[x][y] represents value of row x, column y. """ content = [] for i in range(self.rowCount()): @@ -225,11 +225,4 @@ class TableModule(ModuleBase, QTableWidget, metaclass=Meta): row.append("") content.append(row) - width, height = self.get_size() - - serialized = { - "size": {"width": width, "height": height}, - "content": content, - } - - return serialized + return content diff --git a/soitool/serialize_export_import_soi.py b/soitool/serialize_export_import_soi.py index 88c366882799a13b00df2766a4c8991aa7a0749b..f2057648913b6602353b06eb1677412ef833ad36 100644 --- a/soitool/serialize_export_import_soi.py +++ b/soitool/serialize_export_import_soi.py @@ -34,10 +34,12 @@ def serialize_soi(soi): # Create dict with relevant module-information modules = [] for module in soi.modules: + width, height = module["widget"].get_size() modules.append( { "type": module["widget"].type, - "data": module["widget"].get_as_dict(), + "data": module["widget"].get_data(), + "size": {"width": width, "height": height}, "meta": module["meta"], } ) @@ -45,10 +47,12 @@ def serialize_soi(soi): # Create dict with relevant attachment-information attachments = [] for attachment in soi.attachments: + width, height = attachment["widget"].get_size() attachments.append( { "type": attachment["widget"].type, - "data": attachment["widget"].get_as_dict(), + "data": attachment["widget"].get_data(), + "size": {"width": width, "height": height}, "meta": attachment["meta"], } ) @@ -141,10 +145,10 @@ def import_soi(file_path): module_type = module["type"] if module_type == "TableModule": - size = module["data"]["size"] - content = module["data"]["content"] + size = module["size"] + data = module["data"] modules.append( - {"widget": TableModule(size, content), "meta": module["meta"]} + {"widget": TableModule(size, data), "meta": module["meta"]} ) else: raise TypeError( @@ -157,11 +161,11 @@ def import_soi(file_path): module_type = attachment["type"] if module_type == "TableModule": - size = attachment["data"]["size"] - content = attachment["data"]["content"] + size = attachment["size"] + data = attachment["data"] attachments.append( { - "widget": TableModule(size, content), + "widget": TableModule(size, data), "meta": attachment["meta"], } ) diff --git a/test/test_serialize_export_import.py b/test/test_serialize_export_import.py index c9a22dcb402015ec4d6cdc215706041a26f57366..540735233481ff2a0f1f54a20ce19053039ba2e9 100644 --- a/test/test_serialize_export_import.py +++ b/test/test_serialize_export_import.py @@ -38,14 +38,14 @@ MODULES = [ { "widget": TableModule( size={"width": 50, "height": 75}, - content=[["H1"], ["Row1"], ["Row2"]], + data=[["H1"], ["Row1"], ["Row2"]], ), "meta": {"x": 0, "y": 0, "page": 1, "name": "Table1"}, }, { "widget": TableModule( size={"width": 100, "height": 75}, - content=[["H1", "H2"], ["Row1Col1", "Row1Col2"]], + data=[["H1", "H2"], ["Row1Col1", "Row1Col2"]], ), "meta": {"x": 200, "y": 150, "page": 1, "name": "Table1"}, }, @@ -70,12 +70,10 @@ SCHEMA = Schema( "modules": [ { "type": And(str, len), - "data": { - "size": { - "width": And(Or(int, float), lambda w: w > 0), - "height": And(Or(int, float), lambda h: h > 0), - }, - "content": object, + "data": object, + "size": { + "width": And(Or(int, float), lambda w: w > 0), + "height": And(Or(int, float), lambda h: h > 0), }, "meta": { "x": And(Or(int, float), lambda x: x >= 0), @@ -88,12 +86,10 @@ SCHEMA = Schema( "attachments": [ { "type": And(str, len), - "data": { - "size": { - "width": And(Or(int, float), lambda w: w > 0), - "height": And(Or(int, float), lambda h: h > 0), - }, - "content": object, + "data": object, + "size": { + "width": And(Or(int, float), lambda w: w > 0), + "height": And(Or(int, float), lambda h: h > 0), }, "meta": { "x": And(Or(int, float), lambda x: x >= 0),