Files
LorenEteval_Furious/Furious/Widget/ConnectProgressBar.py
T
2026-08-12 12:20:14 +08:00

136 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Copyright (C) 2024–present Loren Eteval & contributors <loren.eteval@proton.me>
#
# This file is part of Furious.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Provide widgets for connect progress bar."""
from __future__ import annotations
from Furious.Frozenlib import *
from Furious.Qt import *
from Furious.Qt import gettext as _
from PySide6 import QtCore
from PySide6.QtWidgets import *
__all__ = ['ConnectProgressBar']
class AutoUpdateProgressBar(Mixins.ConnectionAware, QProgressBar):
"""Represent auto update progress bar."""
def __init__(self, **kwargs):
"""Initialize the AutoUpdateProgressBar."""
super().__init__(**kwargs)
self.setRange(0, 100)
@QtCore.Slot()
def update():
# Update the progress bar value
"""Update the auto update progress bar."""
if self.value() < 90:
self.setValue(self.value() + 1)
# Stop the timer when the progress bar reaches 100%
if self.value() > 99:
self.timer.stop()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(update)
self.setStyleSheet(self.getStyleSheet())
def start(self, msec: int):
"""Start the auto update progress bar."""
self.timer.start(msec)
def stop(self):
"""Stop the auto update progress bar."""
self.timer.stop()
@staticmethod
def getStyleSheet():
"""Return style sheet."""
return (
f'QProgressBar {{'
f' border-radius: 2px;'
f' text-align: center;'
f'}}'
f''
f'QProgressBar::chunk {{'
f' background: qlineargradient('
f' x1: 0, y1: 0, x2: 1, y2: 0,'
f' stop: 0 {AppHue.disconnectedColor()},'
f' stop: 1 {AppHue.connectedColor()}'
f' );'
f' border-radius: 2px;'
f'}}'
)
def disconnectedCallback(self):
"""Update the auto update progress bar for a disconnected state."""
pass
def connectedCallback(self):
"""Update the auto update progress bar for a connected state."""
pass
class ConnectProgressBar(Mixins.QTranslatable, Mixins.ConnectionAware, QWidget):
"""Provide the connect progress bar widget."""
def __init__(self, parent=None):
"""Initialize the ConnectProgressBar."""
super().__init__(parent)
self.setWindowTitle(_(APPLICATION_NAME))
self.setWindowIcon(AppHue.currentWindowIcon())
self.setFixedSize(280, 61)
# Create a progress bar widget
self._widget = AutoUpdateProgressBar(parent=self)
self._widget.setRange(0, 100)
self._layout = QVBoxLayout()
self._layout.addWidget(self._widget)
self.setLayout(self._layout)
def setValue(self, value: int):
"""Set value."""
self._widget.setValue(value)
def start(self, msec: int):
"""Start the connect progress bar."""
self._widget.start(msec)
def stop(self):
"""Stop the connect progress bar."""
self._widget.stop()
def disconnectedCallback(self):
"""Update the connect progress bar for a disconnected state."""
self.setWindowIcon(AppHue.disconnectedWindowIcon())
def connectedCallback(self):
"""Update the connect progress bar for a connected state."""
self.setWindowIcon(AppHue.connectedWindowIcon())
def retranslate(self):
"""Refresh translated text for the connect progress bar."""
self.setWindowTitle(_(self.windowTitle()))