class WorldTest(TestCase): def setUp(self): ev.events self.world = World() self.world.register_listener(ev) def tearDown(self): self.world.unregister_listener(ev) ev.events del self.world def test_world_objects(self): wo = WorldObject(name='o', data=1) assert wo.name == 'o' assert wo.data == 1 def test_world(self): obj = self.world.add(1, 'a') events = ev.events self.check_events(events, names=['world_object_changed'], values={0: {0: self.world, 1: None, 2: obj}} ) attribute_dict = dict(name='attr1', value=1, interface='IInt', label="Attribute 1") final_dict = dict(name='attr1', value=1, interface='IInt', label="Attribute 1", constraints=None) obj.set_attribute(**attribute_dict) events = ev.events self.check_events(events, names=['world_object_item_changed'], values={0: {0: self.world, 1: obj, 2: "attribute", 3: None, 4: final_dict}} ) assert obj['attr1'] == attribute_dict['value']
class WorldTest(TestCase): def setUp(self): ev.events self.world = World() self.world.register_listener(ev) def tearDown(self): self.world.unregister_listener(ev) ev.events del self.world def test_world_objects(self): wo = WorldObject(name='o', data=1) assert wo.name == 'o' assert wo.data == 1 def test_world(self): obj = self.world.add(1, 'a') events = ev.events self.check_events(events, names=['world_object_changed'], values={0: { 0: self.world, 1: None, 2: obj }}) attribute_dict = dict(name='attr1', value=1, interface='IInt', label="Attribute 1") final_dict = dict(name='attr1', value=1, interface='IInt', label="Attribute 1", constraints=None) obj.set_attribute(**attribute_dict) events = ev.events self.check_events(events, names=['world_object_item_changed'], values={ 0: { 0: self.world, 1: obj, 2: "attribute", 3: None, 4: final_dict } }) assert obj['attr1'] == attribute_dict['value']
class ImageStackViewer(QtGui.QTabWidget, AbstractListener): def __init__(self): QtGui.QTabWidget.__init__(self) AbstractListener.__init__(self) self.setAcceptDrops(True) self._image = {} self._viewer = {} self._create_connections() def initialize(self): self.world = World() self.world.register_listener(self) def notify(self, sender, event=None): signal, data = event if signal == 'world_sync': self.set_world(data) elif signal == 'world_object_removed': world, old = data self.remove_world_object(world, old) elif signal == 'world_object_changed': world, old, new = data self.set_world_object(world, new) elif signal == 'world_object_item_changed': world, obj, item, old, new = data if item == 'attribute': self.update_world_object(world, obj, item, old, new) def set_image(self, name, image): if name in self._viewer: viewer = self._viewer[name] else: viewer = ImageStackViewerWidget() viewer.setAttribute(QtCore.Qt.WA_DeleteOnClose) connect(viewer, viewer.stackChanged, self._on_stack_changed) self._viewer[name] = viewer self._image[viewer] = name self.addTab(viewer, name) viewer.setValue(image) def remove_image(self, name): if name in self._viewer: viewer = self._viewer[name] disconnect(viewer, viewer.stackChanged, self._on_stack_changed) self.removeTab(self.indexOf(viewer)) viewer.close() del self._image[viewer] del self._viewer[name] def set_world_object(self, world, world_object): if not isinstance(world_object.data, SpatialImage): return self.set_image(world_object.name, world_object.data) def remove_world_object(self, world, world_object): if not isinstance(world_object.data, SpatialImage): return self.set_image(world_object.name, world_object.data) def set_world(self, world): # Clear viewer for image in self._viewer.keys(): self.remove_image(image) # Set object in viewer for obj_name, world_object in world.items(): self.set_world_object(world, world_object) def update_world_object(self, world, obj, item, old, new): attr_name = new['name'] if attr_name.endswith('plane_position'): axis = list('xyz').index(attr_name[0]) position = new['value'] if obj.name in self._viewer: viewer = self._viewer[obj.name] viewer.change_axis(axis) viewer.slice_changed(position) def _create_connections(self): self.currentChanged.connect(self._on_current_image_changed) def _on_current_image_changed(self, idx): pass def _on_stack_changed(self, stack): name = self._image[self.sender()] if stack: world_object = self.world[name] cut_plane = '%s_plane_position' % ('xyz'[stack['axis']]) world_object.set_attribute(cut_plane, stack['slice'])
def __init__(self, parent=None): # super(Viewer, self).__init__() AbstractListener.__init__(self) view3D.__init__(self, parent=parent) self.setAccessibleName("3DViewer") self.setMinimumSize(100, 100) self.autofocus = True self._fps = False self.axis = False self.grid = True actionResetZoom = QtGui.QAction(QtGui.QIcon(":/images/resources/resetzoom.png"), "Home", self) self.actionAutoFocus = QtGui.QAction(QtGui.QIcon(":/images/resources/resetzoom.png"), "Auto Focus", self) self.actionAutoFocus.setCheckable(True) self.actionAutoFocus.setChecked(self.autofocus) self.actionAutoFocus.changed.connect(self._on_autofocus_changed) actionZoomOut = QtGui.QAction(QtGui.QIcon(":/images/resources/zoomout.png"), "Zoom Out", self) actionZoomIn = QtGui.QAction(QtGui.QIcon(":/images/resources/zoomin.png"), "Zoom In", self) actionShowAxis = QtGui.QAction(QtGui.QIcon(":/images/resources/axis.png"), "Show Axis", self) actionShowGrid = QtGui.QAction(QtGui.QIcon(":/images/resources/grid.png"), "Show Grid", self) actionRadius = QtGui.QAction(QtGui.QIcon(":/images/resources/growth2.png"), "Focus", self) actionShowFps = QtGui.QAction(QtGui.QIcon(":/images/resources/fps.png"), "Show FPS", self) actionBlack = QtGui.QAction(QtGui.QIcon(""), "Bg Black", self) actionWhite = QtGui.QAction(QtGui.QIcon(""), "Bg White", self) actionShowAxis.setShortcut( QtGui.QApplication.translate("MainWindow", "Ctrl+A", None, QtGui.QApplication.UnicodeUTF8)) actionShowGrid.setShortcut( QtGui.QApplication.translate("MainWindow", "Ctrl+I", None, QtGui.QApplication.UnicodeUTF8)) actionRadius.setShortcut( QtGui.QApplication.translate("MainWindow", "Ctrl+M", None, QtGui.QApplication.UnicodeUTF8)) actionResetZoom.setShortcut( QtGui.QApplication.translate("MainWindow", "Ctrl+0", None, QtGui.QApplication.UnicodeUTF8)) actionZoomOut.setShortcut( QtGui.QApplication.translate("MainWindow", "Ctrl+-", None, QtGui.QApplication.UnicodeUTF8)) actionZoomIn.setShortcut( QtGui.QApplication.translate("MainWindow", "Ctrl++", None, QtGui.QApplication.UnicodeUTF8)) QtCore.QObject.connect(actionResetZoom, QtCore.SIGNAL('triggered(bool)'), self.resetzoom) QtCore.QObject.connect(actionZoomOut, QtCore.SIGNAL('triggered(bool)'), self.zoomout) QtCore.QObject.connect(actionZoomIn, QtCore.SIGNAL('triggered(bool)'), self.zoomin) QtCore.QObject.connect(actionShowAxis, QtCore.SIGNAL('triggered(bool)'), self.show_hide_axis) QtCore.QObject.connect(actionShowGrid, QtCore.SIGNAL('triggered(bool)'), self.show_hide_grid) QtCore.QObject.connect(actionRadius, QtCore.SIGNAL('triggered(bool)'), self.update_radius) QtCore.QObject.connect(actionShowFps, QtCore.SIGNAL('triggered(bool)'), self.show_fps) QtCore.QObject.connect(actionBlack, QtCore.SIGNAL('triggered(bool)'), self.set_bg_black) QtCore.QObject.connect(actionWhite, QtCore.SIGNAL('triggered(bool)'), self.set_bg_white) world = World() world.register_listener(self) self.object_repr = {} self.object_scene = {} self.property = {} self._actions = [["Viewer", "Zoom", actionResetZoom, 0], ["Viewer", "Zoom", actionZoomOut, 0], ["Viewer", "Zoom", actionZoomIn, 0], ["Viewer", "Zoom", self.actionAutoFocus, 0], ["Viewer", "Camera", actionShowAxis, 0], ["Viewer", "Camera", actionShowGrid, 0], ["Viewer", "Camera", actionRadius, 0], ["Viewer", "Camera", actionBlack, 0], ["Viewer", "Camera", actionWhite, 0], #["Viewer", "Informations", actionShowFps, 1] ]