ToolQRCode.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 3/10/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtWidgets, QtCore
  8. from FlatCAMTool import FlatCAMTool
  9. from flatcamGUI.GUIElements import FCDoubleSpinner, EvalEntry, FCCheckBox
  10. from camlib import *
  11. from shapely.geometry import Point
  12. from shapely.geometry.base import *
  13. import math
  14. import io
  15. from datetime import datetime
  16. import logging
  17. import qrcode
  18. import qrcode.image.svg
  19. from lxml import etree as ET
  20. import gettext
  21. import FlatCAMTranslation as fcTranslate
  22. import builtins
  23. fcTranslate.apply_language('strings')
  24. if '_' not in builtins.__dict__:
  25. _ = gettext.gettext
  26. log = logging.getLogger('base')
  27. class QRCode(FlatCAMTool):
  28. toolName = _("QRCode Tool")
  29. def __init__(self, app):
  30. FlatCAMTool.__init__(self, app)
  31. self.app = app
  32. self.canvas = self.app.plotcanvas
  33. self.decimals = 4
  34. self.units = ''
  35. # ## Title
  36. title_label = QtWidgets.QLabel("%s" % self.toolName)
  37. title_label.setStyleSheet("""
  38. QLabel
  39. {
  40. font-size: 16px;
  41. font-weight: bold;
  42. }
  43. """)
  44. self.layout.addWidget(title_label)
  45. def run(self, toggle=True):
  46. self.app.report_usage("QRCode()")
  47. if toggle:
  48. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  49. if self.app.ui.splitter.sizes()[0] == 0:
  50. self.app.ui.splitter.setSizes([1, 1])
  51. else:
  52. try:
  53. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  54. # if tab is populated with the tool but it does not have the focus, focus on it
  55. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  56. # focus on Tool Tab
  57. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  58. else:
  59. self.app.ui.splitter.setSizes([0, 1])
  60. except AttributeError:
  61. pass
  62. else:
  63. if self.app.ui.splitter.sizes()[0] == 0:
  64. self.app.ui.splitter.setSizes([1, 1])
  65. FlatCAMTool.run(self)
  66. self.set_tool_ui()
  67. self.app.ui.notebook.setTabText(2, _("QRCode Tool"))
  68. self.execute()
  69. def install(self, icon=None, separator=None, **kwargs):
  70. FlatCAMTool.install(self, icon, separator, shortcut='ALT+Q', **kwargs)
  71. def set_tool_ui(self):
  72. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  73. def execute(self):
  74. svg_file = io.BytesIO()
  75. svg_file = qrcode.make("FlatCAM - 2D - Computer aided PCB Manufacturing Tool",
  76. image_factory=qrcode.image.svg.SvgFragmentImage)
  77. def obj_init(geo_obj, app_obj):
  78. units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value()
  79. try:
  80. geo_obj.import_svg(svg_file)
  81. except Exception as e:
  82. print(str(e))
  83. with self.app.proc_container.new("Import SVG"):
  84. # Object creation
  85. self.app.new_object('geometry', 'generated_qrcode', obj_init, plot=False)
  86. # # Register recent file
  87. # self.app.file_opened.emit("svg", img)
  88. #
  89. # # GUI feedback
  90. # self.app.inform.emit("Opened: " + img)