- upgraded the UI of Region sub-tool in the Gerber Editor

This commit is contained in:
Marius Stanciu
2022-05-26 00:20:43 +03:00
committed by Marius
parent 95066e352b
commit 4c22e52b08
3 changed files with 319 additions and 17 deletions

View File

@@ -19,6 +19,7 @@ from appEditors.grb_plugins.GrbPadArrayPlugin import GrbPadArrayEditorTool
from appEditors.grb_plugins.GrbTrackPlugin import GrbTrackEditorTool
from appEditors.grb_plugins.GrbSimplificationPlugin import SimplificationTool
from appEditors.grb_plugins.GrbCopyPlugin import CopyEditorTool
from appEditors.grb_plugins.GrberRegionPlugin import GrbRegionEditorTool
# import inspect
@@ -1110,25 +1111,11 @@ class RegionEditorGrb(ShapeToolEditorGrb):
DrawTool.__init__(self, draw_app)
self.name = 'region'
self.draw_app = draw_app
self.app = self.draw_app.app
self.dont_execute = False
self.steps_per_circle = self.draw_app.app.options["gerber_circle_steps"]
# try:
# size_ap = float(self.draw_app.storage_dict[self.draw_app.last_aperture_selected]['size'])
# except KeyError:
# msg = '[ERROR_NOTCL] %s' % _("You need to preselect a aperture in the Aperture Table that has a size.")
# self.draw_app.app.inform.emit(msg)
# try:
# QtGui.QGuiApplication.restoreOverrideCursor()
# except Exception:
# pass
# self.dont_execute = True
# self.draw_app.in_action = False
# self.complete = True
# self.draw_app.select_tool('select')
# return
# regions are added always in the 0 aperture
if 0 not in self.draw_app.storage_dict:
self.draw_app.on_aperture_add(apcode=0)
@@ -1152,18 +1139,37 @@ class RegionEditorGrb(ShapeToolEditorGrb):
self.cursor = QtGui.QCursor(QtGui.QPixmap(self.draw_app.app.resource_location + '/aero.png'))
QtGui.QGuiApplication.setOverrideCursor(self.cursor)
if self.app.use_3d_engine:
self.draw_app.app.plotcanvas.view.camera.zoom_callback = self.draw_cursor_data
self.draw_app.app.jump_signal.connect(lambda x: self.draw_app.update_utility_geometry(data=x))
self.reg_tool = GrbRegionEditorTool(self.app, self.draw_app, plugin_name=_("Region"))
self.reg_tool.run()
self.reg_tool.length = self.draw_app.last_length
self.ui = self.reg_tool.ui
self.set_plugin_ui()
self.draw_app.app.jump_signal.connect(lambda x: self.draw_app.update_utility_geometry(data=x))
self.draw_app.app.inform.emit(_('Corner Mode 1: 45 degrees ...'))
self.start_msg = _("Click on 1st point ...")
def set_plugin_ui(self):
dia = float(self.draw_app.storage_dict[self.draw_app.last_aperture_selected]['size'])
self.ui.dia_entry.set_value(dia)
self.ui.x_entry.set_value(float(self.draw_app.snap_x))
self.ui.y_entry.set_value(float(self.draw_app.snap_y))
def click(self, point):
self.draw_app.in_action = True
if self.inter_point is not None:
self.points.append(self.inter_point)
self.points.append(point)
self.draw_app.last_length = self.reg_tool.length
self.ui.x_entry.set_value(float(self.draw_app.snap_x))
self.ui.y_entry.set_value(float(self.draw_app.snap_y))
if len(self.points) > 0:
self.draw_app.app.inform.emit(_("Click on next Point or click right mouse button to complete ..."))
@@ -1392,6 +1398,56 @@ class RegionEditorGrb(ShapeToolEditorGrb):
self.draw_app.app.inform.emit('[success] %s' % _("Done."))
def draw_cursor_data(self, pos=None, delete=False):
if pos is None:
pos = self.draw_app.snap_x, self.draw_app.snap_y
if delete:
if self.draw_app.app.use_3d_engine:
self.draw_app.app.plotcanvas.text_cursor.parent = None
self.draw_app.app.plotcanvas.view.camera.zoom_callback = lambda *args: None
return
# font size
qsettings = QtCore.QSettings("Open Source", "FlatCAM")
if qsettings.contains("hud_font_size"):
fsize = qsettings.value('hud_font_size', type=int)
else:
fsize = 8
old_x = self.ui.x_entry.get_value()
old_y = self.ui.y_entry.get_value()
x = pos[0]
y = pos[1]
try:
length = abs(np.sqrt((x - old_x) ** 2 + (y - old_y) ** 2))
except IndexError:
length = self.draw_app.app.dec_format(0.0, self.draw_app.app.decimals)
units = self.draw_app.app.app_units.lower()
x_dec = str(self.draw_app.app.dec_format(x, self.draw_app.app.decimals)) if x else '0.0'
y_dec = str(self.draw_app.app.dec_format(y, self.draw_app.app.decimals)) if y else '0.0'
length_dec = str(self.draw_app.app.dec_format(length, self.draw_app.app.decimals)) if length else '0.0'
l1_txt = 'X: %s [%s]' % (x_dec, units)
l2_txt = 'Y: %s [%s]' % (y_dec, units)
l3_txt = 'L: %s [%s]' % (length_dec, units)
cursor_text = '%s\n%s\n\n%s' % (l1_txt, l2_txt, l3_txt)
if self.draw_app.app.use_3d_engine:
new_pos = self.draw_app.app.plotcanvas.translate_coords_2((x, y))
x, y, __, ___ = self.draw_app.app.plotcanvas.translate_coords((new_pos[0]+30, new_pos[1]))
# text
self.draw_app.app.plotcanvas.text_cursor.font_size = fsize
self.draw_app.app.plotcanvas.text_cursor.text = cursor_text
self.draw_app.app.plotcanvas.text_cursor.pos = x, y
self.draw_app.app.plotcanvas.text_cursor.anchors = 'left', 'top'
if self.draw_app.app.plotcanvas.text_cursor.parent is None:
self.draw_app.app.plotcanvas.text_cursor.parent = self.draw_app.app.plotcanvas.view.scene
def on_key(self, key):
# Jump to coords
if key == QtCore.Qt.Key.Key_J or key == 'J':
@@ -1457,6 +1513,56 @@ class RegionEditorGrb(ShapeToolEditorGrb):
return msg
if key in [str(i) for i in range(10)] + ['.', ',', '+', '-', '/', '*'] or \
key in [QtCore.Qt.Key.Key_0, QtCore.Qt.Key.Key_0, QtCore.Qt.Key.Key_1, QtCore.Qt.Key.Key_2,
QtCore.Qt.Key.Key_3, QtCore.Qt.Key.Key_4, QtCore.Qt.Key.Key_5, QtCore.Qt.Key.Key_6,
QtCore.Qt.Key.Key_7, QtCore.Qt.Key.Key_8, QtCore.Qt.Key.Key_9, QtCore.Qt.Key.Key_Minus,
QtCore.Qt.Key.Key_Plus, QtCore.Qt.Key.Key_Comma, QtCore.Qt.Key.Key_Period,
QtCore.Qt.Key.Key_Slash, QtCore.Qt.Key.Key_Asterisk]:
try:
# VisPy keys
if self.reg_tool.length == self.draw_app.last_length:
self.reg_tool.length = str(key.name)
else:
self.reg_tool.length = str(self.reg_tool.length) + str(key.name)
except AttributeError:
# Qt keys
if self.reg_tool.length == self.draw_app.last_length:
self.reg_tool.length = chr(key)
else:
self.reg_tool.length = str(self.reg_tool.length) + chr(key)
if key == 'Enter' or key == QtCore.Qt.Key.Key_Return or key == QtCore.Qt.Key.Key_Enter:
if self.reg_tool.length != 0:
target_length = self.reg_tool.length
if target_length is None:
self.reg_tool.length = 0.0
return _("Failed.")
first_pt = self.ui.x_entry.get_value(), self.ui.y_entry.get_value()
last_pt = self.draw_app.app.mouse_pos
seg_length = math.sqrt((last_pt[0] - first_pt[0])**2 + (last_pt[1] - first_pt[1])**2)
if seg_length == 0.0:
self.draw_app.app.log.debug("GrbRegionEditorGrb.on_key() --> 'ENTER'. Segment is zero.")
return
try:
new_x = first_pt[0] + (last_pt[0] - first_pt[0]) / seg_length * target_length
new_y = first_pt[1] + (last_pt[1] - first_pt[1]) / seg_length * target_length
except ZeroDivisionError as err:
self.clean_up()
return '[ERROR_NOTCL] %s %s' % (_("Failed."), str(err).capitalize())
if first_pt != (new_x, new_y):
self.draw_app.app.on_jump_to(custom_location=(new_x, new_y), fit_center=False)
if len(self.points) > 0:
msg = '%s: %s. %s' % (
_("Projected"), str(self.reg_tool.length),
_("Click on next Point or click right mouse button to complete ..."))
self.draw_app.app.inform.emit(msg)
# self.interpolate_length = ''
# return "Click on next point or hit ENTER to complete ..."
def clean_up(self):
self.draw_app.selected = []
self.draw_app.ui.apertures_table.clearSelection()
@@ -5914,7 +6020,7 @@ class AppGerberEditor(QtCore.QObject):
self.update_utility_geometry(data=(x, y))
if self.active_tool.name in [
'pad', 'array', 'track'
'pad', 'array', 'track', 'region'
]:
try:
self.active_tool.draw_cursor_data(pos=self.app.mouse_pos)

