RefactoringRope/RefactoringDialogBase.py

Wed, 01 Jan 2014 14:47:56 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 01 Jan 2014 14:47:56 +0100
changeset 62
1077db8d0589
parent 55
d501156be247
child 76
936b2a98fe4e
permissions
-rw-r--r--

Updated copyright for 2014.

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

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

"""
Module implementing the Refactoring dialog base class.
"""

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QDialog, QApplication

from E5Gui.E5Application import e5App


class RefactoringDialogBase(QDialog):
    """
    Class implementing the Refactoring dialog base class.
    """
    def __init__(self, refactoring, title, parent=None):
        """
        Constructor
        
        @param refactoring reference to the main refactoring object
            (Refactoring)
        @param title title of the dialog (string)
        @param parent reference to the parent widget (QWidget)
        """
        QDialog.__init__(self, parent)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setWindowTitle(title)
        
        self._refactoring = refactoring
        self._title = title
        self.__handle = None
    
    def __createProgressHandle(self, interruptable=True):
        """
        Private method to create a TaskHandle to update a progress dialog.
        
        @param interruptable flag indicating, that the task may be
            interrupted (boolean)
        """
        from ProgressHandle import ProgressHandle
        self.__handle = ProgressHandle(self._title, interruptable, self)
        self.__handle.show()
        QApplication.processEvents()
    
    def _calculateChanges(self, handle):
        """
        Protected method to calculate the changes.
        
        @param handle reference to the task handle
            (rope.base.taskhandle.TaskHandle)
        @ireturn reference to the ChangeSet object (rope.base.change.ChangeSet)
        @exception NotImplementedError raised to indicate that this method must
            be overridden by subclasses
        """
        raise NotImplementedError("_calculateChanges must be overridden.")
    
    def __getChanges(self):
        """
        Private method to build the Changes object.
        
        @return reference to the Changes object (rope.base.change.ChangeSet)
        """
        self.__createProgressHandle()
        changes = self._calculateChanges(self.__handle)
        self.__handle.reset()
        self.__handle = None
        
        return changes
    
    def previewChanges(self):
        """
        Public method to preview the changes.
        """
        changes = self.__getChanges()
        
        if changes is not None:
            from ChangesPreviewDialog import ChangesPreviewDialog
            dlg = ChangesPreviewDialog(changes, self)
            if dlg.exec_() == QDialog.Accepted:
                self.applyChanges(changes)
    
    def applyChanges(self, changes=None):
        """
        Public method to apply the changes.
        
        @param changes reference to the ChangeSet object
            (rope.base.change.ChangeSet)
        """
        if changes is None:
            changes = self.__getChanges()
        
        if changes is not None:
            self.__createProgressHandle(False)
            try:
                self._refactoring.getProject().do(changes, self.__handle)
            except Exception as err:
                self._refactoring.handleRopeError(
                    err, self._title, self.__handle)
            self.__handle.reset()
            self.__handle = None
            
            self._refactoring.refreshEditors(changes)
            p = e5App().getObject("Project")
            if p.isDirty():
                p.saveProject()
        
        self.accept()

eric ide

mercurial