summaryrefslogtreecommitdiff
path: root/silx/gui/hdf5/Hdf5Node.py
blob: 31bb09735b1dda7b35fab10f75106ea0981d2916 (plain)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "23/09/2016"


class Hdf5Node(object):
    """Abstract tree node

    It provides link to the childs and to the parents, and a link to an
    external object.
    """
    def __init__(self, parent=None, populateAll=False):
        """
        Constructor

        :param Hdf5Node parent: Parent of the node, if exists, else None
        :param bool populateAll: If true, populate all the tree node. Else
            everything is lazy loaded.
        """
        self.__child = None
        self.__parent = parent
        if populateAll:
            self.__child = []
            self._populateChild(populateAll=True)

    @property
    def parent(self):
        """Parent of the node, or None if the node is a root

        :rtype: Hdf5Node
        """
        return self.__parent

    def setParent(self, parent):
        """Redefine the parent of the node.

        It does not set the node as the children of the new parent.

        :param Hdf5Node parent: The new parent
        """
        self.__parent = parent

    def appendChild(self, child):
        """Append a child to the node.

        It does not update the parent of the child.

        :param Hdf5Node child: Child to append to the node.
        """
        self.__initChild()
        self.__child.append(child)

    def removeChildAtIndex(self, index):
        """Remove a child at an index of the children list.

        The child is removed and returned.

        :param int index: Index in the child list.
        :rtype: Hdf5Node
        :raises: IndexError if list is empty or index is out of range.
        """
        self.__initChild()
        return self.__child.pop(index)

    def insertChild(self, index, child):
        """
        Insert a child at a specific index of the child list.

        It does not update the parent of the child.

        :param int index: Index in the child list.
        :param Hdf5Node child: Child to insert in the child list.
        """
        self.__initChild()
        self.__child.insert(index, child)

    def indexOfChild(self, child):
        """
        Returns the index of the child in the child list of this node.

        :param Hdf5Node child: Child to find
        :raises: ValueError if the value is not present.
        """
        self.__initChild()
        return self.__child.index(child)

    def hasChildren(self):
        """Returns true if the node contains children.

        :rtype: bool
        """
        return self.childCount() > 0

    def childCount(self):
        """Returns the number of child in this node.

        :rtype: int
        """
        if self.__child is not None:
            return len(self.__child)
        return self._expectedChildCount()

    def child(self, index):
        """Return the child at an expected index.

        :param int index: Index of the child in the child list of the node
        :rtype: Hdf5Node
        """
        self.__initChild()
        return self.__child[index]

    def __initChild(self):
        """Init the child of the node in case the list was lazy loaded."""
        if self.__child is None:
            self.__child = []
            self._populateChild()

    def _expectedChildCount(self):
        """Returns the expected count of children

        :rtype: int
        """
        return 0

    def _populateChild(self, populateAll=False):
        """Recurse through an HDF5 structure to append groups an datasets
        into the tree model.

        Overwrite it to implement the initialisation of child of the node.
        """
        pass

    def dataName(self, role):
        """Data for the name column

        Overwrite it to implement the content of the 'name' column.

        :rtype: qt.QVariant
        """
        return None

    def dataType(self, role):
        """Data for the type column

        Overwrite it to implement the content of the 'type' column.

        :rtype: qt.QVariant
        """
        return None

    def dataShape(self, role):
        """Data for the shape column

        Overwrite it to implement the content of the 'shape' column.

        :rtype: qt.QVariant
        """
        return None

    def dataValue(self, role):
        """Data for the value column

        Overwrite it to implement the content of the 'value' column.

        :rtype: qt.QVariant
        """
        return None

    def dataDescription(self, role):
        """Data for the description column

        Overwrite it to implement the content of the 'description' column.

        :rtype: qt.QVariant
        """
        return None

    def dataNode(self, role):
        """Data for the node column

        Overwrite it to implement the content of the 'node' column.

        :rtype: qt.QVariant
        """
        return None