RefactoringRope/InlineArgumentDefaultDialog.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 the Inline Argument Default dialog.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5.QtWidgets import QDialogButtonBox, QListWidgetItem, QAbstractButton

from Ui_InlineArgumentDefaultDialog import Ui_InlineArgumentDefaultDialog
from RefactoringDialogBase import RefactoringDialogBase


class InlineArgumentDefaultDialog(RefactoringDialogBase,
                                  Ui_InlineArgumentDefaultDialog):
    """
    Class implementing the Inline Argument Default dialog.
    """
    NameRole = Qt.UserRole
    
    def __init__(self, refactoring, title, changer, parent=None):
        """
        Constructor
        
        @param refactoring reference to the main refactoring object
            (Refactoring)
        @param title title of the dialog (string)
        @param changer reference to the signature changer object
            (rope.refactor.change_signature.ChangeSignature)
        @param parent reference to the parent widget (QWidget)
        """
        RefactoringDialogBase.__init__(self, refactoring, title, parent)
        self.setupUi(self)
        
        self.__signature = changer
        self.__definition_info = self.__signature.get_args()
        
        self.__okButton = self.buttonBox.button(QDialogButtonBox.Ok)
        self.__okButton.setEnabled(False)
        self.__previewButton = self.buttonBox.addButton(
            self.tr("Preview"), QDialogButtonBox.ActionRole)
        self.__previewButton.setDefault(True)
        self.__previewButton.setEnabled(False)
        
        # populate the parameters list
        for arg, default in self.__definition_info:
            if default is not None:
                itm = QListWidgetItem("{0} = {1}".format(arg, default),
                                      self.parameterList)
                itm.setData(InlineArgumentDefaultDialog.NameRole, arg)
    
    @pyqtSlot()
    def on_parameterList_itemSelectionChanged(self):
        """
        Private slot called, when the selection changes.
        """
        self.__okButton.setEnabled(
            len(self.parameterList.selectedItems()) > 0)
        self.__previewButton.setEnabled(
            len(self.parameterList.selectedItems()) > 0)
    
    @pyqtSlot(QAbstractButton)
    def on_buttonBox_clicked(self, button):
        """
        Private slot to act on the button pressed.
        
        @param button reference to the button pressed (QAbstractButton)
        """
        if button == self.__previewButton:
            self.previewChanges()
        elif button == self.__okButton:
            self.applyChanges()
    
    def __getParameterIndex(self, definition_info, name):
        """
        Private method to calculate the index of the given paramter.
        
        @param definition_info object containing the method definition
        @param name parameter name (string)
        @return index of the parameter (integer)
        """
        for index, pair in enumerate(definition_info):
            if pair[0] == name:
                return index

    def _calculateChanges(self, handle):
        """
        Protected method to calculate the changes.
        
        @param handle reference to the task handle
            (rope.base.taskhandle.TaskHandle)
        @return reference to the Changes object (rope.base.change.ChangeSet)
        """
        items = self.parameterList.selectedItems()
        if len(items) > 0:
            import rope.refactor.change_signature
            
            itm = items[0]
            name = itm.data(InlineArgumentDefaultDialog.NameRole)
            index = self.__getParameterIndex(self.__definition_info, name)
            try:
                inliner = \
                    rope.refactor.change_signature.ArgumentDefaultInliner(
                        index)
                changes = self.__signature.get_changes(
                    [inliner], task_handle=handle)
                return changes
            except Exception as err:
                self._refactoring.handleRopeError(err, self._title, handle)
                return None

eric ide

mercurial