FreeCAD
123 строки · 4.6 Кб
1# ***************************************************************************
2# * Copyright (c) 2023 <https://www.freecad.org> *
3# * *
4# * This program is free software; you can redistribute it and/or modify *
5# * it under the terms of the GNU Lesser General Public License (LGPL) *
6# * as published by the Free Software Foundation; either version 2 of *
7# * the License, or (at your option) any later version. *
8# * for detail see the LICENCE text file. *
9# * *
10# * This program is distributed in the hope that it will be useful, *
11# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13# * GNU Library General Public License for more details. *
14# * *
15# * You should have received a copy of the GNU Library General Public *
16# * License along with this program; if not, write to the Free Software *
17# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
18# * USA *
19# * *
20# ***************************************************************************
21
22"""Provides GUI tools to open hyperlinks to internal/external documents."""
23
24## @package gui_hyperlink
25# \ingroup draftguitools
26# \brief Provides GUI tools to open hyperlinks to internal/external documents
27
28## \addtogroup draftguitools
29# @{
30
31import FreeCAD32import os33import re34from draftutils.messages import _msg, _toolmsg35
36if FreeCAD.GuiUp:37import FreeCADGui38import Draft_rc39from PySide.QtCore import QUrl40from PySide.QtGui import QDesktopServices41def QT_TRANSLATE_NOOP(ctx,txt):42return txt43translate = FreeCAD.Qt.translate44
45from PySide import QtWidgets46
47__title__ = "FreeCAD Draft Workbench GUI Tools - Hyperlinks tools"48__author__ = ("")49__url__ = "https://www.freecad.org"50
51
52class Draft_Hyperlink:53"""The Draft_Hyperlink FreeCAD command definition."""54
55def GetResources(self):56d = {'Pixmap': '',57'Accel': "",58'MenuText': QT_TRANSLATE_NOOP("Draft_Hyperlink", "Open hyperlinks"),59'ToolTip': QT_TRANSLATE_NOOP("Draft_Hyperlink", "Open linked documents")}60return d61
62def Activated(self):63self.find_hyperlinks()64
65ret = None66if len(self.hyperlinks_list) > 1:67m = QtWidgets.QMessageBox()68m.setWindowTitle(translate("draft", "Opening multiple hyperlinks"))69m.setText(70translate(71"draft",72"Multiple hyperlinks found."73)74)75m.setInformativeText(76translate(77"draft",78"This may lead to the opening of various windows"79)80)81m.setStandardButtons(m.Ok | m.Cancel)82ret = m.exec_()83
84if len(self.hyperlinks_list) == 1 or ret == m.Ok:85for hyperlink in self.hyperlinks_list:86self.open_hyperlink(hyperlink)87
88def find_hyperlinks(self):89self.hyperlinks_list = []90
91for o in FreeCADGui.Selection.getCompleteSelection():92if hasattr(o.Object, "Text"):93
94for text in o.Object.Text:95hyperlinks = re.findall(r"((\w:[\\/]|%\w+%|\\\\\w+|/\w+|\w{3,5}://)[\w\\/: ]+\.[\S]+)", text)96
97for hyperlink in hyperlinks:98self.hyperlinks_list.append(hyperlink[0])99
100def has_hyperlinks(self):101self.find_hyperlinks()102
103return len(self.hyperlinks_list) > 0104
105def open_hyperlink(self, hyperlink):106file_hyperlink = len(re.findall(r"^(\w:[\\/]|%\w+%|\\\\\w+|/\w+)", hyperlink)) > 0107
108url = None109if file_hyperlink:110if not os.path.isfile(hyperlink):111_msg(translate("draft", "File not found:") + " " + hyperlink)112return113url = QUrl.fromLocalFile(hyperlink)114else:115url = QUrl(hyperlink)116
117_toolmsg(translate("draft", "Opening hyperlink") + " " + hyperlink)118
119QDesktopServices.openUrl(url) #ToDo: add management to open FCStd files in the current instance and to open web pages with the Web Workbench120
121FreeCADGui.addCommand('Draft_Hyperlink', Draft_Hyperlink())122
123## @}
124