-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginColorString.py
More file actions
399 lines (341 loc) · 13.3 KB
/
PluginColorString.py
File metadata and controls
399 lines (341 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# -*- coding: utf-8 -*-
# Copyright (c) 2014 - 2020 Detlev Offenbach <detlev@die-offenbachs.de>
#
"""
Module implementing the 'Color String' tool plug-in.
"""
import os
from PyQt5.QtCore import QObject, QTranslator
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QColorDialog, QMenu, QDialog
from E5Gui.E5Application import e5App
from E5Gui import E5MessageBox
# Start-Of-Header
name = "Color String Plug-in"
author = "Detlev Offenbach <detlev@die-offenbachs.de>"
autoactivate = True
deactivateable = True
version = "3.0.0"
className = "ColorStringPlugin"
packageName = "ColorString"
shortDescription = "Insert color as string"
longDescription = (
"""This plug-in implements a tool to select a color via a"""
""" color selection dialog and insert it as a hex string at the"""
""" current cursor position. Selected text is used to initialize"""
""" the dialog and is replaced with the new color."""
)
needsRestart = False
pyqtApi = 2
# End-Of-Header
error = ""
class ColorStringPlugin(QObject):
"""
Class implementing the 'Color String' tool plug-in.
"""
def __init__(self, ui):
"""
Constructor
@param ui reference to the user interface object (UI.UserInterface)
"""
QObject.__init__(self, ui)
self.__ui = ui
self.__translator = None
self.__loadTranslator()
self.__initMenu()
self.__editors = {}
self.__mainActions = []
def activate(self):
"""
Public method to activate this plugin.
@return tuple of None and activation status (boolean)
"""
global error
error = "" # clear previous error
self.__ui.showMenu.connect(self.__populateMenu)
menu = self.__ui.getMenu("plugin_tools")
if menu is not None:
if not menu.isEmpty():
act = menu.addSeparator()
self.__mainActions.append(act)
act = menu.addMenu(self.__menu)
self.__mainActions.append(act)
e5App().getObject("ViewManager").editorOpenedEd.connect(
self.__editorOpened)
e5App().getObject("ViewManager").editorClosedEd.connect(
self.__editorClosed)
for editor in e5App().getObject("ViewManager").getOpenEditors():
self.__editorOpened(editor)
return None, True
def deactivate(self):
"""
Public method to deactivate this plugin.
"""
self.__ui.showMenu.disconnect(self.__populateMenu)
menu = self.__ui.getMenu("plugin_tools")
if menu is not None:
for act in self.__mainActions:
menu.removeAction(act)
self.__mainActions = []
e5App().getObject("ViewManager").editorOpenedEd.disconnect(
self.__editorOpened)
e5App().getObject("ViewManager").editorClosedEd.disconnect(
self.__editorClosed)
for editor, acts in self.__editors.items():
editor.showMenu.disconnect(self.__editorShowMenu)
menu = editor.getMenu("Tools")
if menu is not None:
for act in acts:
menu.removeAction(act)
self.__editors = {}
def __loadTranslator(self):
"""
Private method to load the translation file.
"""
if self.__ui is not None:
loc = self.__ui.getLocale()
if loc and loc != "C":
locale_dir = os.path.join(
os.path.dirname(__file__), "ColorString", "i18n")
translation = "colorstring_{0}".format(loc)
translator = QTranslator(None)
loaded = translator.load(translation, locale_dir)
if loaded:
self.__translator = translator
e5App().installTranslator(self.__translator)
else:
print("Warning: translation file '{0}' could not be"
" loaded.".format(translation))
print("Using default.")
def __initMenu(self):
"""
Private method to initialize the menu.
"""
self.__menu = QMenu(self.tr("Color String"))
self.__menu.addAction(self.tr("Hex Color"), self.__selectHexColor)
self.__menu.addAction(self.tr("Color Name"), self.__selectColorName)
self.__menu.addAction(self.tr("RGBA Color"), self.__selectRgbaColor)
self.__menu.setEnabled(False)
def __populateMenu(self, name, menu):
"""
Private slot to populate the tools menu with our entry.
@param name name of the menu (string)
@param menu reference to the menu to be populated (QMenu)
"""
if name not in ["Tools", "PluginTools"]:
return
editor = e5App().getObject("ViewManager").activeWindow()
if name == "Tools":
if not menu.isEmpty():
menu.addSeparator()
act = menu.addMenu(self.__menu)
act.setEnabled(editor is not None)
elif name == "PluginTools" and self.__mainActions:
self.__mainActions[-1].setEnabled(editor is not None)
def __editorOpened(self, editor):
"""
Private slot called, when a new editor was opened.
@param editor reference to the new editor (QScintilla.Editor)
"""
menu = editor.getMenu("Tools")
if menu is not None:
self.__editors[editor] = []
if not menu.isEmpty():
act = menu.addSeparator()
self.__editors[editor].append(act)
act = menu.addMenu(self.__menu)
self.__menu.setEnabled(True)
self.__editors[editor].append(act)
editor.showMenu.connect(self.__editorShowMenu)
def __editorClosed(self, editor):
"""
Private slot called, when an editor was closed.
@param editor reference to the editor (QScintilla.Editor)
"""
try:
del self.__editors[editor]
if not self.__editors:
self.__menu.setEnabled(False)
except KeyError:
pass
def __editorShowMenu(self, menuName, menu, editor):
"""
Private slot called, when the the editor context menu or a submenu is
about to be shown.
@param menuName name of the menu to be shown (string)
@param menu reference to the menu (QMenu)
@param editor reference to the editor
"""
if menuName == "Tools":
if self.__menu.menuAction() not in menu.actions():
# Re-add our menu
self.__editors[editor] = []
if not menu.isEmpty():
act = menu.addSeparator()
self.__editors[editor].append(act)
act = menu.addMenu(self.__menu)
self.__editors[editor].append(act)
def __isHexString(self, text):
"""
Private method to check, if a given text is a hex string.
@param text text to check (string)
@return flag indicating a hex string (boolean)
"""
return all(map(lambda c: c in "0123456789abcdefABCDEF", text))
def __isValidColor(self, name):
"""
Private method to check for a valid color name.
@param name color name to check (string)
@return flag indicating a valid color name (boolean)
"""
try:
if self.__isHexString(name) and len(name) in [3, 6, 9, 12]:
return True
return QColor.isValidColor(name)
except AttributeError:
if name.startswith("#"):
if len(name) not in [4, 7, 10, 13]:
return False
hexCheckStr = name[1:]
return self.__isHexString(hexCheckStr)
else:
if self.__isHexString(name) and len(name) in [3, 6, 9, 12]:
return True
return name in QColor.colorNames()
def __selectHexColor(self):
"""
Private slot implementing the hex color string selection.
"""
editor = e5App().getObject("ViewManager").activeWindow()
if editor is None:
return
if editor.hasSelectedText():
currColor = editor.selectedText()
if not self.__isValidColor(currColor):
E5MessageBox.critical(
self.__ui,
self.tr("Color String"),
self.tr(
"""<p>The selected string <b>{0}</b> is not a"""
""" valid color string. Aborting!</p>""")
.format(currColor))
return
if currColor.startswith("#"):
withHash = True
elif self.__isHexString(currColor):
withHash = False
currColor = "#" + currColor
else:
withHash = True
initColor = QColor(currColor)
else:
withHash = True
currColor = ""
initColor = QColor()
color = QColorDialog.getColor(
initColor, self.__ui, self.tr("Color String"))
if color.isValid():
colorStr = color.name()
if not withHash:
colorStr = colorStr[1:]
editor.beginUndoAction()
if editor.hasSelectedText():
editor.replaceSelectedText(colorStr)
else:
line, index = editor.getCursorPosition()
editor.insert(colorStr)
editor.setCursorPosition(line, index + len(colorStr))
editor.endUndoAction()
def __selectColorName(self):
"""
Private slot implementing the named color string selection.
"""
editor = e5App().getObject("ViewManager").activeWindow()
if editor is None:
return
if editor.hasSelectedText():
currColor = editor.selectedText()
if currColor not in QColor.colorNames():
E5MessageBox.critical(
self.__ui,
self.tr("Color String"),
self.tr(
"""<p>The selected string <b>{0}</b> is not a"""
""" valid color name. Aborting!</p>""")
.format(currColor))
return
else:
currColor = ""
from ColorString.ColorSelectionDialog import ColorSelectionDialog
dlg = ColorSelectionDialog(currColor, self.__ui)
if dlg.exec_() == QDialog.Accepted:
colorStr = dlg.getColor()
editor.beginUndoAction()
if editor.hasSelectedText():
editor.replaceSelectedText(colorStr)
else:
line, index = editor.getCursorPosition()
editor.insert(colorStr)
editor.setCursorPosition(line, index + len(colorStr))
editor.endUndoAction()
def __selectRgbaColor(self):
"""
Private slot implementing the RGBA color string selection.
"""
editor = e5App().getObject("ViewManager").activeWindow()
if editor is None:
return
if editor.hasSelectedText():
currColor = editor.selectedText()
valid, rgba = self.__isValidRgbaColor(currColor)
if not valid:
E5MessageBox.critical(
self.__ui,
self.tr("Color String"),
self.tr(
"""<p>The selected string <b>{0}</b> is not a"""
""" valid color string. Aborting!</p>""")
.format(currColor))
return
initColor = QColor(*rgba)
else:
initColor = QColor()
color = QColorDialog.getColor(
initColor, self.__ui, self.tr("Color String"),
QColorDialog.ShowAlphaChannel)
if color.isValid():
rgba = color.getRgb()
if rgba[-1] == 255:
colorStr = "{0}, {1}, {2}".format(*rgba[:-1])
else:
colorStr = "{0}, {1}, {2}, {3}".format(*rgba)
editor.beginUndoAction()
if editor.hasSelectedText():
editor.replaceSelectedText(colorStr)
else:
line, index = editor.getCursorPosition()
editor.insert(colorStr)
editor.setCursorPosition(line, index + len(colorStr))
editor.endUndoAction()
def __isValidRgbaColor(self, color):
"""
Private method to check for a valid RGBA color.
@param color color string to check (string)
@return flag indicating a valid RGBA color (boolean) and a list with
the RGBA components of the color (three or four integers)
"""
rgba = []
parts = color.split(",")
if len(parts) not in [3, 4]:
return False, []
for part in parts:
try:
c = int(part)
except ValueError:
return False, []
if c < 0 or c > 255:
return False, []
rgba.append(c)
return True, rgba
#
# eflag: noqa = M801