From 26983cf71c66945ef57581bc51df7810923d404e Mon Sep 17 00:00:00 2001 From: thomahl <thomahl@stud.ntnu.no> Date: Wed, 29 Apr 2020 10:22:09 +0200 Subject: [PATCH] =?UTF-8?q?#98=20fikset=20bug=20med=20freetextmodule=20&?= =?UTF-8?q?=20gj=C3=B8r=20codephrasemodule=20testbar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FreeTextModule fikk ulik størrelse avhengig av skjerm --- soitool/modules/fit_to_contents_widgets.py | 6 +++++- soitool/modules/module_code_phrase.py | 24 ++++++++++++++++++---- test/test_resolution_smoke_test.py | 20 ++++++++++++++---- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/soitool/modules/fit_to_contents_widgets.py b/soitool/modules/fit_to_contents_widgets.py index d71335c..28f8dcc 100644 --- a/soitool/modules/fit_to_contents_widgets.py +++ b/soitool/modules/fit_to_contents_widgets.py @@ -159,7 +159,11 @@ class TextEditWithSizeOfContent(QTextEdit): def sizeHint(self): """Give size hint using width of text.""" - return QSize(self.document().size().toSize()) + size = QSize(self.document().size().toSize()) + # NOTE that the following is not respected for dimensions below 100,100 + size.setWidth(max(100, size.width())) + size.setHeight(max(100, size.height())) + return size # NOTE: calling updateGeometry here is not the best solution, refer to # LineEditWithSizeOfContent and TableWithSizeOfContent. Should update this diff --git a/soitool/modules/module_code_phrase.py b/soitool/modules/module_code_phrase.py index b64dc16..136f566 100644 --- a/soitool/modules/module_code_phrase.py +++ b/soitool/modules/module_code_phrase.py @@ -14,6 +14,7 @@ from soitool.modules.module_base import ( HEADLINE_FONT, event_is_ctrl_plus, event_is_ctrl_minus, + TABLE_CELL_DEFAULT_FONT, ) from soitool.modules.fit_to_contents_widgets import ( TableWithSizeOfContent, @@ -53,8 +54,14 @@ class CodePhraseModule(ModuleBase, QWidget, metaclass=Meta): Parameters ---------- + data : list + Data to initialize module from. See `self.initialize_from_data` for + format. database : soitool.database.Database Database to fetch categoried words from. + category : str + Category to fetch words from to use as codes. If not passed a unique + category will be chosen at random. Raises ------ @@ -65,7 +72,7 @@ class CodePhraseModule(ModuleBase, QWidget, metaclass=Meta): used_categories = [] """List of categories that are used by other instances of this class.""" - def __init__(self, size=None, data=None, database=None): + def __init__(self, data=None, database=None, category=None): self.type = CodePhraseModule.__name__ QWidget.__init__(self) ModuleBase.__init__(self) @@ -73,6 +80,7 @@ class CodePhraseModule(ModuleBase, QWidget, metaclass=Meta): self.line_edit_header = LineEditWithSizeOfContent("Kodefraser") self.line_edit_header.setFont(HEADLINE_FONT) self.table = TableWithSizeOfContent(0, 2) + self.table.setFont(TABLE_CELL_DEFAULT_FONT) # Qt should let this widget be exactly the size of it's sizeHint self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) @@ -93,7 +101,7 @@ class CodePhraseModule(ModuleBase, QWidget, metaclass=Meta): # Initialize either default values or from data. Either way from this # point on the widget should never have less than one row - if data is None and size is None: + if data is None: self.initialize_default() else: self.initialize_from_data(data) @@ -122,18 +130,26 @@ class CodePhraseModule(ModuleBase, QWidget, metaclass=Meta): self.set_code_table_item(i, row[CODE_COLUMN]) self.set_phrase_table_item(i, row[PHRASE_COLUMN]) - def initialize_default(self): + def initialize_default(self, category=None): """Initialize to default values. Prepares `self.category` and `self.available_words` from `self.database`. + Parameters + ---------- + category : str + Category to use. If 'None' will choose one from the database. + Raises ------ NoMoreAvailableCategories Indicates that there are no more categories to choose from. """ - self.category = self.get_category() + if category is not None: + self.category = category + else: + self.category = self.get_category() self.available_words = self.database.get_category_words(self.category) for _ in range(START_ROWS): self.add_row() diff --git a/test/test_resolution_smoke_test.py b/test/test_resolution_smoke_test.py index 3bc6fcb..a5c33b4 100644 --- a/test/test_resolution_smoke_test.py +++ b/test/test_resolution_smoke_test.py @@ -20,6 +20,7 @@ from soitool.modules.module_authentication_board import ( ) from soitool.modules.module_subtractorcodes import SubtractorcodesModule from soitool.modules.module_predefined_codes import PredefinedCodesModule +from soitool.modules.module_code_phrase import CodePhraseModule from soitool.new_module_dialog import MODULE_CHOICES from soitool.soi_workspace_widget import DATABASE_MODULES from soitool.database import Database @@ -107,8 +108,9 @@ class TestModulesAcrossResolutions(unittest.TestCase): {"x": 0, "y": 0, "page": 1, "name": "PredefinedCodesModule"}, {"x": 654, "y": 0, "page": 1, "name": "AuthenticationBoardModule"}, {"x": 1061.5, "y": 0, "page": 1, "name": "SubtractorcodesModule"}, - {"x": 1287.0, "y": 0, "page": 1, "name": "FreeTextModule"}, - {"x": 1387.0, "y": 0, "page": 1, "name": "TableModule"}, + {"x": 1287.0, "y": 0, "page": 1, "name": "CodePhraseModule"}, + {"x": 1489.0, "y": 0, "page": 1, "name": "FreeTextModule"}, + {"x": 1589.0, "y": 0, "page": 1, "name": "TableModule"}, ] # For use with modules that require a database @@ -137,9 +139,19 @@ class TestModulesAcrossResolutions(unittest.TestCase): QTimer.singleShot(0, press_enter) if module in DATABASE_MODULES: - soi.add_module(module.__name__, module(database=database)) + if module == CodePhraseModule: + # CodePhraseModule needs a category injected in it's init. + # Choosing "Tresort" because it contains words short enough to + # not expand the module beyond default width + module_instance = module( + database=database, category="Tresort" + ) + else: + module_instance = module(database=database) else: - soi.add_module(module.__name__, module()) + module_instance = module() + + soi.add_module(module.__name__, module_instance) soi.reorganize() -- GitLab