diff --git a/src/simudator/gui/module_graphics_item/memory_graphic.py b/src/simudator/gui/module_graphics_item/memory_graphic.py index 08678176e46af9d3fcca35e089b2802fcddd2540..b77f0b8602500d2e68d691bea826b3cad5d10316 100644 --- a/src/simudator/gui/module_graphics_item/memory_graphic.py +++ b/src/simudator/gui/module_graphics_item/memory_graphic.py @@ -32,15 +32,10 @@ from simudator.gui.port_graphics_item import PortGraphicsItem class Base(Enum): NONE = 0 - DECIMAL = 1 - BINARY = 2 - HEXADECIMAL = 3 - - -class Sign(Enum): - NONE = 0 - SIGNED = 1 - UNSIGNED = 2 + DECIMAL_SIGNED = 1 + DECIMAL_UNSIGNED = 2 + BINARY = 3 + HEXADECIMAL = 4 class MemoryWindow(QWidget): @@ -229,18 +224,18 @@ class IntegerMemoryWindow(QWidget): def __init__(self, memory_module: Memory, bit_length: int): super().__init__() - self._sign = Sign.NONE - # Create base buttons, they are exclusive by default # so need a seperate QButtonGroup since these three # have nothing to do with the edit/view buttons - self.decimal_button = QRadioButton("Decimal") + self.dec_signed_button = QRadioButton("Decimal Signed") + self.dec_unsigned_button = QRadioButton("Decimal Unsigned") self.bin_button = QRadioButton("Binary") self.hex_button = QRadioButton("Hexadecimal") self.base_group = QButtonGroup() - self.base_group.addButton(self.decimal_button, 1) - self.base_group.addButton(self.bin_button, 2) - self.base_group.addButton(self.hex_button, 3) + self.base_group.addButton(self.dec_signed_button, 1) + self.base_group.addButton(self.dec_unsigned_button, 2) + self.base_group.addButton(self.bin_button, 3) + self.base_group.addButton(self.hex_button, 4) # Create edit/view buttons, they are exclusive by default self.edit_button = QRadioButton("Edit") @@ -250,18 +245,11 @@ class IntegerMemoryWindow(QWidget): self.edit_group.addButton(self.view_button, 2) # Create signed/unsigned buttons, they are exclusive by default - self.signed_button = QRadioButton("Signed") - self.unsigned_button = QRadioButton("Unsigned") - self.signed_group = QButtonGroup() - self.signed_group.addButton(self.signed_button, 1) - self.signed_group.addButton(self.unsigned_button, 2) # Connect them to the 'set_base' function self.base_group.buttonClicked.connect(self._set_base) # Connect them to the 'set_edit' function self.edit_group.buttonClicked.connect(self._set_edit) - # Connect them to the 'set_sign' function - self.signed_group.buttonClicked.connect(self._set_sign) # Create a layout that expands vertically # so that the buttons can be displayed below the @@ -273,16 +261,12 @@ class IntegerMemoryWindow(QWidget): # Create a layout that expands horizontally, so that # all buttons appear in one row self.first_button_layout = QHBoxLayout() - self.first_button_layout.addWidget(self.decimal_button) + self.first_button_layout.addWidget(self.dec_signed_button) + self.first_button_layout.addWidget(self.dec_unsigned_button) self.first_button_layout.addWidget(self.bin_button) self.first_button_layout.addWidget(self.hex_button) self.layout.addLayout(self.first_button_layout) - self.sign_button_layout = QHBoxLayout() - self.sign_button_layout.addWidget(self.signed_button) - self.sign_button_layout.addWidget(self.unsigned_button) - self.layout.addLayout(self.sign_button_layout) - self.second_button_layout = QHBoxLayout() self.second_button_layout.addWidget(self.edit_button) self.second_button_layout.addWidget(self.view_button) @@ -290,12 +274,10 @@ class IntegerMemoryWindow(QWidget): # Set the default base to decimal and disable editing # from start - self.decimal_button.toggle() + self.bin_button.toggle() self.view_button.toggle() - self.signed_button.toggle() self._set_base() self._set_edit() - self._set_sign() # Sets the size of each column to the smallest possible # width that allows all content in each box to be visible @@ -304,29 +286,19 @@ class IntegerMemoryWindow(QWidget): ) def _set_base(self): - base = Base.DECIMAL pressed_button_id = self.base_group.checkedId() if pressed_button_id == 1: - base = Base.DECIMAL - if pressed_button_id == 2: + base = Base.DECIMAL_SIGNED + elif pressed_button_id == 2: + base = Base.DECIMAL_UNSIGNED + elif pressed_button_id == 3: base = Base.BINARY - if pressed_button_id == 3: + else: base = Base.HEXADECIMAL self._memory_table.set_base(base) self._memory_table.update() - def _set_sign(self): - sign = Sign.NONE - pressed_button_id = self.signed_group.checkedId() - if pressed_button_id == 1: - sign = Sign.SIGNED - if pressed_button_id == 2: - sign = Sign.UNSIGNED - - self._memory_table.set_sign(sign) - self._memory_table.update() - def _set_edit(self): pressed_button_id = self.edit_group.checkedId() if pressed_button_id == 1: @@ -366,7 +338,6 @@ class IntegerMemoryTable(QTableWidget): self.setHorizontalHeaderLabels(["+" + str(i) for i in range(4)]) self.make_table_uneditable() self.base = Base.NONE - self._sign = Sign.NONE self._errorMessageWidget = QErrorMessage() vertical_headers = [] @@ -393,7 +364,7 @@ class IntegerMemoryTable(QTableWidget): value = memory_content[i] row = i // self._column_size col = i % self._column_size - if self.base == Base.DECIMAL: + if self.base == Base.DECIMAL_UNSIGNED or self.base == Base.DECIMAL_SIGNED: value = self._get_dec_from_sign(value) elif self.base == Base.BINARY: value = str(bin(int(value)))[2:] # remove '0b' @@ -426,11 +397,8 @@ class IntegerMemoryTable(QTableWidget): def set_base(self, base: Base): self.base = base - def set_sign(self, sign: Sign): - self._sign = sign - def _get_dec_from_sign(self, value: int) -> str: - if self._sign == Sign.UNSIGNED: + if self.base == Base.DECIMAL_UNSIGNED: return str(int(value)) # Any value above or equal to 'threshold' must have the MSB set to 1 @@ -486,13 +454,13 @@ class IntegerMemoryTable(QTableWidget): value = int(value, 2) elif self.base == Base.HEXADECIMAL: value = int(value, 16) - elif self.base == Base.DECIMAL: + elif self.base == Base.DECIMAL_SIGNED or self.base == Base.DECIMAL_UNSIGNED: value = int(value) except ValueError: msg = None if self.base == Base.BINARY: msg = "You must enter a binary number (e.g. 0b101)." - elif self.base == Base.DECIMAL: + elif self.base == Base.DECIMAL_SIGNED or self.base == Base.DECIMAL_UNSIGNED: msg = "You must enter a decimal number (e.g. 107)." elif self.base == Base.HEXADECIMAL: msg = "You must enter a hexadecimal number (e.g. 0xc3)."