From 5ad678bf4cf77047ee1e531402582b4de6d23271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucio=20Andr=C3=A9s=20Illanes=20Albornoz?= Date: Mon, 16 Sep 2019 11:15:23 +0200 Subject: [PATCH] Allow pasting text when using text tool. libtools/ToolText.py:{_processKeyChar,onKeyboardEvent}(): split from onKeyboardEvent(). libtools/ToolText.py:onKeyboardEvent(): obtain & iteratively _processKeyChar() clipboard text on V. libtools/ToolText.py:onKeyboardEvent(): dispatch cursor patch once after {drawing,pasting}. assets/text/TODO: updated. --- assets/text/TODO | 10 ++++---- libtools/ToolText.py | 60 ++++++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/assets/text/TODO b/assets/text/TODO index 07f1dbc..c27f1ff 100644 --- a/assets/text/TODO +++ b/assets/text/TODO @@ -15,15 +15,15 @@ High-priority list: 3) cleanup & refactor, switch to GTK 4) GUI: a) replace logo w/ canvas panel in About dialogue - b) Settings/Settings window (e.g. autosave, hide cursor on leaving window, ...) - c) replace resize buttons w/ {-,edit box,+} buttons & lock button re: ratio (ty lol3) - d) {copy,cut,delete,insert from,paste}, {edit asset in new canvas,import from {canvas,object}} + b) purge MRU list menu item, remove files from MRU list if inaccessible + c) Settings/Settings window (e.g. autosave, hide cursor on leaving window, ...) + d) replace resize buttons w/ {-,edit box,+} buttons & lock button re: ratio (ty lol3) + e) {copy,cut,delete,insert from,paste}, {edit asset in new canvas,import from {canvas,object}} Queue: 1) scrolling bug: start @ top, down key til cursor below visible canvas, scroll down, cursor gone GRRRR 2) scrolling bug: scroll down, apply operator to entire canvas, scroll up 3) select-related {re,un}do bugs -4) text tool: pasting text -5) clone selection lag +4) clone selection lag vim:ff=dos tw=0 diff --git a/libtools/ToolText.py b/libtools/ToolText.py index 184f910..bcd51f5 100644 --- a/libtools/ToolText.py +++ b/libtools/ToolText.py @@ -11,24 +11,9 @@ class ToolText(Tool): name = "Text" rtlRegEx = r'^[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]+$' - # - # onKeyboardEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, dispatchFn, eventDc, keyChar, keyCode, keyModifiers, mapPoint, viewRect) - def onKeyboardEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, dispatchFn, eventDc, keyChar, keyCode, keyModifiers, mapPoint, viewRect): - if keyCode == wx.WXK_BACK: - if brushPos[0] > 0: - brushPos[0] -= 1 - elif brushPos[1] > 0: - brushPos[0], brushPos[1] = canvas.size[0] - 1, brushPos[1] - 1 - else: - brushPos[0], brushPos[1] = canvas.size[0] - 1, canvas.size[1] - 1 - rc, dirty = True, False; dispatchFn(eventDc, True, [*brushPos, *brushColours, 0, "_"], viewRect); - elif keyCode == wx.WXK_RETURN: - if brushPos[1] < (canvas.size[1] - 1): - brushPos[0], brushPos[1] = 0, brushPos[1] + 1 - else: - brushPos[0], brushPos[1] = 0, 0 - rc, dirty = True, False; dispatchFn(eventDc, True, [*brushPos, *brushColours, 0, "_"], viewRect); - elif (ord(keyChar) != wx.WXK_NONE) \ + # {{{ _processKeyChar(self, brushColours, brushPos, canvas, dispatchFn, eventDc, keyChar, keyModifiers, viewRect) + def _processKeyChar(self, brushColours, brushPos, canvas, dispatchFn, eventDc, keyChar, keyModifiers, viewRect): + if (ord(keyChar) != wx.WXK_NONE) \ and (not keyChar in set("\t\n\v\f\r")) \ and ((ord(keyChar) >= 32) if ord(keyChar) < 127 else True) \ and (keyModifiers in (wx.MOD_NONE, wx.MOD_SHIFT)): @@ -47,11 +32,48 @@ class ToolText(Tool): brushPos[0], brushPos[1] = canvas.size[0] - 1, brushPos[1] - 1 else: brushPos[0], brushPos[1] = canvas.size[0] - 1, canvas.size[1] - 1 - dispatchFn(eventDc, True, [*brushPos, *brushColours, 0, "_"], viewRect) rc, dirty = True, True else: rc, dirty = False, False return rc, dirty + # }}} + + # + # onKeyboardEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, dispatchFn, eventDc, keyChar, keyCode, keyModifiers, mapPoint, viewRect) + def onKeyboardEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, dispatchFn, eventDc, keyChar, keyCode, keyModifiers, mapPoint, viewRect): + if keyCode == wx.WXK_CONTROL_V: + rc, dirty = True, False + if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)) \ + and wx.TheClipboard.Open(): + inBuffer = wx.TextDataObject() + if wx.TheClipboard.GetData(inBuffer): + for inBufferChar in list(inBuffer.GetText()): + rc_, dirty_ = self._processKeyChar(brushColours, brushPos, canvas, dispatchFn, eventDc, inBufferChar, 0, viewRect) + rc = True if rc_ else rc; dirty = True if dirty_ else dirty; + if rc: + dispatchFn(eventDc, True, [*brushPos, *brushColours, 0, "_"], viewRect) + wx.TheClipboard.Close() + else: + rc, error = False, "Clipboard does not contain text data and/or cannot be opened" + elif keyCode == wx.WXK_BACK: + if brushPos[0] > 0: + brushPos[0] -= 1 + elif brushPos[1] > 0: + brushPos[0], brushPos[1] = canvas.size[0] - 1, brushPos[1] - 1 + else: + brushPos[0], brushPos[1] = canvas.size[0] - 1, canvas.size[1] - 1 + rc, dirty = True, False; dispatchFn(eventDc, True, [*brushPos, *brushColours, 0, "_"], viewRect); + elif keyCode == wx.WXK_RETURN: + if brushPos[1] < (canvas.size[1] - 1): + brushPos[0], brushPos[1] = 0, brushPos[1] + 1 + else: + brushPos[0], brushPos[1] = 0, 0 + rc, dirty = True, False; dispatchFn(eventDc, True, [*brushPos, *brushColours, 0, "_"], viewRect); + else: + rc, dirty = self._processKeyChar(brushColours, brushPos, canvas, dispatchFn, eventDc, keyChar, keyModifiers, viewRect) + if rc: + dispatchFn(eventDc, True, [*brushPos, *brushColours, 0, "_"], viewRect) + return rc, dirty # # onMouseEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, dispatchFn, eventDc, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown, viewRect)