def build_head(self): head_separator = SoSeparator() head_separator.setName('head') self.head = SoCone() self.head_transformation = SoTransform() self.head_material = SoMaterial() self.head_material.ambientColor = (.33, .22, .27) self.head_material.diffuseColor = (.78, .57, .11) self.head_material.specularColor = (.99, .94, .81) self.head_material.shininess = .28 head_separator.addChild(self.head_material) head_separator.addChild(self.head_transformation) head_separator.addChild(self.head) return head_separator
def build_tail(self): separator = SoSeparator() separator.setName('tail') self.tail = SoSphere() self.tail.radius = default_tail_radius self.tail_transformation = SoTransform() self.tail_material = SoMaterial() self.tail_material.ambientColor = (.33, .22, .27) self.tail_material.diffuseColor = (.78, .57, .11) self.tail_material.specularColor = (.99, .94, .81) self.tail_material.shininess = .28 separator.addChild(self.tail_material) separator.addChild(self.tail_transformation) separator.addChild(self.tail) return separator
def SimpleSphere(p, radius=.05, mat=None): sep = SoSeparator() sep.setName("Sphere") tr = SoTranslation() tr.setName("Translation") sp = SoSphere() sp.radius = radius tr.translation = p if mat is None: mat = SoMaterial() mat.ambientColor.setValue(.33, .22, .27) mat.diffuseColor.setValue(.78, .57, .11) mat.specularColor.setValue(.99, .94, .81) mat.shininess = .28 sep.addChild(tr) sep.addChild(mat) sep.addChild(sp) return sep
class Chapter(QtCore.QObject): """A Chapter""" pageChanged = QtCore.pyqtSignal(Page, int) def __init__(self, name=""): super(Chapter, self).__init__() self.name = name self.book = None self.root = SoSeparator() self.root.setName("Chapter:root") self.pagesSwitch = SoSwitch() self.pagesSwitch.setName("Chapter:pages") self.root.addChild(self.pagesSwitch) self.__pages = nodeDict() ## ============================ self.setupGui() def setupGui(self): ## self.widget has next, prev buttons, plus a QStackedWidget for holding per page controls self.widget = ChangePageUI() ## the initial state self.widget.previous.hide() self.widget.next.hide() connect(self.widget.next, "clicked(bool)", self.nextPage) connect(self.widget.previous, "clicked(bool)", self.prevPage) ## ============================ self.notesStack = QtGui.QStackedWidget() ## ============================ def setBook(self, book): self.book = book @property def pages(self): """The list of pages""" return self.__pages def createPage(self): """ Creates a new page and appends it to this chapter """ page = Page() self.addPage(page) return page def addPage(self, page): """ Adds 'page' to this chapter. page can be a Page or a SoNode. Searches page for a 'getGui' function, which should return a widget. @param page: Page | SoNode """ ## ============================ ## page can be a Page or SoNode root = getattr(page, "root", page) self.pages[root] = page self.pagesSwitch.addChild(root) ## ============================ guiLayout = QtGui.QVBoxLayout() guiLayout.setMargin(0) guiLayout.setSpacing(0) widget = QtGui.QWidget() widget.setObjectName("PageGui") widget.setLayout(guiLayout) self.widget.pageStack.addWidget(widget) ## ============================ notesLayout = QtGui.QVBoxLayout() notesLayout.setMargin(0) notesLayout.setSpacing(0) widget = QtGui.QWidget() widget.setObjectName("PageNotas") widget.setLayout(notesLayout) self.notesStack.addWidget(widget) ## ============================ ## this sets self.pagesSwitch, self.widget.pageStack, self.notasStack ## only change the page if theres a book already if self.book is not None: self.whichPage = len(self.pagesSwitch) - 1 ## ============================ guiLayout.addWidget(page.getGui()) notesLayout.addWidget(page.getNotes()) ## ============================ if len(self.pagesSwitch) == 2: self.widget.previous.show() self.widget.next.show() def getGui(self): return self.widget def getNotes(self): return self.notesStack def chapterSpecificIn(self): """code to be executed whenever the chapter is displayed this is intended for global changes to the scenegraph that are needed by this chapter """ pass @property def page(self): """the current page""" if self.whichPage < 0: return None return self.pages[self.pagesSwitch[self.whichPage]] def getWhichPage(self): """ Returns the index of the current page """ return self.pagesSwitch.whichChild.getValue() def setWhichPage(self, n): """ Activates the n-th page @param n: """ if len(self.pagesSwitch) > 0: self.pagesSwitch.getChild(n) self.pagesSwitch.whichChild = n self.widget.pageStack.setCurrentIndex(n) self.notesStack.setCurrentIndex(n) self.pageChanged.emit(self.page, n) whichPage = property(getWhichPage, setWhichPage) def changePage(self, direction): self.whichPage = (self.whichPage + direction) % len(self.pagesSwitch) def nextPage(self): self.changePage(1) def prevPage(self): self.changePage(-1)
class Page(QtCore.QObject): """The base class of a container node""" def __init__(self, name=""): QtCore.QObject.__init__(self) self.name = name self.root = SoSeparator() self.root.setName("Page:root") self.children = nodeDict() self.camera_position = None self.camera_point_at = None self.camera_viewAll = True ## ========================= self.animations = [] self.objectsForAnimate = [] self.coordPlanes = {} ## ========================= self.setupGui() self.setupAxis() self.showAxis(False) def setupGui(self): layout = QtGui.QVBoxLayout() self.widget = QtGui.QWidget() self.widget.setLayout(layout) if self.name != "": title = QtGui.QLabel("<center><h1>%s</h1></center>" % self.name) title.setWordWrap(True) layout.addWidget(title) layout.addStretch() ## ============================ notes = QtGui.QLabel(self.__doc__) notes.setWordWrap(True) notes.setTextFormat(QtCore.Qt.RichText) sa = QtGui.QScrollArea() sa.setWidget(notes) sa.setWidgetResizable(True) sa.setHorizontalScrollBarPolicy(1) sa.setFrameStyle(QtGui.QFrame.Plain) notes_layout = QtGui.QVBoxLayout() notes_layout.addWidget(sa) notes_layout.addStretch() self.notesWidget = QtGui.QWidget() self.notesWidget.setLayout(notes_layout) def getGui(self): return self.widget def getNotes(self): return self.notesWidget def addWidget(self, widget): self.widget.layout().addWidget(widget) def addLayout(self, layout): self.widget.layout().addLayout(layout) def addChild(self, node): # if `node` has an attribute root, we assume it is the OpenInventor root object # otherwise node is assumed to be an OI object root = getattr(node, "root", node) self.root.addChild(root) self.children[root] = node if hasattr(node, "getGui"): self.addWidget(node.getGui()) if hasattr(node, "updateAll"): node.updateAll() def addChildren(self, lst): for c in lst: self.addChild(c) def addWidgetChild(self, arg): widget, node = arg self.addWidget(widget) self.addChild(node) def getChildren(self): return self.children.values() def setupPlanes(self, r0=(-1, 1, 5)): self.coordPlanes = { 'xy': BasePlane(plane="xy").setDiffuseColor((1, 1, 0)), 'xz': BasePlane(plane="xz").setDiffuseColor((1, 0, 1)), 'yz': BasePlane(plane="yz").setDiffuseColor((0, 1, 1)) } for p in self.coordPlanes.values(): p.setRange(r0) p.setHeight(r0[0]) self.addChild(p) def showAxis(self, show): """ @param show: bool """ self.axis_x.setVisible(show) self.axis_y.setVisible(show) self.axis_z.setVisible(show) def setupAxis(self): self.axis_x = Arrow(Vec3(-5, 0, 0), Vec3(5, 0, 0)) self.axis_y = Arrow(Vec3(0, -5, 0), Vec3(0, 5, 0)) self.axis_z = Arrow(Vec3(0, 0, -5), Vec3(0, 0, 5)) self.axis_x.setDiffuseColor((1, 0, 0)).setWidthFactor(.2) self.axis_y.setDiffuseColor((0, 1, 0)).setWidthFactor(.2) self.axis_z.setDiffuseColor((0, 0, 1)).setWidthFactor(.2) self.addChildren([self.axis_x, self.axis_y, self.axis_z]) def setupAnimations(self, objects): """ Extracts the 'animation' property of the objects and chains them """ self.objectsForAnimate = objects self.animations = [getattr(ob, 'animation', ob) for ob in objects] Animation.chain(self.animations, pause=1000) Button("inicio", self.animate, parent=self) def animate(self): for ob in self.objectsForAnimate: ob.resetObjectForAnimation() self.animations[0].start()
class Book(QtCore.QObject): """A Book-like object""" chapterChanged = QtCore.pyqtSignal(int) pageChanged = QtCore.pyqtSignal(Page, int) def __init__(self): super(Book, self).__init__() self.root = SoSeparator() self.root.setName("Book:root") self.chapters = SoSwitch() self.chapters.setName("Book:chapters") self.root.addChild(self.chapters) ## this dictionary contains the chapters python objects ## not only the SoSeparator self.chaptersObjects = nodeDict() self.setupGui() def __len__(self): """The number of chapters""" return len(self.chapters) def setupGui(self): ## chapterStack has one widget (of controls) per chapter self.chaptersStack = QtGui.QStackedWidget() self.notesStack = QtGui.QStackedWidget() def createChapter(self): """Creates a new empty Chapter""" chapter = Chapter() self.addChapter(chapter) return chapter def addChapter(self, chapter): """ Appends chapter to this book @param chapter: Chapter """ ## we probably should check that chapter is derived ## from base.Chapter self.chaptersObjects[chapter.pagesSwitch] = chapter self.chapters.addChild(chapter.root) chapter.setBook(self) ## ============================ ## setup the UI self.chaptersStack.addWidget(chapter.getGui()) self.notesStack.addWidget(chapter.getNotes()) self.whichChapter = len(self.chapters) - 1 #======================================================================= chapter.pageChanged.connect(self.relayPageChangedSignal) def relayPageChangedSignal(self, page, n): logger.debug('relayPageChangedSignal: %s', n) self.pageChanged.emit(page, n) @property def chapterSwitch(self): """the switch of the current chapter""" if self.whichChapter < 0: return None return self.chapters[self.whichChapter][0] @property def chapter(self): """returns the current chapter""" if self.whichChapter < 0: return None return self.chaptersObjects[self.chapterSwitch] @property def page(self): """returns the current page in the chapter""" return self.chapter.page if self.whichChapter >= 0 else None def getWhichChapter(self): """returns the selected chapter""" return self.chapters.whichChild.getValue() def setWhichChapter(self, n): """ Sets the current chapter @param n: int """ self.chapters.whichChild = n self.chaptersStack.setCurrentIndex(n) self.notesStack.setCurrentIndex(n) self.chapter.chapterSpecificIn() self.chapterChanged.emit(n) # noinspection PyStatementEffect self.chapter.page and self.pageChanged.emit(self.chapter.page, self.chapter.whichPage) whichChapter = property(getWhichChapter, setWhichChapter)
class Page(QtCore.QObject): """The base class of a container node""" def __init__(self, name=""): QtCore.QObject.__init__(self) self.name = name self.root = SoSeparator() self.root.setName("Page:root") self.children = nodeDict() self.camera_position = None self.camera_point_at = None self.camera_viewAll = True ## ========================= self.animations = [] self.objectsForAnimate = [] self.coordPlanes = {} ## ========================= self.setupGui() self.setupAxis() self.showAxis(False) def setupGui(self): layout = QtGui.QVBoxLayout() self.widget = QtGui.QWidget() self.widget.setLayout(layout) if self.name != "": title = QtGui.QLabel("<center><h1>%s</h1></center>" % self.name) title.setWordWrap(True) layout.addWidget(title) layout.addStretch() ## ============================ notes = QtGui.QLabel(self.__doc__) notes.setWordWrap(True) notes.setTextFormat(QtCore.Qt.RichText) sa = QtGui.QScrollArea() sa.setWidget(notes) sa.setWidgetResizable(True) sa.setHorizontalScrollBarPolicy(1) sa.setFrameStyle(QtGui.QFrame.Plain) notes_layout = QtGui.QVBoxLayout() notes_layout.addWidget(sa) notes_layout.addStretch() self.notesWidget = QtGui.QWidget() self.notesWidget.setLayout(notes_layout) def getGui(self): return self.widget def getNotes(self): return self.notesWidget def addWidget(self, widget): self.widget.layout().addWidget(widget) def addLayout(self, layout): self.widget.layout().addLayout(layout) def addChild(self, node): # if `node` has an attribute root, we assume it is the OpenInventor root object # otherwise node is assumed to be an OI object root = getattr(node, "root", node) self.root.addChild(root) self.children[root] = node if hasattr(node, "getGui"): self.addWidget(node.getGui()) if hasattr(node, "updateAll"): node.updateAll() def addChildren(self, lst): for c in lst: self.addChild(c) def addWidgetChild(self, arg): widget, node = arg self.addWidget(widget) self.addChild(node) def getChildren(self): return self.children.values() def setupPlanes(self, r0=(-1, 1, 5)): self.coordPlanes = { 'xy': BasePlane(plane="xy").setDiffuseColor((1, 1, 0)), 'xz': BasePlane(plane="xz").setDiffuseColor((1, 0, 1)), 'yz': BasePlane(plane="yz").setDiffuseColor((0, 1, 1)) } for p in self.coordPlanes.values(): p.setRange(r0) p.setHeight(r0[0]) self.addChild(p) def showAxis(self, show): """ @param show: bool """ self.axis_x.setVisible(show) self.axis_y.setVisible(show) self.axis_z.setVisible(show) def setupAxis(self): self.axis_x = Arrow(Vec3(-5, 0, 0), Vec3(5, 0, 0)) self.axis_y = Arrow(Vec3(0, -5, 0), Vec3(0, 5, 0)) self.axis_z = Arrow(Vec3(0, 0, -5), Vec3(0, 0, 5)) self.axis_x.setDiffuseColor((1, 0, 0)).setWidthFactor(.2) self.axis_y.setDiffuseColor((0, 1, 0)).setWidthFactor(.2) self.axis_z.setDiffuseColor((0, 0, 1)).setWidthFactor(.2) self.addChildren([self.axis_x, self.axis_y, self.axis_z ]) def setupAnimations(self, objects): """ Extracts the 'animation' property of the objects and chains them """ self.objectsForAnimate = objects self.animations = [ getattr(ob, 'animation', ob) for ob in objects ] Animation.chain(self.animations, pause=1000) Button("inicio", self.animate, parent=self) def animate(self): for ob in self.objectsForAnimate: ob.resetObjectForAnimation() self.animations[0].start()