class GPendulum(Graphics.Items.ItemGroupBase): def _setup(self): self.rod = QGraphicsLineItem(QLineF(0, 0, 0, 100)) p = QPen(QColor(100, 100, 100)) p.setWidth(5) self.rod.setPen(p) self.rod.setToolTip('This is the rod of the pendulum') self.ball = QGraphicsEllipseItem(QRectF(-20, 80, 40, 40)) b = QBrush(Qt.SolidPattern) b.setColor(QColor(0, 255, 0)) self.ball.setBrush(b) self.ball.setToolTip('This is the ball of the pendulum where the mass is concentrated') self.addToGroup(self.rod) self.addToGroup(self.ball) self.setFlags(QGraphicsItem.ItemIsSelectable) def setProperties(self, q): self.properties = q def contextMenuEvent(self, e): e.accept() m = QMenu() p = m.addAction("Properties") a = m.exec_(e.screenPos()) if a == p: dlg = SimTools.RichTypes.Qt4Widgets.SimpleRichTypesDialog(mainWin, 'Pendulum properties', text='Change physical properties', scrolling=False) dlg.addRichTypes(self.properties) dlg.exec_()
def iLabel(node, *args, **kargs): #code for making specialized faces for intermediates mostly cribbed from the ete2 website example (though not interactive): # http://pythonhosted.org/ete2/tutorial/tutorial_drawing.html#creating-your-custom-interactive-item-faces my_label = node.name ellipse = QGraphicsEllipseItem(0,0,fontSize*2,fontSize*2) #I think the first two are coords of center; second pair is major/minor axis ellipse.setPen(QPen(QColor( 'black' ))) ellipse.setBrush(QBrush(QColor( 'white' ))) text = QGraphicsSimpleTextItem(my_label) text.setParentItem(ellipse) text.setBrush(QBrush(QColor("black"))) font = QFont("Arial",fontSize*.9,weight=80) font.setLetterSpacing(1, 2) #add 2 pixels between letters for legibility text.setFont(font) #Center text according to masterItem size tw = text.boundingRect().width() th = text.boundingRect().height() center = ellipse.boundingRect().center() text.setPos(center.x()+1-tw/2, center.y()-th/2) #since the last letter has an extra 2 pixels after it from the spacing command, adjust center to compensate return ellipse
def bgc_name_face(node, *args, **kargs): """ This is the item generator. It must receive a node object, and returns a Qt4 graphics item that can be used as a node face. """ # Receive an arbitrary number of arguments, in this case width and # Height of the faces and the information about the BGC width = args[0] height = args[1] # Add the popup interactive_face = InteractiveItem("Class : {}\nRelated MIBiG : {}\nCluster family : {}".format(args[2], args[4], args[3]), 0, 0, width, height) # Keep a link within the item to access node info interactive_face.node = node # Remove border around the masterItem interactive_face.setPen(QPen(QtCore.Qt.NoPen)) # Add ellipse around text ellipse = QGraphicsEllipseItem(interactive_face.rect()) ellipse.setParentItem(interactive_face) # Change ellipse color ellipse.setBrush(QBrush(QColor(args[6]))) # Add node name within the ellipse text = QGraphicsTextItem(args[5]) text.setTextWidth(50) text.setParentItem(ellipse) # Center text according to masterItem size text_width = text.boundingRect().width() text_height = text.boundingRect().height() center = interactive_face.boundingRect().center() text.setPos(center.x()-text_width/2, center.y()-text_height/2) return interactive_face
def point(scene, x, y, color='black', fill='black', diam=10, toolTip=None): item = QGraphicsEllipseItem(x-diam/2, y-diam/2, diam, diam) brush = QBrush(QColor(color), style=Qt.SolidPattern) item.setBrush(brush) if toolTip: item.setToolTip(toolTip) scene.addItem(item)
def iLabel(node, *args, **kargs): #code for making specialized faces for intermediates mostly cribbed from the ete2 website example (though not interactive): # http://pythonhosted.org/ete2/tutorial/tutorial_drawing.html#creating-your-custom-interactive-item-faces my_label = node.name ellipse = QGraphicsEllipseItem( 0, 0, fontSize * 2, fontSize * 2 ) #I think the first two are coords of center; second pair is major/minor axis ellipse.setPen(QPen(QColor('black'))) ellipse.setBrush(QBrush(QColor('white'))) text = QGraphicsSimpleTextItem(my_label) text.setParentItem(ellipse) text.setBrush(QBrush(QColor("black"))) font = QFont("Arial", fontSize * .9, weight=80) font.setLetterSpacing(1, 2) #add 2 pixels between letters for legibility text.setFont(font) #Center text according to masterItem size tw = text.boundingRect().width() th = text.boundingRect().height() center = ellipse.boundingRect().center() text.setPos( center.x() + 1 - tw / 2, center.y() - th / 2 ) #since the last letter has an extra 2 pixels after it from the spacing command, adjust center to compensate return ellipse
def _finish_select_area(self): self.unregister_handler(self.home_map_scene, QEvent.GraphicsSceneMouseRelease) self.unregister_handler(self.home_map_scene, QEvent.KeyRelease) item = None for id,it in self.current_op_stat['items'].items() : print "%d,%s" % (id,HMItem.repr(it)) p = it.polygon().first() item = None self.current_op = '' self.current_op_stat = None el_it = QGraphicsEllipseItem(p.x()-2,p.y()-2,4,4) HMItem.module(el_it,self.__class__.__name__) HMItem.type(el_it,HMItem.ARTIFACT) HMItem.level(el_it, -1) el_it.setPen(QPen(Qt.blue, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) el_it.setBrush(QBrush(Qt.blue)) el_it.setZValue(1) self.home_map_scene.addItem(el_it)
def constructPieChart(self, distrib, numbs): pie = QGraphicsItemGroup() totArc = 0 for j, num, dist in reversed(zip(range(len(numbs)), numbs, distrib)): arc = QGraphicsEllipseItem(-self.pieWidth/2, -self.pieWidth/2, self.pieWidth, self.pieHeight) arc.setPen(QPen(QColor(0, 0, 0), 1, Qt.SolidLine)) arc.setBrush(QBrush(self.discPalette[j])) arc.setToolTip("%s: %.3f" % (self.attributes[self.selectedAttributes[j]][0], num) + (" (%2.1f%%)" % (dist/self.barHeight*100) if self.normalize else "")) arc.setStartAngle(totArc) arc.setSpanAngle(dist * 2880 / self.barHeight) pie.addToGroup(arc) totArc += dist * 2880 / self.barHeight return pie
class SingleJoystickView(QGraphicsView): def __init__(self, *args): QGraphicsView.__init__(self, *args) self.outerD = 125 self.innerD = 25 self.innerRange = 50 self.inputRange = 256 self.thresh = 3 self.worker = JoystickThread() self.worker.valueUpdated.connect(self.moveJoystick) self.worker.start() self.move(30, 100) self.setContentsMargins(0, 0, 0, 0) self.setMaximumHeight(140) self.setMaximumWidth(140) self.adjustSize() self.scene = QGraphicsScene(self) self.outerCircle = QGraphicsEllipseItem(0, 0, self.outerD, self.outerD) self.outerCircle.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.outerCircle.setBrush(Qt.gray) self.innerCircle = QGraphicsEllipseItem(self.outerD / 2 - self.innerD / 2, self.outerD / 2 - self.innerD / 2, self.innerD, self.innerD) self.innerCircle.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle.setBrush(Qt.lightGray) self.scene.addItem(self.outerCircle) self.scene.addItem(self.innerCircle) self.setScene(self.scene) self.setStyleSheet('background-color:transparent;color:red') self.currentX = 0 self.currentY = 0 def moveJoystick(self, x, y): x2 = x * self.innerRange / self.inputRange - self.innerRange / 2 y2 = y * self.innerRange / self.inputRange - self.innerRange / 2 if -self.thresh <= x2 <= self.thresh: x2 = 0 if -self.thresh <= y2 <= self.thresh: y2 = 0 self.tl = QTimeLine(10) self.tl.setFrameRange(0, 10) self.a = QGraphicsItemAnimation() self.a.setItem(self.innerCircle) self.a.setTimeLine(self.tl) self.a.setPosAt(0, QPointF(self.currentX, self.currentY)) self.a.setTranslationAt(1, x2, y2) self.currentX = x2 self.currentY = y2 self.tl.start() print 'x:%d y:%d' % (x2, y2)
def _update_add_area(self,event): p = event.scenePos() self.current_op_stat['points'].append(p) el_it = QGraphicsEllipseItem(p.x()-2,p.y()-2,4,4) HMItem.module(el_it,self.__class__.__name__) HMItem.type(el_it,HMItem.OUTLINE_POINT) HMItem.level(el_it, -1) el_it.setPen(QPen(Qt.black, 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) el_it.setBrush(QBrush(Qt.Dense4Pattern)) el_it.setZValue(1) self.home_map_scene.addItem(el_it)
class CircleCurve(OWCurve): """ Displays a circle on the plot :param pen: The pen used to draw the outline of the circle :type pen: QPen :param brush: The brush used to paint the inside of the circle :type brush: QBrush :param xCenter: The x coordinate of the circle's center :type xCenter: float :param yCenter: The y coordinate of the circle's center :type yCenter: float :param radius: The circle's radius :type radius: float """ def __init__(self, pen=QPen(Qt.black), brush=QBrush(Qt.NoBrush), xCenter=0.0, yCenter=0.0, radius=1.0): OWCurve.__init__(self) self._item = QGraphicsEllipseItem(self) self.center = xCenter, yCenter self.radius = radius self._rect = QRectF(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius) self.set_pen(pen) self.set_brush(brush) def update_properties(self): self._item.setRect(self.graph_transform().mapRect(self.data_rect())) self._item.setPen(self.pen()) self._item.setBrush(self.brush()) def data_rect(self): x, y = self.center r = self.radius return QRectF(x - r, y - r, 2 * r, 2 * r)
def ugly_name_face(node, *args, **kargs): """ This is my item generator. It must receive a node object, and returns a Qt4 graphics item that can be used as a node face. """ # receive an arbitrary number of arguments, in this case width and # height of the faces width = args[0][0] height = args[0][1] ## Creates a main master Item that will contain all other elements ## Items can be standard QGraphicsItem # masterItem = QGraphicsRectItem(0, 0, width, height) # Or your custom Items, in which you can re-implement interactive # functions, etc. Check QGraphicsItem doc for details. masterItem = InteractiveItem(0, 0, width, height) # Keep a link within the item to access node info masterItem.node = node # I dont want a border around the masterItem masterItem.setPen(QPen(QtCore.Qt.NoPen)) # Add ellipse around text ellipse = QGraphicsEllipseItem(masterItem.rect()) ellipse.setParentItem(masterItem) # Change ellipse color ellipse.setBrush(QBrush(QColor( random_color()))) # Add node name within the ellipse text = QGraphicsSimpleTextItem(node.name) text.setParentItem(ellipse) text.setPen(QPen(QPen(QColor("white")))) # Center text according to masterItem size tw = text.boundingRect().width() th = text.boundingRect().height() center = masterItem.boundingRect().center() text.setPos(center.x()-tw/2, center.y()-th/2) return masterItem
def ugly_name_face(node, *args, **kargs): """ This is my item generator. It must receive a node object, and returns a Qt4 graphics item that can be used as a node face. """ # receive an arbitrary number of arguments, in this case width and # height of the faces width = args[0][0] height = args[0][1] ## Creates a main master Item that will contain all other elements ## Items can be standard QGraphicsItem # masterItem = QGraphicsRectItem(0, 0, width, height) # Or your custom Items, in which you can re-implement interactive # functions, etc. Check QGraphicsItem doc for details. masterItem = InteractiveItem(0, 0, width, height) # Keep a link within the item to access node info masterItem.node = node # I dont want a border around the masterItem masterItem.setPen(QPen(QtCore.Qt.NoPen)) # Add ellipse around text ellipse = QGraphicsEllipseItem(masterItem.rect()) ellipse.setParentItem(masterItem) # Change ellipse color ellipse.setBrush(QBrush(QColor(random_color()))) # Add node name within the ellipse text = QGraphicsSimpleTextItem(node.name) text.setParentItem(ellipse) text.setPen(QPen(QPen(QColor("white")))) # Center text according to masterItem size tw = text.boundingRect().width() th = text.boundingRect().height() center = masterItem.boundingRect().center() text.setPos(center.x() - tw / 2, center.y() - th / 2) return masterItem
class Joystick(Button): def __init__(self, outer, inner, filename=None, width=None, height=None, x=0, y=0, pixmap=None, group=None, pos=None, size=None, padding=None): Button.__init__(self, filename, width, height, x, y, pixmap, group, pos, size, padding) self.outer = outer self.inner = inner self.innerRange = 48 self.inputRange = 256 self.thresh = 5 self.outerCircle = QGraphicsEllipseItem(self.getX(), self.getY(), self.outer, self.outer) self.outerCircle.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.outerCircle.setBrush(Qt.gray) self.innerCircle = QGraphicsEllipseItem(self.getX() + self.outer / 2 - self.inner / 2, self.getY() + self.outer / 2 - self.inner / 2, self.inner, self.inner) self.innerCircle.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle.setBrush(Qt.lightGray) self.currentX = 0 self.currentY = 0 self.items = [self.outerCircle, self.innerCircle] def getItems(self): return self.items def setScene(self, scene): self.scene = scene for item in self.items : self.scene.addItem(item) def update(self,x,y,z): # y = y - sqrt(x + y) if x > 0 else y # x = x - sqrt(x + y) if y > 0 else x x2 = x * self.innerRange / self.inputRange - self.innerRange / 2 y2 = y * self.innerRange / self.inputRange - self.innerRange / 2 x2 = x2 - sqrt(abs(y2)) if x2 >= 0 else x2 + sqrt(abs(y2)) y2 = y2 - sqrt(abs(x2)) if y2 >= 0 else y2 + sqrt(abs(x2)) if self.inputRange / 2 - self.thresh <= x <= self.inputRange / 2 + self.thresh: x2 = 0 if self.inputRange / 2 - self.thresh <= y <= self.inputRange / 2 + self.thresh: y2 = 0 self.tl = QTimeLine(10) self.tl.setFrameRange(0, 10) self.a = QGraphicsItemAnimation() self.a.setItem(self.innerCircle) self.a.setTimeLine(self.tl) self.a.setPosAt(0, QPointF(self.currentX, self.currentY)) self.a.setTranslationAt(1, x2, y2) if z: self.innerCircle.setPen(QPen(QColor(Qt.white), 1, Qt.SolidLine)) self.innerCircle.setBrush(QColor(200, 225, 3)) else: self.innerCircle.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle.setBrush(Qt.lightGray) self.currentX = x2 self.currentY = y2 self.tl.start()
class DraggedLineItem(QGraphicsPathItem): def __init__(self, p0, p1, parent=None): QGraphicsPathItem.__init__(self, parent=parent) self._p0 = p0 self._p1 = p1 self._startPoint = QGraphicsEllipseItem(-3, -3, 6, 6, parent=self) self._startPoint.setPos(p0) self._endPoint = QGraphicsEllipseItem(-3, -3, 6, 6, parent=self) self._endPoint.setVisible(False) brush = QBrush(QColor(Qt.black)) self._startPoint.setBrush(brush) self._endPoint.setBrush(brush) pen = QPen(brush, 2.0) self.setPen(pen) def showEndpoint(self, show): self._endPoint.setVisible(show) def setEndpoint(self, pos): self._p1 = pos self._endPoint.setPos(pos) self._updatePath() def _updatePath(self): p0 = self._p0 p1 = self._p1 path = QPainterPath() path.moveTo(p0) dx = p1.x() - p0.x() x0 = p0.x() + 0.7 * dx x1 = p1.x() - 0.7 * dx path.cubicTo(QPointF(x0, p0.y()), QPointF(x1, p1.y()), p1) self.setPath(path)
def iLabel(node, *args, **kargs): #code for making specialized faces for intermediates mostly cribbed from the ete2 website example (though not interactive): # http://pythonhosted.org/ete2/tutorial/tutorial_drawing.html#creating-your-custom-interactive-item-faces my_label = args[0][0] #or maybe just node.name? ellipse = QGraphicsEllipseItem(0,0,fontSize*2,fontSize*2) #I think the first two are coords of center; second pair is major/minor axis ellipse.setBrush(QBrush(QColor( 'black' ))) text = QGraphicsSimpleTextItem(my_label) text.setParentItem(ellipse) text.setBrush(QBrush(QColor("white"))) text.setFont(QFont("Arial",fontSize*.75)) #Center text according to masterItem size tw = text.boundingRect().width() th = text.boundingRect().height() center = ellipse.boundingRect().center() text.setPos(center.x()-tw/2, center.y()-th/2) return ellipse
class CircleCurve(OWCurve): """ Displays a circle on the plot :param pen: The pen used to draw the outline of the circle :type pen: QPen :param brush: The brush used to paint the inside of the circle :type brush: QBrush :param xCenter: The x coordinate of the circle's center :type xCenter: float :param yCenter: The y coordinate of the circle's center :type yCenter: float :param radius: The circle's radius :type radius: float """ def __init__(self, pen=QPen(Qt.black), brush=QBrush(Qt.NoBrush), xCenter=0.0, yCenter=0.0, radius=1.0): OWCurve.__init__(self) self._item = QGraphicsEllipseItem(self) self.center = xCenter, yCenter self.radius = radius self._rect = QRectF(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius) self.set_pen(pen) self.set_brush(brush) def update_properties(self): self._item.setRect(self.graph_transform().mapRect(self.data_rect())) self._item.setPen(self.pen()) self._item.setBrush(self.brush()) def data_rect(self): x, y = self.center r = self.radius return QRectF(x - r, y - r, 2 * r, 2 * r)
def iLabel(node, *args, **kargs): #code for making specialized faces for intermediates mostly cribbed from the ete2 website example (though not interactive): # http://pythonhosted.org/ete2/tutorial/tutorial_drawing.html#creating-your-custom-interactive-item-faces my_label = args[0][0] #or maybe just node.name? ellipse = QGraphicsEllipseItem( 0, 0, fontSize * 2, fontSize * 2 ) #I think the first two are coords of center; second pair is major/minor axis ellipse.setBrush(QBrush(QColor('black'))) text = QGraphicsSimpleTextItem(my_label) text.setParentItem(ellipse) text.setBrush(QBrush(QColor("white"))) text.setFont(QFont("Arial", fontSize * .75)) #Center text according to masterItem size tw = text.boundingRect().width() th = text.boundingRect().height() center = ellipse.boundingRect().center() text.setPos(center.x() - tw / 2, center.y() - th / 2) return ellipse
def item(self): if self.item_ is None: num_pts = len(self.pts_) if num_pts == 1: # draw a point item = QGraphicsEllipseItem(self.pts_[0][0]-self.radius_, self.pts_[0][1]-self.radius_, 2*self.radius_, 2*self.radius_) item.setBrush(self.qcolor) elif num_pts == 2: item = QGraphicsLineItem(self.pts_[0][0], self.pts_[0][1], self.pts_[1][0], self.pts_[1][1]) else: poly = QPolygonF() for p in self.pts_: poly.append(QPointF(p[0],p[1])) item = QGraphicsPolygonItem(poly) item.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsFocusable) item.setPen(self.qcolor) item.setEnabled(True) item.setActive(True) self.item_ = item return self.item_
class AxisScene(QGraphicsScene): def __init__(self, parent = None): super(AxisScene, self).__init__(parent) self.guidePen = QPen(QColor("blue"),1) self.dot = QGraphicsEllipseItem(-10, -10, 20, 20) self.dot.setPos(QPointF(0,0)) self.dot.setPen(QPen(QColor("red"), 4)) self.dot.setBrush(QColor("black")) self.lastPos = {} self.lastPos['x'] = 0 self.lastPos['y'] = 0 self.grid = False self.gridPen = QPen(QColor("blue"), 2) self.pathItem = QGraphicsPathItem() self.pathItem.setPen(QPen(QColor("red"), 1, Qt.DotLine)) self.path = None self.xAxis = "Select Axis..." self.yAxis = self.xAxis self.addItem(self.dot) def setXAxisName(self, x): self.xAxis = "Axis " + str(x) self.invalidate() def setYAxisName(self, y): self.yAxis = "Axis " + str(y) self.invalidate() def pathToggled(self, toggled): if toggled: self.path = QPainterPath() self.path.moveTo(0,0) self.pathItem.setPath(self.path) self.addItem(self.pathItem) else: if self.path != None: self.removeItem(self.pathItem) self.path = None self.invalidate() def gridToggled(self, toggled): self.grid = toggled self.invalidate() def updateDotX(self, x): self.lastPos['x'] = x * (self.sceneRect().width() / 2) self.update(self.lastPos['x'], self.lastPos['y']) def updateDotY(self, y): self.lastPos['y'] = y * (self.sceneRect().height() / 2) self.update(self.lastPos['x'], self.lastPos['y']) def update(self, x, y): if self.path != None: self.path.lineTo(x, y) self.pathItem.setPath(self.path) self.dot.setPos(self.lastPos['x'], self.lastPos['y']) self.invalidate() def drawBackground(self, painter, rect): if self.grid: painter.setClipRect(rect) painter.setPen(self.gridPen) def drawForeground(self, painter, rect): painter.setClipRect(rect) painter.setPen(self.gridPen) r = self.sceneRect() if self.grid: painter.drawLine(r.center().x(), r.top(), r.center().x(), r.bottom()) painter.drawLine(r.left(), r.center().y(), r.right(), r.center().y()) painter.setPen(QPen(QColor("red"), 1)) painter.drawText(QRectF(r.left(), r.center().y(), 80, 80), self.xAxis) painter.drawText(QRectF(r.center().x()+5, r.top(), 80, 80), self.yAxis) if self.path != None: painter.setPen(QPen(QColor("red"), 1)) painter.drawLine(QPointF(self.lastPos['x'], r.top()), QPointF(self.lastPos['x'], r.bottom())) painter.drawLine(QPointF(r.left(), self.lastPos['y']), QPointF(r.right(), self.lastPos['y']))
class DualJoystickView(QGraphicsView): def __init__(self, *args): QGraphicsView.__init__(self, *args) self.outerD = 125 self.innerD = 35 self.innerRange = 48 self.inputRange = 256 self.thresh = 3 self.padding = 40 self.worker = QSixAxisThread() self.worker.valueUpdated.connect(self.moveJoysticks) self.worker.start() self.move(2, 100) self.setContentsMargins(0, 0, 0, 0) self.setMaximumHeight(180) self.setMaximumWidth(420) self.setMinimumHeight(140) self.setMinimumWidth(300) self.adjustSize() self.scene = QGraphicsScene(self) self.outerCircle1 = QGraphicsEllipseItem(0, 0, self.outerD, self.outerD) self.outerCircle1.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.outerCircle1.setBrush(Qt.gray) self.innerCircle1 = QGraphicsEllipseItem(self.outerD / 2 - self.innerD / 2, self.outerD / 2 - self.innerD / 2, self.innerD, self.innerD) self.innerCircle1.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle1.setBrush(Qt.lightGray) self.scene.addItem(self.outerCircle1) self.scene.addItem(self.innerCircle1) self.outerCircle2 = QGraphicsEllipseItem(self.outerD + self.padding, 0, self.outerD, self.outerD) self.outerCircle2.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.outerCircle2.setBrush(Qt.gray) self.innerCircle2 = QGraphicsEllipseItem(self.outerD + self.padding + self.outerD / 2 - self.innerD / 2, self.outerD / 2 - self.innerD / 2, self.innerD, self.innerD) self.innerCircle2.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle2.setBrush(Qt.lightGray) self.scene.addItem(self.outerCircle2) self.scene.addItem(self.innerCircle2) self.setScene(self.scene) self.setStyleSheet('background-color:transparent;') self.currentX = 0 self.currentY = 0 self.currentA = 0 self.currentZ = 0 self.test = QGraphicsItem(self.outerCircle2) def moveJoysticks(self, x, y, a, z, l3, r3): x2 = x * self.innerRange / self.inputRange - self.innerRange / 2 y2 = y * self.innerRange / self.inputRange - self.innerRange / 2 a2 = a * self.innerRange / self.inputRange - self.innerRange / 2 z2 = z * self.innerRange / self.inputRange - self.innerRange / 2 if -self.thresh <= x2 <= self.thresh: x2 = 0 if -self.thresh <= y2 <= self.thresh: y2 = 0 if -self.thresh <= a2 <= self.thresh: a2 = 0 if -self.thresh <= z2 <= self.thresh: z2 = 0 self.tl = QTimeLine(10) self.tl.setFrameRange(0, 10) self.a = QGraphicsItemAnimation() self.a.setItem(self.innerCircle1) self.a.setTimeLine(self.tl) self.a.setPosAt(0, QPointF(self.currentX, self.currentY)) self.a.setTranslationAt(1, x2, y2) if l3: self.innerCircle1.setPen(QPen(QColor(Qt.white), 1, Qt.SolidLine)) self.innerCircle1.setBrush(QColor(200, 225, 3)) else: self.innerCircle1.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle1.setBrush(Qt.lightGray) if r3: self.innerCircle2.setPen(QPen(QColor(Qt.white), 1, Qt.SolidLine)) self.innerCircle2.setBrush(QColor(200, 225, 3)) else: self.innerCircle2.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle2.setBrush(Qt.lightGray) self.b = QGraphicsItemAnimation() self.b.setItem(self.innerCircle2) self.b.setTimeLine(self.tl) self.b.setPosAt(0, QPointF(self.currentA, self.currentZ)) self.b.setTranslationAt(1, a2, z2) self.currentX = x2 self.currentY = y2 self.currentA = a2 self.currentZ = z2 self.tl.start()
class SixAxisView(QGraphicsView): def __init__(self, *args): QGraphicsView.__init__(self, *args) self.move(20, 240) self.outerD = 125 self.innerD = 35 self.innerRange = 48 self.inputRange = 256 self.thresh = 3 self.padding = 40 self.marginTop = 10 self.worker = QSixAxisThread() self.worker.valueUpdated.connect(self.moveJoysticks) self.worker.start() self.setContentsMargins(0, 0, 0, 0) self.setMaximumHeight(180) self.setMaximumWidth(420) self.setMinimumHeight(240) self.setMinimumWidth(300) self.adjustSize() self.scene = QGraphicsScene(self) self.outerCircle1 = QGraphicsEllipseItem(0, self.marginTop, self.outerD, self.outerD) self.outerCircle1.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.outerCircle1.setBrush(Qt.gray) self.innerCircle1 = QGraphicsEllipseItem(self.outerD / 2 - self.innerD / 2, self.outerD / 2 - self.innerD / 2 + self.marginTop, self.innerD, self.innerD) self.innerCircle1.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle1.setBrush(Qt.lightGray) self.scene.addItem(self.outerCircle1) self.scene.addItem(self.innerCircle1) self.outerCircle2 = QGraphicsEllipseItem(self.outerD + self.padding, self.marginTop, self.outerD, self.outerD) self.outerCircle2.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.outerCircle2.setBrush(Qt.gray) self.innerCircle2 = QGraphicsEllipseItem(self.outerD + self.padding + self.outerD / 2 - self.innerD / 2, self.outerD / 2 - self.innerD / 2 + self.marginTop, self.innerD, self.innerD) self.innerCircle2.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle2.setBrush(Qt.lightGray) self.scene.addItem(self.outerCircle2) self.scene.addItem(self.innerCircle2) self.setScene(self.scene) self.setStyleSheet('background-color:transparent; border-width: 0px; border: 0px;') self.currentX = 0 self.currentY = 0 self.currentA = 0 self.currentZ = 0 self.psBtn = QPixmap(os.getcwd() + '/../icons/bt_PS.png') self.psBtn = self.psBtn.scaled(50, 50, Qt.KeepAspectRatio) self.psItem = QGraphicsPixmapItem(self.psBtn) self.psItem.setOffset(QPointF(self.outerD - 4, 0)) self.effect = QGraphicsDropShadowEffect() self.effect.setOffset(0, 0) self.effect.setBlurRadius(20) self.effect.setColor(Qt.green) self.psItem.setGraphicsEffect(self.effect) self.scene.addItem(self.psItem) self.tl2 = QTimeLine(10000) self.tl2.setFrameRange(0, 10000) self.c = QGraphicsItemAnimation() self.c.setItem(self.psItem) self.c.setTimeLine(self.tl2) self.tl2.connect(self.tl2, SIGNAL('frameChanged(int)'), self.updateEffect) self.effectd = 3 self.tl2.start() def updateEffect(self): if self.effect.blurRadius() > 50: self.effectd = -3 elif self.effect.blurRadius() < 5: self.effectd = 3 self.effect.setBlurRadius(self.effect.blurRadius() + self.effectd) def update(self, *args, **kwargs): return QGraphicsView.update(self, *args, **kwargs) def moveJoysticks(self, x, y, a, z, l3, r3): x2 = x * self.innerRange / self.inputRange - self.innerRange / 2 y2 = y * self.innerRange / self.inputRange - self.innerRange / 2 a2 = a * self.innerRange / self.inputRange - self.innerRange / 2 z2 = z * self.innerRange / self.inputRange - self.innerRange / 2 if -self.thresh <= x2 <= self.thresh: x2 = 0 if -self.thresh <= y2 <= self.thresh: y2 = 0 if -self.thresh <= a2 <= self.thresh: a2 = 0 if -self.thresh <= z2 <= self.thresh: z2 = 0 self.tl = QTimeLine(10) self.tl.setFrameRange(0, 10) self.a = QGraphicsItemAnimation() self.a.setItem(self.innerCircle1) self.a.setTimeLine(self.tl) self.a.setPosAt(0, QPointF(self.currentX, self.currentY)) self.a.setTranslationAt(1, x2, y2) if l3: self.innerCircle1.setPen(QPen(QColor(Qt.white), 1, Qt.SolidLine)) self.innerCircle1.setBrush(QColor(200, 225, 3)) else: self.innerCircle1.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle1.setBrush(Qt.lightGray) if r3: self.innerCircle2.setPen(QPen(QColor(Qt.white), 1, Qt.SolidLine)) self.innerCircle2.setBrush(QColor(200, 225, 3)) else: self.innerCircle2.setPen(QPen(QColor(Qt.darkGray), 1, Qt.SolidLine)) self.innerCircle2.setBrush(Qt.lightGray) self.b = QGraphicsItemAnimation() self.b.setItem(self.innerCircle2) self.b.setTimeLine(self.tl) self.b.setPosAt(0, QPointF(self.currentA, self.currentZ)) self.b.setTranslationAt(1, a2, z2) self.currentX = x2 self.currentY = y2 self.currentA = a2 self.currentZ = z2 self.tl.start()
class TaskGraphicsItem (QGraphicsEllipseItem): def __init__(self, rect=None): super(TaskGraphicsItem, self).__init__() if rect is not None: self.setRect(rect) self.setPen(QPen(Qt.NoPen)) # Setup the text item self.textItem = QGraphicsTextItem() self.textItem.setParentItem(self) self.textItem.rotate(-90) self.textItem.setDefaultTextColor(QColor(255, 255, 255)) # The dimensions to reach via a LERP. self.startPos = QPointF(0, 0) self.endPos = QPointF(0, 0) self.startDiameter = 1 self.endDiameter = 1 self.centerMark = QGraphicsEllipseItem() self.centerMark.setBrush(QBrush(Qt.white)) self.centerMark.setPen(QPen(Qt.NoPen)) self.centerMark.setParentItem(self) self.pid = -1 # To determine if it is associated with an active process. self.used = False def mousePressEvent(self, event): print "Clicked On Ellipse at: ", self.rect().topLeft() def set_pid(self, pid): self.pid = pid def set_name(self, str_name): self.textItem.setPlainText(str_name) def update_name_pos(self): rect = self.boundingRect() text_rect = self.textItem.boundingRect() # Center text (on the x-axis) and offset (on the y-axis) so it doesn't overlap the ellipse item. x_text = rect.x() + rect.width()/2 - text_rect.height()/2 y_text = rect.y() + 100 + text_rect.width() + rect.height() self.textItem.setPos(x_text, y_text) # Time step is in seconds. def update(self, time_step): diameter = self.rect().width() + self.lerp_rate(self.startDiameter, self.endDiameter, time_step) if diameter <= 1: diameter = 1 pos = self.rect().topLeft() x = pos.x() + self.lerp_rate(self.startPos.x(), self.endPos.x(), time_step) y = pos.y() + self.lerp_rate(self.startPos.y(), self.endPos.y(), time_step) self.setRect(QRectF(x, y, diameter, diameter)) self.update_name_pos() self.update_center_mark() def update_center_mark(self): scale = self.scene().views()[0].currentScale hwidth = self.rect().width() / 2.0 diam = 2.0 / scale # Only mark center for large enough items. if hwidth * 0.2 > diam: self.centerMark.setVisible(True) hdiam = diam / 2.0 pos = self.rect().topLeft() x = pos.x() - hdiam + hwidth y = pos.y() - hdiam + hwidth self.centerMark.setRect(QRectF(x, y, diam, diam)) else: self.centerMark.setVisible(False) # Return the linear interpolation rate. Reach start to end at a rate of 'growth rate' @staticmethod def lerp_rate(start, end, time_step): return (end - start) * time_step