Completed
Push — master ( e3e1f6...34077a )
by Olivier
01:07
created

GetNodeTextButton.__init__()   A

Complexity

Conditions 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
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
        print(self.lineEdit.text())
45
        print(Node(self.server, ua.NodeId.from_string(self.lineEdit.text())))
46
        return Node(self.server, ua.NodeId.from_string(self.lineEdit.text()))
47
48
49
50
class GetNodeButton(QPushButton):
51
    """
52
    Create Button which will query a node
53
    """
54
55
    value_changed = pyqtSignal(Node)
56
57
    def __init__(self, parent, currentnode, startnode):
58
        if currentnode.nodeid.is_null():
59
            text = "Null"
60
        else:
61
            text = currentnode.get_browse_name().to_string()
62
        QPushButton.__init__(self, text, parent)
63
        self._current_node = currentnode
64
        self.start_node = startnode
65
        self.clicked.connect(self.get_new_node)
66
67
    def get_new_node(self):
68
        node, ok = GetNodeDialog.getNode(self, self.start_node, currentnode=self._current_node)
69
        if ok:
70
            self._current_node = node
71
            self.setText(self._current_node.get_browse_name().to_string())
72
            self.value_changed.emit(self._current_node)
73
74
    def get_node(self):
75
        return self._current_node
76
77
78
class GetNodeDialog(QDialog):
79
    def __init__(self, parent, startnode, currentnode=None):
80
        QDialog.__init__(self, parent)
81
82
        layout = QVBoxLayout(self)
83
        
84
        self.treeview = QTreeView(self)
85
        self.treeview.setEditTriggers(QAbstractItemView.NoEditTriggers)
86
        self.tree = TreeWidget(self.treeview)
87
        self.tree.set_root_node(startnode)
88
        layout.addWidget(self.treeview)
89
90
        self.buttons = QDialogButtonBox(
91
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
92
            Qt.Horizontal, self)
93
        layout.addWidget(self.buttons)
94
        self.resize(800, 600)
95
96
        self.buttons.accepted.connect(self.accept)
97
        self.buttons.rejected.connect(self.reject)
98
        self.treeview.activated.connect(self.accept)
99
100
        if currentnode:
101
            self.tree.expand_to_node(currentnode)
102
103
104
    def get_node(self):
105
        return self.tree.get_current_node()
106
107
    @staticmethod
108
    def getNode(parent, startnode, currentnode=None):
109
        dialog = GetNodeDialog(parent, startnode, currentnode)
110
        result = dialog.exec_()
111
        node = dialog.get_node()
112
        return node, result == QDialog.Accepted
113