class Robot(RobotPart): def __init__(self): super(Robot, self).__init__() self.torsoItem = RobotTorso(self) self.headItem = RobotHead(self.torsoItem) self.upperLeftArmItem = RobotLimb(self.torsoItem) self.lowerLeftArmItem = RobotLimb(self.upperLeftArmItem) self.upperRightArmItem = RobotLimb(self.torsoItem) self.lowerRightArmItem = RobotLimb(self.upperRightArmItem) self.upperRightLegItem = RobotLimb(self.torsoItem) self.lowerRightLegItem = RobotLimb(self.upperRightLegItem) self.upperLeftLegItem = RobotLimb(self.torsoItem) self.lowerLeftLegItem = RobotLimb(self.upperLeftLegItem) self.timeline = QTimeLine() settings = [ # item position rotation at # x y time 0 / 1 ( self.headItem, 0, -18, 20, -20 ), ( self.upperLeftArmItem, -15, -10, 190, 180 ), ( self.lowerLeftArmItem, 30, 0, 50, 10 ), ( self.upperRightArmItem, 15, -10, 300, 310 ), ( self.lowerRightArmItem, 30, 0, 0, -70 ), ( self.upperRightLegItem, 10, 32, 40, 120 ), ( self.lowerRightLegItem, 30, 0, 10, 50 ), ( self.upperLeftLegItem, -10, 32, 150, 80 ), ( self.lowerLeftLegItem, 30, 0, 70, 10 ), ( self.torsoItem, 0, 0, 5, -20 ) ] self.animations = [] for item, pos_x, pos_y, rotation1, rotation2 in settings: item.setPos(pos_x,pos_y) animation = QGraphicsItemAnimation() animation.setItem(item) animation.setTimeLine(self.timeline) animation.setRotationAt(0, rotation1) animation.setRotationAt(1, rotation2) self.animations.append(animation) self.animations[0].setScaleAt(1, 1.1, 1.1) self.timeline.setUpdateInterval(1000 / 25) self.timeline.setCurveShape(QTimeLine.SineCurve) self.timeline.setLoopCount(0) self.timeline.setDuration(2000) self.timeline.start() def boundingRect(self): return QRectF() def paint(self, painter, option, widget=None): pass
class Robot(RobotPart): def __init__(self): super(Robot, self).__init__() self.torsoItem = RobotTorso(self) self.headItem = RobotHead(self.torsoItem) self.upperLeftArmItem = RobotLimb(self.torsoItem) self.lowerLeftArmItem = RobotLimb(self.upperLeftArmItem) self.upperRightArmItem = RobotLimb(self.torsoItem) self.lowerRightArmItem = RobotLimb(self.upperRightArmItem) self.upperRightLegItem = RobotLimb(self.torsoItem) self.lowerRightLegItem = RobotLimb(self.upperRightLegItem) self.upperLeftLegItem = RobotLimb(self.torsoItem) self.lowerLeftLegItem = RobotLimb(self.upperLeftLegItem) self.timeline = QTimeLine() settings = [ # item position rotation at # x y time 0 / 1 (self.headItem, 0, -18, 20, -20), (self.upperLeftArmItem, -15, -10, 190, 180), (self.lowerLeftArmItem, 30, 0, 50, 10), (self.upperRightArmItem, 15, -10, 300, 310), (self.lowerRightArmItem, 30, 0, 0, -70), (self.upperRightLegItem, 10, 32, 40, 120), (self.lowerRightLegItem, 30, 0, 10, 50), (self.upperLeftLegItem, -10, 32, 150, 80), (self.lowerLeftLegItem, 30, 0, 70, 10), (self.torsoItem, 0, 0, 5, -20) ] self.animations = [] for item, pos_x, pos_y, rotation1, rotation2 in settings: item.setPos(pos_x, pos_y) animation = QGraphicsItemAnimation() animation.setItem(item) animation.setTimeLine(self.timeline) animation.setRotationAt(0, rotation1) animation.setRotationAt(1, rotation2) self.animations.append(animation) self.animations[0].setScaleAt(1, 1.1, 1.1) self.timeline.setUpdateInterval(1000 / 25) self.timeline.setCurveShape(QTimeLine.SineCurve) self.timeline.setLoopCount(0) self.timeline.setDuration(2000) self.timeline.start() def boundingRect(self): return QRectF() def paint(self, painter, option, widget=None): pass
class AnimationView(QWidget): """docstring for AnimationView""" frameIndex = 0 frameCount = 0 def __init__(self, bmpFrames): super(AnimationView, self).__init__() self.setFixedSize(frameWidth,frameHeight) self.bmpSize =64 if bmpFrames : self.bmpFrames = bmpFrames self.frameCount = len(bmpFrames) self.bmpSize = bmpFrames[0].width() else : self.initDefaultFrames() def initDefaultFrames(self): self.bmpFrames = [] defaultImage = QImage("..\\resource\\default.bmp") imageWidth = defaultImage.width() imageHeight = defaultImage.height() #判断各幀图片是否是横向排列 isHorizontal = min(imageWidth,imageHeight) == imageHeight #计算幀数 self.frameCount = imageWidth//frameWidth if isHorizontal else imageHeight//frameHeight for i in range(0,self.frameCount): pixmap = QPixmap(defaultImage.copy(i*frameWidth if isHorizontal else 0, 0 if isHorizontal else i*frameHeight,frameWidth,frameHeight)) eliminateBackgroundColor(pixmap) self.bmpFrames.append(pixmap) def createAnimation(self): self.timeLine = QTimeLine(10*1000) self.timeLine.setFrameRange(0,60//(4/self.frameCount)) self.timeLine.frameChanged.connect(self.refreshFrameIndex) self.timeLine.setLoopCount(0) self.timeLine.setCurveShape(3) self.timeLine.start() def refreshFrameIndex(self,currFrame): self.update() self.frameIndex = (self.frameIndex+1) % self.frameCount def paintEvent(self,event): painter = QPainter(self) painter.drawPixmap((frameWidth-self.bmpSize)//2,(frameHeight-self.bmpSize)//2,self.bmpFrames[self.frameIndex])
class MainWindow(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle("Screen and Gaze Capture") grid = QGridLayout() layout_frame = QFrame() layout_frame.setLayout(grid) self.setCentralWidget(layout_frame) self.tracker_label = QLabel("No eye tracker connected.") grid.addWidget(self.tracker_label, 0, 0) connect_button = QPushButton("Connect to Eye Tracker") connect_button.pressed.connect(connect_to_tracker) grid.addWidget(connect_button, 1, 0) calibrate_button = QPushButton("Calibrate Eye Tracker") calibrate_button.pressed.connect(calibrate) grid.addWidget(calibrate_button, 2, 0) self.record_button = QPushButton("Record Screen and Gaze") self.record_button.pressed.connect(capture_screen) grid.addWidget(self.record_button, 3, 0) self.author_vid_button = QPushButton("Write Author Video") self.author_vid_button.pressed.connect(create_video) grid.addWidget(self.author_vid_button, 4, 0) self.author_vid_button.setEnabled(False) self.timeline = QTimeLine() self.timeline.setCurveShape(QTimeLine.LinearCurve) self.timeline.setDuration(360000) #Totsl video lengthn in milliseconds self.timeline.setFrameRange( 0, 7500) #Maximum Frames in this video as 30 fps self.timeline.frameChanged.connect(update_frame) self.timeline.setUpdateInterval(10) #Rate of Frame Capture def keyPressEvent(self, event): if event.key() == Qt.Key_Escape: app.closeAllWindows()
class Window(QWidget): def __init__(self, *args, **kwargs): super(Window, self).__init__(*args, **kwargs) self.resize(600, 200) layout = QVBoxLayout(self) # 配置全局属性(也可以通过start方法里的参数配置单独的属性) CLoadingBar.config(height=2, direction=CLoadingBar.TOP, color='#2d8cf0', failedColor='#ed4014') # 子控件顶部进度 self.widget1 = QWidget(self) layout.addWidget(self.widget1) CLoadingBar.start(self.widget1, color='#19be6b', failedColor='#ff9900') widget = QWidget(self) layoutc = QHBoxLayout(widget) layoutc.addWidget(QPushButton('开始', self, clicked=self.doStart)) layoutc.addWidget(QPushButton('结束', self, clicked=self.doFinish)) layoutc.addWidget(QPushButton('错误', self, clicked=self.doError)) layout.addWidget(widget) # 子控件底部进度 self.widget2 = QWidget(self) layout.addWidget(self.widget2) CLoadingBar.start(self.widget2, direction=CLoadingBar.BOTTOM, height=6) # 模拟进度 self.updateTimer = QTimeLine(10000, self, frameChanged=self.doUpdateProgress) self.updateTimer.setFrameRange(0, 100) # 设置数字变化曲线模拟进度的不规则变化 self.updateTimer.setCurveShape(QTimeLine.EaseInOutCurve) def doStart(self): """模拟开始 """ self.updateTimer.stop() self.updateTimer.start() def doFinish(self): """模拟结束 """ self.updateTimer.stop() CLoadingBar.finish(self.widget1) CLoadingBar.finish(self.widget2) def doError(self): """模拟出错 """ self.updateTimer.stop() CLoadingBar.error(self.widget1) CLoadingBar.error(self.widget2) def doUpdateProgress(self, value): """模拟进度值变化 :param value: """ CLoadingBar.update(self.widget1, value) CLoadingBar.update(self.widget2, value) if value == 100: self.updateTimer.stop() self.doFinish()