Bookmark.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. from PyQt5 import QtGui, QtCore, QtWidgets
  2. from appGUI.GUIElements import FCTable, FCEntry, FCButton, FCFileSaveDialog
  3. import sys
  4. import webbrowser
  5. from copy import deepcopy
  6. from datetime import datetime
  7. import gettext
  8. import appTranslation as fcTranslate
  9. import builtins
  10. fcTranslate.apply_language('strings')
  11. if '_' not in builtins.__dict__:
  12. _ = gettext.gettext
  13. class BookmarkManager(QtWidgets.QWidget):
  14. # mark_rows = QtCore.pyqtSignal()
  15. def __init__(self, app, storage, parent=None):
  16. super(BookmarkManager, self).__init__(parent)
  17. self.app = app
  18. assert isinstance(storage, dict), "Storage argument is not a dictionary"
  19. self.bm_dict = deepcopy(storage)
  20. # Icon and title
  21. # self.setWindowIcon(parent.app_icon)
  22. # self.setWindowTitle(_("Bookmark Manager"))
  23. # self.resize(600, 400)
  24. # title = QtWidgets.QLabel(
  25. # "<font size=8><B>FlatCAM</B></font><BR>"
  26. # )
  27. # title.setOpenExternalLinks(True)
  28. # layouts
  29. layout = QtWidgets.QVBoxLayout()
  30. self.setLayout(layout)
  31. table_hlay = QtWidgets.QHBoxLayout()
  32. layout.addLayout(table_hlay)
  33. self.table_widget = FCTable(drag_drop=True, protected_rows=[0, 1])
  34. self.table_widget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
  35. table_hlay.addWidget(self.table_widget)
  36. self.table_widget.setColumnCount(3)
  37. self.table_widget.setColumnWidth(0, 20)
  38. self.table_widget.setHorizontalHeaderLabels(
  39. [
  40. '#',
  41. _('Title'),
  42. _('Web Link')
  43. ]
  44. )
  45. self.table_widget.horizontalHeaderItem(0).setToolTip(
  46. _("Index.\n"
  47. "The rows in gray color will populate the Bookmarks menu.\n"
  48. "The number of gray colored rows is set in Preferences."))
  49. self.table_widget.horizontalHeaderItem(1).setToolTip(
  50. _("Description of the link that is set as an menu action.\n"
  51. "Try to keep it short because it is installed as a menu item."))
  52. self.table_widget.horizontalHeaderItem(2).setToolTip(
  53. _("Web Link. E.g: https://your_website.org "))
  54. # pal = QtGui.QPalette()
  55. # pal.setColor(QtGui.QPalette.Background, Qt.white)
  56. # New Bookmark
  57. new_vlay = QtWidgets.QVBoxLayout()
  58. layout.addLayout(new_vlay)
  59. new_title_lbl = QtWidgets.QLabel('<b>%s</b>' % _("New Bookmark"))
  60. new_vlay.addWidget(new_title_lbl)
  61. form0 = QtWidgets.QFormLayout()
  62. new_vlay.addLayout(form0)
  63. title_lbl = QtWidgets.QLabel('%s:' % _("Title"))
  64. self.title_entry = FCEntry()
  65. form0.addRow(title_lbl, self.title_entry)
  66. link_lbl = QtWidgets.QLabel('%s:' % _("Web Link"))
  67. self.link_entry = FCEntry()
  68. self.link_entry.set_value('http://')
  69. form0.addRow(link_lbl, self.link_entry)
  70. # Buttons Layout
  71. button_hlay = QtWidgets.QHBoxLayout()
  72. layout.addLayout(button_hlay)
  73. add_entry_btn = FCButton(_("Add Entry"))
  74. remove_entry_btn = FCButton(_("Remove Entry"))
  75. export_list_btn = FCButton(_("Export List"))
  76. import_list_btn = FCButton(_("Import List"))
  77. # closebtn = QtWidgets.QPushButton(_("Close"))
  78. # button_hlay.addStretch()
  79. button_hlay.addWidget(add_entry_btn)
  80. button_hlay.addWidget(remove_entry_btn)
  81. button_hlay.addWidget(export_list_btn)
  82. button_hlay.addWidget(import_list_btn)
  83. # button_hlay.addWidget(closebtn)
  84. # ##############################################################################
  85. # ######################## SIGNALS #############################################
  86. # ##############################################################################
  87. add_entry_btn.clicked.connect(self.on_add_entry)
  88. remove_entry_btn.clicked.connect(self.on_remove_entry)
  89. export_list_btn.clicked.connect(self.on_export_bookmarks)
  90. import_list_btn.clicked.connect(self.on_import_bookmarks)
  91. self.title_entry.returnPressed.connect(self.on_add_entry)
  92. self.link_entry.returnPressed.connect(self.on_add_entry)
  93. # closebtn.clicked.connect(self.accept)
  94. self.ui_connect()
  95. self.build_bm_ui()
  96. def ui_connect(self):
  97. self.table_widget.drag_drop_sig.connect(self.mark_table_rows_for_actions)
  98. def ui_disconnect(self):
  99. try:
  100. self.table_widget.drag_drop_sig.connect(self.mark_table_rows_for_actions)
  101. except (TypeError, AttributeError):
  102. pass
  103. def build_bm_ui(self):
  104. self.table_widget.setRowCount(len(self.bm_dict))
  105. nr_crt = 0
  106. sorted_bookmarks = sorted(list(self.bm_dict.items()), key=lambda x: int(x[0]))
  107. for entry, bookmark in sorted_bookmarks:
  108. row = nr_crt
  109. nr_crt += 1
  110. title = bookmark[0]
  111. weblink = bookmark[1]
  112. id_item = QtWidgets.QTableWidgetItem('%d' % int(nr_crt))
  113. # id.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  114. self.table_widget.setItem(row, 0, id_item) # Tool name/id
  115. title_item = QtWidgets.QTableWidgetItem(title)
  116. self.table_widget.setItem(row, 1, title_item)
  117. weblink_txt = QtWidgets.QTextBrowser()
  118. weblink_txt.setOpenExternalLinks(True)
  119. weblink_txt.setFrameStyle(QtWidgets.QFrame.NoFrame)
  120. weblink_txt.document().setDefaultStyleSheet("a{ text-decoration: none; }")
  121. weblink_txt.setHtml('<a href=%s>%s</a>' % (weblink, weblink))
  122. self.table_widget.setCellWidget(row, 2, weblink_txt)
  123. vertical_header = self.table_widget.verticalHeader()
  124. vertical_header.hide()
  125. horizontal_header = self.table_widget.horizontalHeader()
  126. horizontal_header.setMinimumSectionSize(10)
  127. horizontal_header.setDefaultSectionSize(70)
  128. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  129. horizontal_header.resizeSection(0, 20)
  130. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
  131. horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch)
  132. self.mark_table_rows_for_actions()
  133. self.app.defaults["global_bookmarks"].clear()
  134. for key, val in self.bm_dict.items():
  135. self.app.defaults["global_bookmarks"][key] = deepcopy(val)
  136. def on_add_entry(self, **kwargs):
  137. """
  138. Add a entry in the Bookmark Table and in the menu actions
  139. :return: None
  140. """
  141. if 'title' in kwargs:
  142. title = kwargs['title']
  143. else:
  144. title = self.title_entry.get_value()
  145. if title == '':
  146. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Title entry is empty."))
  147. return 'fail'
  148. if 'link' in kwargs:
  149. link = kwargs['link']
  150. else:
  151. link = self.link_entry.get_value()
  152. if link == 'http://':
  153. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Web link entry is empty."))
  154. return 'fail'
  155. # if 'http' not in link or 'https' not in link:
  156. # link = 'http://' + link
  157. for bookmark in self.bm_dict.values():
  158. if title == bookmark[0] or link == bookmark[1]:
  159. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Either the Title or the Weblink already in the table."))
  160. return 'fail'
  161. # for some reason if the last char in the weblink is a slash it does not make the link clickable
  162. # so I remove it
  163. if link[-1] == '/':
  164. link = link[:-1]
  165. # add the new entry to storage
  166. new_entry = len(self.bm_dict) + 1
  167. self.bm_dict[str(new_entry)] = [title, link]
  168. # add the link to the menu but only if it is within the set limit
  169. bm_limit = int(self.app.defaults["global_bookmarks_limit"])
  170. if len(self.bm_dict) < bm_limit:
  171. act = QtWidgets.QAction(parent=self.app.ui.menuhelp_bookmarks)
  172. act.setText(title)
  173. act.setIcon(QtGui.QIcon(self.app.resource_location + '/link16.png'))
  174. act.triggered.connect(lambda: webbrowser.open(link))
  175. self.app.ui.menuhelp_bookmarks.insertAction(self.app.ui.menuhelp_bookmarks_manager, act)
  176. self.app.inform.emit('[success] %s' % _("Bookmark added."))
  177. # add the new entry to the bookmark manager table
  178. self.build_bm_ui()
  179. def on_remove_entry(self):
  180. """
  181. Remove an Entry in the Bookmark table and from the menu actions
  182. :return:
  183. """
  184. index_list = []
  185. for model_index in self.table_widget.selectionModel().selectedRows():
  186. index = QtCore.QPersistentModelIndex(model_index)
  187. index_list.append(index)
  188. title_to_remove = self.table_widget.item(model_index.row(), 1).text()
  189. if title_to_remove == 'FlatCAM' or title_to_remove == 'Backup Site':
  190. self.app.inform.emit('[WARNING_NOTCL] %s.' % _("This bookmark can not be removed"))
  191. self.build_bm_ui()
  192. return
  193. else:
  194. for k, bookmark in list(self.bm_dict.items()):
  195. if title_to_remove == bookmark[0]:
  196. # remove from the storage
  197. self.bm_dict.pop(k, None)
  198. for act in self.app.ui.menuhelp_bookmarks.actions():
  199. if act.text() == title_to_remove:
  200. # disconnect the signal
  201. try:
  202. act.triggered.disconnect()
  203. except TypeError:
  204. pass
  205. # remove the action from the menu
  206. self.app.ui.menuhelp_bookmarks.removeAction(act)
  207. # house keeping: it pays to have keys increased by one
  208. new_key = 0
  209. new_dict = {}
  210. for k, v in self.bm_dict.items():
  211. # we start with key 1 so we can use the len(self.bm_dict)
  212. # when adding bookmarks (keys in bm_dict)
  213. new_key += 1
  214. new_dict[str(new_key)] = v
  215. self.bm_dict = deepcopy(new_dict)
  216. new_dict.clear()
  217. self.app.inform.emit('[success] %s' % _("Bookmark removed."))
  218. # for index in index_list:
  219. # self.table_widget.model().removeRow(index.row())
  220. self.build_bm_ui()
  221. def on_export_bookmarks(self):
  222. self.app.defaults.report_usage("on_export_bookmarks")
  223. self.app.log.debug("on_export_bookmarks()")
  224. date = str(datetime.today()).rpartition('.')[0]
  225. date = ''.join(c for c in date if c not in ':-')
  226. date = date.replace(' ', '_')
  227. filter__ = "Text File (*.TXT);;All Files (*.*)"
  228. filename, _f = FCFileSaveDialog.get_saved_filename(caption=_("Export Bookmarks"),
  229. directory='{l_save}/{n}_{date}'.format(
  230. l_save=str(self.app.get_last_save_folder()),
  231. n=_("Bookmarks"),
  232. date=date),
  233. ext_filter=filter__)
  234. filename = str(filename)
  235. if filename == "":
  236. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled."))
  237. return
  238. else:
  239. try:
  240. f = open(filename, 'w')
  241. f.close()
  242. except PermissionError:
  243. self.app.inform.emit('[WARNING] %s' %
  244. _("Permission denied, saving not possible.\n"
  245. "Most likely another app is holding the file open and not accessible."))
  246. return
  247. except IOError:
  248. self.app.log.debug('Creating a new bookmarks file ...')
  249. f = open(filename, 'w')
  250. f.close()
  251. except Exception:
  252. e = sys.exc_info()[0]
  253. self.app.log.error("Could not load defaults file.")
  254. self.app.log.error(str(e))
  255. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Could not load bookmarks file."))
  256. return
  257. # Save Bookmarks to a file
  258. try:
  259. with open(filename, "w") as f:
  260. for title, link in self.bm_dict.items():
  261. line2write = str(title) + ':' + str(link) + '\n'
  262. f.write(line2write)
  263. except Exception:
  264. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Failed to write bookmarks to file."))
  265. return
  266. self.app.inform.emit('[success] %s: %s' % (_("Exported bookmarks to"), filename))
  267. def on_import_bookmarks(self):
  268. self.app.log.debug("on_import_bookmarks()")
  269. filter_ = "Text File (*.txt);;All Files (*.*)"
  270. filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import Bookmarks"), filter=filter_)
  271. filename = str(filename)
  272. if filename == "":
  273. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled."))
  274. else:
  275. try:
  276. with open(filename) as f:
  277. bookmarks = f.readlines()
  278. except IOError:
  279. self.app.log.error("Could not load bookmarks file.")
  280. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Could not load bookmarks file."))
  281. return
  282. for line in bookmarks:
  283. proc_line = line.replace(' ', '').partition(':')
  284. self.on_add_entry(title=proc_line[0], link=proc_line[2])
  285. self.app.inform.emit('[success] %s: %s' % (_("Imported Bookmarks from"), filename))
  286. def mark_table_rows_for_actions(self):
  287. for row in range(self.table_widget.rowCount()):
  288. item_to_paint = self.table_widget.item(row, 0)
  289. if row < self.app.defaults["global_bookmarks_limit"]:
  290. item_to_paint.setBackground(QtGui.QColor('gray'))
  291. # item_to_paint.setForeground(QtGui.QColor('black'))
  292. else:
  293. item_to_paint.setBackground(QtGui.QColor('white'))
  294. # item_to_paint.setForeground(QtGui.QColor('black'))
  295. def rebuild_actions(self):
  296. # rebuild the storage to reflect the order of the lines
  297. self.bm_dict.clear()
  298. for row in range(self.table_widget.rowCount()):
  299. title = self.table_widget.item(row, 1).text()
  300. wlink = self.table_widget.cellWidget(row, 2).toPlainText()
  301. entry = int(row) + 1
  302. self.bm_dict.update(
  303. {
  304. str(entry): [title, wlink]
  305. }
  306. )
  307. self.app.install_bookmarks(book_dict=self.bm_dict)
  308. # def accept(self):
  309. # self.rebuild_actions()
  310. # super().accept()
  311. def closeEvent(self, QCloseEvent):
  312. self.rebuild_actions()
  313. self.ui_disconnect()
  314. super().closeEvent(QCloseEvent)