- added Preferences values for PDF margins when saving text in Code Editor as PDF

- when clicking Cancel in Preferences now the values are reverted to what they used to be before opening Preferences tab and start changing values
This commit is contained in:
Marius Stanciu 2019-12-19 16:26:19 +02:00
parent 04d30fb1b4
commit 80f1d30a28
5 changed files with 167 additions and 28 deletions

View File

@ -416,6 +416,10 @@ class App(QtCore.QObject):
"global_stats": dict(),
"global_tabs_detachable": True,
"global_jump_ref": 'abs',
"global_tpdf_tmargin": 15.0,
"global_tpdf_bmargin": 10.0,
"global_tpdf_lmargin": 20.0,
"global_tpdf_rmargin": 20.0,
# General
"global_graphic_engine": '3D',
@ -978,6 +982,10 @@ class App(QtCore.QObject):
self.current_units = self.defaults['units']
# store here the current self.defaults so it can be restored if Preferences changes are cancelled
self.current_defaults = dict()
self.current_defaults.update(self.defaults)
# #############################################################################
# ##################### CREATE MULTIPROCESSING POOL ###########################
# #############################################################################
@ -1085,6 +1093,11 @@ class App(QtCore.QObject):
"global_bookmarks_limit": self.ui.general_defaults_form.general_app_group.bm_limit_spinner,
"global_machinist_setting": self.ui.general_defaults_form.general_app_group.machinist_cb,
"global_tpdf_tmargin": self.ui.general_defaults_form.general_app_group.tmargin_entry,
"global_tpdf_bmargin": self.ui.general_defaults_form.general_app_group.bmargin_entry,
"global_tpdf_lmargin": self.ui.general_defaults_form.general_app_group.lmargin_entry,
"global_tpdf_rmargin": self.ui.general_defaults_form.general_app_group.rmargin_entry,
# General GUI Preferences
"global_gridx": self.ui.general_defaults_form.general_gui_group.gridx_entry,
"global_gridy": self.ui.general_defaults_form.general_gui_group.gridy_entry,
@ -2940,17 +2953,25 @@ class App(QtCore.QObject):
except Exception as e:
log.debug("App.defaults_read_form() --> %s" % str(e))
def defaults_write_form(self, factor=None, fl_units=None):
def defaults_write_form(self, factor=None, fl_units=None, source_dict=None):
"""
Will set the values for all the GUI elements in Preferences GUI based on the values found in the
self.defaults dictionary.
:param factor: will apply a factor to the values that written in the GUI elements
:param fl_units: current measuring units in FlatCAM: Metric or Inch
:param source_dict: the repository of options, usually is the self.defaults
:return: None
"""
for option in self.defaults:
self.defaults_write_form_field(option, factor=factor, units=fl_units)
options_storage = self.defaults if source_dict is None else source_dict
for option in options_storage:
if source_dict:
self.defaults_write_form_field(option, factor=factor, units=fl_units, defaults_dict=source_dict)
else:
self.defaults_write_form_field(option, factor=factor, units=fl_units)
# try:
# self.defaults_form_fields[option].set_value(self.defaults[option])
# except KeyError:
@ -2958,7 +2979,7 @@ class App(QtCore.QObject):
# # TODO: Rethink this?
# pass
def defaults_write_form_field(self, field, factor=None, units=None):
def defaults_write_form_field(self, field, factor=None, units=None, defaults_dict=None):
"""
Basically it is the worker in the self.defaults_write_form()
@ -2967,21 +2988,23 @@ class App(QtCore.QObject):
:param units: current FLatCAM measuring units
:return: None, it updates GUI elements
"""
def_dict = self.defaults if defaults_dict is None else defaults_dict
try:
if factor is None:
if units is None:
self.defaults_form_fields[field].set_value(self.defaults[field])
self.defaults_form_fields[field].set_value(def_dict[field])
elif units == 'IN' and (field == 'global_gridx' or field == 'global_gridy'):
self.defaults_form_fields[field].set_value(self.defaults[field])
self.defaults_form_fields[field].set_value(def_dict[field])
elif units == 'MM' and (field == 'global_gridx' or field == 'global_gridy'):
self.defaults_form_fields[field].set_value(self.defaults[field])
self.defaults_form_fields[field].set_value(def_dict[field])
else:
if units is None:
self.defaults_form_fields[field].set_value(self.defaults[field] * factor)
self.defaults_form_fields[field].set_value(def_dict[field] * factor)
elif units == 'IN' and (field == 'global_gridx' or field == 'global_gridy'):
self.defaults_form_fields[field].set_value((self.defaults[field] * factor))
self.defaults_form_fields[field].set_value((def_dict[field] * factor))
elif units == 'MM' and (field == 'global_gridx' or field == 'global_gridy'):
self.defaults_form_fields[field].set_value((self.defaults[field] * factor))
self.defaults_form_fields[field].set_value((def_dict[field] * factor))
except KeyError:
# self.log.debug("defaults_write_form(): No field for: %s" % option)
# TODO: Rethink this?
@ -3891,6 +3914,10 @@ class App(QtCore.QObject):
_("Failed to parse defaults file."))
return
self.defaults.update(defaults_from_file)
# update the dict that is used to restore the values in the defaults form if Cancel is clicked in the
# Preferences window
self.current_defaults.update(defaults_from_file)
self.on_preferences_edited()
self.inform.emit('[success] %s: %s' %
(_("Imported Defaults from"), filename))
@ -5764,14 +5791,15 @@ class App(QtCore.QObject):
"tools_cr_trace_size_val", "tools_cr_c2c_val", "tools_cr_c2o_val", "tools_cr_s2s_val",
"tools_cr_s2sm_val", "tools_cr_s2o_val", "tools_cr_sm2sm_val", "tools_cr_ri_val",
"tools_cr_h2h_val", "tools_cr_dh_val", "tools_fiducials_dia", "tools_fiducials_margin",
"tools_fiducials_mode", "tools_fiducials_second_pos", "tools_fiducials_type",
"tools_fiducials_line_thickness",
"tools_copper_thieving_clearance", "tools_copper_thieving_margin",
"tools_copper_thieving_dots_dia", "tools_copper_thieving_dots_spacing",
"tools_copper_thieving_squares_size", "tools_copper_thieving_squares_spacing",
"tools_copper_thieving_lines_size", "tools_copper_thieving_lines_spacing",
"tools_copper_thieving_rb_margin", "tools_copper_thieving_rb_thickness",
'global_gridx', 'global_gridy', 'global_snap_max', "global_tolerance"]
'global_gridx', 'global_gridy', 'global_snap_max', "global_tolerance",
'global_tpdf_bmargin', 'global_tpdf_tmargin', 'global_tpdf_rmargin', 'global_tpdf_lmargin']
def scale_defaults(sfactor):
for dim in dimensions:
@ -5796,6 +5824,7 @@ class App(QtCore.QObject):
tools_diameters = [eval(a) for a in tools_string if a != '']
except Exception as e:
log.debug("App.on_toggle_units().scale_options() --> %s" % str(e))
continue
self.defaults['geometry_cnctooldia'] = ''
for t in range(len(tools_diameters)):
@ -5808,6 +5837,7 @@ class App(QtCore.QObject):
ncctools = [eval(a) for a in tools_string if a != '']
except Exception as e:
log.debug("App.on_toggle_units().scale_options() --> %s" % str(e))
continue
self.defaults['tools_ncctools'] = ''
for t in range(len(ncctools)):
@ -5820,6 +5850,7 @@ class App(QtCore.QObject):
sptools = [eval(a) for a in tools_string if a != '']
except Exception as e:
log.debug("App.on_toggle_units().scale_options() --> %s" % str(e))
continue
self.defaults['tools_solderpaste_tools'] = ""
for t in range(len(sptools)):
@ -5839,6 +5870,7 @@ class App(QtCore.QObject):
val = float(self.defaults[dim]) * sfactor
except Exception as e:
log.debug('App.on_toggle_units().scale_defaults() --> %s' % str(e))
continue
self.defaults[dim] = float('%.*f' % (self.decimals, val))
else:
@ -5847,6 +5879,7 @@ class App(QtCore.QObject):
val = float(self.defaults[dim]) * sfactor
except Exception as e:
log.debug('App.on_toggle_units().scale_defaults() --> %s' % str(e))
continue
self.defaults[dim] = float('%.*f' % (self.decimals, val))
else:
@ -5855,7 +5888,8 @@ class App(QtCore.QObject):
try:
val = float(self.defaults[dim]) * sfactor
except Exception as e:
log.debug('App.on_toggle_units().scale_defaults() --> %s' % str(e))
log.debug('App.on_toggle_units().scale_defaults() --> Value: %s %s' % (str(dim), str(e)))
continue
self.defaults[dim] = val
@ -7029,6 +7063,9 @@ class App(QtCore.QObject):
self.inform.emit('%s' % _("Preferences applied."))
# make sure we update the self.current_defaults dict used to undo changes to self.defaults
self.current_defaults.update(self.defaults)
if save_to_file:
self.save_defaults(silent=False)
# load the defaults so they are updated into the app
@ -7067,7 +7104,18 @@ class App(QtCore.QObject):
except TypeError:
pass
self.defaults_write_form()
try:
self.ui.general_defaults_form.general_app_group.units_radio.activated_custom.disconnect()
except (TypeError, AttributeError):
pass
self.defaults_write_form(source_dict=self.current_defaults)
self.ui.general_defaults_form.general_app_group.units_radio.activated_custom.connect(
lambda: self.on_toggle_units(no_pref=False))
self.defaults.update(self.current_defaults)
# shared_items = {k: self.defaults[k] for k in self.defaults if k in self.current_defaults and
# self.defaults[k] == self.current_defaults[k]}
# print(len(self.defaults), len(shared_items))
# Preferences save, update the color of the Preferences Tab text
for idx in range(self.ui.plot_tab_area.count()):
@ -7797,8 +7845,7 @@ class App(QtCore.QObject):
pass
def on_preferences_edited(self):
self.inform.emit('[WARNING_NOTCL] %s' %
_("Preferences edited but not saved."))
self.inform.emit('[WARNING_NOTCL] %s' % _("Preferences edited but not saved."))
for idx in range(self.ui.plot_tab_area.count()):
if self.ui.plot_tab_area.tabText(idx) == _("Preferences"):

