|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the split, merge or convert camel case plug-in. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import re |
|
12 |
|
13 from PyQt4.QtCore import QObject, QTranslator |
|
14 from PyQt4.QtGui import QMenu |
|
15 |
|
16 from E5Gui.E5Application import e5App |
|
17 |
|
18 # Start-Of-Header |
|
19 name = "Camel Case Handling Plug-in" |
|
20 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
21 autoactivate = True |
|
22 deactivateable = True |
|
23 version = "0.1.0" |
|
24 className = "SplitMergeCamelCasePlugin" |
|
25 packageName = "SplitMergeCamelCase" |
|
26 shortDescription = "Split, merge or convert camel case text" |
|
27 longDescription = \ |
|
28 """This plug-in implements a tool to split, merge or convert""" \ |
|
29 """ camel case text. It works with the text of the current""" \ |
|
30 """ editor. The menu entries will only be selectable, if the""" \ |
|
31 """ current editor has some selected text.""" |
|
32 needsRestart = False |
|
33 pyqtApi = 2 |
|
34 # End-Of-Header |
|
35 |
|
36 error = "" |
|
37 |
|
38 |
|
39 class SplitMergeCamelCasePlugin(QObject): |
|
40 """ |
|
41 Class implementing the split, merge or convert camel case plug-in. |
|
42 """ |
|
43 def __init__(self, ui): |
|
44 """ |
|
45 Constructor |
|
46 |
|
47 @param ui reference to the user interface object (UI.UserInterface) |
|
48 """ |
|
49 QObject.__init__(self, ui) |
|
50 self.__ui = ui |
|
51 |
|
52 self.__translator = None |
|
53 self.__loadTranslator() |
|
54 |
|
55 self.__initMenu() |
|
56 |
|
57 def activate(self): |
|
58 """ |
|
59 Public method to activate this plugin. |
|
60 |
|
61 @return tuple of None and activation status (boolean) |
|
62 """ |
|
63 global error |
|
64 error = "" # clear previous error |
|
65 |
|
66 self.__ui.showMenu.connect(self.__populateMenu) |
|
67 |
|
68 return None, True |
|
69 |
|
70 def deactivate(self): |
|
71 """ |
|
72 Public method to deactivate this plugin. |
|
73 """ |
|
74 self.__ui.showMenu.disconnect(self.__populateMenu) |
|
75 |
|
76 def __loadTranslator(self): |
|
77 """ |
|
78 Private method to load the translation file. |
|
79 """ |
|
80 if self.__ui is not None: |
|
81 loc = self.__ui.getLocale() |
|
82 if loc and loc != "C": |
|
83 locale_dir = os.path.join( |
|
84 os.path.dirname(__file__), "SplitMergeCamelCase", "i18n") |
|
85 translation = "splitmergecamelcase_{0}".format(loc) |
|
86 translator = QTranslator(None) |
|
87 loaded = translator.load(translation, locale_dir) |
|
88 if loaded: |
|
89 self.__translator = translator |
|
90 e5App().installTranslator(self.__translator) |
|
91 else: |
|
92 print("Warning: translation file '{0}' could not be" |
|
93 " loaded.".format(translation)) |
|
94 print("Using default.") |
|
95 |
|
96 def __initMenu(self): |
|
97 """ |
|
98 Private method to initialize the camel case handling menu. |
|
99 """ |
|
100 self.__menu = QMenu(self.tr("CamelCase Handling")) |
|
101 self.__menu.addAction(self.tr("Split from CamelCase"), |
|
102 self.__splitCamelCase) |
|
103 self.__menu.addAction(self.tr("Merge to CamelCase"), |
|
104 self.__mergeCamelCase) |
|
105 self.__menu.addAction(self.tr("CamelCase to under_score"), |
|
106 self.__camelCaseToUnderscore) |
|
107 self.__menu.addAction(self.tr("under_score to CamelCase"), |
|
108 self.__underscoreToCamelCase) |
|
109 self.__menu.addAction( |
|
110 self.tr("under_score to CamelCase (upper case first)"), |
|
111 self.__underscoreToCamelCaseUppercaseFirst) |
|
112 |
|
113 def __populateMenu(self, name, menu): |
|
114 """ |
|
115 Private slot to populate the tools menu with our entries. |
|
116 |
|
117 @param name name of the menu (string) |
|
118 @param menu reference to the menu to be populated (QMenu) |
|
119 """ |
|
120 if name != "Tools": |
|
121 return |
|
122 |
|
123 editor = e5App().getObject("ViewManager").activeWindow() |
|
124 if editor is None: |
|
125 return |
|
126 |
|
127 if not menu.isEmpty(): |
|
128 menu.addSeparator() |
|
129 |
|
130 act = menu.addMenu(self.__menu) |
|
131 act.setEnabled(editor.hasSelectedText()) |
|
132 |
|
133 def __applyChange(self, newText, editor): |
|
134 """ |
|
135 Private method to change the selected text. |
|
136 |
|
137 @param newText new (converted) text (string) |
|
138 @param editor reference to the editor to apply the text |
|
139 change to (Editor) |
|
140 """ |
|
141 editor.beginUndoAction() |
|
142 editor.replaceSelectedText(newText) |
|
143 editor.endUndoAction() |
|
144 |
|
145 def __splitCamelCase(self): |
|
146 """ |
|
147 Private slot to split the selected camel case text. |
|
148 """ |
|
149 editor = e5App().getObject("ViewManager").activeWindow() |
|
150 if editor is None: |
|
151 return |
|
152 |
|
153 text = editor.selectedText() |
|
154 newText = re.sub(r"([A-Z])", r" \1", text) |
|
155 if newText.startswith(" "): |
|
156 newText = newText[1:] |
|
157 if newText != text: |
|
158 self.__applyChange(newText, editor) |
|
159 |
|
160 def __mergeCamelCase(self): |
|
161 """ |
|
162 Private slot to merge the selected text to camel case. |
|
163 """ |
|
164 editor = e5App().getObject("ViewManager").activeWindow() |
|
165 if editor is None: |
|
166 return |
|
167 |
|
168 text = editor.selectedText() |
|
169 newText = "".join(text.split()) |
|
170 if newText != text: |
|
171 self.__applyChange(newText, editor) |
|
172 |
|
173 def __camelCaseToUnderscore(self): |
|
174 """ |
|
175 Private slot to convert camel case text to underscore separated text. |
|
176 """ |
|
177 editor = e5App().getObject("ViewManager").activeWindow() |
|
178 if editor is None: |
|
179 return |
|
180 |
|
181 text = editor.selectedText() |
|
182 newText = re.sub(r"([A-Z])", r"_\1", text).lower() |
|
183 if newText.startswith("_"): |
|
184 newText = newText[1:] |
|
185 if newText != text: |
|
186 self.__applyChange(newText, editor) |
|
187 |
|
188 def __underscoreToCamelCase(self, uppercaseFirst=False): |
|
189 """ |
|
190 Private slot to convert underscore separated text to camel case. |
|
191 |
|
192 @param uppercaseFirst flag indicating to upper case the |
|
193 first character (boolean) |
|
194 """ |
|
195 editor = e5App().getObject("ViewManager").activeWindow() |
|
196 if editor is None: |
|
197 return |
|
198 |
|
199 text = editor.selectedText() |
|
200 newText = "".join([s.capitalize() for s in text.split("_")]) |
|
201 if not uppercaseFirst: |
|
202 newText = newText[0].lower() + newText[1:] |
|
203 if newText != text: |
|
204 self.__applyChange(newText, editor) |
|
205 |
|
206 def __underscoreToCamelCaseUppercaseFirst(self): |
|
207 """ |
|
208 Private slot to convert underscore separated text to camel case |
|
209 upper casing the first character. |
|
210 """ |
|
211 self.__underscoreToCamelCase(True) |