GetNodeDialog.getNode()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
1
from PyQt5.QtCore import pyqtSignal
2
from PyQt5.QtWidgets import QTreeView, QDialog, QHBoxLayout, QVBoxLayout, QDialogButtonBox, QAbstractItemView, QPushButton, QLineEdit, QWidget
3
from PyQt5.QtCore import Qt
4
5
from opcua import Node, ua
6
7
from uawidgets.tree_widget import TreeWidget
8
9
10
class GetNodeTextButton(QWidget):
11
    """
12
    Create a text field with  a button which will query a node
13
    """
14
15
    def __init__(self, parent, currentnode, startnode):
16
        QWidget.__init__(self, parent)
17
        #QWidget.__init__(self)
18
        if currentnode.nodeid.is_null():
19
            text = "Null"
20
        else:
21
            text = currentnode.nodeid.to_string()
22
        self.lineEdit = QLineEdit(parent)
23
        self.lineEdit.setText(text)
24
        self.button = QPushButton(parent)
25
        self.button.setText("...")
26
        self.button.setMinimumWidth(5)
27
        self.layout = QHBoxLayout(parent)
28
        self.layout.setSpacing(0)
29
        self.layout.setContentsMargins(0, 0, 0, 0)
30
        self.layout.addWidget(self.lineEdit)
31
        self.layout.addWidget(self.button)
32
        self.setLayout(self.layout)
33
        self.server = currentnode.server
34
        self.start_node = startnode
35
        self.button.clicked.connect(self.get_new_node)
36
37
    def get_new_node(self):
38
        node = self.get_node()
39
        node, ok = GetNodeDialog.getNode(self, self.start_node, currentnode=node)
40
        if ok:
41
            self.lineEdit.setText(node.nodeid.to_string())
42
43
    def get_node(self):
44
        text = self.lineEdit.text()
45
        if text and text not in ("None", "Null"):
46
            current = ua.NodeId.from_string(text)
47
        else:
48
            current = ua.NodeId() 
49
        return Node(self.server, current)
50
51
52
53
class GetNodeButton(QPushButton):
54
    """
55
    Create Button which will query a node
56
    """
57
58
    value_changed = pyqtSignal(Node)
59
60
    def __init__(self, parent, currentnode, startnode):
61
        text = "Null"
62
        try:
63
            text = currentnode.get_browse_name().to_string()
64
        except ua.UaError:
65
            pass
66
        QPushButton.__init__(self, text, parent)
67
        self._current_node = currentnode
68
        self.start_node = startnode
69
        self.clicked.connect(self.get_new_node)
70
71
    def get_new_node(self):
72
        node, ok = GetNodeDialog.getNode(self, self.start_node, currentnode=self._current_node)
73
        if ok:
74
            self._current_node = node
75
            self.setText(self._current_node.get_browse_name().to_string())
76
            self.value_changed.emit(self._current_node)
77
78
    def get_node(self):
79
        return self._current_node
80
81
82
class GetNodeDialog(QDialog):
83
    def __init__(self, parent, startnode, currentnode=None):
84
        QDialog.__init__(self, parent)
85
86
        layout = QVBoxLayout(self)
87
        
88
        self.treeview = QTreeView(self)
89
        self.treeview.setEditTriggers(QAbstractItemView.NoEditTriggers)
90
        self.tree = TreeWidget(self.treeview)
91
        self.tree.set_root_node(startnode)
92
        layout.addWidget(self.treeview)
93
94
        self.buttons = QDialogButtonBox(
95
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
96
            Qt.Horizontal, self)
97
        layout.addWidget(self.buttons)
98
        self.resize(800, 600)
99
100
        self.buttons.accepted.connect(self.accept)
101
        self.buttons.rejected.connect(self.reject)
102
        self.treeview.activated.connect(self.accept)
103
104
        if currentnode:
105
            self.tree.expand_to_node(currentnode)
106
107
108
    def get_node(self):
109
        return self.tree.get_current_node()
110
111
    @staticmethod
112
    def getNode(parent, startnode, currentnode=None):
113
        dialog = GetNodeDialog(parent, startnode, currentnode)
114
        result = dialog.exec_()
115
        node = dialog.get_node()
116
        return node, result == QDialog.Accepted
117