treemodel.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18  '''
19 from PyQt5.QtCore import Qt, QModelIndex, QAbstractItemModel
20 from PyQt5.QtGui import QColor
21 
22 from visualstates.gui.tree.treenode import TreeNode
23 
24 
25 class TreeModel(QAbstractItemModel):
26  def __init__(self, parent=None):
27  super(QAbstractItemModel, self).__init__(parent)
28  self.rootNode = TreeNode("ID", "Name", "white")
29 
30  def columnCount(self, parent):
31  if parent.isValid():
32  return parent.internalPointer().columnCount()
33  else:
34  return self.rootNode.columnCount()
35 
36  def data(self, index, role):
37  if not index.isValid():
38  return None
39 
40  if role == Qt.DisplayRole:
41  item = index.internalPointer()
42  return item.data(index.column())
43 
44  elif role == Qt.BackgroundColorRole:
45  item = index.internalPointer()
46  return item.background()
47 
48  return None
49 
50  def rowId(self, index):
51  if not index.isValid():
52  return 0
53 
54  if not index.parent().isValid():
55  item = index.internalPointer()
56  return item.id
57  else:
58  return 0
59 
60  def flags(self, index):
61  if not index.isValid():
62  return Qt.NoItemFlags
63 
64  return Qt.ItemIsEnabled | Qt.ItemIsSelectable
65 
66  def headerData(self, section, orientation, role):
67  if orientation == Qt.Horizontal and role == Qt.DisplayRole:
68  return self.rootNode.data(section)
69 
70  return None
71 
72  def index(self, row, column, parent):
73  if not self.hasIndex(row, column, parent):
74  return QModelIndex()
75 
76  if not parent.isValid():
77  parentItem = self.rootNode
78  else:
79  parentItem = parent.internalPointer()
80 
81  childItem = parentItem.child(row)
82  if childItem:
83  return self.createIndex(row, column, childItem)
84  else:
85  return QModelIndex()
86 
87  def indexOf(self, child):
88  return self.createIndex(child.row(), 0, child)
89 
90  def parent(self, index):
91  if not index.isValid():
92  return QModelIndex()
93 
94  childItem = index.internalPointer()
95  parentItem = childItem.parent()
96 
97  if parentItem == self.rootNode:
98  return QModelIndex()
99 
100  return self.createIndex(parentItem.row(), 0, parentItem)
101 
102  def rowCount(self, parent):
103  if parent.column() > 0:
104  return 0
105 
106  if not parent.isValid():
107  parentItem = self.rootNode
108  else:
109  parentItem = parent.internalPointer()
110 
111  return parentItem.childCount()
112 
113  def insertState(self, state, color, parent=None):
114  self.insertStateData(state.stateData, color, parent)
115 
116  def insertStateData(self, stateData, color, parent=None):
117  if parent is None:
118  parent = self.rootNode
119  newNode = TreeNode(stateData.id, stateData.name, color, parent)
120  parent.appendChild(newNode)
121  # TODO: this redraws whole tree, just update according to the insertion
122  self.layoutChanged.emit()
123 
124  def removeState(self, state, parent=None):
125  if parent is None:
126  parent = self.rootNode
127  childToBeRemoved = None
128  for s in parent.getChildren():
129  if s.id == state.id:
130  childToBeRemoved = s
131  break
132 
133  if childToBeRemoved is not None:
134  # print('remove child.id:' + str(childToBeRemoved.id))
135  parent.removeChild(childToBeRemoved)
136  self.layoutChanged.emit()
137 
138  def getChildren(self, node):
139  mychildren = []
140  if node != self.rootNode:
141  mychildren.append(node)
142 
143  for child in node.childItems:
144  mychildren = mychildren + self.getChildren(child)
145 
146  return mychildren
147 
148  def getByDataId(self, id):
149  if id == 0:
150  return self.rootNode
151  for item in self.getChildren(self.rootNode):
152  if item.id == id:
153  return item
154  return None
155 
156  def removeAll(self):
157  self.rootNode.removeChildren()
158  self.layoutChanged.emit()
159 
160  def loadFromRoot(self, rootState, parentState=None):
161  for child in rootState.getChildren():
162  if parentState is not None:
163  self.insertStateData(child, QColor(Qt.white), self.getByDataId(parentState.id))
164  else:
165  self.insertStateData(child, QColor(Qt.white), self.getByDataId(rootState.id))
166  self.loadFromRoot(child)
167 
168  def setAllBackgroundByParentId(self, color, parentId):
169  parent = None
170  if parentId is None:
171  parent = self.rootNode
172  else:
173  parent = self.getByDataId(parentId)
174 
175  for node in parent.getChildren():
176  node.setColor(color)
177  self.setAllBackgroundByParentId(color, node.id)
178 
179  def setBackgroundById(self, id, color):
180  # print('set bg color:' + str(id))
181  data = self.getByDataId(id)
182  data.setColor(color)
183  self.layoutChanged.emit()
def headerData(self, section, orientation, role)
Definition: treemodel.py:66
def insertStateData(self, stateData, color, parent=None)
Definition: treemodel.py:116
def index(self, row, column, parent)
Definition: treemodel.py:72
def insertState(self, state, color, parent=None)
Definition: treemodel.py:113
def removeState(self, state, parent=None)
Definition: treemodel.py:124
def loadFromRoot(self, rootState, parentState=None)
Definition: treemodel.py:160
def setAllBackgroundByParentId(self, color, parentId)
Definition: treemodel.py:168


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20