src/eric7/Plugins/PluginWizardQFileDialog.py

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

eric ide

mercurial