RefactoringRope/GetterSetterDialog.py

branch
server_client_variant
changeset 187
c7600eee9047
parent 147
3f8a995f6e49
child 189
2711fdd91925
equal deleted inserted replaced
186:d547caa795d7 187:c7600eee9047
2 2
3 # Copyright (c) 2010 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> 3 # Copyright (c) 2010 - 2017 Detlev Offenbach <detlev@die-offenbachs.de>
4 # 4 #
5 5
6 """ 6 """
7 Module implementing a dialog to create getter and setter method names. 7 Module implementing the encapsulate attribute dialog.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 from PyQt5.QtCore import pyqtSlot 12 from PyQt5.QtCore import pyqtSlot
13 from PyQt5.QtWidgets import QDialog 13 from PyQt5.QtWidgets import QDialogButtonBox, QAbstractButton
14 14
15 from Ui_GetterSetterDialog import Ui_GetterSetterDialog 15 from Ui_GetterSetterDialog import Ui_GetterSetterDialog
16 from RefactoringDialogBase import RefactoringDialogBase
16 17
17 18
18 class GetterSetterDialog(QDialog, Ui_GetterSetterDialog): 19 class GetterSetterDialog(RefactoringDialogBase, Ui_GetterSetterDialog):
19 """ 20 """
20 Class implementing a dialog to create getter and setter method names. 21 Class implementing the encapsulate attribute dialog.
21 """ 22 """
22 def __init__(self, fieldName, parent=None): 23 def __init__(self, refactoring, title, filename, offset, parent=None):
23 """ 24 """
24 Constructor 25 Constructor
25 26
26 @param fieldName name of the field to create getter and setter 27 @param refactoring reference to the main refactoring object
27 method names (string) 28 @type Refactoring
28 @param parent parent widget of the dialog (QWidget) 29 @param title title of the dialog
30 @type str
31 @param filename file name to be worked on
32 @type str
33 @param offset offset within file
34 @type int or None
35 @param parent reference to the parent widget
36 @type QWidget
29 """ 37 """
30 QDialog.__init__(self, parent) 38 RefactoringDialogBase.__init__(self, refactoring, title, parent)
31 self.setupUi(self) 39 self.setupUi(self)
32 40
33 self.__fieldName = fieldName 41 self._changeGroupName = "GetterSetter"
42
43 self.__filename = filename
44 self.__offset = offset
45
46 self.__okButton = self.buttonBox.button(QDialogButtonBox.Ok)
47 self.__okButton.setEnabled(False)
48 self.__previewButton = self.buttonBox.addButton(
49 self.tr("Preview"), QDialogButtonBox.ActionRole)
50 self.__previewButton.setDefault(True)
51 self.__previewButton.setEnabled(False)
52
53 self.__fieldName = ""
54
55 self._refactoring.sendJson("RequestFieldName", {
56 "ChangeGroup": self._changeGroupName,
57 "Title": self._title,
58 "FileName": self.__filename,
59 "Offset": self.__offset,
60 })
61
62 def __processFieldName(self, data):
63 """
64 Private method to process the field name data sent by the refactoring
65 client in order to polish the dialog.
66
67 @param data dictionary containing the inline type data
68 @type dict
69 """
70 self.__fieldName = data["Name"]
34 71
35 self.on_typeCheckBox_toggled(False) 72 self.on_typeCheckBox_toggled(False)
36 73
37 msh = self.minimumSizeHint() 74 msh = self.minimumSizeHint()
38 self.resize(max(self.width(), msh.width()), msh.height()) 75 self.resize(max(self.width(), msh.width()), msh.height())
39 76
77 def __updateUI(self):
78 """
79 Private slot to update the UI.
80 """
81 enable = bool(self.getterEdit.text()) and bool(self.setterEdit.text())
82
83 self.__okButton.setEnabled(enable)
84 self.__previewButton.setEnabled(enable)
85
86 @pyqtSlot(str)
87 def on_getterEdit_textChanged(self, text):
88 """
89 Private slot to react to changes of the getter method.
90
91 @param text text entered into the edit
92 @type str
93 """
94 self.__updateUI()
95
96 @pyqtSlot(str)
97 def on_setterEdit_textChanged(self, text):
98 """
99 Private slot to react to changes of the setter method.
100
101 @param text text entered into the edit
102 @type str
103 """
104 self.__updateUI()
105
40 @pyqtSlot(bool) 106 @pyqtSlot(bool)
41 def on_typeCheckBox_toggled(self, checked): 107 def on_typeCheckBox_toggled(self, checked):
42 """ 108 """
43 Private slot to react to changes of the type checkbox. 109 Private slot to react to changes of the type checkbox.
44 110
45 @param checked state of the checkbox (boolean) 111 @param checked state of the checkbox
112 @type bool
46 """ 113 """
47 if checked: 114 if checked:
48 self.getterEdit.setText("get_{0}".format(self.__fieldName)) 115 self.getterEdit.setText("get_{0}".format(self.__fieldName))
49 self.setterEdit.setText("set_{0}".format(self.__fieldName)) 116 self.setterEdit.setText("set_{0}".format(self.__fieldName))
50 else: 117 else:
51 self.getterEdit.setText( 118 self.getterEdit.setText(
52 "get{0}{1}".format(self.__fieldName[0].upper(), 119 "get{0}".format(self.__fieldName.capitalize()))
53 self.__fieldName[1:]))
54 self.setterEdit.setText( 120 self.setterEdit.setText(
55 "set{0}{1}".format(self.__fieldName[0].upper(), 121 "set{0}".format(self.__fieldName.capitalize()))
56 self.__fieldName[1:]))
57 122
58 def getData(self): 123 @pyqtSlot(QAbstractButton)
124 def on_buttonBox_clicked(self, button):
59 """ 125 """
60 Public method to return the getter and setter method names. 126 Private slot to act on the button pressed.
61 127
62 @return tuple of two strings with getter and setter method names 128 @param button reference to the button pressed
129 @type QAbstractButton
63 """ 130 """
64 return (self.getterEdit.text(), self.setterEdit.text()) 131 if button == self.__previewButton:
132 self.requestPreview()
133 elif button == self.__okButton:
134 self.applyChanges()
135
136 def _calculateChanges(self):
137 """
138 Protected method to initiate the calculation of the changes.
139 """
140 self._refactoring.sendJson("CalculateEncapsulateFieldChanges", {
141 "ChangeGroup": self._changeGroupName,
142 "Title": self._title,
143 "FileName": self.__filename,
144 "Offset": self.__offset,
145 "Getter": self.getterEdit.text(),
146 "Setter": self.setterEdit.text(),
147 })
148
149 def processChangeData(self, data):
150 """
151 Public method to process the change data sent by the refactoring
152 client.
153
154 @param data dictionary containing the change data
155 @type dict
156 """
157 subcommand = data["Subcommand"]
158 if subcommand == "FieldName":
159 self.__processFieldName(data)
160 else:
161 # pass on to base class
162 RefactoringDialogBase.processChangeData(self, data)

eric ide

mercurial