def draw_word_boxes(self, word_boxes: list) -> None: """Sets up and draws translated words and their corresponding frames :param word_boxes: list of WordBox namedtuples, each representing a word and frame to be drawn. ex: [WordBox('word'=str, 'geometry'=[(int, int)], WordBox('word'=str, 'geometry'=[(int, int)]) """ for wPoly in self.__word_polygons: self.__scene.removeItem(wPoly[0]) self.__scene.removeItem(wPoly[1]) self.__word_polygons = [] for word_box in word_boxes: points = list(map(lambda x: QPoint(x[0], x[1]), word_box.geometry)) text = QGraphicsTextItem(word_box.word) text.setOpacity(0) text.setAcceptHoverEvents(False) font = QFont() font.setPixelSize(abs(points[0].y() - points[3].y())) text.setFont(font) w = text.boundingRect().width() h = text.boundingRect().height() text.setPos( points[0].x() + abs(points[0].x() - points[1].x()) / 2 - w / 2, points[0].y() + abs(points[0].y() - points[3].y()) / 2 - h / 2) frame = WordPolygon(QPolygonF(QPolygon(points)), text) self.__word_polygons.append([text, frame]) self.__scene.addItem(frame) self.__scene.addItem(text)
class fadingPic(QObject): """ Wrap a QGraphicsPixmapItem and impliment the fade in/out animation""" def __init__(self, pixmap, parent=None): super().__init__(parent) self.pixmap_item = QGraphicsPixmapItem(pixmap) self.text_item = QGraphicsTextItem( "Nerual Network based Channel Learning") font = QFont("Nokia Pure Text Light", 18) # QFont.Bold self.text_item.setFont(font) self.text_item.setDefaultTextColor(QColor(18, 65, 145)) def _set_opacity(self, opc): self.pixmap_item.setOpacity(opc) self.text_item.setOpacity(opc) def fadeIn(self): anim = QPropertyAnimation(self, b'opacity') anim.setDuration(800) anim.setStartValue(0) anim.setEndValue(1) #anim.setLoopCount(1) return anim def fadeOut(self): anim = QPropertyAnimation(self, b'opacity') anim.setDuration(800) anim.setStartValue(1) anim.setEndValue(0) #anim.setLoopCount(1) return anim opacity = pyqtProperty(float, fset=_set_opacity)
def populate(self): font = QtGui.QFont('White Rabbit') font.setPointSize(120) self.dot1 = QGraphicsTextItem(':') self.dot1.setFont(font) self.dot1.setPos(140, 0) self.scene.addItem(self.dot1) self.dot2 = QGraphicsTextItem(':') self.dot2.setFont(font) self.dot2.setPos(410, 0) self.scene.addItem(self.dot2) for i in range(60): l = QGraphicsTextItem(str(i % 10)) l.setFont(font) l.setZValue(-100) l.setPos(randint(0, 500), randint(150, 300)) l.setOpacity(.3) self.scene.addItem(l) self.digits.append(l)
def populate(self): self.digits = [] self.animations = [] # This is just a nice font, use any font you like, or none font = QFont('White Rabbit') font.setPointSize(120) # Create three ":" and place them in our scene self.dot1 = QGraphicsTextItem(':') # from QtGui self.dot1.setFont(font) self.dot1.setPos(140, 0) self._scene.addItem(self.dot1) self.dot2 = QGraphicsTextItem(':') self.dot2.setFont(font) self.dot2.setPos(410, 0) self._scene.addItem(self.dot2) # Create 6 sets of 0-9 digits for i in range(60): t_item = QGraphicsTextItem(str(i % 10)) t_item.setFont(font) # The zvalue is what controls what appears "on top" of what. # Send them to "the bottom" of the scene. t_item.setZValue(-100) # Place them anywhere t_item.setPos(randint(0, 500), randint(150, 300)) # Make them semi-transparent t_item.setOpacity(.3) # Put them in the scene self._scene.addItem(t_item) # Keep a reference for internal purposes self.digits.append(t_item)