eric6/Plugins/PluginWizardQFileDialog.py

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

eric ide

mercurial