Exemplo n.º 1
0
class PlayButton(QWidget):
    def __init__(self, parent):
        super(PlayButton, self).__init__(parent)
        self.parent = parent
        self.main_window = parent.main_window

        self.setStyleSheet("background: rgb(244,84,67,100);" +
                           "border-radius: 10;" + "border-style: solid;" +
                           "border-width: 5px;" + "border-color: #232;")
        self.label = QLabel(self)
        self.transition = None
        self.setCursor(Qt.CursorShape.PointingHandCursor)

    def restyle(self, bg):
        r, g, b, a = bg.red(), bg.green(), bg.blue(), bg.alpha()
        self.setStyleSheet("background: rgb(" + str(r) + "," + str(g) + "," +
                           str(b) + "," + str(a) + ");" +
                           "border-radius: 10;" + "border-style: solid;" +
                           "border-width: 5px;" + "border-color: #232;")

    def mousePressEvent(self, event):
        self.main_window.start_game(self.parent.is_force_move(),
                                    self.parent.is_pc_move(),
                                    self.parent.get_difficulty())
        self.parent.save_remembered()

    def enterEvent(self, event):
        super(PlayButton, self).enterEvent(event)
        self.animate_hover(True)

    def leaveEvent(self, event):
        super(PlayButton, self).leaveEvent(event)
        self.animate_hover(False)

    def paintEvent(self, pe):
        o = QStyleOption()
        o.initFrom(self)
        p = QPainter(self)
        self.style().drawPrimitive(QStyle.PE_Widget, o, p, self)

    def set_text(self, text):
        self.label.setText(text)
        self.label.move(0, 0)
        self.label.setAlignment(Qt.AlignCenter)
        self.label.setStyleSheet("background: transparent; color:white;")
        self.label.resize(self.width(), self.height())

        font = self.label.font()
        font.setBold(True)
        font.setPointSize(14)
        self.label.setFont(font)

    def update_size(self):
        self.resize(self.width(), self.height())
        self.label.resize(self.width(), self.height())

    def animate_hover(self, enter):
        base_color = QColor(244, 84, 67, 100)
        hover_color = QColor(244, 84, 67, 255)

        start_value = base_color if enter else hover_color

        if self.transition:
            start_value = self.transition.currentValue()
            self.transition.stop()

        self.transition = QPropertyAnimation(self, b"palete")

        self.transition.setStartValue(start_value)
        self.transition.setEndValue(hover_color if enter else base_color)
        self.transition.setDuration(100)
        self.transition.setDirection(QPropertyAnimation.Forward)
        self.transition.valueChanged.connect(
            lambda: self.restyle(self.transition.currentValue()))

        self.transition.start()
Exemplo n.º 2
0
class ProgressComponent(QGroupBox):
    def __init__(self):
        super().__init__()

        self.setTitle("Progression")

        self._max_value = 1
        self._taskbar_button = None

        self._current_image_label = QLabel()
        self._time_label = QLabel()
        self._next_image_label = QLabel()

        info_layout = QHBoxLayout()
        info_layout.addWidget(self._current_image_label)
        info_layout.addWidget(self._time_label)
        info_layout.addWidget(self._next_image_label)

        self._progress_bar = QProgressBar()
        self._progress_bar_animation = QPropertyAnimation(
            self._progress_bar, b"value")
        self._progress_bar_animation.setDuration(500)

        main_layout = QVBoxLayout()
        main_layout.setSpacing(15)
        main_layout.addLayout(info_layout)
        main_layout.addWidget(self._progress_bar)

        self.setLayout(main_layout)

    def reset(self, max_value: int):
        self._max_value = max_value
        self._progress_bar.setValue(0)
        self._progress_bar.setMaximum(10 * max_value)

        self._current_image_label.setText("Image : 0/%d" % max_value)
        self._time_label.setText("Temps restant : ")
        self._next_image_label.setText("Prochaine image : ")

        if _QT_WIN_EXTRAS_LOADED:
            window = self.window().windowHandle()
            if window is None:
                self._taskbar_button = None
            else:
                self._taskbar_button = QWinTaskbarButton(self)
                self._taskbar_button.setWindow(window)
                self._taskbar_button.progress().setVisible(True)
                self._taskbar_button.progress().setRange(0, self._max_value)

    def stop(self):
        if self._taskbar_button is not None:
            self._taskbar_button.progress().setVisible(False)

    def update(self, next_image: str, value: int, time_left: QTime):
        self._progress_bar_animation.stop()

        start_value = self._progress_bar.value()
        if self._progress_bar_animation.currentValue():
            if self._progress_bar_animation.currentValue(
            ) < self._progress_bar.value():
                start_value = self._progress_bar_animation.currentValue()

        self._progress_bar_animation.setStartValue(start_value)
        self._progress_bar_animation.setEndValue(value * 10)
        self._progress_bar_animation.start()

        self._current_image_label.setText("Image : " + str(value) + "/" +
                                          str(self._max_value))

        time_left_str = ""
        if time_left is None:
            time_left_str = "Estimation..."
        else:
            time_left_str = "%dh %dm %ds" % (
                time_left.hour(), time_left.minute(), time_left.second())

        self._time_label.setText("Temps restant : " + time_left_str)
        self._next_image_label.setText("Prochaine image : " + next_image)

        if self._taskbar_button is not None:
            self._taskbar_button.progress().setValue(value)