doorstop.gui.utilTkinter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 39
dl 0
loc 67
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A HyperlinkManager.__init__() 0 10 1
A HyperlinkManager.add() 0 16 1
A HyperlinkManager._enter() 0 2 1
A HyperlinkManager.reset() 0 3 1
A HyperlinkManager._leave() 0 2 1
A HyperlinkManager._click() 0 5 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A getAllChildren() 0 5 2
1
#!/usr/bin/env python
2
# SPDX-License-Identifier: LGPL-3.0-only
3
# type: ignore
4
5
import sys
6
from unittest.mock import Mock
7
8
try:
9
    import tkinter as tk
10
except ImportError as _exc:
11
    sys.stderr.write("WARNING: {}\n".format(_exc))
12
    tk = Mock()
13
    ttk = Mock()
14
15
16
class HyperlinkManager:
17
    def __init__(self, text) -> None:
18
        self.text = text
19
20
        self.text.tag_config("hyper", underline=True)
21
22
        self.text.tag_bind("hyper", "<Enter>", self._enter)
23
        self.text.tag_bind("hyper", "<Leave>", self._leave)
24
        self.text.tag_bind("hyper", "<Button-1>", self._click)
25
26
        self.reset()
27
28
    def reset(self):
29
        """Remove all hyperlinks."""
30
        self.links = {}
31
32
    def add(self, action, p_id, p_Tags=[]):  # pylint: disable=W0102
33
        """
34
        Add a new hyper link.
35
36
        @param action: method that will be called for this hyperlink
37
        @param p_id: the arbitration id that we are associating this action.
38
        """
39
        # add an action to the manager.  returns tags to use in
40
        # associated text widget
41
        thetags = []
42
        uniquetag = "hyper-%d" % len(self.links)
43
        thetags.extend(p_Tags)
44
        thetags.append("hyper")
45
        thetags.append(uniquetag)
46
        self.links[uniquetag] = [action, p_id]
47
        return tuple(thetags)
48
49
    def _enter(self, event):  # pylint: disable=W0613
50
        self.text.config(cursor="hand2")
51
52
    def _leave(self, event):  # pylint: disable=W0613
53
        self.text.config(cursor="")
54
55
    def _click(self, event):  # pylint: disable=W0613
56
        """If somebody clicks on the link it will find the method to call."""
57
        for tag in self.text.tag_names(tk.CURRENT):
58
            if tag[:6] == "hyper-":
59
                self.links[tag][0](self.links[tag][1])
60
61
62
def getAllChildren(treeView, item=None):
63
    """Recursive generator of all the children item of the provided ttk.Treeview."""
64
    for c_currUID in treeView.get_children(item):
65
        yield c_currUID
66
        yield from getAllChildren(treeView, c_currUID)
67