RefactoringRope/ProgressHandle.py

Sun, 06 Jul 2014 14:17:24 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 06 Jul 2014 14:17:24 +0200
branch
eric5
changeset 86
3339c75dc777
parent 76
936b2a98fe4e
child 88
e71619898d0f
child 94
03d6a17c66ac
permissions
-rw-r--r--

Ported to PyQt5 and eric6.

# -*- coding: utf-8 -*-

# Copyright (c) 2010 - 2014 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a TaskHandle class with a progress dialog.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import QCoreApplication
from PyQt5.QtWidgets import QApplication, QLabel

import rope.base.taskhandle


class ProgressHandle(rope.base.taskhandle.TaskHandle):
    """
    Class implementing TaskHandle with a progress dialog.
    """
    def __init__(self, title, interruptable=True, parent=None):
        """
        Constructor
        
        @param title title for the taskhandle (string)
        @param interruptable flag indicating, that the task may be
            interrupted (boolean)
        @param parent reference to the parent widget (QWidget)
        """
        rope.base.taskhandle.TaskHandle.__init__(self, title, interruptable)
        if interruptable:
            cancelLabel = QCoreApplication.translate(
                "ProgressHandle", "Interrupt")
        else:
            cancelLabel = ""
        barFormat = QCoreApplication.translate("ProgressHandle", "%v/%m files")
        try:
            from E5Gui.E5ProgressDialog import E5ProgressDialog
            self.__progress = E5ProgressDialog("", cancelLabel, 0, 1,
                                               barFormat, parent)
        except ImportError:
            from PyQt5.QtWidgets import QProgressDialog, QProgressBar
            self.__progress = QProgressDialog("", cancelLabel, 0, 1, parent)
            bar = QProgressBar(self.__progress)
            bar.setFormat(barFormat)
            self.__progress.setBar(bar)
        label = QLabel("")
        label.setWordWrap(True)
        self.__progress.setLabel(label)
        self.__progress.setWindowTitle(
            QCoreApplication.translate("ProgressHandle", "eric6 - {0}")
            .format(title))
        self.add_observer(self.__updateProgress)
    
    def __updateProgress(self):
        """
        Private slot to handle the task progress.
        """
        jobset = self.current_jobset()
        if jobset:
            text = ''
            if jobset.get_name() is not None:
                text += jobset.get_name()
            if jobset.get_active_job_name() is not None:
                text += ': ' + jobset.get_active_job_name()
            self.__progress.setLabelText(text)
            if jobset.count is not None:
                self.__progress.setMaximum(jobset.count)
                self.__progress.setValue(jobset.done)
        QApplication.processEvents()
        if not self.is_stopped() and self.__progress.wasCanceled():
            self.stop()
    
    def show(self):
        """
        Public slot to show the progress dialog.
        """
        self.__progress.show()
    
    def reset(self):
        """
        Public slot to reset the progress dialog.
        """
        self.__progress.reset()

eric ide

mercurial