def _on_right_arrow_click(self): """ User clicks the right arrow """ start_index = self.ui.stackedWidget.currentIndex() next_index = start_index + 1 if next_index == (self._num_images - 1): # we arrived at the last slide! Hide right arrow self.ui.right_arrow.setVisible(False) self.ui.left_arrow.setVisible(True) else: self.ui.right_arrow.setVisible(True) self.ui.left_arrow.setVisible(True) this_page = self._pages[start_index] next_page = self._pages[next_index] # rest positions next_page.move(next_page.x()+650, next_page.y()) self.ui.stackedWidget.setCurrentIndex(next_index) this_page.show() this_page.raise_() self.anim = QtCore.QPropertyAnimation(this_page, "pos") self.anim.setDuration(600) self.anim.setStartValue(QtCore.QPoint(this_page.x(), this_page.y())) self.anim.setEndValue(QtCore.QPoint(this_page.x()-650, this_page.y())) self.anim.setEasingCurve(QtCore.QEasingCurve.OutCubic) self.anim2 = QtCore.QPropertyAnimation(next_page, "pos") self.anim2.setDuration(600) self.anim2.setStartValue(QtCore.QPoint(next_page.x()+650, next_page.y())) self.anim2.setEndValue(QtCore.QPoint(next_page.x(), next_page.y())) self.anim2.setEasingCurve(QtCore.QEasingCurve.OutCubic) self.grp = QtCore.QParallelAnimationGroup() self.grp.addAnimation(self.anim) self.grp.addAnimation(self.anim2) self.grp.start()
def slide_view(self, new_page, from_direction="right"): offsetx = self.ui.stack.frameRect().width() offsety = self.ui.stack.frameRect().height() current_page = self.ui.stack.currentWidget() new_page.setGeometry(0, 0, offsetx, offsety) if from_direction == "left": offsetx = -offsetx curr_pos = new_page.pos() new_page.move(curr_pos.x() + offsetx, curr_pos.y()) new_page.show() new_page.raise_() anim_old = QtCore.QPropertyAnimation(current_page, "pos", self) anim_old.setDuration(500) anim_old.setStartValue(QtCore.QPoint(curr_pos.x(), curr_pos.y())) anim_old.setEndValue( QtCore.QPoint(curr_pos.x() - offsetx, curr_pos.y())) anim_old.setEasingCurve(QtCore.QEasingCurve.OutBack) anim_new = QtCore.QPropertyAnimation(new_page, "pos", self) anim_new.setDuration(500) anim_new.setStartValue( QtCore.QPoint(curr_pos.x() + offsetx, curr_pos.y())) anim_new.setEndValue(QtCore.QPoint(curr_pos.x(), curr_pos.y())) anim_new.setEasingCurve(QtCore.QEasingCurve.OutBack) anim_group = QtCore.QParallelAnimationGroup(self) anim_group.addAnimation(anim_old) anim_group.addAnimation(anim_new) def slide_finished(): self.ui.stack.setCurrentWidget(new_page) anim_group.finished.connect(slide_finished) anim_group.start()
def __turn_page(self, direction=NEXT_PAGE): """ Turn the page in the direction specified :param direction: The direction to turn the page """ current_index = self.ui.stackedWidget.currentIndex() dst_index = current_index page_offset = 650 # depending on the direction, figure out the destination page # and page offset for animation: if direction == Dialog.NEXT_PAGE: dst_index += 1 page_offset = 650 # update the arrow visibility so that the right arrow is # hidden if we're on the last page: self.ui.right_arrow.setVisible(dst_index < (self._num_images - 1)) self.ui.left_arrow.setVisible(True) else: # going back a page dst_index -= 1 page_offset = -650 # update the arrow visibility so that the left arrow is # hidden if we're on the first page: self.ui.right_arrow.setVisible(True) self.ui.left_arrow.setVisible(dst_index > 0) if not hasattr(QtCore, "QAbstractAnimation"): # this version of Qt (probably PyQt4) doesn't contain # Q*Animation classes so just change the page: self.ui.stackedWidget.setCurrentIndex(dst_index) else: anim_duration = 600 # milliseconds if self.__page_anim_grp and self.__page_anim_grp.state( ) == QtCore.QAbstractAnimation.Running: # the previous animation hasn't finished yet so jump to the end! self.__page_anim_grp.setCurrentTime(anim_duration) # animate the transition from one page to the next: current_page = self._pages[current_index] dst_page = self._pages[dst_index] # reset positions dst_page.move(dst_page.x() + page_offset, dst_page.y()) self.ui.stackedWidget.setCurrentIndex(dst_index) # still need to show the current page whilst it transitions current_page.show() current_page.raise_() # animate the current page away self.__anim = QtCore.QPropertyAnimation(current_page, "pos") self.__anim.setDuration(anim_duration) self.__anim.setStartValue( QtCore.QPoint(current_page.x(), current_page.y())) self.__anim.setEndValue( QtCore.QPoint(current_page.x() - page_offset, current_page.y())) self.__anim.setEasingCurve(QtCore.QEasingCurve.OutCubic) # animate the new page in: self.__anim2 = QtCore.QPropertyAnimation(dst_page, "pos") self.__anim2.setDuration(anim_duration) self.__anim2.setStartValue( QtCore.QPoint(dst_page.x() + page_offset, dst_page.y())) self.__anim2.setEndValue(QtCore.QPoint(dst_page.x(), dst_page.y())) self.__anim2.setEasingCurve(QtCore.QEasingCurve.OutCubic) # create a parallel animation group so that both pages animate at # the same time: self.__page_anim_grp = QtCore.QParallelAnimationGroup() self.__page_anim_grp.addAnimation(self.__anim) self.__page_anim_grp.addAnimation(self.__anim2) # run the animation/transition self.__page_anim_grp.start()