FreeCAD

Форк
0
/
gui_hyperlink.py 
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

31
import FreeCAD
32
import os
33
import re
34
from draftutils.messages import _msg, _toolmsg
35

36
if FreeCAD.GuiUp:
37
    import FreeCADGui
38
    import Draft_rc
39
    from PySide.QtCore import QUrl
40
    from PySide.QtGui import QDesktopServices
41
def QT_TRANSLATE_NOOP(ctx,txt):
42
    return txt
43
translate = FreeCAD.Qt.translate
44

45
from PySide import QtWidgets
46

47
__title__ = "FreeCAD Draft Workbench GUI Tools - Hyperlinks tools"
48
__author__ = ("")
49
__url__ = "https://www.freecad.org"
50

51

52
class Draft_Hyperlink:
53
    """The Draft_Hyperlink FreeCAD command definition."""
54

55
    def GetResources(self):
56
        d = {'Pixmap': '',
57
             'Accel': "",
58
             'MenuText': QT_TRANSLATE_NOOP("Draft_Hyperlink", "Open hyperlinks"),
59
             'ToolTip': QT_TRANSLATE_NOOP("Draft_Hyperlink", "Open linked documents")}
60
        return d
61

62
    def Activated(self):
63
        self.find_hyperlinks()
64

65
        ret = None
66
        if len(self.hyperlinks_list) > 1:
67
            m = QtWidgets.QMessageBox()
68
            m.setWindowTitle(translate("draft", "Opening multiple hyperlinks"))
69
            m.setText(
70
                translate(
71
                    "draft",
72
                    "Multiple hyperlinks found."
73
                )
74
            )
75
            m.setInformativeText(
76
                translate(
77
                    "draft",
78
                    "This may lead to the opening of various windows"
79
                )
80
            )
81
            m.setStandardButtons(m.Ok | m.Cancel)
82
            ret = m.exec_()
83

84
        if len(self.hyperlinks_list) == 1 or ret == m.Ok:
85
            for hyperlink in self.hyperlinks_list:
86
                self.open_hyperlink(hyperlink)
87

88
    def find_hyperlinks(self):
89
        self.hyperlinks_list = []
90

91
        for o in FreeCADGui.Selection.getCompleteSelection():
92
            if hasattr(o.Object, "Text"):
93

94
                for text in o.Object.Text:
95
                    hyperlinks = re.findall(r"((\w:[\\/]|%\w+%|\\\\\w+|/\w+|\w{3,5}://)[\w\\/: ]+\.[\S]+)", text)
96

97
                    for hyperlink in hyperlinks:
98
                        self.hyperlinks_list.append(hyperlink[0])
99

100
    def has_hyperlinks(self):
101
        self.find_hyperlinks()
102

103
        return len(self.hyperlinks_list) > 0
104

105
    def open_hyperlink(self, hyperlink):
106
        file_hyperlink = len(re.findall(r"^(\w:[\\/]|%\w+%|\\\\\w+|/\w+)", hyperlink)) > 0
107

108
        url = None
109
        if file_hyperlink:
110
            if not os.path.isfile(hyperlink):
111
                _msg(translate("draft", "File not found:") + " " + hyperlink)
112
                return
113
            url = QUrl.fromLocalFile(hyperlink)
114
        else:
115
            url = QUrl(hyperlink)
116

117
        _toolmsg(translate("draft", "Opening hyperlink") + " " + hyperlink)
118

119
        QDesktopServices.openUrl(url) #ToDo: add management to open FCStd files in the current instance and to open web pages with the Web Workbench
120

121
FreeCADGui.addCommand('Draft_Hyperlink', Draft_Hyperlink())
122

123
## @}
124

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.