Implement new spinner for metadata download dialog

This commit is contained in:
Kovid Goyal 2020-12-04 22:35:33 +05:30
parent 261f153fe6
commit 79dc721611
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 37 additions and 20 deletions

View File

@ -29,7 +29,7 @@ from calibre.ebooks.metadata.sources.identify import urls_from_identifiers
from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.metadata.book.base import Metadata
from calibre.ebooks.metadata.opf2 import OPF from calibre.ebooks.metadata.opf2 import OPF
from calibre.gui2 import error_dialog, rating_font, gprefs from calibre.gui2 import error_dialog, rating_font, gprefs
from calibre.gui2.progress_indicator import draw_snake_spinner from calibre.gui2.progress_indicator import SpinAnimator
from calibre.gui2.widgets2 import HTMLDisplay from calibre.gui2.widgets2 import HTMLDisplay
from calibre.utils.date import (utcnow, fromordinal, format_date, from calibre.utils.date import (utcnow, fromordinal, format_date,
UNDEFINED_DATE, as_utc) UNDEFINED_DATE, as_utc)
@ -88,33 +88,25 @@ class CoverDelegate(QStyledItemDelegate): # {{{
def __init__(self, parent): def __init__(self, parent):
QStyledItemDelegate.__init__(self, parent) QStyledItemDelegate.__init__(self, parent)
self.animator = SpinAnimator(self)
self.angle = 0 self.animator.updated.connect(self.needs_redraw)
self.timer = QTimer(self) self.color = parent.palette().color(QPalette.ColorRole.WindowText)
self.timer.timeout.connect(self.frame_changed)
self.dark_color = parent.palette().color(QPalette.ColorRole.WindowText)
self.light_color = parent.palette().color(QPalette.ColorRole.Window)
self.spinner_width = 64 self.spinner_width = 64
def frame_changed(self, *args):
self.angle = (self.angle-2)%360
self.needs_redraw.emit()
def start_animation(self): def start_animation(self):
self.angle = 0 self.animator.start()
self.timer.start(10)
def stop_animation(self): def stop_animation(self):
self.timer.stop() self.animator.stop()
def paint(self, painter, option, index): def paint(self, painter, option, index):
QStyledItemDelegate.paint(self, painter, option, index) QStyledItemDelegate.paint(self, painter, option, index)
style = QApplication.style() style = QApplication.style()
waiting = self.timer.isActive() and bool(index.data(Qt.ItemDataRole.UserRole)) waiting = self.animator.is_running() and bool(index.data(Qt.ItemDataRole.UserRole))
if waiting: if waiting:
rect = QRect(0, 0, self.spinner_width, self.spinner_width) rect = QRect(0, 0, self.spinner_width, self.spinner_width)
rect.moveCenter(option.rect.center()) rect.moveCenter(option.rect.center())
draw_snake_spinner(painter, rect, self.angle, self.light_color, self.dark_color) self.animator.draw(painter, rect, self.color)
else: else:
# Ensure the cover is rendered over any selection rect # Ensure the cover is rendered over any selection rect
style.drawItemPixmap(painter, option.rect, Qt.AlignmentFlag.AlignTop|Qt.AlignmentFlag.AlignHCenter, style.drawItemPixmap(painter, option.rect, Qt.AlignmentFlag.AlignTop|Qt.AlignmentFlag.AlignHCenter,

View File

@ -19,7 +19,7 @@ class SpinAnimator : QObject {
%End %End
public: public:
SpinAnimator(QWidget *parent /TransferThis/ = 0, int speed_factor=300); SpinAnimator(QObject *parent /TransferThis/ = 0, int speed_factor=300);
float get_arc_length() const ; float get_arc_length() const ;
int get_arc_rotation() const ; int get_arc_rotation() const ;
int get_overall_rotation() const ; int get_overall_rotation() const ;

View File

@ -3,8 +3,8 @@
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
from PyQt5.Qt import ( from PyQt5.Qt import (
QDialog, QLabel, QSizePolicy, QStackedLayout, QStackedWidget, Qt, QVBoxLayout, QDialog, QLabel, QObject, QSizePolicy, QStackedLayout, QStackedWidget, Qt,
QWidget QVBoxLayout, QWidget, pyqtSignal
) )
from calibre_extensions.progress_indicator import ( from calibre_extensions.progress_indicator import (
@ -13,6 +13,30 @@ from calibre_extensions.progress_indicator import (
draw_snake_spinner draw_snake_spinner
try:
from calibre_extensions.progress_indicator import SpinAnimator
except ImportError:
# dummy class for people running from source without updated binaries
class SpinAnimator(QObject):
updated = pyqtSignal()
def __init__(self, parent):
QObject.__init__(self, parent)
self.running = False
def draw(self, *a):
pass
def start(self):
self.running = True
def stop(self):
self.running = False
def is_running(self):
return self.running
class WaitPanel(QWidget): class WaitPanel(QWidget):
@ -95,7 +119,8 @@ class WaitLayout(QStackedLayout):
def develop(): def develop():
from PyQt5.Qt import QPalette, QPainter from PyQt5.Qt import QPainter, QPalette
from calibre.gui2 import Application from calibre.gui2 import Application
from calibre_extensions.progress_indicator import SpinAnimator from calibre_extensions.progress_indicator import SpinAnimator