|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2007 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Tabnanny plugin. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt4.QtCore import QObject, SIGNAL |
|
13 |
|
14 from E4Gui.E4Application import e4App |
|
15 |
|
16 from E4Gui.E4Action import E4Action |
|
17 |
|
18 from CheckerPlugins.Tabnanny.TabnannyDialog import TabnannyDialog |
|
19 |
|
20 # Start-Of-Header |
|
21 name = "Tabnanny Plugin" |
|
22 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
23 autoactivate = True |
|
24 deactivateable = True |
|
25 version = "5.0.0" |
|
26 className = "TabnannyPlugin" |
|
27 packageName = "__core__" |
|
28 shortDescription = "Show the Tabnanny dialog." |
|
29 longDescription = """This plugin implements the Tabnanny dialog.""" \ |
|
30 """ Tabnanny is used to check Python source files for correct indentations.""" |
|
31 pyqtApi = 2 |
|
32 # End-Of-Header |
|
33 |
|
34 error = "" |
|
35 |
|
36 class TabnannyPlugin(QObject): |
|
37 """ |
|
38 Class implementing the Tabnanny plugin. |
|
39 """ |
|
40 def __init__(self, ui): |
|
41 """ |
|
42 Constructor |
|
43 |
|
44 @param ui reference to the user interface object (UI.UserInterface) |
|
45 """ |
|
46 QObject.__init__(self, ui) |
|
47 self.__ui = ui |
|
48 self.__initialize() |
|
49 |
|
50 def __initialize(self): |
|
51 """ |
|
52 Private slot to (re)initialize the plugin. |
|
53 """ |
|
54 self.__projectAct = None |
|
55 self.__projectTabnannyDialog = None |
|
56 |
|
57 self.__projectBrowserAct = None |
|
58 self.__projectBrowserMenu = None |
|
59 self.__projectBrowserTabnannyDialog = None |
|
60 |
|
61 self.__editors = [] |
|
62 self.__editorAct = None |
|
63 self.__editorTabnannyDialog = None |
|
64 |
|
65 def activate(self): |
|
66 """ |
|
67 Public method to activate this plugin. |
|
68 |
|
69 @return tuple of None and activation status (boolean) |
|
70 """ |
|
71 menu = e4App().getObject("Project").getMenu("Checks") |
|
72 if menu: |
|
73 self.__projectAct = E4Action(self.trUtf8('Check Indentations'), |
|
74 self.trUtf8('&Indentations...'), 0, 0, |
|
75 self,'project_check_indentations') |
|
76 self.__projectAct.setStatusTip(\ |
|
77 self.trUtf8('Check indentations using tabnanny.')) |
|
78 self.__projectAct.setWhatsThis(self.trUtf8( |
|
79 """<b>Check Indentations...</b>""" |
|
80 """<p>This checks Python files""" |
|
81 """ for bad indentations using tabnanny.</p>""" |
|
82 )) |
|
83 self.connect(self.__projectAct, SIGNAL('triggered()'), self.__projectTabnanny) |
|
84 e4App().getObject("Project").addE4Actions([self.__projectAct]) |
|
85 menu.addAction(self.__projectAct) |
|
86 |
|
87 self.__editorAct = E4Action(self.trUtf8('Check Indentations'), |
|
88 self.trUtf8('&Indentations...'), 0, 0, |
|
89 self, "") |
|
90 self.__editorAct.setWhatsThis(self.trUtf8( |
|
91 """<b>Check Indentations...</b>""" |
|
92 """<p>This checks Python files""" |
|
93 """ for bad indentations using tabnanny.</p>""" |
|
94 )) |
|
95 self.connect(self.__editorAct, SIGNAL('triggered()'), self.__editorTabnanny) |
|
96 |
|
97 self.connect(e4App().getObject("Project"), SIGNAL("showMenu"), |
|
98 self.__projectShowMenu) |
|
99 self.connect(e4App().getObject("ProjectBrowser").getProjectBrowser("sources"), |
|
100 SIGNAL("showMenu"), self.__projectBrowserShowMenu) |
|
101 self.connect(e4App().getObject("ViewManager"), SIGNAL("editorOpenedEd"), |
|
102 self.__editorOpened) |
|
103 self.connect(e4App().getObject("ViewManager"), SIGNAL("editorClosedEd"), |
|
104 self.__editorClosed) |
|
105 |
|
106 for editor in e4App().getObject("ViewManager").getOpenEditors(): |
|
107 self.__editorOpened(editor) |
|
108 |
|
109 return None, True |
|
110 |
|
111 def deactivate(self): |
|
112 """ |
|
113 Public method to deactivate this plugin. |
|
114 """ |
|
115 self.disconnect(e4App().getObject("Project"), SIGNAL("showMenu"), |
|
116 self.__projectShowMenu) |
|
117 self.disconnect(e4App().getObject("ProjectBrowser").getProjectBrowser("sources"), |
|
118 SIGNAL("showMenu"), self.__projectBrowserShowMenu) |
|
119 self.disconnect(e4App().getObject("ViewManager"), SIGNAL("editorOpenedEd"), |
|
120 self.__editorOpened) |
|
121 self.disconnect(e4App().getObject("ViewManager"), SIGNAL("editorClosedEd"), |
|
122 self.__editorClosed) |
|
123 |
|
124 menu = e4App().getObject("Project").getMenu("Checks") |
|
125 if menu: |
|
126 menu.removeAction(self.__projectAct) |
|
127 |
|
128 if self.__projectBrowserMenu: |
|
129 if self.__projectBrowserAct: |
|
130 self.__projectBrowserMenu.removeAction(self.__projectBrowserAct) |
|
131 |
|
132 for editor in self.__editors: |
|
133 self.disconnect(editor, SIGNAL("showMenu"), self.__editorShowMenu) |
|
134 menu = editor.getMenu("Checks") |
|
135 if menu is not None: |
|
136 menu.removeAction(self.__editorAct) |
|
137 |
|
138 self.__initialize() |
|
139 |
|
140 def __projectShowMenu(self, menuName, menu): |
|
141 """ |
|
142 Private slot called, when the the project menu or a submenu is |
|
143 about to be shown. |
|
144 |
|
145 @param menuName name of the menu to be shown (string) |
|
146 @param menu reference to the menu (QMenu) |
|
147 """ |
|
148 if menuName == "Checks" and self.__projectAct is not None: |
|
149 self.__projectAct.setEnabled(\ |
|
150 e4App().getObject("Project").getProjectLanguage() == "Python") |
|
151 |
|
152 def __projectBrowserShowMenu(self, menuName, menu): |
|
153 """ |
|
154 Private slot called, when the the project browser context menu or a submenu is |
|
155 about to be shown. |
|
156 |
|
157 @param menuName name of the menu to be shown (string) |
|
158 @param menu reference to the menu (QMenu) |
|
159 """ |
|
160 if menuName == "Checks" and \ |
|
161 e4App().getObject("Project").getProjectLanguage() == "Python": |
|
162 self.__projectBrowserMenu = menu |
|
163 if self.__projectBrowserAct is None: |
|
164 self.__projectBrowserAct = E4Action(self.trUtf8('Check Indentations'), |
|
165 self.trUtf8('&Indentations...'), 0, 0, |
|
166 self, "") |
|
167 self.__projectBrowserAct.setWhatsThis(self.trUtf8( |
|
168 """<b>Check Indentations...</b>""" |
|
169 """<p>This checks Python files""" |
|
170 """ for bad indentations using tabnanny.</p>""" |
|
171 )) |
|
172 self.connect(self.__projectBrowserAct, SIGNAL('triggered()'), |
|
173 self.__projectBrowserTabnanny) |
|
174 if not self.__projectBrowserAct in menu.actions(): |
|
175 menu.addAction(self.__projectBrowserAct) |
|
176 |
|
177 def __projectTabnanny(self): |
|
178 """ |
|
179 Public slot used to check the project files for bad indentations. |
|
180 """ |
|
181 project = e4App().getObject("Project") |
|
182 project.saveAllScripts() |
|
183 files = [os.path.join(project.ppath, file) \ |
|
184 for file in project.pdata["SOURCES"] \ |
|
185 if file.endswith(".py") or \ |
|
186 file.endswith(".pyw") or \ |
|
187 file.endswith(".ptl")] |
|
188 |
|
189 self.__projectTabnannyDialog = TabnannyDialog() |
|
190 self.__projectTabnannyDialog.show() |
|
191 self.__projectTabnannyDialog.start(files) |
|
192 |
|
193 def __projectBrowserTabnanny(self): |
|
194 """ |
|
195 Private method to handle the tabnanny context menu action of the project |
|
196 sources browser. |
|
197 """ |
|
198 browser = e4App().getObject("ProjectBrowser").getProjectBrowser("sources") |
|
199 itm = browser.model().item(browser.currentIndex()) |
|
200 try: |
|
201 fn = itm.fileName() |
|
202 except AttributeError: |
|
203 fn = itm.dirName() |
|
204 |
|
205 self.__projectBrowserTabnannyDialog = TabnannyDialog() |
|
206 self.__projectBrowserTabnannyDialog.show() |
|
207 self.__projectBrowserTabnannyDialog.start(fn) |
|
208 |
|
209 def __editorOpened(self, editor): |
|
210 """ |
|
211 Private slot called, when a new editor was opened. |
|
212 |
|
213 @param editor reference to the new editor (QScintilla.Editor) |
|
214 """ |
|
215 menu = editor.getMenu("Checks") |
|
216 if menu is not None: |
|
217 menu.addAction(self.__editorAct) |
|
218 self.connect(editor, SIGNAL("showMenu"), self.__editorShowMenu) |
|
219 self.__editors.append(editor) |
|
220 |
|
221 def __editorClosed(self, editor): |
|
222 """ |
|
223 Private slot called, when an editor was closed. |
|
224 |
|
225 @param editor reference to the editor (QScintilla.Editor) |
|
226 """ |
|
227 try: |
|
228 self.__editors.remove(editor) |
|
229 except ValueError: |
|
230 pass |
|
231 |
|
232 def __editorShowMenu(self, menuName, menu, editor): |
|
233 """ |
|
234 Private slot called, when the the editor context menu or a submenu is |
|
235 about to be shown. |
|
236 |
|
237 @param menuName name of the menu to be shown (string) |
|
238 @param menu reference to the menu (QMenu) |
|
239 @param editor reference to the editor |
|
240 """ |
|
241 if menuName == "Checks": |
|
242 if not self.__editorAct in menu.actions(): |
|
243 menu.addAction(self.__editorAct) |
|
244 self.__editorAct.setEnabled(editor.isPyFile()) |
|
245 |
|
246 def __editorTabnanny(self): |
|
247 """ |
|
248 Private slot to handle the tabnanny context menu action of the editors. |
|
249 """ |
|
250 editor = e4App().getObject("ViewManager").activeWindow() |
|
251 if editor is not None: |
|
252 if not editor.checkDirty(): |
|
253 return |
|
254 |
|
255 self.__editorTabnannyDialog = TabnannyDialog() |
|
256 self.__editorTabnannyDialog.show() |
|
257 self.__editorTabnannyDialog.start(editor.getFileName()) |