File size: 4,686 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *   Copyright (c) 2023 <https://www.freecad.org>                          *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

"""Provides GUI tools to open hyperlinks to internal/external documents."""

## @package gui_hyperlink
# \ingroup draftguitools
# \brief Provides GUI tools to open hyperlinks to internal/external documents

## \addtogroup draftguitools
# @{

import FreeCAD
import os
import re
from draftutils.messages import _msg, _toolmsg

if FreeCAD.GuiUp:
    import FreeCADGui
    import Draft_rc
    from PySide.QtCore import QUrl
    from PySide.QtGui import QDesktopServices


def QT_TRANSLATE_NOOP(ctx, txt):
    return txt


translate = FreeCAD.Qt.translate

from PySide import QtWidgets

__title__ = "FreeCAD Draft Workbench GUI Tools - Hyperlinks tools"
__author__ = ""
__url__ = "https://www.freecad.org"


class Draft_Hyperlink:
    """The Draft_Hyperlink FreeCAD command definition."""

    def GetResources(self):
        d = {
            "Pixmap": "",
            "Accel": "",
            "MenuText": QT_TRANSLATE_NOOP("Draft_Hyperlink", "Open Links"),
            "ToolTip": QT_TRANSLATE_NOOP("Draft_Hyperlink", "Opens linked documents"),
        }
        return d

    def Activated(self):
        self.find_hyperlinks()

        ret = None
        if len(self.hyperlinks_list) > 1:
            m = QtWidgets.QMessageBox()
            m.setWindowTitle(translate("draft", "Opening Multiple Links"))
            m.setText(translate("draft", "Multiple links found"))
            m.setInformativeText(
                translate("draft", "This may lead to the opening of various windows")
            )
            m.setStandardButtons(m.Ok | m.Cancel)
            ret = m.exec_()

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

    def find_hyperlinks(self):
        self.hyperlinks_list = []

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

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

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

    def has_hyperlinks(self):
        self.find_hyperlinks()

        return len(self.hyperlinks_list) > 0

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

        url = None
        if file_hyperlink:
            if not os.path.isfile(hyperlink):
                _msg(translate("draft", "File not found:") + " " + hyperlink)
                return
            url = QUrl.fromLocalFile(hyperlink)
        else:
            url = QUrl(hyperlink)

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

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


FreeCADGui.addCommand("Draft_Hyperlink", Draft_Hyperlink())

## @}