PluginColorString.py

changeset 20
ffe2ec016dfb
parent 18
a1e72b3e06cc
child 23
8a7d311ecf96
equal deleted inserted replaced
19:ff19ebcd800d 20:ffe2ec016dfb
21 # Start-Of-Header 21 # Start-Of-Header
22 name = "Color String Plug-in" 22 name = "Color String Plug-in"
23 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 23 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
24 autoactivate = True 24 autoactivate = True
25 deactivateable = True 25 deactivateable = True
26 version = "2.0.1" 26 version = "2.1.0"
27 className = "ColorStringPlugin" 27 className = "ColorStringPlugin"
28 packageName = "ColorString" 28 packageName = "ColorString"
29 shortDescription = "Insert color as string" 29 shortDescription = "Insert color as string"
30 longDescription = \ 30 longDescription = \
31 """This plug-in implements a tool to select a color via a""" \ 31 """This plug-in implements a tool to select a color via a""" \
122 122
123 def __initMenu(self): 123 def __initMenu(self):
124 """ 124 """
125 Private method to initialize the menu. 125 Private method to initialize the menu.
126 """ 126 """
127 self.__menu = QMenu("Color String") 127 self.__menu = QMenu(self.tr("Color String"))
128 self.__menu.addAction("Hex Color", self.__selectHexColor) 128 self.__menu.addAction(self.tr("Hex Color"), self.__selectHexColor)
129 self.__menu.addAction("Color Name", self.__selectColorName) 129 self.__menu.addAction(self.tr("Color Name"), self.__selectColorName)
130 self.__menu.addAction(self.tr("RGBA Color"), self.__selectRgbaColor)
130 self.__menu.setEnabled(False) 131 self.__menu.setEnabled(False)
131 132
132 def __populateMenu(self, name, menu): 133 def __populateMenu(self, name, menu):
133 """ 134 """
134 Private slot to populate the tools menu with our entry. 135 Private slot to populate the tools menu with our entry.
306 else: 307 else:
307 line, index = editor.getCursorPosition() 308 line, index = editor.getCursorPosition()
308 editor.insert(colorStr) 309 editor.insert(colorStr)
309 editor.setCursorPosition(line, index + len(colorStr)) 310 editor.setCursorPosition(line, index + len(colorStr))
310 editor.endUndoAction() 311 editor.endUndoAction()
312
313 def __selectRgbaColor(self):
314 """
315 Private slot implementing the RGBA color string selection.
316 """
317 editor = e5App().getObject("ViewManager").activeWindow()
318 if editor is None:
319 return
320
321 if editor.hasSelectedText():
322 currColor = editor.selectedText()
323 valid, rgba = self.__isValidRgbaColor(currColor)
324 if not valid:
325 E5MessageBox.critical(
326 self.__ui,
327 self.tr("Color String"),
328 self.tr(
329 """<p>The selected string <b>{0}</b> is not a"""
330 """ valid color string. Aborting!</p>""")
331 .format(currColor))
332 return
333 initColor = QColor(*rgba)
334 else:
335 initColor = QColor()
336
337 color = QColorDialog.getColor(
338 initColor, self.__ui, self.tr("Color String"),
339 QColorDialog.ShowAlphaChannel)
340 if color.isValid():
341 rgba = color.getRgb()
342 if rgba[-1] == 255:
343 colorStr = "{0}, {1}, {2}".format(*rgba[:-1])
344 else:
345 colorStr = "{0}, {1}, {2}, {3}".format(*rgba)
346 editor.beginUndoAction()
347 if editor.hasSelectedText():
348 editor.replaceSelectedText(colorStr)
349 else:
350 line, index = editor.getCursorPosition()
351 editor.insert(colorStr)
352 editor.setCursorPosition(line, index + len(colorStr))
353 editor.endUndoAction()
354
355 def __isValidRgbaColor(self, color):
356 """
357 Private method to check for a valid RGBA color.
358
359 @param name color string to check (string)
360 @return flag indicating a valid RGBA color (boolean) and a list with
361 the RGBA components of the color (three or four integers)
362 """
363 rgba = []
364
365 parts = color.split(",")
366 if len(parts) not in [3, 4]:
367 return False, []
368
369 for part in parts:
370 try:
371 c = int(part)
372 except ValueError:
373 return False, []
374
375 if c < 0 or c > 255:
376 return False, []
377
378 rgba.append(c)
379
380 return True, rgba

eric ide

mercurial