Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

""" HierarchyWidget modul from pySUMO 

""" 

 

from PySide.QtCore import Qt 

from PySide.QtGui import QApplication, QMainWindow, QTreeWidgetItem, QAbstractItemView 

from PySide.QtGui import QStringListModel 

import sys 

 

from pySUMOQt.Designer.HierarchyWidget import Ui_Form 

from pySUMOQt.Widget.Widget import RWWidget 

 

 

class OntologyHierarchyNode(QTreeWidgetItem): 

 

    def __init__(self, data=None): 

        super(OntologyHierarchyNode, self).__init__() 

        if data is not None : 

            self.setText(0, data) 

        self.dataObj = data 

 

    def __str__(self, *args, **kwargs): 

        return self.dataObj 

 

def buildRootNode(relations, treeview): 

    """ Build the Qtreeitem from the abstract graph and it's children.""" 

    items = [] 

    createdNodes = [] 

    for i in relations.keys() : 

        node = None 

        foundNode = False 

        for k in createdNodes : 

            if k.dataObj == i : 

                node = k 

                foundNode = True 

                break 

        if node is None : 

            node = OntologyHierarchyNode(data=i) 

            createdNodes.append(node) 

        for j in relations[i] : 

            subNode = None 

            for k in createdNodes : 

                if k.dataObj == j : 

                    subNode = k 

                    break 

            if subNode is None : 

                subNode = OntologyHierarchyNode(data=j) 

                createdNodes.append(subNode) 

            node.addChild(subNode) 

        if not foundNode : 

            items.append(node) 

    treeview.addTopLevelItems(items) 

    return createdNodes 

 

class HierarchyWidget(RWWidget, Ui_Form): 

 

    """ The hierarchy widget displays the ontology in a tree form conformly to 

    the SUMO hierarchy model. The user can edit the ontology from this widget. 

    It can show and hide nodes in it's display. 

 

    Methods: 

 

    - hide: hides the node's content that the user selected in the widget view. 

    - show: shows the node's content that the user selected in the widget view. 

 

    """ 

 

    def __init__(self, mainwindow): 

        """ Initializes the hierarchy widget. """ 

        super(HierarchyWidget, self).__init__(mainwindow) 

        self.setupUi(self.mw) 

        self.relationSelector.setCurrentIndex(-1) 

        self.relationSelector.currentIndexChanged.connect(self.refresh) 

        self.rootSelector.setCurrentIndex(-1) 

        self.rootSelector.currentIndexChanged[int].connect(self._relationChanged_) 

        self.createdNodes = None 

 

    def refresh(self): 

        RWWidget.refresh(self) 

        idx = self.relationSelector.currentIndex() 

        self.treeWidget.clear() 

        variant = None 

        if idx != -1 : 

            variant = self.relationSelector.currentText() 

        if variant is None : 

            return 

        abstractGraph = self.IA.get_graph([(0, variant)]) 

        if abstractGraph is None or len(abstractGraph.relations) == 0: 

            return 

        createdNodes = buildRootNode(abstractGraph.relations, treeview=self.treeWidget) 

        self.rootSelector.clear() 

        model = QStringListModel() 

        nodeList = list() 

        for i in createdNodes : 

            nodeList.append(i.text(0)) 

        nodeList.sort() 

        model.setStringList(nodeList) 

        self.createdNodes = createdNodes 

        self.rootSelector.setModel(model) 

        self.rootSelector.setCurrentIndex(-1) 

 

    def findNodeByText(self, val): 

        if not self.createdNodes is None : 

            for i in self.createdNodes : 

                if i.text(0) == val : 

                    return i 

        return None 

 

    def _relationChanged_(self, val): 

        self.treeWidget.clearSelection() 

        if val == -1 : 

            self.treeWidget.collapseAll() 

            return 

        else : 

            textVal = self.rootSelector.itemText(val) 

            node = self.findNodeByText(textVal) 

            if node is not None : 

                idx = self.treeWidget.indexFromItem(node, 0) 

                node = self.treeWidget.itemFromIndex(idx) 

                self.treeWidget.setItemExpanded(node, True) 

                self.treeWidget.setItemSelected(node, True) 

                self.treeWidget.scrollToItem(node, QAbstractItemView.PositionAtCenter) 

                self.rootSelector.clearFocus() 

                self.treeWidget.setFocus() 

 

    def _expandAll_(self): 

        self.treeWidget.expandAll() 

 

    def _collapseAll_(self): 

        self.treeWidget.collapseAll() 

 

    def hide(self, entry): 

        """ Hides the part the user selected of the Ontology. This function is called as a slot. 

 

        Args: 

 

        - entry: The user-selected entry. 

 

        """ 

        pass 

 

    def show(self, entry): 

        """ Shows the part the user selected of the ontolgy. This function is called as a slot. 

 

        Args: 

 

        - entry: The user-selected entry. 

 

        """ 

        pass