Skip to content

Commit

Permalink
Understandable change for the best, no more gettattrs, setattrs(I AM …
Browse files Browse the repository at this point in the history
…FUCKIN STUPD AND DUMB STUPID AND DUMB WTF< WHY I DIID NOT USE THE SETPREOPRTY AND PEORPTRY QOBJECT METHODS I AM DUMB, HLLO??)
  • Loading branch information
ZyMa-1 committed Jan 18, 2024
1 parent c3c8712 commit d9cc27e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@


class ConwaysGameOfLifeConfigManager(QObject):
"""Class for saving and loading widget properties"""
"""
Class for saving and loading widget properties to '.json' format.
"""

def __init__(self, conways_game_of_life_widget: ConwaysGameOfLife, parent=None):
super().__init__(parent)
Expand Down Expand Up @@ -66,11 +68,11 @@ def load_config(self) -> Optional[str]:
def _save_properties(self):
self._property_dict.clear()
for name in self.conways_game_of_life_widget.savable_properties_names():
value = self.conways_game_of_life_widget.get_property(name)
value = self.conways_game_of_life_widget.property(name)
value = to_json_type(value)
self._property_dict[name] = value

def _load_properties(self):
for name, value in self._property_dict.items():
value = to_property_type(value)
self.conways_game_of_life_widget.set_property(name, value)
self.conways_game_of_life_widget.setProperty(name, value)
75 changes: 47 additions & 28 deletions src/conways_game_of_life/ConwaysGameOfLife.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from enum import Enum
from typing import Optional, Tuple, List, Any
from typing import Optional, Tuple, List

from PySide6.QtCore import QPoint, QTimer, Qt, Signal, Slot, Property, QPointF, QSizeF, QRectF, QSize
from PySide6.QtGui import QPainter, QColor
from PySide6.QtWidgets import QWidget

from .utils import property_setter_error_handle, ColorProperty, PatternSchema
from .ConwaysGameOfLifeEngine import CELL_ALIVE, CELL_DEAD, ConwaysGameOfLifeEngine
from .ConwaysGameOfLifeEngine import CELL_ALIVE, CELL_DEAD, ConwaysGameOfLifeEngine, StateMatrixT

MINIMUM_SIZE = (322, 322)

Expand Down Expand Up @@ -327,7 +327,7 @@ def resizeEvent(self, event):
super().resizeEvent(event)

# Signal suffix for the properties signals
_signal_suffix = "_changed"
_SIGNAL_SUFFIX = "_changed"

# Properties signals
is_game_running_changed = Signal(bool)
Expand All @@ -341,44 +341,63 @@ def resizeEvent(self, event):
# Read only
is_game_running = Property(bool, get_is_game_running, notify=is_game_running_changed)

# Wrapped properties and signals of the engine
##############
##############
def get_state(self):
return self.engine.get_state()

def set_state(self, value: StateMatrixT):
self.engine.set_state(value)

def get_cols(self):
return self.engine.get_cols()

def set_cols(self, value: int):
self.engine.set_cols(value)

def get_rows(self):
return self.engine.get_rows()

def set_rows(self, value: int):
self.engine.set_rows(value)

def get_turn_number(self):
return self.engine.get_turn_number()

# Properties signals
turn_number_changed = Signal(int)

# Pyqt properties (notify is not 'automatic'):
state = Property(list, get_state, set_state) # Does not work in Qt-Designer for some reason
cols = Property(int, get_cols, set_cols)
rows = Property(int, get_rows, set_rows)
# read_only
turn_number = Property(int, get_turn_number, notify=turn_number_changed)
##############
##############

# Stuff to json serialize the widget
_self_savable_properties = \
_SELF_SAVABLE_PROPERTIES = \
["turn_duration",
"border_thickness",
"border_color",
"cell_dead_color",
"cell_alive_color"]
_engine_savable_properties = \
_ENGINE_SAVABLE_PROPERTIES = \
["cols",
"rows",
"state"]
_savable_properties = _self_savable_properties + _engine_savable_properties

# Methods for interacting with widget/engine properties
def __objs(self):
return [self, self.engine]

def get_property(self, name: str):
for obj in self.__objs():
if isinstance(getattr(type(obj), name, None), Property):
return getattr(obj, name)
raise ValueError
_SAVABLE_PROPERTIES = _SELF_SAVABLE_PROPERTIES + _ENGINE_SAVABLE_PROPERTIES

# Extra methods regarding properties and their signals
def get_property_changed_signal(self, name: str) -> Signal:
name += self._signal_suffix
for obj in self.__objs():
if isinstance(signal := getattr(obj, name, None), Signal):
return signal
raise ValueError

def set_property(self, name: str, value: Any):
for obj in self.__objs():
if isinstance(getattr(type(obj), name, None), Property):
setattr(obj, name, value)
return
name += self._SIGNAL_SUFFIX
if isinstance(signal := getattr(self, name, None), Signal):
return signal
raise ValueError

@classmethod
def savable_properties_names(cls) -> List[str]:
"""Returns list of savable properties associated specifically with this widget"""
return cls._savable_properties
return cls._SAVABLE_PROPERTIES
12 changes: 6 additions & 6 deletions src/conways_game_of_life/PatternsDataLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def _error_catch_slot(*args: str):

game_widget = ConwaysGameOfLife()
game_widget.property_setter_error_signal.connect(_error_catch_slot)
game_widget.set_property('rows', parsed_data["rows"])
game_widget.set_property('cols', parsed_data["cols"])
game_widget.set_property('state', parsed_data["state"])
game_widget.setProperty('rows', parsed_data["rows"])
game_widget.setProperty('cols', parsed_data["cols"])
game_widget.setProperty('state', parsed_data["state"])
game_widget.property_setter_error_signal.disconnect(_error_catch_slot)
del game_widget

Expand All @@ -80,9 +80,9 @@ def _error_catch_slot(*args: str):
def generate_pixmap(parsed_data: dict) -> QPixmap:
game_widget = ConwaysGameOfLife()
game_widget._active_cell = (-1, -1)
game_widget.set_property('rows', parsed_data["rows"])
game_widget.set_property('cols', parsed_data["cols"])
game_widget.set_property('state', parsed_data["state"])
game_widget.setProperty('rows', parsed_data["rows"])
game_widget.setProperty('cols', parsed_data["cols"])
game_widget.setProperty('state', parsed_data["state"])

pixmap = QPixmap(game_widget.size())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class ConwaysGameOfLifePropertiesManager(QObject):
"""
Class for linking game properties to the widgets representing them and vice versa.
Class for linking game properties to the widgets values representing them and vice versa.
"""

def __init__(self, game_widget: ConwaysGameOfLife, parent=None):
Expand All @@ -33,7 +33,7 @@ def connect_widget_and_property(self, widget: QWidget, property_name: str,
disable widget to property connection.
"""
# Exception can occur in that, so catching it and raising another one is meh
value = self.game_widget.get_property(property_name)
value = self.game_widget.property(property_name)
signal = None
if property_has_signal:
signal = self.game_widget.get_property_changed_signal(property_name)
Expand All @@ -48,9 +48,9 @@ def connect_widget_and_property(self, widget: QWidget, property_name: str,
def assign_widget_values_to_properties(self):
for name, widget in self._property_to_widget.items():
value = convert_widget_value(widget, name)
self.game_widget.set_property(name, value)
self.game_widget.setProperty(name, value)

def assign_properties_values_to_widgets(self):
for name, slot in self._property_to_widget_slot.items():
value = self.game_widget.get_property(name)
value = self.game_widget.property(name)
slot(value)

0 comments on commit d9cc27e

Please sign in to comment.