View File

@ -12,6 +12,9 @@ CAD program, and create G-Code for Isolation routing.
19.12.2019
- in 2-Sided Tool added a way to calculate the bounding box values for a selection of objects, and also the centroid
- in 2-Sided Tool fixed the Reset Tool button handler to reset the bounds value too; changed a string
- added Preferences values for PDF margins when saving text in Code Editor as PDF
- when clicking Cancel in Preferences now the values are reverted to what they used to be before opening Preferences tab and start changing values
18.12.2019

View File

@ -196,7 +196,7 @@ class TextEditor(QtWidgets.QWidget):
_filter_ = filt
else:
_filter_ = "G-Code Files (*.nc);; G-Code Files (*.txt);; G-Code Files (*.tap);; G-Code Files (*.cnc);; " \
"All Files (*.*)"
"PDF Files (*.pdf);;All Files (*.*)"
if name:
obj_name = name
@ -206,7 +206,7 @@ class TextEditor(QtWidgets.QWidget):
except AttributeError:
obj_name = 'file'
if filt is None:
_filter_ = "FlatConfig Files (*.FlatConfig);;All Files (*.*)"
_filter_ = "FlatConfig Files (*.FlatConfig);;PDF Files (*.pdf);;All Files (*.*)"
try:
filename = str(QtWidgets.QFileDialog.getSaveFileName(
@ -237,13 +237,24 @@ class TextEditor(QtWidgets.QWidget):
styleH = styles['Heading1']
story = []
if self.app.defaults['units'].lower() == 'mm':
bmargin = self.app.defaults['global_tpdf_bmargin'] * mm
tmargin = self.app.defaults['global_tpdf_tmargin'] * mm
rmargin = self.app.defaults['global_tpdf_rmargin'] * mm
lmargin = self.app.defaults['global_tpdf_lmargin'] * mm
else:
bmargin = self.app.defaults['global_tpdf_bmargin'] * inch
tmargin = self.app.defaults['global_tpdf_tmargin'] * inch
rmargin = self.app.defaults['global_tpdf_rmargin'] * inch
lmargin = self.app.defaults['global_tpdf_lmargin'] * inch
doc = SimpleDocTemplate(
filename,
pagesize=page_size,
bottomMargin=0.4 * inch,
topMargin=0.6 * inch,
rightMargin=0.8 * inch,
leftMargin=0.8 * inch)
bottomMargin=bmargin,
topMargin=tmargin,
rightMargin=rmargin,
leftMargin=lmargin)
P = Paragraph(lined_gcode, styleN)
story.append(P)

View File

@ -1330,6 +1330,76 @@ class GeneralAppPrefGroupUI(OptionsGroupUI):
grid0.addWidget(self.machinist_cb, 21, 0, 1, 2)
separator_line = QtWidgets.QFrame()
separator_line.setFrameShape(QtWidgets.QFrame.HLine)
separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.layout.addWidget(separator_line)
self.layout.addWidget(QtWidgets.QLabel(''))
grid1 = QtWidgets.QGridLayout()
self.layout.addLayout(grid1)
grid1.setColumnStretch(0, 0)
grid1.setColumnStretch(1, 1)
self.pdf_param_label = QtWidgets.QLabel('<B>%s:</b>' % _("Text to PDF parameters"))
self.pdf_param_label.setToolTip(
_("Used when saving text in Code Editor or in FlatCAM Document objects.")
)
grid1.addWidget(self.pdf_param_label, 0, 0, 1, 2)
# Top Margin value
self.tmargin_entry = FCDoubleSpinner()
self.tmargin_entry.set_precision(self.decimals)
self.tmargin_entry.set_range(0.0000, 9999.9999)
self.tmargin_label = QtWidgets.QLabel('%s:' % _("Top Margin"))
self.tmargin_label.setToolTip(
_("Distance between text body and the top of the PDF file.")
)
grid1.addWidget(self.tmargin_label, 1, 0)
grid1.addWidget(self.tmargin_entry, 1, 1)
# Bottom Margin value
self.bmargin_entry = FCDoubleSpinner()
self.bmargin_entry.set_precision(self.decimals)
self.bmargin_entry.set_range(0.0000, 9999.9999)
self.bmargin_label = QtWidgets.QLabel('%s:' % _("Bottom Margin"))
self.bmargin_label.setToolTip(
_("Distance between text body and the bottom of the PDF file.")
)
grid1.addWidget(self.bmargin_label, 2, 0)
grid1.addWidget(self.bmargin_entry, 2, 1)
# Left Margin value
self.lmargin_entry = FCDoubleSpinner()
self.lmargin_entry.set_precision(self.decimals)
self.lmargin_entry.set_range(0.0000, 9999.9999)
self.lmargin_label = QtWidgets.QLabel('%s:' % _("Left Margin"))
self.lmargin_label.setToolTip(
_("Distance between text body and the left of the PDF file.")
)
grid1.addWidget(self.lmargin_label, 3, 0)
grid1.addWidget(self.lmargin_entry, 3, 1)
# Right Margin value
self.rmargin_entry = FCDoubleSpinner()
self.rmargin_entry.set_precision(self.decimals)
self.rmargin_entry.set_range(0.0000, 9999.9999)
self.rmargin_label = QtWidgets.QLabel('%s:' % _("Right Margin"))
self.rmargin_label.setToolTip(
_("Distance between text body and the right of the PDF file.")
)
grid1.addWidget(self.rmargin_label, 4, 0)
grid1.addWidget(self.rmargin_entry, 4, 1)
self.layout.addStretch()
if sys.platform != 'win32':

View File

@ -281,13 +281,12 @@ class DblSidedTool(FlatCAMTool):
self.drill_dia.set_precision(self.decimals)
self.drill_dia.set_range(0.0000, 9999.9999)
self.dd_label = QtWidgets.QLabel('%s:' % _("Drill dia"))
self.dd_label.setToolTip(
self.drill_dia.setToolTip(
_("Diameter of the drill for the "
"alignment holes.")
)
grid0.addWidget(self.dd_label, 1, 0)
grid0.addWidget(self.drill_dia, 1, 1)
grid0.addWidget(self.drill_dia, 1, 0, 1, 2)
# ## Buttons
self.create_alignment_hole_button = QtWidgets.QPushButton(_("Create Excellon Object"))
@ -309,6 +308,8 @@ class DblSidedTool(FlatCAMTool):
separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.layout.addWidget(separator_line)
self.layout.addWidget(QtWidgets.QLabel(''))
grid1 = QtWidgets.QGridLayout()
self.layout.addLayout(grid1)
grid1.setColumnStretch(0, 0)
@ -384,7 +385,7 @@ class DblSidedTool(FlatCAMTool):
grid1.addWidget(self.center_entry, 5, 1)
# Calculate Bounding box
self.calculate_bb_button = QtWidgets.QPushButton(_("Calculate Bounding Box"))
self.calculate_bb_button = QtWidgets.QPushButton(_("Calculate Bounds Values"))
self.calculate_bb_button.setToolTip(
_("Calculate the enveloping rectangular shape coordinates,\n"
"for the selection of objects.\n"
@ -419,7 +420,6 @@ class DblSidedTool(FlatCAMTool):
self.mirror_geo_button.clicked.connect(self.on_mirror_geo)
self.add_point_button.clicked.connect(self.on_point_add)
self.add_drill_point_button.clicked.connect(self.on_drill_add)
self.reset_button.clicked.connect(self.reset_fields)
self.box_combo_type.currentIndexChanged.connect(self.on_combo_box_type)
self.axis_location.group_toggle_fn = self.on_toggle_pointbox
@ -427,6 +427,8 @@ class DblSidedTool(FlatCAMTool):
self.create_alignment_hole_button.clicked.connect(self.on_create_alignment_holes)
self.calculate_bb_button.clicked.connect(self.on_bbox_coordinates)
self.reset_button.clicked.connect(self.set_tool_ui)
self.drill_values = ""
def install(self, icon=None, separator=None, **kwargs):
@ -469,6 +471,12 @@ class DblSidedTool(FlatCAMTool):
self.axis_location.set_value(self.app.defaults["tools_2sided_axis_loc"])
self.drill_dia.set_value(self.app.defaults["tools_2sided_drilldia"])
self.xmin_entry.set_value(0.0)
self.ymin_entry.set_value(0.0)
self.xmax_entry.set_value(0.0)
self.ymax_entry.set_value(0.0)
self.center_entry.set_value('')
def on_combo_box_type(self):
obj_type = self.box_combo_type.currentIndex()
self.box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))