- in Tools: Film, Image, InvertGerber, Optimal, PcbWizard - moved the Tool UI in its own class
This commit is contained in:
@@ -21,23 +21,165 @@ if '_' not in builtins.__dict__:
|
||||
|
||||
class ToolImage(AppTool):
|
||||
|
||||
toolName = _("Image as Object")
|
||||
|
||||
def __init__(self, app):
|
||||
AppTool.__init__(self, app)
|
||||
|
||||
self.app = app
|
||||
self.decimals = self.app.decimals
|
||||
|
||||
# Title
|
||||
title_label = QtWidgets.QLabel("%s" % _('Image to PCB'))
|
||||
# #############################################################################
|
||||
# ######################### Tool GUI ##########################################
|
||||
# #############################################################################
|
||||
self.ui = ImageUI(layout=self.layout, app=self.app)
|
||||
self.toolName = self.ui.toolName
|
||||
|
||||
# ## Signals
|
||||
self.ui.import_button.clicked.connect(self.on_file_importimage)
|
||||
self.ui.image_type.activated_custom.connect(self.ui.on_image_type)
|
||||
|
||||
def run(self, toggle=True):
|
||||
self.app.defaults.report_usage("ToolImage()")
|
||||
|
||||
if toggle:
|
||||
# if the splitter is hidden, display it, else hide it but only if the current widget is the same
|
||||
if self.app.ui.splitter.sizes()[0] == 0:
|
||||
self.app.ui.splitter.setSizes([1, 1])
|
||||
else:
|
||||
try:
|
||||
if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
|
||||
# if tab is populated with the tool but it does not have the focus, focus on it
|
||||
if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
|
||||
# focus on Tool Tab
|
||||
self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
|
||||
else:
|
||||
self.app.ui.splitter.setSizes([0, 1])
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
if self.app.ui.splitter.sizes()[0] == 0:
|
||||
self.app.ui.splitter.setSizes([1, 1])
|
||||
|
||||
AppTool.run(self)
|
||||
self.set_tool_ui()
|
||||
|
||||
self.app.ui.notebook.setTabText(2, _("Image Tool"))
|
||||
|
||||
def install(self, icon=None, separator=None, **kwargs):
|
||||
AppTool.install(self, icon, separator, **kwargs)
|
||||
|
||||
def set_tool_ui(self):
|
||||
# ## Initialize form
|
||||
self.ui.dpi_entry.set_value(96)
|
||||
self.ui.image_type.set_value('black')
|
||||
self.ui.mask_bw_entry.set_value(250)
|
||||
self.ui.mask_r_entry.set_value(250)
|
||||
self.ui.mask_g_entry.set_value(250)
|
||||
self.ui.mask_b_entry.set_value(250)
|
||||
|
||||
def on_file_importimage(self):
|
||||
"""
|
||||
Callback for menu item File->Import IMAGE.
|
||||
:param type_of_obj: to import the IMAGE as Geometry or as Gerber
|
||||
:type type_of_obj: str
|
||||
:return: None
|
||||
"""
|
||||
mask = []
|
||||
self.app.log.debug("on_file_importimage()")
|
||||
|
||||
_filter = "Image Files(*.BMP *.PNG *.JPG *.JPEG);;" \
|
||||
"Bitmap File (*.BMP);;" \
|
||||
"PNG File (*.PNG);;" \
|
||||
"Jpeg File (*.JPG);;" \
|
||||
"All Files (*.*)"
|
||||
try:
|
||||
filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import IMAGE"),
|
||||
directory=self.app.get_last_folder(), filter=_filter)
|
||||
except TypeError:
|
||||
filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import IMAGE"), filter=filter)
|
||||
|
||||
filename = str(filename)
|
||||
type_obj = self.ui.tf_type_obj_combo.get_value()
|
||||
dpi = self.ui.dpi_entry.get_value()
|
||||
mode = self.ui.image_type.get_value()
|
||||
mask = [
|
||||
self.ui.mask_bw_entry.get_value(),
|
||||
self.ui.mask_r_entry.get_value(),
|
||||
self.ui.mask_g_entry.get_value(),
|
||||
self.ui.mask_b_entry.get_value()
|
||||
]
|
||||
|
||||
if filename == "":
|
||||
self.app.inform.emit(_("Cancelled."))
|
||||
else:
|
||||
self.app.worker_task.emit({'fcn': self.import_image,
|
||||
'params': [filename, type_obj, dpi, mode, mask]})
|
||||
|
||||
def import_image(self, filename, o_type=_("Gerber"), dpi=96, mode='black', mask=None, outname=None):
|
||||
"""
|
||||
Adds a new Geometry Object to the projects and populates
|
||||
it with shapes extracted from the SVG file.
|
||||
|
||||
:param filename: Path to the SVG file.
|
||||
:param o_type: type of FlatCAM objeect
|
||||
:param dpi: dot per inch
|
||||
:param mode: black or color
|
||||
:param mask: dictate the level of detail
|
||||
:param outname: name for the resulting file
|
||||
:return:
|
||||
"""
|
||||
|
||||
self.app.defaults.report_usage("import_image()")
|
||||
|
||||
if mask is None:
|
||||
mask = [250, 250, 250, 250]
|
||||
|
||||
if o_type is None or o_type == _("Geometry"):
|
||||
obj_type = "geometry"
|
||||
elif o_type == _("Gerber"):
|
||||
obj_type = "gerber"
|
||||
else:
|
||||
self.app.inform.emit('[ERROR_NOTCL] %s' %
|
||||
_("Not supported type is picked as parameter. "
|
||||
"Only Geometry and Gerber are supported"))
|
||||
return
|
||||
|
||||
def obj_init(geo_obj, app_obj):
|
||||
geo_obj.import_image(filename, units=units, dpi=dpi, mode=mode, mask=mask)
|
||||
geo_obj.multigeo = False
|
||||
|
||||
with self.app.proc_container.new(_("Importing Image")) as proc:
|
||||
|
||||
# Object name
|
||||
name = outname or filename.split('/')[-1].split('\\')[-1]
|
||||
units = self.app.defaults['units']
|
||||
|
||||
self.app.app_obj.new_object(obj_type, name, obj_init)
|
||||
|
||||
# Register recent file
|
||||
self.app.file_opened.emit("image", filename)
|
||||
|
||||
# GUI feedback
|
||||
self.app.inform.emit('[success] %s: %s' % (_("Opened"), filename))
|
||||
|
||||
|
||||
class ImageUI:
|
||||
|
||||
toolName = _("Image as Object")
|
||||
|
||||
def __init__(self, layout, app):
|
||||
self.app = app
|
||||
self.decimals = self.app.decimals
|
||||
self.layout = layout
|
||||
|
||||
# ## Title
|
||||
title_label = QtWidgets.QLabel("%s" % self.toolName)
|
||||
title_label.setStyleSheet("""
|
||||
QLabel
|
||||
{
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
}
|
||||
""")
|
||||
QLabel
|
||||
{
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
}
|
||||
""")
|
||||
self.layout.addWidget(title_label)
|
||||
|
||||
# Form Layout
|
||||
@@ -53,8 +195,8 @@ class ToolImage(AppTool):
|
||||
|
||||
self.tf_type_obj_combo_label = QtWidgets.QLabel('%s:' % _("Object Type"))
|
||||
self.tf_type_obj_combo_label.setToolTip(
|
||||
_("Specify the type of object to create from the image.\n"
|
||||
"It can be of type: Gerber or Geometry.")
|
||||
_("Specify the type of object to create from the image.\n"
|
||||
"It can be of type: Gerber or Geometry.")
|
||||
|
||||
)
|
||||
ti_form_layout.addRow(self.tf_type_obj_combo_label, self.tf_type_obj_combo)
|
||||
@@ -63,7 +205,7 @@ class ToolImage(AppTool):
|
||||
self.dpi_entry = FCSpinner(callback=self.confirmation_message_int)
|
||||
self.dpi_entry.set_range(0, 99999)
|
||||
self.dpi_label = QtWidgets.QLabel('%s:' % _("DPI value"))
|
||||
self.dpi_label.setToolTip(_("Specify a DPI value for the image.") )
|
||||
self.dpi_label.setToolTip(_("Specify a DPI value for the image."))
|
||||
ti_form_layout.addRow(self.dpi_label, self.dpi_entry)
|
||||
|
||||
self.emty_lbl = QtWidgets.QLabel("")
|
||||
@@ -150,48 +292,8 @@ class ToolImage(AppTool):
|
||||
|
||||
self.on_image_type(val=False)
|
||||
|
||||
# ## Signals
|
||||
self.import_button.clicked.connect(self.on_file_importimage)
|
||||
self.image_type.activated_custom.connect(self.on_image_type)
|
||||
|
||||
def run(self, toggle=True):
|
||||
self.app.defaults.report_usage("ToolImage()")
|
||||
|
||||
if toggle:
|
||||
# if the splitter is hidden, display it, else hide it but only if the current widget is the same
|
||||
if self.app.ui.splitter.sizes()[0] == 0:
|
||||
self.app.ui.splitter.setSizes([1, 1])
|
||||
else:
|
||||
try:
|
||||
if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
|
||||
# if tab is populated with the tool but it does not have the focus, focus on it
|
||||
if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
|
||||
# focus on Tool Tab
|
||||
self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
|
||||
else:
|
||||
self.app.ui.splitter.setSizes([0, 1])
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
if self.app.ui.splitter.sizes()[0] == 0:
|
||||
self.app.ui.splitter.setSizes([1, 1])
|
||||
|
||||
AppTool.run(self)
|
||||
self.set_tool_ui()
|
||||
|
||||
self.app.ui.notebook.setTabText(2, _("Image Tool"))
|
||||
|
||||
def install(self, icon=None, separator=None, **kwargs):
|
||||
AppTool.install(self, icon, separator, **kwargs)
|
||||
|
||||
def set_tool_ui(self):
|
||||
# ## Initialize form
|
||||
self.dpi_entry.set_value(96)
|
||||
self.image_type.set_value('black')
|
||||
self.mask_bw_entry.set_value(250)
|
||||
self.mask_r_entry.set_value(250)
|
||||
self.mask_g_entry.set_value(250)
|
||||
self.mask_b_entry.set_value(250)
|
||||
# #################################### FINSIHED GUI ###########################
|
||||
# #############################################################################
|
||||
|
||||
def on_image_type(self, val):
|
||||
if val == 'color':
|
||||
@@ -215,83 +317,19 @@ class ToolImage(AppTool):
|
||||
self.mask_bw_label.setDisabled(False)
|
||||
self.mask_bw_entry.setDisabled(False)
|
||||
|
||||
def on_file_importimage(self):
|
||||
"""
|
||||
Callback for menu item File->Import IMAGE.
|
||||
:param type_of_obj: to import the IMAGE as Geometry or as Gerber
|
||||
:type type_of_obj: str
|
||||
:return: None
|
||||
"""
|
||||
mask = []
|
||||
self.app.log.debug("on_file_importimage()")
|
||||
|
||||
_filter = "Image Files(*.BMP *.PNG *.JPG *.JPEG);;" \
|
||||
"Bitmap File (*.BMP);;" \
|
||||
"PNG File (*.PNG);;" \
|
||||
"Jpeg File (*.JPG);;" \
|
||||
"All Files (*.*)"
|
||||
try:
|
||||
filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import IMAGE"),
|
||||
directory=self.app.get_last_folder(), filter=_filter)
|
||||
except TypeError:
|
||||
filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import IMAGE"), filter=filter)
|
||||
|
||||
filename = str(filename)
|
||||
type_obj = self.tf_type_obj_combo.get_value()
|
||||
dpi = self.dpi_entry.get_value()
|
||||
mode = self.image_type.get_value()
|
||||
mask = [self.mask_bw_entry.get_value(), self.mask_r_entry.get_value(), self.mask_g_entry.get_value(),
|
||||
self.mask_b_entry.get_value()]
|
||||
|
||||
if filename == "":
|
||||
self.app.inform.emit(_("Cancelled."))
|
||||
def confirmation_message(self, accepted, minval, maxval):
|
||||
if accepted is False:
|
||||
self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%.*f, %.*f]' % (_("Edited value is out of range"),
|
||||
self.decimals,
|
||||
minval,
|
||||
self.decimals,
|
||||
maxval), False)
|
||||
else:
|
||||
self.app.worker_task.emit({'fcn': self.import_image,
|
||||
'params': [filename, type_obj, dpi, mode, mask]})
|
||||
self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)
|
||||
|
||||
def import_image(self, filename, o_type=_("Gerber"), dpi=96, mode='black', mask=None, outname=None):
|
||||
"""
|
||||
Adds a new Geometry Object to the projects and populates
|
||||
it with shapes extracted from the SVG file.
|
||||
|
||||
:param filename: Path to the SVG file.
|
||||
:param o_type: type of FlatCAM objeect
|
||||
:param dpi: dot per inch
|
||||
:param mode: black or color
|
||||
:param mask: dictate the level of detail
|
||||
:param outname: name for the resulting file
|
||||
:return:
|
||||
"""
|
||||
|
||||
self.app.defaults.report_usage("import_image()")
|
||||
|
||||
if mask is None:
|
||||
mask = [250, 250, 250, 250]
|
||||
|
||||
if o_type is None or o_type == _("Geometry"):
|
||||
obj_type = "geometry"
|
||||
elif o_type == _("Gerber"):
|
||||
obj_type = "gerber"
|
||||
def confirmation_message_int(self, accepted, minval, maxval):
|
||||
if accepted is False:
|
||||
self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%d, %d]' %
|
||||
(_("Edited value is out of range"), minval, maxval), False)
|
||||
else:
|
||||
self.app.inform.emit('[ERROR_NOTCL] %s' %
|
||||
_("Not supported type is picked as parameter. "
|
||||
"Only Geometry and Gerber are supported"))
|
||||
return
|
||||
|
||||
def obj_init(geo_obj, app_obj):
|
||||
geo_obj.import_image(filename, units=units, dpi=dpi, mode=mode, mask=mask)
|
||||
geo_obj.multigeo = False
|
||||
|
||||
with self.app.proc_container.new(_("Importing Image")) as proc:
|
||||
|
||||
# Object name
|
||||
name = outname or filename.split('/')[-1].split('\\')[-1]
|
||||
units = self.app.defaults['units']
|
||||
|
||||
self.app.app_obj.new_object(obj_type, name, obj_init)
|
||||
|
||||
# Register recent file
|
||||
self.app.file_opened.emit("image", filename)
|
||||
|
||||
# GUI feedback
|
||||
self.app.inform.emit('[success] %s: %s' % (_("Opened"), filename))
|
||||
self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)
|
||||
|
||||
Reference in New Issue
Block a user