/usr/share/pyshared/PyMca/Object3D/SceneTree.py is in pymca 4.5.0-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | import sys
import Object3DQt as qt
from Object3DIcons import IconDict
import ObjectTree
import weakref
DEBUG = 0
class ObjectTreeWidget(qt.QTreeWidget):
def __init__(self, parent=None, tree=None, labels=None):
qt.QTreeWidget.__init__(self, parent)
if labels is None:
labels = ['Name', 'Type'] #, 'Vertices']
if tree is None:
self.tree = ObjectTree.ObjectTree('__Scene__', 'Scene')
else:
self.tree = tree
ncols = len(labels)
self.setColumnCount(ncols)
self.setHeaderLabels(labels)
def focusInEvent(self, event):
event.accept()
def addObject(self, item, name=None, parent=None, update=True):
if name is None:
name = item.name()
if parent is None:
self.tree.addChild(item, name)
else:
self[parent].addChild(item, name)
if update:
self.updateView()
def removeObject(self, name):
#An object has to correspond to an entry in the tree
treeObject = self.tree.find(name)
if treeObject is None:
# do nothing?
return
treeObject.erase()
self.updateView()
def updateView(self):
self.clear()
self.showInView(self.tree)
def showInView(self, tree, parent=None):
"""
Represent a tree in the QTreeWidget
"""
if parent is None:
widgetItem = Object3DTreeWidgetItem(0, tree)
self.addTopLevelItem(widgetItem)
else:
#find the parent item
itemList = self.findItems(parent.name(),
qt.Qt.MatchExactly|qt.Qt.MatchCaseSensitive|qt.Qt.MatchRecursive,
0)
if len(itemList):
widgetItemParent = itemList[0]
widgetItem = Object3DTreeWidgetItem(1, tree)
widgetItemParent.addChild(widgetItem)
else:
return
ob = tree.root[0]
if hasattr(ob,'selected'):
if ob.selected():
widgetItem.setSelected(True)
self.scrollToItem(widgetItem,
qt.QAbstractItemView.EnsureVisible)
for subTree in tree.childList():
self.showInView(subTree, tree.parent(subTree.name()))
def setSelected(self, name):
itemList = self.findItems(name,
qt.Qt.MatchExactly|qt.Qt.MatchCaseSensitive|qt.Qt.MatchRecursive,
0)
if len(itemList):
itemList[0].setSelected(True)
class Object3DTreeWidgetItem(qt.QTreeWidgetItem):
def __init__(self, wtype, object3D):
if type(wtype) != type(1):
raise TypeError, "First argument must be an integer"
#if (wtype != 0) and (wtype < qt.QTreeWidgetItem.UserType):
# raise TypeError, "First argument must be 0 or an integer >= 1000"
actualType = wtype
qt.QTreeWidgetItem.__init__(self, wtype + qt.QTreeWidgetItem.UserType)
self.__object3D = object3D
self.setText(0, object3D.name())
if wtype == 0:
text = "Scene"
else:
text = "3D Object"
self.setText(1, text)
class Object3DObjectTree(qt.QGroupBox):
def __init__(self, parent = None, tree=None):
qt.QGroupBox.__init__(self, parent)
self.setTitle('Objects Tree')
self.mainLayout = qt.QVBoxLayout(self)
self.mainLayout.setMargin(0)
self.mainLayout.setMargin(0)
self.treeWidget = ObjectTreeWidget(self, tree=tree)
self.tree = weakref.proxy(self.treeWidget.tree)
self.actions = ObjectActions(self)
self.mainLayout.addWidget(self.treeWidget)
self.mainLayout.addWidget(self.actions)
self.addObject = self.treeWidget.addObject
self.__current = 'Scene'
self.__previous= None
self.__cutObject = None
self.__replacing = False
self.connect(self.actions.cutButton,
qt.SIGNAL('clicked()'),
self.cutObject)
self.connect(self.actions.pasteButton,
qt.SIGNAL('clicked()'),
self.pasteObject)
self.connect(self.actions.deleteButton,
qt.SIGNAL('clicked()'),
self.deleteObject)
self.connect(self.actions.replaceButton,
qt.SIGNAL('clicked()'),
self.replaceWithObject)
self.connect(self.treeWidget,
qt.SIGNAL('currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *)'),
self.itemChanged)
def updateView(self, expand=False):
self.treeWidget.updateView()
if expand:
if qt.qVersion() >= '4.2.0':
self.treeWidget.expandAll()
objectList = self.getSelectedObjectList()
if len(objectList):
self.__current = objectList[0]
def getSelectedObjectList(self):
selected = []
for item in self.tree.childList():
ob = item.root[0]
if hasattr(ob, 'selected'):
if ob.selected():
selected.append(item.name())
return selected
def setSelectedObject(self, name=None):
if name is None:
name = self.__current
else:
self.__current = name
#reset all the children
for item in self.tree.childList():
ob = item.root[0]
if hasattr(ob, 'selected'):
ob.setSelected(False)
#but do not forget the scene itself
if hasattr(self.tree.root[0], "setSelected"):
self.tree.root[0].setSelected(False)
#now select the proper one
if self.tree.name() == name:
self.tree.root[0].setSelected(True)
else:
child = self.tree.find(name)
if child is not None:
ob = child.root[0]
if hasattr(ob, 'selected'):
ob.setSelected(True)
self.treeWidget.setSelected(child.name())
def cutObject(self):
if self.__current == 'Scene':
self.__cutObject = None
qt.QMessageBox.critical(self, "Error on cut",
"You cannot cut the Scene itself.",
qt.QMessageBox.Ok | qt.QMessageBox.Default,
qt.QMessageBox.NoButton)
elif self.__current is None:
qt.QMessageBox.critical(self, "Error on cut",
"Please select an object.",
qt.QMessageBox.Ok | qt.QMessageBox.Default,
qt.QMessageBox.NoButton)
else:
self.__cutObject = self.__current
def pasteObject(self):
if self.__cutObject is None:
qt.QMessageBox.critical(self, "Error on paste",
"Please cut an object first.",
qt.QMessageBox.Ok | qt.QMessageBox.Default,
qt.QMessageBox.NoButton)
return
if self.__cutObject == self.__current:
#do nothing
if DEBUG:
print "Doing nothing"
self.__cutObject = None
self.treeWidget.resizeColumnToContents(0)
return
child = self.tree.find(self.__cutObject)
self.tree.delChild(self.__cutObject)
destination = self.tree.find(self.__current)
destination.addChildTree(child)
if DEBUG:
print "TREE after addition = ", self.tree
self.updateView()
if 1:
#this works
itemList = self.treeWidget.findItems(self.__cutObject,
qt.Qt.MatchExactly|qt.Qt.MatchCaseSensitive|qt.Qt.MatchRecursive,
0)
if len(itemList):
self.treeWidget.scrollToItem(itemList[0],
qt.QAbstractItemView.EnsureVisible)
else:
if DEBUG:
print "Is this a problem?"
else:
#this too
name = self.__cutObject
while name != 'Scene':
name = self.tree.parent(name).name()
itemList = self.treeWidget.findItems(name,
qt.Qt.MatchExactly|qt.Qt.MatchCaseSensitive|qt.Qt.MatchRecursive,
0)
if len(itemList):
self.treeWidget.expandItem(itemList[0])
else:
if DEBUG:
print "Is this a problem?"
break
self.treeWidget.resizeColumnToContents(0)
self.__cutObject = None
self.emitSignal('treeChanged')
def deleteObject(self):
if self.__current == 'Scene':
qt.QMessageBox.critical(self, "Error on deletion",
"You cannot delete the Scene itself.",
qt.QMessageBox.Ok | qt.QMessageBox.Default,
qt.QMessageBox.NoButton)
self.tree.delChild(self.__current)
self.__previous = str(self.__current)
self.setSelectedObject(self.tree.name())
self.emitSignal('objectDeleted')
self.updateView()
def replaceWithObject(self):
if self.__current in [None, 'None']:
return
self.__replacing = True
if self.__current == 'Scene':
itemList = self.tree.childList()
for item in itemList:
self.tree.delChild(item.name())
self.updateView()
self.__cutObject = self.__current * 1
else:
self.__cutObject = self.__current * 1
self.__current = 'Scene'
child = self.tree.find(self.__cutObject)
self.tree.delChild(self.__cutObject)
itemList = self.tree.childList()
for item in itemList:
self.tree.delChild(item.name())
self.tree.addChildTree(child)
self.updateView()
itemList = self.treeWidget.findItems(self.__cutObject,
qt.Qt.MatchExactly|qt.Qt.MatchCaseSensitive|qt.Qt.MatchRecursive,
0)
if len(itemList):
self.treeWidget.scrollToItem(itemList[0],
qt.QAbstractItemView.EnsureVisible)
self.treeWidget.resizeColumnToContents(0)
self.__current = self.__cutObject * 1
self.__cutObject = None
self.__replacing = False
self.emitSignal('objectReplaced')
def itemChanged(self, current, previous):
if current is None:
#This happens when updating because I clear the tree
return
#This was giving a lot of problems:
self.__current = 'Scene'
else:
self.__current = current.text(0)
if previous is None:
self.__previous = None
else:
self.__previous = previous.text(0)
if DEBUG:
print "current = ", self.__current
print "previous = ", self.__previous
if self.__current != self.__previous:
self.setSelectedObject(str(self.__current))
self.emitSignal('objectSelected')
def emitSignal(self, event):
if self.__replacing:
if DEBUG:
print "EVENT = ", event, "NOT SENT"
ddict = {}
ddict['event'] = event
ddict['current'] = str(self.__current)
ddict['previous'] = str(self.__previous)
qt.QObject.emit(self,
qt.SIGNAL('ObjectTreeSignal'),
ddict)
class ObjectActions(qt.QGroupBox):
def __init__(self, parent = None):
qt.QGroupBox.__init__(self, parent)
self.setTitle('Object Actions')
self.mainLayout = qt.QVBoxLayout(self)
self.mainLayout.setSpacing(0)
self.mainLayout.setMargin(0)
self.cutButtonIcon = qt.QIcon(qt.QPixmap(IconDict['cut']))
self.cutButton = qt.QPushButton(self)
self.cutButton.setIcon(self.cutButtonIcon)
self.cutButton.setText('Cut')
self.pasteButtonIcon = qt.QIcon(qt.QPixmap(IconDict['paste']))
self.pasteButton = qt.QPushButton(self)
self.pasteButton.setIcon(self.pasteButtonIcon)
self.pasteButton.setText('Paste')
self.deleteButtonIcon = qt.QIcon(qt.QPixmap(IconDict['delete']))
self.deleteButton = qt.QPushButton(self)
self.deleteButton.setIcon(self.deleteButtonIcon)
self.deleteButton.setText('Delete')
self.replaceButton = qt.QPushButton(self)
self.replaceButton.setText('Replace')
self.mainLayout.addWidget(self.cutButton)
self.mainLayout.addWidget(self.pasteButton)
self.mainLayout.addWidget(self.deleteButton)
self.mainLayout.addWidget(self.replaceButton)
if __name__ == "__main__":
import Object3DBase
app = qt.QApplication([])
qt.QObject.connect(app, qt.SIGNAL("lastWindowClosed()"),
app, qt.SLOT("quit()"))
o0 = Object3DBase.Object3D("DummyObject0")
o1 = Object3DBase.Object3D("DummyObject1")
o01 = Object3DBase.Object3D("DummyObject01")
w = Object3DObjectTree()
w.addObject(o0, update=False)
w.addObject(o1, update=False)
w.addObject(o01, update=True)
tree = w.tree.find("DummyObject0")
w.tree.delChild("DummyObject01")
tree.addChild(o01)
w.updateView()
w.show()
app.exec_()
|