From 4e8eab3d01f65a3203ac52a830825d17c3d02e79 Mon Sep 17 00:00:00 2001 From: alireza-amirsamimi Date: Tue, 24 Sep 2024 01:08:41 +0330 Subject: [PATCH] Progress bar is updated more smoothly. Issue #1019 Changes to be committed: modified: persepolis/gui/progress_ui.py modified: persepolis/gui/video_finder_progress_ui.py modified: persepolis/scripts/mainwindow.py modified: persepolis/scripts/progress.py modified: persepolis/scripts/video_finder_progress.py --- persepolis/gui/progress_ui.py | 56 +++++++++++++++++++-- persepolis/gui/video_finder_progress_ui.py | 4 +- persepolis/scripts/mainwindow.py | 8 ++- persepolis/scripts/progress.py | 2 +- persepolis/scripts/video_finder_progress.py | 2 +- 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/persepolis/gui/progress_ui.py b/persepolis/gui/progress_ui.py index ce81b067..de41b862 100644 --- a/persepolis/gui/progress_ui.py +++ b/persepolis/gui/progress_ui.py @@ -17,18 +17,65 @@ try: from PySide6.QtWidgets import QDial, QCheckBox, QProgressBar, QFrame, QComboBox, QWidget, QTabWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel - from PySide6.QtCore import Qt, QTranslator, QCoreApplication, QLocale, QSize + from PySide6.QtCore import Qt, QTranslator, QCoreApplication, QLocale, QSize, QThread, Signal from PySide6.QtGui import QIcon except: from PyQt5.QtWidgets import QDial, QCheckBox, QProgressBar, QFrame, QComboBox, QWidget, QTabWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel - from PyQt5.QtCore import Qt, QTranslator, QCoreApplication, QLocale, QSize + from PyQt5.QtCore import Qt, QTranslator, QCoreApplication, QLocale, QSize, QThread + from PyQt5.QtCore import pyqtSignal as Signal from PyQt5.QtGui import QIcon from persepolis.gui import resources +import time + + +class UpdateProgressBarThread(QThread): + UPDATEPROGRESSBAR = Signal(int) + + def __init__(self, parent, new_value): + QThread.__init__(self) + self.new_value = new_value + self.parent = parent + + def run(self): + dif = self.new_value - self.parent.value() + while self.parent.value() != self.new_value and not (self.parent.finished): + if dif < 0: + self.UPDATEPROGRESSBAR.emit((self.parent.value() - 1)) + + time.sleep(0.5 / (dif * -1)) + elif dif > 0: + self.UPDATEPROGRESSBAR.emit((self.parent.value() + 1)) + time.sleep(0.5 / dif) + + self.parent.update_lock = False + + +# This widget updated more smoothly than QProgressBar +class MyProgressBar(QProgressBar): + def __init__(self, main_window): + super().__init__() + self.setValue(0) + self.setMaximum(100) + self.update_lock = False + self.main_window = main_window + self.finished = False + + def setValueSmoothly(self, new_value): + if self.update_lock: + return + self.update_lock = True + update_progress_bar = UpdateProgressBarThread(self, new_value) + update_progress_bar.UPDATEPROGRESSBAR.connect(self.setValueSmoothly2) + self.main_window.threadPool.append(update_progress_bar) + self.main_window.threadPool[-1].start() + + def setValueSmoothly2(self, value): + self.setValue(value) class ProgressWindow_Ui(QWidget): - def __init__(self, persepolis_setting): + def __init__(self, persepolis_setting, parent): super().__init__() self.persepolis_setting = persepolis_setting icons = ':/' + str(persepolis_setting.value('settings/icons')) + '/' @@ -163,9 +210,8 @@ def __init__(self, persepolis_setting): verticalLayout.addWidget(self.progress_tabWidget) - # download_progressBar - self.download_progressBar = QProgressBar(self) + self.download_progressBar = MyProgressBar(parent) verticalLayout.addWidget(self.download_progressBar) self.download_progressBar.setTextVisible(True) # changing the alignment of progress bar diff --git a/persepolis/gui/video_finder_progress_ui.py b/persepolis/gui/video_finder_progress_ui.py index 1686a836..0ad9e929 100644 --- a/persepolis/gui/video_finder_progress_ui.py +++ b/persepolis/gui/video_finder_progress_ui.py @@ -25,8 +25,8 @@ class VideoFinderProgressWindow_Ui(ProgressWindow_Ui): - def __init__(self, persepolis_setting): - super().__init__(persepolis_setting) + def __init__(self, persepolis_setting, parent): + super().__init__(persepolis_setting, parent) # status_tab self.status_tab = QWidget() diff --git a/persepolis/scripts/mainwindow.py b/persepolis/scripts/mainwindow.py index 4e394199..c23e7a55 100644 --- a/persepolis/scripts/mainwindow.py +++ b/persepolis/scripts/mainwindow.py @@ -1917,7 +1917,7 @@ def checkDownloadInfo(self, list): value = int(value[:-1]) except: value = 0 - progress_window.download_progressBar.setValue(value) + progress_window.download_progressBar.setValueSmoothly(value) # status progress_window.status = str(download_status_dict['status']) @@ -5416,7 +5416,11 @@ def addLinkSpiderCallBack(self, spider_dict, child): if file_size: file_size = 'Size: ' + str(file_size) - child.size_label.setText(file_size) + if child.size_label.text() == 'None' or child.size_label.text() == '': + child.size_label.setText(file_size) + else: + # It's updated before! dont change it. + return if file_name and not (child.change_name_checkBox.isChecked()): child.change_name_lineEdit.setText(file_name) diff --git a/persepolis/scripts/progress.py b/persepolis/scripts/progress.py index ef6b6b47..8f4c4ab7 100644 --- a/persepolis/scripts/progress.py +++ b/persepolis/scripts/progress.py @@ -45,7 +45,7 @@ def run(self): class ProgressWindow(ProgressWindow_Ui): def __init__(self, parent, gid, persepolis_setting): - super().__init__(persepolis_setting) + super().__init__(persepolis_setting, parent) self.persepolis_setting = persepolis_setting self.main_window = parent self.gid = gid diff --git a/persepolis/scripts/video_finder_progress.py b/persepolis/scripts/video_finder_progress.py index 4e3150fb..ee54c148 100644 --- a/persepolis/scripts/video_finder_progress.py +++ b/persepolis/scripts/video_finder_progress.py @@ -44,7 +44,7 @@ def run(self): class VideoFinderProgressWindow(VideoFinderProgressWindow_Ui): def __init__(self, parent, gid_list, persepolis_setting): - super().__init__(persepolis_setting) + super().__init__(persepolis_setting, parent) self.persepolis_setting = persepolis_setting self.main_window = parent