Skip to content

Commit

Permalink
Fix: usability + setts loading (#172)
Browse files Browse the repository at this point in the history
* fix: little fix on the project name

* fix: update load settings method

* fix: do not raise
  • Loading branch information
lvjonok authored Jan 26, 2025
1 parent 2969972 commit a537177
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 60 deletions.
66 changes: 8 additions & 58 deletions src/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,72 +828,22 @@ def load_settings_file(self):
filename = str(Path(filename))
if file_ext == ".YAML":
try:
# TODO: right now to maintain good transfer
# we need to copy the project_path setting manually
# everything else will work alright
old_project_path = sett().project_path
load_settings(filename)
sett().project_path = old_project_path
self.display_settings()
except:
showErrorDialog("Error during reading settings file")
except Exception as e:
showErrorDialog("Error during reading settings file: " + str(e))
else:
showErrorDialog("This file format isn't supported:" + file_ext)
except IOError as e:
showErrorDialog("Error during file opening:" + str(e))

def display_settings(self):
s = sett()
self.view.line_width_value.setText(str(s.slicing.line_width))
self.view.layer_height_value.setText(str(s.slicing.layer_height))
self.view.wall_thickness_value.setText(str(s.slicing.wall_thickness))
self.view.number_of_bottom_layers_value.setText(str(s.slicing.bottoms_depth))
self.view.number_of_lid_layers_value.setText(str(s.slicing.lids_depth))
self.view.extruder_temp_value.setText(str(s.slicing.extruder_temperature))
self.view.bed_temp_value.setText(str(s.slicing.bed_temperature))
self.view.skirt_line_count_value.setText(str(s.slicing.skirt_line_count))
self.view.fan_speed_value.setText(str(s.slicing.fan_speed))
if s.slicing.fan_off_layer1:
self.view.fan_off_layer1_box.setCheckState(QtCore.Qt.Checked)
else:
self.view.fan_off_layer1_box.setCheckState(QtCore.Qt.Unchecked)
self.view.print_speed_value.setText(str(s.slicing.print_speed))
self.view.print_speed_layer1_value.setText(str(s.slicing.print_speed_layer1))
self.view.print_speed_wall_value.setText(str(s.slicing.print_speed_wall))
ind = locales.getLocaleByLang("en").FillingTypeValues.index(
s.slicing.filling_type
)
self.view.filling_type_values.setCurrentIndex(ind)
self.view.fill_density_value.setText(str(s.slicing.fill_density))
self.view.overlapping_infill_value.setText(
str(s.slicing.overlapping_infill_percentage)
)
if s.slicing.retraction_on:
self.view.retraction_on_box.setCheckState(QtCore.Qt.Checked)
else:
self.view.retraction_on_box.setCheckState(QtCore.Qt.Unchecked)
self.view.retraction_distance_value.setText(str(s.slicing.retraction_distance))
self.view.retraction_speed_value.setText(str(s.slicing.retraction_speed))
self.view.retract_compensation_amount_value.setText(
str(s.slicing.retract_compensation_amount)
)
if s.supports.enabled:
self.view.supports_on_box.setCheckState(QtCore.Qt.Checked)
else:
self.view.supports_on_box.setCheckState(QtCore.Qt.Unchecked)
self.view.support_density_value.setText(str(s.supports.fill_density))
ind = locales.getLocaleByLang("en").FillingTypeValues.index(
s.supports.fill_type
)
self.view.support_fill_type_values.setCurrentIndex(ind)
self.view.support_xy_offset_value.setText(str(s.supports.xy_offset))
self.view.support_z_offset_layers_value.setText(str(s.supports.z_offset_layers))
if s.supports.priority_z_offset:
self.view.support_priority_z_offset_box.setCheckState(QtCore.Qt.Checked)
else:
self.view.support_priority_z_offset_box.setCheckState(QtCore.Qt.Unchecked)
self.view.supports_number_of_bottom_layers_value.setText(
str(s.supports.bottoms_depth)
)
self.view.supports_number_of_lid_layers_value.setText(
str(int(s.supports.lids_depth))
)
self.view.colorize_angle_value.setText(str(s.slicing.angle))
self.view.setts.reload()

def colorize_model(self):
shutil.copyfile(PathBuilder.stl_model_temp(), PathBuilder.colorizer_stl())
Expand Down
5 changes: 5 additions & 0 deletions src/entry_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ def create_new_project(self):

import pathlib

# new project name should not have any spaces, join with underscore
self.project_name_text_edit.setText(
self.project_name_text_edit.text().replace(" ", "_")
)

full_path = pathlib.Path(
self.project_directory_edit.text(), self.project_name_text_edit.text()
)
Expand Down
42 changes: 40 additions & 2 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,17 @@ def add_recent_project(recent_projects, project_path):


def load_settings(filename=""):
global _sett
old_setts = _sett

data = read_settings(filename)
if data != None:
global _sett
_sett = Settings(data)

print(f"after loading stl_file is {_sett.slicing.stl_file}")
# check if the format is similar
if old_setts is not None and not old_setts.has_same_attributes(_sett):
_sett = old_setts
raise Exception("Check the settings file")


def read_settings(filename=""):
Expand Down Expand Up @@ -335,6 +340,7 @@ def __eq__(self, other):
ignore_attributes = [
"splanes_file",
"figures",
"project_path",
"print_time",
"consumption_material",
"planes_contact_with_nozzle",
Expand All @@ -360,6 +366,38 @@ def __eq__(self, other):

return True

def has_same_attributes(self, other):
if not isinstance(other, Settings):
return False

ignore_attributes = [
"printer_dir",
"project_path",
"splanes_file",
"figures",
"print_time",
"consumption_material",
"planes_contact_with_nozzle",
]

# try to compare attributes from left to right
for attr in self.__dict__:
if attr in ignore_attributes:
continue
if not hasattr(other, attr):
print(f"Attribute {attr} not found in other")
return False

# try to compare attributes from right to left
for attr in other.__dict__:
if attr in ignore_attributes:
continue
if not hasattr(self, attr):
print(f"Attribute {attr} not found in self")
return False

return True


class PathBuilder:
# class to build paths to files and folders
Expand Down
52 changes: 52 additions & 0 deletions src/settings_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def __init__(self, parent=None, settings_provider: callable = None):
self.setLayout(self.panel)

self.__elements = {}
self.__order = [] # order of keys in the panel from top to bottom

self.locale: locales.Locale = locales.getLocale()

Expand Down Expand Up @@ -144,6 +145,54 @@ def __init__(self, parent=None, settings_provider: callable = None):
"support_number_of_lid_layers": self.locale.NumberOfLidLayers,
}

def reload(self):
# reloads all settings from the sett
# right now we do it by deleting previous elements
# and creating new ones with the same order

# TODO: a little bit of copying, but it's ok for now
def get_row_widgets(row_idx):
widgets = []
for i in range(7): # TODO: move to constant
item = self.panel.itemAtPosition(row_idx, i)
if item is None:
continue
if len(widgets) != 0 and item.widget() == widgets[-1]:
continue

widgets.append((item.widget(), i))
return widgets

def remove_row(row_idx):
dlt_row = get_row_widgets(row_idx)
for _, col_idx in dlt_row:
self.panel.itemAtPosition(row_idx, col_idx).widget().deleteLater()

# find key by row index
key = None
for k in self.__elements:
if self.__elements[k]["row_idx"] == row_idx:
key = k
break

# remove key from elements
del self.__elements[key]

for key in self.__order:
try:
row_idx = self.__elements[key]["row_idx"]
remove_row(row_idx)
except KeyError:
print(f"Key {key} not found in elements")
pass

self.__current_row = 1
copied_order = self.__order.copy()
self.__elements = {}
self.__order = []
for key in copied_order:
self = self.with_sett(key)

@property
def cur_row(self):
return self.__current_row
Expand Down Expand Up @@ -349,6 +398,8 @@ def with_sett(self, name: str):
if name in self.__elements:
return self

self.__order.append(name)

# we match the given name with each setting and add it to the layout
if name == "printer_path":
# self.ensure_sett("hardware.printer_path")
Expand Down Expand Up @@ -390,6 +441,7 @@ def with_sett(self, name: str):
"label": label,
"edit": printer_path_edit,
"add_btn": printer_add_btn,
"sett_path": "hardware.printer_dir",
}
elif name == "uninterrupted_print":
self.ensure_sett("uninterrupted_print.enabled")
Expand Down

0 comments on commit a537177

Please sign in to comment.