src/eric7/Plugins/PluginWizardQFileDialog.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
36 36
37 class FileDialogWizard(QObject): 37 class FileDialogWizard(QObject):
38 """ 38 """
39 Class implementing the QFileDialog wizard plugin. 39 Class implementing the QFileDialog wizard plugin.
40 """ 40 """
41
41 def __init__(self, ui): 42 def __init__(self, ui):
42 """ 43 """
43 Constructor 44 Constructor
44 45
45 @param ui reference to the user interface object (UI.UserInterface) 46 @param ui reference to the user interface object (UI.UserInterface)
46 """ 47 """
47 super().__init__(ui) 48 super().__init__(ui)
48 self.__ui = ui 49 self.__ui = ui
49 50
50 # PyQt5/PyQt6 51 # PyQt5/PyQt6
51 self.__pyqtRe = re.compile(r"(?:import|from)\s+PyQt([56])") 52 self.__pyqtRe = re.compile(r"(?:import|from)\s+PyQt([56])")
52 53
53 def activate(self): 54 def activate(self):
54 """ 55 """
55 Public method to activate this plugin. 56 Public method to activate this plugin.
56 57
57 @return tuple of None and activation status (boolean) 58 @return tuple of None and activation status (boolean)
58 """ 59 """
59 self.__initActions() 60 self.__initActions()
60 self.__initMenu() 61 self.__initMenu()
61 62
62 return None, True 63 return None, True
63 64
64 def deactivate(self): 65 def deactivate(self):
65 """ 66 """
66 Public method to deactivate this plugin. 67 Public method to deactivate this plugin.
68 menu = self.__ui.getMenu("wizards") 69 menu = self.__ui.getMenu("wizards")
69 if menu: 70 if menu:
70 menu.removeAction(self.qFileDialogAction) 71 menu.removeAction(self.qFileDialogAction)
71 menu.removeAction(self.ericFileDialogAction) 72 menu.removeAction(self.ericFileDialogAction)
72 self.__ui.removeEricActions( 73 self.__ui.removeEricActions(
73 [self.qFileDialogAction, self.ericFileDialogAction], 74 [self.qFileDialogAction, self.ericFileDialogAction], "wizards"
74 'wizards') 75 )
75 76
76 def __initActions(self): 77 def __initActions(self):
77 """ 78 """
78 Private method to initialize the actions. 79 Private method to initialize the actions.
79 """ 80 """
80 self.qFileDialogAction = EricAction( 81 self.qFileDialogAction = EricAction(
81 self.tr('QFileDialog Wizard'), 82 self.tr("QFileDialog Wizard"),
82 self.tr('Q&FileDialog Wizard...'), 0, 0, self, 83 self.tr("Q&FileDialog Wizard..."),
83 'wizards_qfiledialog') 84 0,
84 self.qFileDialogAction.setStatusTip(self.tr('QFileDialog Wizard')) 85 0,
85 self.qFileDialogAction.setWhatsThis(self.tr( 86 self,
86 """<b>QFileDialog Wizard</b>""" 87 "wizards_qfiledialog",
87 """<p>This wizard opens a dialog for entering all the parameters""" 88 )
88 """ needed to create a QFileDialog. The generated code is""" 89 self.qFileDialogAction.setStatusTip(self.tr("QFileDialog Wizard"))
89 """ inserted at the current cursor position.</p>""" 90 self.qFileDialogAction.setWhatsThis(
90 )) 91 self.tr(
92 """<b>QFileDialog Wizard</b>"""
93 """<p>This wizard opens a dialog for entering all the parameters"""
94 """ needed to create a QFileDialog. The generated code is"""
95 """ inserted at the current cursor position.</p>"""
96 )
97 )
91 self.qFileDialogAction.triggered.connect(self.__handleQFileDialog) 98 self.qFileDialogAction.triggered.connect(self.__handleQFileDialog)
92 99
93 self.ericFileDialogAction = EricAction( 100 self.ericFileDialogAction = EricAction(
94 self.tr('EricFileDialog Wizard'), 101 self.tr("EricFileDialog Wizard"),
95 self.tr('EricFileDialog Wizard...'), 0, 0, self, 102 self.tr("EricFileDialog Wizard..."),
96 'wizards_e5filedialog') 103 0,
97 self.ericFileDialogAction.setStatusTip(self.tr( 104 0,
98 'EricFileDialog Wizard')) 105 self,
99 self.ericFileDialogAction.setWhatsThis(self.tr( 106 "wizards_e5filedialog",
100 """<b>EricFileDialog Wizard</b>""" 107 )
101 """<p>This wizard opens a dialog for entering all the parameters""" 108 self.ericFileDialogAction.setStatusTip(self.tr("EricFileDialog Wizard"))
102 """ needed to create an EricFileDialog. The generated code is""" 109 self.ericFileDialogAction.setWhatsThis(
103 """ inserted at the current cursor position.</p>""" 110 self.tr(
104 )) 111 """<b>EricFileDialog Wizard</b>"""
105 self.ericFileDialogAction.triggered.connect( 112 """<p>This wizard opens a dialog for entering all the parameters"""
106 self.__handleEricFileDialog) 113 """ needed to create an EricFileDialog. The generated code is"""
107 114 """ inserted at the current cursor position.</p>"""
115 )
116 )
117 self.ericFileDialogAction.triggered.connect(self.__handleEricFileDialog)
118
108 self.__ui.addEricActions( 119 self.__ui.addEricActions(
109 [self.qFileDialogAction, self.ericFileDialogAction], 120 [self.qFileDialogAction, self.ericFileDialogAction], "wizards"
110 'wizards') 121 )
111 122
112 def __initMenu(self): 123 def __initMenu(self):
113 """ 124 """
114 Private method to add the actions to the right menu. 125 Private method to add the actions to the right menu.
115 """ 126 """
116 menu = self.__ui.getMenu("wizards") 127 menu = self.__ui.getMenu("wizards")
117 if menu: 128 if menu:
118 menu.addAction(self.ericFileDialogAction) 129 menu.addAction(self.ericFileDialogAction)
119 menu.addAction(self.qFileDialogAction) 130 menu.addAction(self.qFileDialogAction)
120 131
121 def __callForm(self, editor, variant): 132 def __callForm(self, editor, variant):
122 """ 133 """
123 Private method to display a dialog and get the code. 134 Private method to display a dialog and get the code.
124 135
125 @param editor reference to the current editor 136 @param editor reference to the current editor
126 @type Editor 137 @type Editor
127 @param variant variant of code to be generated 138 @param variant variant of code to be generated
128 (-1 = EricFileDialog, 0 = unknown, 5 = PyQt5, 6 = PyQt6) 139 (-1 = EricFileDialog, 0 = unknown, 5 = PyQt5, 6 = PyQt6)
129 @type int 140 @type int
130 @return the generated code (string) 141 @return the generated code (string)
131 """ 142 """
132 from WizardPlugins.FileDialogWizard.FileDialogWizardDialog import ( 143 from WizardPlugins.FileDialogWizard.FileDialogWizardDialog import (
133 FileDialogWizardDialog 144 FileDialogWizardDialog,
134 ) 145 )
146
135 dlg = FileDialogWizardDialog(variant, None) 147 dlg = FileDialogWizardDialog(variant, None)
136 if dlg.exec() == QDialog.DialogCode.Accepted: 148 if dlg.exec() == QDialog.DialogCode.Accepted:
137 line, index = editor.getCursorPosition() 149 line, index = editor.getCursorPosition()
138 indLevel = editor.indentation(line) // editor.indentationWidth() 150 indLevel = editor.indentation(line) // editor.indentationWidth()
139 if editor.indentationsUseTabs(): 151 if editor.indentationsUseTabs():
140 indString = '\t' 152 indString = "\t"
141 else: 153 else:
142 indString = editor.indentationWidth() * ' ' 154 indString = editor.indentationWidth() * " "
143 return (dlg.getCode(indLevel, indString), 1) 155 return (dlg.getCode(indLevel, indString), 1)
144 else: 156 else:
145 return (None, 0) 157 return (None, 0)
146 158
147 def __handle(self, variant): 159 def __handle(self, variant):
148 """ 160 """
149 Private method to handle the wizards action. 161 Private method to handle the wizards action.
150 162
151 @param variant dialog variant to be generated 163 @param variant dialog variant to be generated
152 (EricFileDialog or QFileDialog) 164 (EricFileDialog or QFileDialog)
153 @type str 165 @type str
154 @exception ValueError raised to indicate an illegal file dialog variant 166 @exception ValueError raised to indicate an illegal file dialog variant
155 """ 167 """
156 editor = ericApp().getObject("ViewManager").activeWindow() 168 editor = ericApp().getObject("ViewManager").activeWindow()
157 169
158 if editor is None: 170 if editor is None:
159 EricMessageBox.critical( 171 EricMessageBox.critical(
160 self.__ui, 172 self.__ui,
161 self.tr('No current editor'), 173 self.tr("No current editor"),
162 self.tr('Please open or create a file first.')) 174 self.tr("Please open or create a file first."),
175 )
163 else: 176 else:
164 if variant not in ("QFileDialog", "EricFileDialog"): 177 if variant not in ("QFileDialog", "EricFileDialog"):
165 raise ValueError("Illegal dialog variant given") 178 raise ValueError("Illegal dialog variant given")
166 179
167 if variant == "QFileDialog": 180 if variant == "QFileDialog":
168 match = self.__pyqtRe.search(editor.text()) 181 match = self.__pyqtRe.search(editor.text())
169 if match is None: 182 if match is None:
170 # unknown 183 # unknown
171 dialogVariant = 0 184 dialogVariant = 0
173 # PyQt5/PyQt6 186 # PyQt5/PyQt6
174 dialogVariant = int(match.group(1)) 187 dialogVariant = int(match.group(1))
175 else: 188 else:
176 # EricFileDialog 189 # EricFileDialog
177 dialogVariant = -1 190 dialogVariant = -1
178 191
179 code, ok = self.__callForm(editor, dialogVariant) 192 code, ok = self.__callForm(editor, dialogVariant)
180 if ok: 193 if ok:
181 line, index = editor.getCursorPosition() 194 line, index = editor.getCursorPosition()
182 # It should be done on this way to allow undo 195 # It should be done on this way to allow undo
183 editor.beginUndoAction() 196 editor.beginUndoAction()
184 editor.insertAt(code, line, index) 197 editor.insertAt(code, line, index)
185 editor.endUndoAction() 198 editor.endUndoAction()
186 199
187 def __handleQFileDialog(self): 200 def __handleQFileDialog(self):
188 """ 201 """
189 Private slot to handle the wizard QFileDialog action. 202 Private slot to handle the wizard QFileDialog action.
190 """ 203 """
191 self.__handle("QFileDialog") 204 self.__handle("QFileDialog")
192 205
193 def __handleEricFileDialog(self): 206 def __handleEricFileDialog(self):
194 """ 207 """
195 Private slot to handle the wizard EricFileDialog action. 208 Private slot to handle the wizard EricFileDialog action.
196 """ 209 """
197 self.__handle("EricFileDialog") 210 self.__handle("EricFileDialog")

eric ide

mercurial