diff --git a/MiRCARTCanvas.py b/MiRCARTCanvas.py index 3743b5c..f8b5a47 100644 --- a/MiRCARTCanvas.py +++ b/MiRCARTCanvas.py @@ -23,6 +23,7 @@ # from MiRCARTCanvasJournal import MiRCARTCanvasJournal +from MiRCARTCanvasStore import MiRCARTCanvasStore from MiRCARTColours import MiRCARTColours import wx @@ -32,8 +33,18 @@ class MiRCARTCanvas(wx.Panel): canvasPos = canvasSize = canvasWinSize = cellPos = cellSize = None canvasBitmap = canvasMap = canvasTools = None mircBg = mircFg = mircBrushes = mircPens = None - canvasJournal = None + canvasJournal = canvasStore = None + # {{{ _initBrushesAndPens(self): XXX + def _initBrushesAndPens(self): + self.mircBrushes = [None for x in range(len(MiRCARTColours))] + self.mircPens = [None for x in range(len(MiRCARTColours))] + for mircColour in range(0, len(MiRCARTColours)): + self.mircBrushes[mircColour] = wx.Brush( \ + wx.Colour(MiRCARTColours[mircColour]), wx.BRUSHSTYLE_SOLID) + self.mircPens[mircColour] = wx.Pen( \ + wx.Colour(MiRCARTColours[mircColour]), 1) + # }}} # {{{ _drawPatch(self, patch, eventDc, tmpDc, atPoint): XXX def _drawPatch(self, patch, eventDc, tmpDc, atPoint): absPoint = self._relMapPointToAbsPoint((patch[0], patch[1]), atPoint) @@ -69,6 +80,7 @@ class MiRCARTCanvas(wx.Panel): def _setMapCell(self, absMapPoint, colourFg, colourBg, char): self.canvasMap[absMapPoint[1]][absMapPoint[0]] = [colourFg, colourBg, char] # }}} + # {{{ onClose(self, event): XXX def onClose(self, event): self.Destroy(); self.__del__(); @@ -106,10 +118,30 @@ class MiRCARTCanvas(wx.Panel): def redo(self): return self.canvasJournal.redo() # }}} + # {{{ resize(self, newCanvasSize): XXX + def resize(self, newCanvasSize): + if newCanvasSize != self.canvasSize: + self.SetSize(*self.canvasPos, \ + newCanvasSize[0] * self.cellSize[0], \ + newCanvasSize[1] * self.cellSize[1]) + for numRow in range(0, self.canvasSize[1]): + for numNewCol in range(self.canvasSize[0], newCanvasSize[0]): + self.canvasMap[numRow].append([1, 1, " "]) + for numNewRow in range(self.canvasSize[1], newCanvasSize[1]): + self.canvasMap.append([]) + for numNewCol in range(0, newCanvasSize[0]): + self.canvasMap[numNewRow].append([1, 1, " "]) + self.canvasSize = newCanvasSize + canvasWinSize = ( \ + self.cellSize[0] * self.canvasSize[0], \ + self.cellSize[1] * self.canvasSize[1]) + self.canvasBitmap = wx.Bitmap(canvasWinSize) + # }}} # {{{ undo(self): XXX def undo(self): return self.canvasJournal.undo() # }}} + # {{{ __del__(self): destructor method def __del__(self): if self.canvasBitmap != None: @@ -124,30 +156,19 @@ class MiRCARTCanvas(wx.Panel): # # _init__(self, parent, parentFrame, canvasPos, cellSize, canvasSize, canvasTools): initialisation method - def __init__(self, parent, parentFrame, canvasPos, cellSize, canvasSize, canvasTools): + def __init__(self, parent, parentFrame, canvasPos, canvasSize, canvasTools, cellSize): self.parentFrame = parentFrame - canvasWinSize = (cellSize[0] * canvasSize[0], cellSize[1] * canvasSize[1]) - super().__init__(parent, pos=canvasPos, size=canvasWinSize) - self.canvasPos = canvasPos; self.canvasSize = canvasSize; self.canvasWinSize = canvasWinSize; - self.cellPos = (0, 0); self.cellSize = cellSize; + self.canvasPos = canvasPos; self.canvasSize = canvasSize; + self.canvasTools = [canvasTool(self) for canvasTool in canvasTools] + self.cellSize = cellSize - self.canvasBitmap = wx.Bitmap(canvasWinSize) - self.canvasMap = [[[1, 1, " "] for x in range(canvasSize[0])] for y in range(canvasSize[1])] - self.canvasTools = [] - for canvasTool in canvasTools: - self.canvasTools.append(canvasTool(self)) - - self.mircBg = 1; self.mircFg = 4; - self.mircBrushes = [None for x in range(len(MiRCARTColours))] - self.mircPens = [None for x in range(len(MiRCARTColours))] - for mircColour in range(0, len(MiRCARTColours)): - self.mircBrushes[mircColour] = wx.Brush( \ - wx.Colour(MiRCARTColours[mircColour]), wx.BRUSHSTYLE_SOLID) - self.mircPens[mircColour] = wx.Pen( \ - wx.Colour(MiRCARTColours[mircColour]), 1) - - self.canvasJournal = MiRCARTCanvasJournal(self) + self.cellPos = (0, 0) + self.mircBg = 1; self.mircFg = 4; self._initBrushesAndPens(); + self.canvasJournal = MiRCARTCanvasJournal(parentCanvas=self) + self.canvasStore = MiRCARTCanvasStore(parentCanvas=self) + super().__init__(parent, pos=canvasPos, \ + size=[w*h for w,h in zip(canvasSize, cellSize)]) self.Bind(wx.EVT_CLOSE, self.onClose) self.Bind(wx.EVT_LEFT_DOWN, self.onMouseEvent) self.Bind(wx.EVT_MOTION, self.onMouseEvent) diff --git a/MiRCARTCanvasJournal.py b/MiRCARTCanvasJournal.py index 03e5f93..a712d36 100644 --- a/MiRCARTCanvasJournal.py +++ b/MiRCARTCanvasJournal.py @@ -83,6 +83,11 @@ class MiRCARTCanvasJournal(): else: return False # }}} + # {{{ reset(self): XXX + def reset(self): + self.patchesTmp = [] + self.patchesUndo = [None]; self.patchesUndoLevel = 0; + # }}} # {{{ undo(self): XXX def undo(self): if self.patchesUndo[self.patchesUndoLevel] != None: @@ -98,8 +103,6 @@ class MiRCARTCanvasJournal(): # # __init__(self, parentCanvas): initialisation method def __init__(self, parentCanvas): - self.parentCanvas = parentCanvas - self.patchesTmp = [] - self.patchesUndo = [None]; self.patchesUndoLevel = 0; + self.parentCanvas = parentCanvas; self.reset(); # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 diff --git a/MiRCARTFromTextFile.py b/MiRCARTCanvasStore.py similarity index 54% rename from MiRCARTFromTextFile.py rename to MiRCARTCanvasStore.py index 6459877..a3b106e 100644 --- a/MiRCARTFromTextFile.py +++ b/MiRCARTCanvasStore.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # -# MiRCARTFromTextFile.py -- XXX +# MiRCARTCanvasStore.py -- XXX # Copyright (c) 2018 Lucio Andrés Illanes Albornoz # # Permission is hereby granted, free of charge, to any person obtaining a copy @@ -22,9 +22,26 @@ # SOFTWARE. # -class MiRCARTFromTextFile(): +import base64 +import io +import wx + +try: + from MiRCARTToPngFile import MiRCARTToPngFile + haveMiRCARTToPngFile = True +except ImportError: + haveMiRCARTToPngFile = False + +try: + import requests, urllib.request + haveUrllib = True +except ImportError: + haveUrllib = False + +class MiRCARTCanvasStore(): """XXX""" inFile = inSize = outMap = None + parentCanvas = None # # _CellState(): Cell state @@ -60,8 +77,72 @@ class MiRCARTFromTextFile(): else: return cellState | bit # }}} - # {{{ _transform(self): XXX - def _transform(self): + + # {{{ exportPastebin(self, apiDevKey): XXX + def exportPastebin(self, apiDevKey): + if haveUrllib: + outFile = io.StringIO(); self.exportTextFile(outFile); + requestData = { \ + "api_dev_key": self.apiDevKey, \ + "api_option": "paste", \ + "api_paste_code": base64.b64encode(outFile.read()), \ + "api_paste_name": pasteName, \ + "api_paste_private": pastePrivate} + responseHttp = requests.post("https://pastebin.com/post.php", \ + data=requestData) + if responseHttp.status_code == 200: + return responseHttp.text + else: + return None + else: + return None + # }}} + # {{{ exportPngFile(self): XXX + def exportPngFile(self, pathName): + if haveMiRCARTToPngFile: + outFile = io.StringIO(); self.exportTextFile(outFile); + MiRCARTToPng(outFile).export(pathName) + return True + else: + return False + # }}} + # {{{ exportTextFile(self, outFile): XXX + def exportTextFile(self, outFile): + canvasMap = self.parentCanvas.canvasMap + canvasSize = self.parentCanvas.canvasSize + for canvasRow in range(0, canvasSize[1]): + canvasLastColours = [] + for canvasCol in range(0, canvasSize[0]): + canvasColColours = canvasMap[canvasRow][canvasCol][0:2] + canvasColText = self.canvasMap[canvasRow][canvasCol][2] + if canvasColColours != canvasLastColours: + canvasLastColours = canvasColColours + outFile.write("\x03" + \ + str(canvasColColours[0]) + \ + "," + str(canvasColColours[1])) + outFile.write(canvasColText) + outFile.write("\n") + # }}} + # {{{ importIntoPanel(self): XXX + def importIntoPanel(self): + canvasSize = self.inSize; self.parentCanvas.resize(canvasSize); + self.parentCanvas.canvasJournal.reset() + eventDc = wx.ClientDC(self.parentCanvas); tmpDc = wx.MemoryDC(); + tmpDc.SelectObject(self.parentCanvas.canvasBitmap) + for numRow in range(0, len(self.outMap)): + for numCol in range(0, len(self.outMap[numRow])): + self.parentCanvas.onJournalUpdate(False, \ + (numCol, numRow), [numCol, numRow, \ + self.outMap[numRow][numCol][0][0], \ + self.outMap[numRow][numCol][0][1], \ + self.outMap[numRow][numCol][2]], \ + eventDc, tmpDc, (0, 0)) + wx.SafeYield() + # }}} + # {{{ importTextFile(self, pathName): XXX + def importTextFile(self, pathName): + self.inFile = open(pathName, "r") + self.inSize = self.outMap = None; inCurColourSpec = ""; inCurRow = -1; inLine = self.inFile.readline() inSize = [0, 0]; outMap = []; inMaxCols = 0; @@ -125,23 +206,38 @@ class MiRCARTFromTextFile(): inMaxCols = max(inMaxCols, inRowCols) inLine = self.inFile.readline() inSize[0] = inMaxCols; self.inSize = inSize; self.outMap = outMap; + self.inFile.close() # }}} - # {{{ getMap(self): XXX - def getMap(self): - if self.outMap == None: - self._transform() - return self.outMap - # }}} - # {{{ getSize(self): XXX - def getSize(self): - if self.inSize == None: - self._transform() - return self.inSize + # {{{ importNew(self, newCanvasSize=None): XXX + def importNew(self, newCanvasSize=None): + if newCanvasSize != None: + self.parentCanvas.resize(newCanvasSize) + self.parentCanvas.canvasJournal.reset() + self.parentCanvas.canvasMap = [[[1, 1, " "] \ + for x in range(self.parentCanvas.canvasSize[0])] \ + for y in range(self.parentCanvas.canvasSize[1])] + canvasWinSize = ( \ + self.parentCanvas.cellSize[0] * self.parentCanvas.canvasSize[0], \ + self.parentCanvas.cellSize[1] * self.parentCanvas.canvasSize[1]) + if self.parentCanvas.canvasBitmap != None: + self.parentCanvas.canvasBitmap.Destroy() + self.parentCanvas.canvasBitmap = wx.Bitmap(canvasWinSize) + eventDc = wx.ClientDC(self.parentCanvas); tmpDc = wx.MemoryDC(); + tmpDc.SelectObject(self.parentCanvas.canvasBitmap) + for numRow in range(0, len(self.parentCanvas.canvasMap)): + for numCol in range(0, len(self.parentCanvas.canvasMap[numRow])): + self.parentCanvas.onJournalUpdate(False, \ + (numCol, numRow), [numCol, numRow, 1, 1, " "], \ + eventDc, tmpDc, (0, 0)) + wx.SafeYield() # }}} # - # __init__(self): initialisation method - def __init__(self, inFile): - self.inFile = inFile; self.inSize = None; self.outMap = None; + # __init__(self, inFile=None, parentCanvas=None): initialisation method + def __init__(self, inFile=None, parentCanvas=None): + self.inFile = inFile; self.inSize = self.outMap = None; + self.parentCanvas = parentCanvas + if inFile != None: + self.importTextFile(inFile) # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 diff --git a/MiRCARTFrame.py b/MiRCARTFrame.py index 8c8ae20..e60c41a 100644 --- a/MiRCARTFrame.py +++ b/MiRCARTFrame.py @@ -24,8 +24,6 @@ from MiRCARTCanvas import MiRCARTCanvas from MiRCARTColours import MiRCARTColours -from MiRCARTFromTextFile import MiRCARTFromTextFile -from MiRCARTToTextFile import MiRCARTToTextFile import os, wx try: @@ -194,31 +192,26 @@ class MiRCARTFrame(wx.Frame): if dialog.ShowModal() == wx.ID_CANCEL: return False else: - outPathName = dialog.GetPath() - outTmpFile = io.StringIO() - outToTextFile = MiRCARTToTextFile( \ - self.panelCanvas.canvasMap, self.canvasSize) - outToTextFile.export(outTmpFile) - MiRCARTToPngFile(tmpFile).export(outPathName) - return True + try: + outPathName = dialog.GetPath() + self.panelCanvas.exportPngFile(outhPathName) + return True + except IOError as error: + pass # }}} # {{{ canvasExportPastebin(self): XXX def canvasExportPastebin(self): - MiRCARTToPastebin("", \ - self.panelCanvas.canvasMap, self.canvasSize).export() + try: + self.panelCanvas.exportPastebin("") + return True + except IOError as error: + pass # }}} - # {{{ canvasNew(self, canvasPos=None, canvasSize=None, cellSize=None): XXX - def canvasNew(self, canvasPos=None, canvasSize=None, cellSize=None): - canvasPos = canvasPos if canvasPos != None else self.canvasPos - canvasSize = canvasSize if canvasSize != None else self.canvasSize - cellSize = cellSize if cellSize != None else self.cellSize - if self.panelCanvas != None: - self.panelCanvas.Close(); self.panelCanvas = None; - self.canvasPos = canvasPos; self.canvasSize = canvasSize; - self.cellSize = cellSize - self.panelCanvas = MiRCARTCanvas(self.panelSkin, parentFrame=self, \ - canvasPos=self.canvasPos, cellSize=self.cellSize, \ - canvasSize=self.canvasSize, canvasTools=self.canvasTools) + # {{{ canvasNew(self, newCanvasSize=None): XXX + def canvasNew(self, newCanvasSize=None): + if newCanvasSize == None: + newCanvasSize = (100, 30) + self.panelCanvas.canvasStore.importNew(newCanvasSize) self._updateStatusBar(); self.onCanvasUpdate(); # }}} # {{{ canvasOpen(self): XXX @@ -229,23 +222,10 @@ class MiRCARTFrame(wx.Frame): return False else: self.canvasPathName = dialog.GetPath() - with open(self.canvasPathName, "r") as newFile: - newFromTextFile = MiRCARTFromTextFile(newFile) - newMap = newFromTextFile.getMap() - self.canvasNew(canvasSize=newFromTextFile.getSize()) - eventDc = wx.ClientDC(self); tmpDc = wx.MemoryDC(); - tmpDc.SelectObject(self.panelCanvas.canvasBitmap) - for newNumRow in range(0, len(newMap)): - for newNumCol in range(0, len(newMap[newNumRow])): - self.panelCanvas.onJournalUpdate(False, \ - (newNumCol, newNumRow), \ - [newNumCol, newNumRow, \ - newMap[newNumRow][newNumCol][0][0], \ - newMap[newNumRow][newNumCol][0][1], \ - newMap[newNumRow][newNumCol][2]], \ - eventDc, tmpDc, (0, 0)) - wx.SafeYield() - return True + self.panelCanvas.canvasStore.importTextFile(self.canvasPathName) + self.panelCanvas.canvasStore.importIntoPanel() + self._updateStatusBar(); self.onCanvasUpdate(); + return True # }}} # {{{ canvasSave(self): XXX def canvasSave(self): @@ -253,9 +233,8 @@ class MiRCARTFrame(wx.Frame): if self.canvasSaveAs() == False: return try: - with open(self.canvasPathName, "w") as outFile: - MiRCARTToTextFile(self.panelCanvas.canvasMap, \ - self.panelCanvas.canvasSize).export(outFile) + self.panelCanvas.exportTextFile(self.canvasPathName) + return True except IOError as error: pass # }}} @@ -345,10 +324,11 @@ class MiRCARTFrame(wx.Frame): def __init__(self, parent, appSize=(800, 600), canvasPos=(25, 50), cellSize=(7, 14), canvasSize=(100, 30), canvasTools=[]): super().__init__(parent, wx.ID_ANY, "MiRCART", size=appSize) self.panelSkin = wx.Panel(self, wx.ID_ANY) + self.canvasPos = canvasPos; self.cellSize = cellSize; self.canvasSize = canvasSize; self.canvasPathName = None self.menuItemsById = {}; self.menuBar = wx.MenuBar(); - self._initMenus(self.menuBar, \ + self._initMenus(self.menuBar, \ [self.MID_FILE, self.MID_EDIT, self.MID_TOOLS], self.onFrameCommand) self.SetMenuBar(self.menuBar) if not haveMiRCARTToPastebin: @@ -356,13 +336,13 @@ class MiRCARTFrame(wx.Frame): if not haveMiRCARTToPngFile: self.menuItemsById[self.CID_EXPORT_AS_PNG[0]].Enable(False) - self.toolBar = wx.ToolBar(self.panelSkin, -1, \ + self.toolBar = wx.ToolBar(self.panelSkin, -1, \ style=wx.HORIZONTAL|wx.TB_FLAT|wx.TB_NODIVIDER) self.toolBar.SetToolBitmapSize((16,16)) self._initToolBars(self.toolBar, [self.BID_TOOLBAR], self.onFrameCommand) self.toolBar.Realize(); self.toolBar.Fit(); - self.accelTable = wx.AcceleratorTable( \ + self.accelTable = wx.AcceleratorTable( \ self._initAccelTable(self.AID_EDIT, self.onFrameCommand)) self.SetAcceleratorTable(self.accelTable) @@ -370,6 +350,9 @@ class MiRCARTFrame(wx.Frame): self.statusBar = self.CreateStatusBar(); self.SetFocus(); self.Show(True); self.canvasTools = canvasTools - self.canvasNew(canvasPos, canvasSize, cellSize) + self.panelCanvas = MiRCARTCanvas(self.panelSkin, parentFrame=self, \ + canvasPos=self.canvasPos, canvasSize=self.canvasSize, \ + canvasTools=self.canvasTools, cellSize=self.cellSize) + self.canvasNew() # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 diff --git a/MiRCARTToPastebin.py b/MiRCARTToPastebin.py deleted file mode 100644 index 88b25bc..0000000 --- a/MiRCARTToPastebin.py +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env python3 -# -# MiRCARTToPastebin.py -- XXX -# Copyright (c) 2018 Lucio Andrés Illanes Albornoz -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# - -from MiRCARTToTextFile import MiRCARTToTextFile -import io -import base64 -import requests, urllib.request - -class MiRCARTToPastebin(): - apiDevKey = outFile = outToTextFile = None - - # export(self): XXX - def export(self): - self.outToTextFile.export(self.outFile, pasteName="", pastePrivate=0) - requestData = { \ - "api_dev_key": self.apiDevKey, \ - "api_option": "paste", \ - "api_paste_code": base64.b64encode(self.outFile.read()), \ - "api_paste_name": pasteName, \ - "api_paste_private": pastePrivate} - responseHttp = requests.post("https://pastebin.com/post.php", \ - data=requestData) - if responseHttp.status_code == 200: - return responseHttp.text - else: - return None - - # __init__(self, canvasMap, canvasSize): XXX - def __init__(self, apiDevKey, canvasMap, canvasSize): - self.apiDevKey = apiDevKey - self.outFile = io.StringIO() - self.outToTextFile = MiRCARTToTextFile(canvasMap, canvasSize) - -# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 diff --git a/MiRCARTToTextFile.py b/MiRCARTToTextFile.py deleted file mode 100644 index ee0950d..0000000 --- a/MiRCARTToTextFile.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env python3 -# -# MiRCARTToTextFile.py -- XXX -# Copyright (c) 2018 Lucio Andrés Illanes Albornoz -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# - -class MiRCARTToTextFile(): - canvasMap = canvasSize = None - - # export(self, outFile): XXX - def export(self, outFile): - for canvasRow in range(0, self.canvasSize[1]): - canvasLastColours = [] - for canvasCol in range(0, self.canvasSize[0]): - canvasColColours = self.canvasMap[canvasRow][canvasCol][0:2] - canvasColText = self.canvasMap[canvasRow][canvasCol][2] - if canvasColColours != canvasLastColours: - canvasLastColours = canvasColColours - outFile.write("\x03" + \ - str(canvasColColours[0]) + \ - "," + str(canvasColColours[1])) - outFile.write(canvasColText) - outFile.write("\n") - - # __init__(self, canvasMap, canvasSize): XXX - def __init__(self, canvasMap, canvasSize): - self.canvasMap = canvasMap; self.canvasSize = canvasSize; - -# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120