doorstop.gui.utilTkinter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A HyperlinkManager.__init__() 0 11 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
19
        self.text = text
20
21
        self.text.tag_config("hyper", underline=True)
22
23
        self.text.tag_bind("hyper", "<Enter>", self._enter)
24
        self.text.tag_bind("hyper", "<Leave>", self._leave)
25
        self.text.tag_bind("hyper", "<Button-1>", self._click)
26
27
        self.reset()
28
29
    def reset(self):
30
        """Remove all hyperlinks."""
31
        self.links = {}
32
33
    def add(self, action, p_id, p_Tags=[]):  # pylint: disable=W0102
34
        """
35
        Add a new hyper link.
36
37
        @param action: method that will be called for this hyperlink
38
        @param p_id: the arbitration id that we are associating this action.
39
        """
40
        # add an action to the manager.  returns tags to use in
41
        # associated text widget
42
        thetags = []
43
        uniquetag = "hyper-%d" % len(self.links)
44
        thetags.extend(p_Tags)
45
        thetags.append("hyper")
46
        thetags.append(uniquetag)
47
        self.links[uniquetag] = [action, p_id]
48
        return tuple(thetags)
49
50
    def _enter(self, event):  # pylint: disable=W0613
51
        self.text.config(cursor="hand2")
52
53
    def _leave(self, event):  # pylint: disable=W0613
54
        self.text.config(cursor="")
55
56
    def _click(self, event):  # pylint: disable=W0613
57
        """If somebody clicks on the link it will find the method to call."""
58
        for tag in self.text.tag_names(tk.CURRENT):
59
            if tag[:6] == "hyper-":
60
                self.links[tag][0](self.links[tag][1])
61
62
63
def getAllChildren(treeView, item=None):
64
    """Recursive generator of all the children item of the provided ttk.Treeview."""
65
    for c_currUID in treeView.get_children(item):
66
        yield c_currUID
67
        yield from getAllChildren(treeView, c_currUID)
68