View File

@@ -0,0 +1,192 @@
from appTool import *
fcTranslate.apply_language('strings')
if '_' not in builtins.__dict__:
_ = gettext.gettext
class GrbRegionEditorTool(AppToolEditor):
"""
Simple input for buffer distance.
"""
def __init__(self, app, draw_app, plugin_name):
AppToolEditor.__init__(self, app)
self.draw_app = draw_app
self.decimals = app.decimals
self.plugin_name = plugin_name
self.ui = GrbRegionEditorUI(layout=self.layout, reg_class=self, plugin_name=plugin_name)
self.connect_signals_at_init()
self.set_tool_ui()
def connect_signals_at_init(self):
# Signals
self.ui.clear_btn.clicked.connect(self.on_clear)
def disconnect_signals(self):
# Signals
try:
self.ui.clear_btn.clicked.disconnect()
except (TypeError, AttributeError):
pass
def run(self):
self.app.defaults.report_usage("Geo Editor ToolPath()")
super().run()
# if the splitter us hidden, display it
if self.app.ui.splitter.sizes()[0] == 0:
self.app.ui.splitter.setSizes([1, 1])
# if the Tool Tab is hidden display it, else hide it but only if the objectName is the same
found_idx = None
for idx in range(self.app.ui.notebook.count()):
if self.app.ui.notebook.widget(idx).objectName() == "plugin_tab":
found_idx = idx
break
# show the Tab
if not found_idx:
try:
self.app.ui.notebook.addTab(self.app.ui.plugin_tab, _("Plugin"))
except RuntimeError:
self.app.ui.plugin_tab = QtWidgets.QWidget()
self.app.ui.plugin_tab.setObjectName("plugin_tab")
self.app.ui.plugin_tab_layout = QtWidgets.QVBoxLayout(self.app.ui.plugin_tab)
self.app.ui.plugin_tab_layout.setContentsMargins(2, 2, 2, 2)
self.app.ui.plugin_scroll_area = VerticalScrollArea()
self.app.ui.plugin_tab_layout.addWidget(self.app.ui.plugin_scroll_area)
self.app.ui.notebook.addTab(self.app.ui.plugin_tab, _("Plugin"))
# focus on Tool Tab
self.app.ui.notebook.setCurrentWidget(self.app.ui.plugin_tab)
# self.app.ui.notebook.callback_on_close = self.on_tab_close
self.app.ui.notebook.setTabText(2, self.plugin_name)
def set_tool_ui(self):
# Init appGUI
self.length = 0.0
def on_tab_close(self):
self.disconnect_signals()
self.hide_tool()
# self.app.ui.notebook.callback_on_close = lambda: None
def on_clear(self):
self.set_tool_ui()
@property
def length(self):
return self.ui.project_line_entry.get_value()
@length.setter
def length(self, val):
self.ui.project_line_entry.set_value(val)
def hide_tool(self):
self.ui.reg_tool_frame.hide()
self.app.ui.notebook.setCurrentWidget(self.app.ui.properties_tab)
if self.draw_app.active_tool.name != 'select':
self.draw_app.select_tool("select")
class GrbRegionEditorUI:
def __init__(self, layout, reg_class, plugin_name):
self.pluginName = plugin_name
self.reg_class = reg_class
self.decimals = self.reg_class.app.decimals
self.layout = layout
# Title
title_label = FCLabel("%s" % ('Editor ' + self.pluginName), size=16, bold=True)
self.layout.addWidget(title_label)
# this way I can hide/show the frame
self.reg_tool_frame = QtWidgets.QFrame()
self.reg_tool_frame.setContentsMargins(0, 0, 0, 0)
self.layout.addWidget(self.reg_tool_frame)
self.editor_vbox = QtWidgets.QVBoxLayout()
self.editor_vbox.setContentsMargins(0, 0, 0, 0)
self.reg_tool_frame.setLayout(self.editor_vbox)
# Position
self.tool_lbl = FCLabel('%s' % _("Diameter"), bold=True, color='blue')
self.editor_vbox.addWidget(self.tool_lbl)
# #############################################################################################################
# Diameter Frame
# #############################################################################################################
dia_frame = FCFrame()
self.editor_vbox.addWidget(dia_frame)
dia_grid = GLay(v_spacing=5, h_spacing=3, c_stretch=[0, 1, 0])
dia_frame.setLayout(dia_grid)
# Dia Value
self.dia_lbl = FCLabel('%s:' % _("Value"))
self.dia_entry = NumericalEvalEntry(border_color='#0069A9')
self.dia_entry.setDisabled(True)
self.dia_unit = FCLabel('%s' % 'mm')
dia_grid.addWidget(self.dia_lbl, 0, 0)
dia_grid.addWidget(self.dia_entry, 0, 1)
dia_grid.addWidget(self.dia_unit, 0, 2)
# Position
self.pos_lbl = FCLabel('%s' % _("Position"), bold=True, color='red')
self.editor_vbox.addWidget(self.pos_lbl)
# #############################################################################################################
# Position Frame
# #############################################################################################################
pos_frame = FCFrame()
self.editor_vbox.addWidget(pos_frame)
pos_grid = GLay(v_spacing=5, h_spacing=3)
pos_frame.setLayout(pos_grid)
# X Pos
self.x_lbl = FCLabel('%s:' % _("X"))
self.x_entry = FCDoubleSpinner()
self.x_entry.set_precision(self.decimals)
self.x_entry.set_range(-10000.0000, 10000.0000)
pos_grid.addWidget(self.x_lbl, 2, 0)
pos_grid.addWidget(self.x_entry, 2, 1)
# Y Pos
self.y_lbl = FCLabel('%s:' % _("Y"))
self.y_entry = FCDoubleSpinner()
self.y_entry.set_precision(self.decimals)
self.y_entry.set_range(-10000.0000, 10000.0000)
pos_grid.addWidget(self.y_lbl, 4, 0)
pos_grid.addWidget(self.y_entry, 4, 1)
# #############################################################################################################
# Projection Frame
# #############################################################################################################
pro_frame = FCFrame()
self.editor_vbox.addWidget(pro_frame)
pro_grid = GLay(v_spacing=5, h_spacing=3, c_stretch=[0, 1, 0])
pro_frame.setLayout(pro_grid)
# Project distance
self.project_line_lbl = FCLabel('%s:' % _("Projection"))
self.project_line_lbl.setToolTip(
_("Length of the current segment/move.")
)
self.project_line_entry = NumericalEvalEntry(border_color='#0069A9')
pro_grid.addWidget(self.project_line_lbl, 0, 0)
pro_grid.addWidget(self.project_line_entry, 0, 1)
self.clear_btn = QtWidgets.QToolButton()
self.clear_btn.setIcon(QtGui.QIcon(self.reg_class.app.resource_location + '/trash32.png'))
pro_grid.addWidget(self.clear_btn, 0, 2)
GLay.set_common_column_size([dia_grid, pos_grid, pro_grid], 0)
self.layout.addStretch(1)