FlatCAMCommon.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. # ##########################################################
  8. # ##########################################################
  9. # File Modified (major mod): Marius Adrian Stanciu #
  10. # Date: 11/4/2019 #
  11. # ##########################################################
  12. from PyQt5 import QtGui, QtCore, QtWidgets
  13. from flatcamGUI.GUIElements import FCTable, FCEntry, FCButton, FCSpinner, FCDoubleSpinner, FCComboBox, FCCheckBox
  14. import sys
  15. import webbrowser
  16. from copy import deepcopy
  17. from datetime import datetime
  18. import gettext
  19. import FlatCAMTranslation as fcTranslate
  20. import builtins
  21. fcTranslate.apply_language('strings')
  22. if '_' not in builtins.__dict__:
  23. _ = gettext.gettext
  24. class LoudDict(dict):
  25. """
  26. A Dictionary with a callback for
  27. item changes.
  28. """
  29. def __init__(self, *args, **kwargs):
  30. dict.__init__(self, *args, **kwargs)
  31. self.callback = lambda x: None
  32. def __setitem__(self, key, value):
  33. """
  34. Overridden __setitem__ method. Will emit 'changed(QString)'
  35. if the item was changed, with key as parameter.
  36. """
  37. if key in self and self.__getitem__(key) == value:
  38. return
  39. dict.__setitem__(self, key, value)
  40. self.callback(key)
  41. def update(self, *args, **kwargs):
  42. if len(args) > 1:
  43. raise TypeError("update expected at most 1 arguments, got %d" % len(args))
  44. other = dict(*args, **kwargs)
  45. for key in other:
  46. self[key] = other[key]
  47. def set_change_callback(self, callback):
  48. """
  49. Assigns a function as callback on item change. The callback
  50. will receive the key of the object that was changed.
  51. :param callback: Function to call on item change.
  52. :type callback: func
  53. :return: None
  54. """
  55. self.callback = callback
  56. class FCSignal:
  57. """
  58. Taken from here: https://blog.abstractfactory.io/dynamic-signals-in-pyqt/
  59. """
  60. def __init__(self):
  61. self.__subscribers = []
  62. def emit(self, *args, **kwargs):
  63. for subs in self.__subscribers:
  64. subs(*args, **kwargs)
  65. def connect(self, func):
  66. self.__subscribers.append(func)
  67. def disconnect(self, func):
  68. try:
  69. self.__subscribers.remove(func)
  70. except ValueError:
  71. print('Warning: function %s not removed '
  72. 'from signal %s' % (func, self))
  73. class BookmarkManager(QtWidgets.QWidget):
  74. mark_rows = QtCore.pyqtSignal()
  75. def __init__(self, app, storage, parent=None):
  76. super(BookmarkManager, self).__init__(parent)
  77. self.app = app
  78. assert isinstance(storage, dict), "Storage argument is not a dictionary"
  79. self.bm_dict = deepcopy(storage)
  80. # Icon and title
  81. # self.setWindowIcon(parent.app_icon)
  82. # self.setWindowTitle(_("Bookmark Manager"))
  83. # self.resize(600, 400)
  84. # title = QtWidgets.QLabel(
  85. # "<font size=8><B>FlatCAM</B></font><BR>"
  86. # )
  87. # title.setOpenExternalLinks(True)
  88. # layouts
  89. layout = QtWidgets.QVBoxLayout()
  90. self.setLayout(layout)
  91. table_hlay = QtWidgets.QHBoxLayout()
  92. layout.addLayout(table_hlay)
  93. self.table_widget = FCTable(drag_drop=True, protected_rows=[0, 1])
  94. self.table_widget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
  95. table_hlay.addWidget(self.table_widget)
  96. self.table_widget.setColumnCount(3)
  97. self.table_widget.setColumnWidth(0, 20)
  98. self.table_widget.setHorizontalHeaderLabels(
  99. [
  100. '#',
  101. _('Title'),
  102. _('Web Link')
  103. ]
  104. )
  105. self.table_widget.horizontalHeaderItem(0).setToolTip(
  106. _("Index.\n"
  107. "The rows in gray color will populate the Bookmarks menu.\n"
  108. "The number of gray colored rows is set in Preferences."))
  109. self.table_widget.horizontalHeaderItem(1).setToolTip(
  110. _("Description of the link that is set as an menu action.\n"
  111. "Try to keep it short because it is installed as a menu item."))
  112. self.table_widget.horizontalHeaderItem(2).setToolTip(
  113. _("Web Link. E.g: https://your_website.org "))
  114. # pal = QtGui.QPalette()
  115. # pal.setColor(QtGui.QPalette.Background, Qt.white)
  116. # New Bookmark
  117. new_vlay = QtWidgets.QVBoxLayout()
  118. layout.addLayout(new_vlay)
  119. new_title_lbl = QtWidgets.QLabel('<b>%s</b>' % _("New Bookmark"))
  120. new_vlay.addWidget(new_title_lbl)
  121. form0 = QtWidgets.QFormLayout()
  122. new_vlay.addLayout(form0)
  123. title_lbl = QtWidgets.QLabel('%s:' % _("Title"))
  124. self.title_entry = FCEntry()
  125. form0.addRow(title_lbl, self.title_entry)
  126. link_lbl = QtWidgets.QLabel('%s:' % _("Web Link"))
  127. self.link_entry = FCEntry()
  128. self.link_entry.set_value('http://')
  129. form0.addRow(link_lbl, self.link_entry)
  130. # Buttons Layout
  131. button_hlay = QtWidgets.QHBoxLayout()
  132. layout.addLayout(button_hlay)
  133. add_entry_btn = FCButton(_("Add Entry"))
  134. remove_entry_btn = FCButton(_("Remove Entry"))
  135. export_list_btn = FCButton(_("Export List"))
  136. import_list_btn = FCButton(_("Import List"))
  137. closebtn = QtWidgets.QPushButton(_("Close"))
  138. # button_hlay.addStretch()
  139. button_hlay.addWidget(add_entry_btn)
  140. button_hlay.addWidget(remove_entry_btn)
  141. button_hlay.addWidget(export_list_btn)
  142. button_hlay.addWidget(import_list_btn)
  143. # button_hlay.addWidget(closebtn)
  144. # ##############################################################################
  145. # ######################## SIGNALS #############################################
  146. # ##############################################################################
  147. add_entry_btn.clicked.connect(self.on_add_entry)
  148. remove_entry_btn.clicked.connect(self.on_remove_entry)
  149. export_list_btn.clicked.connect(self.on_export_bookmarks)
  150. import_list_btn.clicked.connect(self.on_import_bookmarks)
  151. self.title_entry.returnPressed.connect(self.on_add_entry)
  152. self.link_entry.returnPressed.connect(self.on_add_entry)
  153. # closebtn.clicked.connect(self.accept)
  154. self.table_widget.drag_drop_sig.connect(self.mark_table_rows_for_actions)
  155. self.build_bm_ui()
  156. def build_bm_ui(self):
  157. self.table_widget.setRowCount(len(self.bm_dict))
  158. nr_crt = 0
  159. sorted_bookmarks = sorted(list(self.bm_dict.items()), key=lambda x: int(x[0]))
  160. for entry, bookmark in sorted_bookmarks:
  161. row = nr_crt
  162. nr_crt += 1
  163. title = bookmark[0]
  164. weblink = bookmark[1]
  165. id_item = QtWidgets.QTableWidgetItem('%d' % int(nr_crt))
  166. # id.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  167. self.table_widget.setItem(row, 0, id_item) # Tool name/id
  168. title_item = QtWidgets.QTableWidgetItem(title)
  169. self.table_widget.setItem(row, 1, title_item)
  170. weblink_txt = QtWidgets.QTextBrowser()
  171. weblink_txt.setOpenExternalLinks(True)
  172. weblink_txt.setFrameStyle(QtWidgets.QFrame.NoFrame)
  173. weblink_txt.document().setDefaultStyleSheet("a{ text-decoration: none; }")
  174. weblink_txt.setHtml('<a href=%s>%s</a>' % (weblink, weblink))
  175. self.table_widget.setCellWidget(row, 2, weblink_txt)
  176. vertical_header = self.table_widget.verticalHeader()
  177. vertical_header.hide()
  178. horizontal_header = self.table_widget.horizontalHeader()
  179. horizontal_header.setMinimumSectionSize(10)
  180. horizontal_header.setDefaultSectionSize(70)
  181. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  182. horizontal_header.resizeSection(0, 20)
  183. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
  184. horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch)
  185. self.mark_table_rows_for_actions()
  186. self.app.defaults["global_bookmarks"].clear()
  187. for key, val in self.bm_dict.items():
  188. self.app.defaults["global_bookmarks"][key] = deepcopy(val)
  189. def on_add_entry(self, **kwargs):
  190. """
  191. Add a entry in the Bookmark Table and in the menu actions
  192. :return: None
  193. """
  194. if 'title' in kwargs:
  195. title = kwargs['title']
  196. else:
  197. title = self.title_entry.get_value()
  198. if title == '':
  199. self.app.inform.emit(f'[ERROR_NOTCL] {_("Title entry is empty.")}')
  200. return 'fail'
  201. if 'link' is kwargs:
  202. link = kwargs['link']
  203. else:
  204. link = self.link_entry.get_value()
  205. if link == 'http://':
  206. self.app.inform.emit(f'[ERROR_NOTCL] {_("Web link entry is empty.")}')
  207. return 'fail'
  208. # if 'http' not in link or 'https' not in link:
  209. # link = 'http://' + link
  210. for bookmark in self.bm_dict.values():
  211. if title == bookmark[0] or link == bookmark[1]:
  212. self.app.inform.emit(f'[ERROR_NOTCL] {_("Either the Title or the Weblink already in the table.")}')
  213. return 'fail'
  214. # for some reason if the last char in the weblink is a slash it does not make the link clickable
  215. # so I remove it
  216. if link[-1] == '/':
  217. link = link[:-1]
  218. # add the new entry to storage
  219. new_entry = len(self.bm_dict) + 1
  220. self.bm_dict[str(new_entry)] = [title, link]
  221. # add the link to the menu but only if it is within the set limit
  222. bm_limit = int(self.app.defaults["global_bookmarks_limit"])
  223. if len(self.bm_dict) < bm_limit:
  224. act = QtWidgets.QAction(parent=self.app.ui.menuhelp_bookmarks)
  225. act.setText(title)
  226. act.setIcon(QtGui.QIcon('share/link16.png'))
  227. act.triggered.connect(lambda: webbrowser.open(link))
  228. self.app.ui.menuhelp_bookmarks.insertAction(self.app.ui.menuhelp_bookmarks_manager, act)
  229. self.app.inform.emit(f'[success] {_("Bookmark added.")}')
  230. # add the new entry to the bookmark manager table
  231. self.build_bm_ui()
  232. def on_remove_entry(self):
  233. """
  234. Remove an Entry in the Bookmark table and from the menu actions
  235. :return:
  236. """
  237. index_list = []
  238. for model_index in self.table_widget.selectionModel().selectedRows():
  239. index = QtCore.QPersistentModelIndex(model_index)
  240. index_list.append(index)
  241. title_to_remove = self.table_widget.item(model_index.row(), 1).text()
  242. if title_to_remove == 'FlatCAM' or title_to_remove == 'Backup Site':
  243. self.app.inform.emit('[WARNING_NOTCL] %s.' % _("This bookmark can not be removed"))
  244. self.build_bm_ui()
  245. return
  246. else:
  247. for k, bookmark in list(self.bm_dict.items()):
  248. if title_to_remove == bookmark[0]:
  249. # remove from the storage
  250. self.bm_dict.pop(k, None)
  251. for act in self.app.ui.menuhelp_bookmarks.actions():
  252. if act.text() == title_to_remove:
  253. # disconnect the signal
  254. try:
  255. act.triggered.disconnect()
  256. except TypeError:
  257. pass
  258. # remove the action from the menu
  259. self.app.ui.menuhelp_bookmarks.removeAction(act)
  260. # house keeping: it pays to have keys increased by one
  261. new_key = 0
  262. new_dict = dict()
  263. for k, v in self.bm_dict.items():
  264. # we start with key 1 so we can use the len(self.bm_dict)
  265. # when adding bookmarks (keys in bm_dict)
  266. new_key += 1
  267. new_dict[str(new_key)] = v
  268. self.bm_dict = deepcopy(new_dict)
  269. new_dict.clear()
  270. self.app.inform.emit(f'[success] {_("Bookmark removed.")}')
  271. # for index in index_list:
  272. # self.table_widget.model().removeRow(index.row())
  273. self.build_bm_ui()
  274. def on_export_bookmarks(self):
  275. self.app.report_usage("on_export_bookmarks")
  276. self.app.log.debug("on_export_bookmarks()")
  277. date = str(datetime.today()).rpartition('.')[0]
  278. date = ''.join(c for c in date if c not in ':-')
  279. date = date.replace(' ', '_')
  280. filter__ = "Text File (*.TXT);;All Files (*.*)"
  281. filename, _f = QtWidgets.QFileDialog.getSaveFileName(caption=_("Export FlatCAM Preferences"),
  282. directory='{l_save}/FlatCAM_{n}_{date}'.format(
  283. l_save=str(self.app.get_last_save_folder()),
  284. n=_("Bookmarks"),
  285. date=date),
  286. filter=filter__)
  287. filename = str(filename)
  288. if filename == "":
  289. self.app.inform.emit('[WARNING_NOTCL] %s' % _("FlatCAM bookmarks export cancelled."))
  290. return
  291. else:
  292. try:
  293. f = open(filename, 'w')
  294. f.close()
  295. except PermissionError:
  296. self.app.inform.emit('[WARNING] %s' %
  297. _("Permission denied, saving not possible.\n"
  298. "Most likely another app is holding the file open and not accessible."))
  299. return
  300. except IOError:
  301. self.app.log.debug('Creating a new bookmarks file ...')
  302. f = open(filename, 'w')
  303. f.close()
  304. except:
  305. e = sys.exc_info()[0]
  306. self.app.log.error("Could not load defaults file.")
  307. self.app.log.error(str(e))
  308. self.app.inform.emit('[ERROR_NOTCL] %s' %
  309. _("Could not load bookmarks file."))
  310. return
  311. # Save update options
  312. try:
  313. with open(filename, "w") as f:
  314. for title, link in self.bm_dict.items():
  315. line2write = str(title) + ':' + str(link) + '\n'
  316. f.write(line2write)
  317. except:
  318. self.app.inform.emit('[ERROR_NOTCL] %s' %
  319. _("Failed to write bookmarks to file."))
  320. return
  321. self.app.inform.emit('[success] %s: %s' %
  322. (_("Exported bookmarks to"), filename))
  323. def on_import_bookmarks(self):
  324. self.app.log.debug("on_import_bookmarks()")
  325. filter_ = "Text File (*.txt);;All Files (*.*)"
  326. filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import FlatCAM Bookmarks"),
  327. filter=filter_)
  328. filename = str(filename)
  329. if filename == "":
  330. self.app.inform.emit('[WARNING_NOTCL] %s' %
  331. _("FlatCAM bookmarks import cancelled."))
  332. else:
  333. try:
  334. with open(filename) as f:
  335. bookmarks = f.readlines()
  336. except IOError:
  337. self.app.log.error("Could not load bookmarks file.")
  338. self.app.inform.emit('[ERROR_NOTCL] %s' %
  339. _("Could not load bookmarks file."))
  340. return
  341. for line in bookmarks:
  342. proc_line = line.replace(' ', '').partition(':')
  343. self.on_add_entry(title=proc_line[0], link=proc_line[2])
  344. self.app.inform.emit('[success] %s: %s' %
  345. (_("Imported Bookmarks from"), filename))
  346. def mark_table_rows_for_actions(self):
  347. for row in range(self.table_widget.rowCount()):
  348. item_to_paint = self.table_widget.item(row, 0)
  349. if row < self.app.defaults["global_bookmarks_limit"]:
  350. item_to_paint.setBackground(QtGui.QColor('gray'))
  351. # item_to_paint.setForeground(QtGui.QColor('black'))
  352. else:
  353. item_to_paint.setBackground(QtGui.QColor('white'))
  354. # item_to_paint.setForeground(QtGui.QColor('black'))
  355. def rebuild_actions(self):
  356. # rebuild the storage to reflect the order of the lines
  357. self.bm_dict.clear()
  358. for row in range(self.table_widget.rowCount()):
  359. title = self.table_widget.item(row, 1).text()
  360. wlink = self.table_widget.cellWidget(row, 2).toPlainText()
  361. entry = int(row) + 1
  362. self.bm_dict.update(
  363. {
  364. str(entry): [title, wlink]
  365. }
  366. )
  367. self.app.install_bookmarks(book_dict=self.bm_dict)
  368. # def accept(self):
  369. # self.rebuild_actions()
  370. # super().accept()
  371. def closeEvent(self, QCloseEvent):
  372. self.rebuild_actions()
  373. super().closeEvent(QCloseEvent)
  374. class ToolsDB(QtWidgets.QWidget):
  375. mark_tools_rows = QtCore.pyqtSignal()
  376. def __init__(self, app, parent=None):
  377. super(ToolsDB, self).__init__(parent)
  378. self.app = app
  379. self.decimals = 4
  380. # layouts
  381. layout = QtWidgets.QVBoxLayout()
  382. self.setLayout(layout)
  383. table_hlay = QtWidgets.QHBoxLayout()
  384. layout.addLayout(table_hlay)
  385. self.table_widget = FCTable(drag_drop=True)
  386. self.table_widget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
  387. table_hlay.addWidget(self.table_widget)
  388. self.table_widget.setColumnCount(21)
  389. # self.table_widget.setColumnWidth(0, 20)
  390. self.table_widget.setHorizontalHeaderLabels(
  391. [
  392. '#',
  393. _("Tool Diameter"),
  394. _("Tool Type"),
  395. _("Tool Shape"),
  396. _("Cut Z"),
  397. _("V-Tip Diameter"),
  398. _("V-Tip Angle"),
  399. _("Travel Z"),
  400. _("Feedrate"),
  401. _("Feedrate Z"),
  402. _("Feedrate Rapids"),
  403. _("Spindle Speed"),
  404. _("Dwell"),
  405. _("Dwelltime"),
  406. _("MultiDepth"),
  407. _("Postprocessor"),
  408. _("Probe Z"),
  409. _("Probe Feedrate"),
  410. _("ExtraCut"),
  411. _("Toolchange"),
  412. _("Toolchange Z"),
  413. _("End Z"),
  414. ]
  415. )
  416. self.table_widget.horizontalHeaderItem(0).setToolTip(
  417. _("Index.\n"
  418. "The rows in gray color will populate the Bookmarks menu.\n"
  419. "The number of gray colored rows is set in Preferences."))
  420. # pal = QtGui.QPalette()
  421. # pal.setColor(QtGui.QPalette.Background, Qt.white)
  422. # New Bookmark
  423. new_vlay = QtWidgets.QVBoxLayout()
  424. layout.addLayout(new_vlay)
  425. new_tool_lbl = QtWidgets.QLabel('<b>%s</b>' % _("New Tool"))
  426. new_vlay.addWidget(new_tool_lbl)
  427. form0 = QtWidgets.QFormLayout()
  428. new_vlay.addLayout(form0)
  429. diameter_lbl = QtWidgets.QLabel('%s:' % _("Diameter"))
  430. self.dia_entry = FCDoubleSpinner()
  431. self.dia_entry.set_precision(self.decimals)
  432. self.dia_entry.set_range(0.000001, 9999.9999)
  433. form0.addRow(diameter_lbl, self.dia_entry)
  434. link_lbl = QtWidgets.QLabel('%s:' % _("Web Link"))
  435. self.link_entry = FCEntry()
  436. self.link_entry.set_value('http://')
  437. form0.addRow(link_lbl, self.link_entry)
  438. # Buttons Layout
  439. button_hlay = QtWidgets.QHBoxLayout()
  440. layout.addLayout(button_hlay)
  441. add_entry_btn = FCButton(_("Add Tool"))
  442. remove_entry_btn = FCButton(_("Remove Tool"))
  443. export_list_btn = FCButton(_("Export List"))
  444. import_list_btn = FCButton(_("Import List"))
  445. closebtn = QtWidgets.QPushButton(_("Close"))
  446. # button_hlay.addStretch()
  447. button_hlay.addWidget(add_entry_btn)
  448. button_hlay.addWidget(remove_entry_btn)
  449. button_hlay.addWidget(export_list_btn)
  450. button_hlay.addWidget(import_list_btn)
  451. # button_hlay.addWidget(closebtn)
  452. # ##############################################################################
  453. # ######################## SIGNALS #############################################
  454. # ##############################################################################
  455. add_entry_btn.clicked.connect(self.on_add_entry)
  456. remove_entry_btn.clicked.connect(self.on_remove_entry)
  457. export_list_btn.clicked.connect(self.on_export_bookmarks)
  458. import_list_btn.clicked.connect(self.on_import_bookmarks)
  459. self.dia_entry.returnPressed.connect(self.on_add_entry)
  460. self.link_entry.returnPressed.connect(self.on_add_entry)
  461. # closebtn.clicked.connect(self.accept)
  462. self.bm_dict = {
  463. 1: 'tool'
  464. }
  465. self.build_bm_ui()
  466. def build_bm_ui(self):
  467. self.table_widget.setRowCount(len(self.bm_dict))
  468. nr_crt = 0
  469. sorted_bookmarks = sorted(list(self.bm_dict.items()), key=lambda x: int(x[0]))
  470. for k, v in sorted_bookmarks:
  471. row = nr_crt
  472. nr_crt += 1
  473. id_item = QtWidgets.QTableWidgetItem('%d' % int(nr_crt))
  474. id_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  475. self.table_widget.setItem(row, 0, id_item) # Tool name/id
  476. dia_item = FCDoubleSpinner()
  477. self.table_widget.setCellWidget(row, 1, dia_item)
  478. tt_item = FCComboBox()
  479. self.table_widget.setCellWidget(row, 2, tt_item)
  480. tshape_item = FCComboBox()
  481. self.table_widget.setCellWidget(row, 3, tshape_item)
  482. cutz_item = FCDoubleSpinner()
  483. self.table_widget.setCellWidget(row, 4, cutz_item)
  484. vtip_dia_item = FCDoubleSpinner()
  485. self.table_widget.setCellWidget(row, 5, vtip_dia_item)
  486. vtip_angle_item = FCDoubleSpinner()
  487. self.table_widget.setCellWidget(row, 6, vtip_angle_item)
  488. travelz_item = FCDoubleSpinner()
  489. self.table_widget.setCellWidget(row, 7, travelz_item)
  490. fr_item = FCDoubleSpinner()
  491. self.table_widget.setCellWidget(row, 8, fr_item)
  492. frz_item = FCDoubleSpinner()
  493. self.table_widget.setCellWidget(row, 9, frz_item)
  494. frrapids_item = FCDoubleSpinner()
  495. self.table_widget.setCellWidget(row, 10, frrapids_item)
  496. spindlespeed_item = FCDoubleSpinner()
  497. self.table_widget.setCellWidget(row, 11, spindlespeed_item)
  498. dwell_item = FCCheckBox()
  499. self.table_widget.setCellWidget(row, 12, dwell_item)
  500. dwelltime_item = FCDoubleSpinner()
  501. self.table_widget.setCellWidget(row, 13, dwelltime_item)
  502. multidepth_item = FCCheckBox()
  503. self.table_widget.setCellWidget(row, 14, multidepth_item)
  504. pp_item = FCComboBox()
  505. self.table_widget.setCellWidget(row, 15, pp_item)
  506. probez_item = FCDoubleSpinner()
  507. self.table_widget.setCellWidget(row, 16, probez_item)
  508. probefeedrate_item = FCDoubleSpinner()
  509. self.table_widget.setCellWidget(row, 17, probefeedrate_item)
  510. ecut_item = FCCheckBox()
  511. self.table_widget.setCellWidget(row, 18, ecut_item)
  512. toolchange_item = FCCheckBox()
  513. self.table_widget.setCellWidget(row, 19, toolchange_item)
  514. toolchangez_item = FCDoubleSpinner()
  515. self.table_widget.setCellWidget(row, 20, toolchangez_item)
  516. endz_item = FCDoubleSpinner()
  517. self.table_widget.setCellWidget(row, 21, endz_item)
  518. vertical_header = self.table_widget.verticalHeader()
  519. vertical_header.hide()
  520. horizontal_header = self.table_widget.horizontalHeader()
  521. horizontal_header.setMinimumSectionSize(10)
  522. horizontal_header.setDefaultSectionSize(70)
  523. self.table_widget.setSizeAdjustPolicy(
  524. QtWidgets.QAbstractScrollArea.AdjustToContents)
  525. for x in range(1, 21):
  526. self.table_widget.resizeColumnsToContents()
  527. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  528. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
  529. horizontal_header.setSectionResizeMode(13, QtWidgets.QHeaderView.Fixed)
  530. horizontal_header.setSectionResizeMode(15, QtWidgets.QHeaderView.Fixed)
  531. horizontal_header.setSectionResizeMode(19, QtWidgets.QHeaderView.Fixed)
  532. horizontal_header.setSectionResizeMode(20, QtWidgets.QHeaderView.Fixed)
  533. horizontal_header.resizeSection(0, 20)
  534. # horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
  535. # horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch)
  536. def on_add_entry(self, **kwargs):
  537. """
  538. Add a entry in the Bookmark Table and in the menu actions
  539. :return: None
  540. """
  541. if 'title' in kwargs:
  542. title = kwargs['title']
  543. else:
  544. title = self.title_entry.get_value()
  545. if title == '':
  546. self.app.inform.emit(f'[ERROR_NOTCL] {_("Title entry is empty.")}')
  547. return 'fail'
  548. if 'link' is kwargs:
  549. link = kwargs['link']
  550. else:
  551. link = self.link_entry.get_value()
  552. if link == 'http://':
  553. self.app.inform.emit(f'[ERROR_NOTCL] {_("Web link entry is empty.")}')
  554. return 'fail'
  555. # if 'http' not in link or 'https' not in link:
  556. # link = 'http://' + link
  557. for bookmark in self.bm_dict.values():
  558. if title == bookmark[0] or link == bookmark[1]:
  559. self.app.inform.emit(f'[ERROR_NOTCL] {_("Either the Title or the Weblink already in the table.")}')
  560. return 'fail'
  561. # for some reason if the last char in the weblink is a slash it does not make the link clickable
  562. # so I remove it
  563. if link[-1] == '/':
  564. link = link[:-1]
  565. # add the new entry to storage
  566. new_entry = len(self.bm_dict) + 1
  567. self.bm_dict[str(new_entry)] = [title, link]
  568. # add the link to the menu but only if it is within the set limit
  569. bm_limit = int(self.app.defaults["global_bookmarks_limit"])
  570. if len(self.bm_dict) < bm_limit:
  571. act = QtWidgets.QAction(parent=self.app.ui.menuhelp_bookmarks)
  572. act.setText(title)
  573. act.setIcon(QtGui.QIcon('share/link16.png'))
  574. act.triggered.connect(lambda: webbrowser.open(link))
  575. self.app.ui.menuhelp_bookmarks.insertAction(self.app.ui.menuhelp_bookmarks_manager, act)
  576. self.app.inform.emit(f'[success] {_("Bookmark added.")}')
  577. # add the new entry to the bookmark manager table
  578. self.build_bm_ui()
  579. def on_remove_entry(self):
  580. """
  581. Remove an Entry in the Bookmark table and from the menu actions
  582. :return:
  583. """
  584. index_list = []
  585. for model_index in self.table_widget.selectionModel().selectedRows():
  586. index = QtCore.QPersistentModelIndex(model_index)
  587. index_list.append(index)
  588. title_to_remove = self.table_widget.item(model_index.row(), 1).text()
  589. if title_to_remove == 'FlatCAM' or title_to_remove == 'Backup Site':
  590. self.app.inform.emit('[WARNING_NOTCL] %s.' % _("This bookmark can not be removed"))
  591. self.build_bm_ui()
  592. return
  593. else:
  594. for k, bookmark in list(self.bm_dict.items()):
  595. if title_to_remove == bookmark[0]:
  596. # remove from the storage
  597. self.bm_dict.pop(k, None)
  598. for act in self.app.ui.menuhelp_bookmarks.actions():
  599. if act.text() == title_to_remove:
  600. # disconnect the signal
  601. try:
  602. act.triggered.disconnect()
  603. except TypeError:
  604. pass
  605. # remove the action from the menu
  606. self.app.ui.menuhelp_bookmarks.removeAction(act)
  607. # house keeping: it pays to have keys increased by one
  608. new_key = 0
  609. new_dict = dict()
  610. for k, v in self.bm_dict.items():
  611. # we start with key 1 so we can use the len(self.bm_dict)
  612. # when adding bookmarks (keys in bm_dict)
  613. new_key += 1
  614. new_dict[str(new_key)] = v
  615. self.bm_dict = deepcopy(new_dict)
  616. new_dict.clear()
  617. self.app.inform.emit(f'[success] {_("Bookmark removed.")}')
  618. # for index in index_list:
  619. # self.table_widget.model().removeRow(index.row())
  620. self.build_bm_ui()
  621. def on_export_bookmarks(self):
  622. self.app.report_usage("on_export_bookmarks")
  623. self.app.log.debug("on_export_bookmarks()")
  624. date = str(datetime.today()).rpartition('.')[0]
  625. date = ''.join(c for c in date if c not in ':-')
  626. date = date.replace(' ', '_')
  627. filter__ = "Text File (*.TXT);;All Files (*.*)"
  628. filename, _f = QtWidgets.QFileDialog.getSaveFileName(caption=_("Export FlatCAM Preferences"),
  629. directory='{l_save}/FlatCAM_{n}_{date}'.format(
  630. l_save=str(self.app.get_last_save_folder()),
  631. n=_("Bookmarks"),
  632. date=date),
  633. filter=filter__)
  634. filename = str(filename)
  635. if filename == "":
  636. self.app.inform.emit('[WARNING_NOTCL] %s' % _("FlatCAM bookmarks export cancelled."))
  637. return
  638. else:
  639. try:
  640. f = open(filename, 'w')
  641. f.close()
  642. except PermissionError:
  643. self.app.inform.emit('[WARNING] %s' %
  644. _("Permission denied, saving not possible.\n"
  645. "Most likely another app is holding the file open and not accessible."))
  646. return
  647. except IOError:
  648. self.app.log.debug('Creating a new bookmarks file ...')
  649. f = open(filename, 'w')
  650. f.close()
  651. except:
  652. e = sys.exc_info()[0]
  653. self.app.log.error("Could not load defaults file.")
  654. self.app.log.error(str(e))
  655. self.app.inform.emit('[ERROR_NOTCL] %s' %
  656. _("Could not load bookmarks file."))
  657. return
  658. # Save update options
  659. try:
  660. with open(filename, "w") as f:
  661. for title, link in self.bm_dict.items():
  662. line2write = str(title) + ':' + str(link) + '\n'
  663. f.write(line2write)
  664. except:
  665. self.app.inform.emit('[ERROR_NOTCL] %s' %
  666. _("Failed to write bookmarks to file."))
  667. return
  668. self.app.inform.emit('[success] %s: %s' %
  669. (_("Exported bookmarks to"), filename))
  670. def on_import_bookmarks(self):
  671. self.app.log.debug("on_import_bookmarks()")
  672. filter_ = "Text File (*.txt);;All Files (*.*)"
  673. filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import FlatCAM Bookmarks"),
  674. filter=filter_)
  675. filename = str(filename)
  676. if filename == "":
  677. self.app.inform.emit('[WARNING_NOTCL] %s' %
  678. _("FlatCAM bookmarks import cancelled."))
  679. else:
  680. try:
  681. with open(filename) as f:
  682. bookmarks = f.readlines()
  683. except IOError:
  684. self.app.log.error("Could not load bookmarks file.")
  685. self.app.inform.emit('[ERROR_NOTCL] %s' %
  686. _("Could not load bookmarks file."))
  687. return
  688. for line in bookmarks:
  689. proc_line = line.replace(' ', '').partition(':')
  690. self.on_add_entry(title=proc_line[0], link=proc_line[2])
  691. self.app.inform.emit('[success] %s: %s' %
  692. (_("Imported Bookmarks from"), filename))
  693. def rebuild_actions(self):
  694. # rebuild the storage to reflect the order of the lines
  695. self.bm_dict.clear()
  696. for row in range(self.table_widget.rowCount()):
  697. title = self.table_widget.item(row, 1).text()
  698. wlink = self.table_widget.cellWidget(row, 2).toPlainText()
  699. entry = int(row) + 1
  700. self.bm_dict.update(
  701. {
  702. str(entry): [title, wlink]
  703. }
  704. )
  705. self.app.install_bookmarks(book_dict=self.bm_dict)
  706. # def accept(self):
  707. # self.rebuild_actions()
  708. # super().accept()
  709. def closeEvent(self, QCloseEvent):
  710. self.rebuild_actions()
  711. super().closeEvent(QCloseEvent)