SHARE
TWEET

Untitled

a guest Jun 19th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: iso-8859-15 -*-
  3. # generated by wxGlade 0.6.3 on Wed Oct 06 08:05:30 2010
  4.  
  5. import wx
  6.  
  7. # begin wxGlade: extracode
  8. # end wxGlade
  9.  
  10.  
  11.  
  12. class drawFrame(wx.Frame):
  13.     menuColours = { 100 : 'Black'}
  14.     maxThickness = 16
  15.     def __init__(self, *args, **kwds):
  16.         # begin wxGlade: drawFrame.__init__
  17.         kwds["style"] = wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER|wx.RESIZE_BOX|wx.MAXIMIZE_BOX)
  18.         wx.Frame.__init__(self, *args, **kwds)
  19.         self.SetSizeHints(300,400,300,400)
  20.        
  21.         self.listeners = []
  22.         self.thickness = 3
  23.         self.SetColour("Black")
  24.         self.lines = []
  25.         self.pos = wx.Point(0,0)
  26.         self.MakeMenu()
  27.  
  28.         self.clearButton = wx.Button(self, -1, "Clear")
  29.         self.bitmap_1 = wx.StaticBitmap(self, -1, wx.Bitmap("C:\\pyoctane\\trunk\\pixmaps\\1250693098_back.png", wx.BITMAP_TYPE_ANY))
  30.         self.submitButton = wx.Button(self, -1, "Submit")
  31.  
  32.         self.__set_properties()
  33.         self.__do_layout()
  34.         # end wxGlade
  35.  
  36.     def __set_properties(self):
  37.         # begin wxGlade: drawFrame.__set_properties
  38.         self.SetTitle("frame_1")
  39.         self.SetBackgroundColour(wx.Colour(244, 244, 244))
  40.         # end wxGlade
  41.  
  42.     def __do_layout(self):
  43.         # begin wxGlade: drawFrame.__do_layout
  44.         sizer_1 = wx.BoxSizer(wx.VERTICAL)
  45.         sizer_1.Add(self.clearButton, 0, 0, 0)
  46.         sizer_1.Add(self.bitmap_1, 0, 0, 0)
  47.         sizer_1.Add(self.submitButton, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
  48.         self.SetSizer(sizer_1)
  49.         sizer_1.Fit(self)
  50.         sizer_1.SetSizeHints(self)
  51.         self.Layout()
  52.         self.Centre()
  53.         # end wxGlade
  54.  
  55.     def Cleanup(self, evt):
  56.         if hasattr(self, "menu"):
  57.             self.menu.Destroy()
  58.             del self.menu
  59.  
  60.  
  61.     def InitBuffer(self):
  62.         """Initialize the bitmap used for buffering the display."""
  63.         size = self.GetClientSize()
  64.         self.buffer = wx.EmptyBitmap(max(1,size.width), max(1,size.height))
  65.         self.dc = wx.BufferedDC(None, self.buffer)
  66.         self.dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
  67.         self.dc.Clear()
  68.         self.DrawLines(self.dc)
  69.         self.reInitBuffer = False
  70.  
  71.  
  72.     def SetColour(self, colour):
  73.         """Set a new colour and make a matching pen"""
  74.         self.colour = colour
  75.         self.pen = wx.Pen(self.colour, self.thickness, wx.SOLID)
  76.         self.Notify()
  77.  
  78.  
  79.     def SetThickness(self, num):
  80.         """Set a new line thickness and make a matching pen"""
  81.         self.thickness = num
  82.         self.pen = wx.Pen(self.colour, self.thickness, wx.SOLID)
  83.         self.Notify()
  84.  
  85.  
  86.     def GetLinesData(self):
  87.         return self.lines[:]
  88.  
  89.  
  90.     def SetLinesData(self, lines):
  91.         self.lines = lines[:]
  92.         self.InitBuffer()
  93.         self.Refresh()
  94.  
  95.  
  96.     def MakeMenu(self):
  97.         """Make a menu that can be popped up later"""
  98.         menu = wx.Menu()
  99.         keys = self.menuColours.keys()
  100.         keys.sort()
  101.         for k in keys:
  102.             text = self.menuColours[k]
  103.             menu.Append(k, text, kind=wx.ITEM_CHECK)
  104.         self.Bind(wx.EVT_MENU_RANGE, self.OnMenuSetColour, id=100, id2=200)
  105.         self.Bind(wx.EVT_UPDATE_UI_RANGE, self.OnCheckMenuColours, id=100, id2=200)
  106.         menu.Break()
  107.  
  108.         for x in range(1, self.maxThickness+1):
  109.             menu.Append(x, str(x), kind=wx.ITEM_CHECK)
  110.  
  111.         self.Bind(wx.EVT_MENU_RANGE, self.OnMenuSetThickness, id=1, id2=self.maxThickness)
  112.         self.Bind(wx.EVT_UPDATE_UI_RANGE, self.OnCheckMenuThickness, id=1, id2=self.maxThickness)
  113.         self.menu = menu
  114.  
  115.  
  116.     # These two event handlers are called before the menu is displayed
  117.     # to determine which items should be checked.
  118.     def OnCheckMenuColours(self, event):
  119.         text = self.menuColours[event.GetId()]
  120.         if text == self.colour:
  121.             event.Check(True)
  122.             event.SetText(text.upper())
  123.         else:
  124.             event.Check(False)
  125.             event.SetText(text)
  126.  
  127.     def OnCheckMenuThickness(self, event):
  128.         if event.GetId() == self.thickness:
  129.             event.Check(True)
  130.         else:
  131.             event.Check(False)
  132.  
  133.  
  134.     def OnLeftDown(self, event):
  135.         """called when the left mouse button is pressed"""
  136.         self.curLine = []
  137.         self.pos = event.GetPosition()
  138.         self.CaptureMouse()
  139.  
  140.  
  141.     def OnLeftUp(self, event):
  142.         """called when the left mouse button is released"""
  143.         if self.HasCapture():
  144.             self.lines.append( (self.colour, self.thickness, self.curLine) )
  145.             self.curLine = []
  146.             self.ReleaseMouse()
  147.  
  148.  
  149.     def OnRightUp(self, event):
  150.         """called when the right mouse button is released, will popup the menu"""
  151.         pt = event.GetPosition()
  152.         self.PopupMenu(self.menu, pt)
  153.  
  154.  
  155.  
  156.     def OnMotion(self, event):
  157.         """
  158.        Called when the mouse is in motion.  If the left button is
  159.        dragging then draw a line from the last event position to the
  160.        current one.  Save the coordinants for redraws.
  161.        """
  162.         if event.Dragging() and event.LeftIsDown():
  163.             dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
  164.             dc.BeginDrawing()
  165.             dc.SetPen(self.pen)
  166.             pos = event.GetPosition()
  167.             coords = (self.pos.x, self.pos.y, pos.x, pos.y)
  168.             self.curLine.append(coords)
  169.             dc.DrawLine(*coords)
  170.             self.pos = pos
  171.             dc.EndDrawing()
  172.  
  173.  
  174.     def OnSize(self, event):
  175.         """
  176.        Called when the window is resized.  We set a flag so the idle
  177.        handler will resize the buffer.
  178.        """
  179.         self.reInitBuffer = True
  180.  
  181.  
  182.     def OnIdle(self, event):
  183.         """
  184.        If the size was changed then resize the bitmap used for double
  185.        buffering to match the window size.  We do it in Idle time so
  186.        there is only one refresh after resizing is done, not lots while
  187.        it is happening.
  188.        """
  189.         if self.reInitBuffer:
  190.             self.InitBuffer()
  191.             self.Refresh(False)
  192.  
  193.  
  194.     def OnPaint(self, event):
  195.         """
  196.        Called when the window is exposed.
  197.        """
  198.         # Create a buffered paint DC.  It will create the real
  199.         # wx.PaintDC and then blit the bitmap to it when dc is
  200.         # deleted.  Since we don't need to draw anything else
  201.         # here that's all there is to it.
  202.         dc = wx.BufferedPaintDC(self, self.buffer)
  203.  
  204.  
  205.     def DrawLines(self, dc):
  206.         """
  207.        Redraws all the lines that have been drawn already.
  208.        """
  209.         self.dc.BeginDrawing()
  210.         for colour, thickness, line in self.lines:
  211.             pen = wx.Pen(colour, thickness, wx.SOLID)
  212.             self.dc.SetPen(pen)
  213.             for coords in line:
  214.                 self.dc.DrawLine(*coords)
  215.         self.dc.EndDrawing()
  216.  
  217.  
  218.     # Event handlers for the popup menu, uses the event ID to determine
  219.     # the colour or the thickness to set.
  220.     def OnMenuSetColour(self, event):
  221.         self.SetColour(self.menuColours[event.GetId()])
  222.  
  223.     def OnMenuSetThickness(self, event):
  224.         self.SetThickness(event.GetId())
  225.  
  226.  
  227.     # Observer pattern.  Listeners are registered and then notified
  228.     # whenever doodle settings change.
  229.     def AddListener(self, listener):
  230.         self.listeners.append(listener)
  231.  
  232.     def Notify(self):
  233.         for other in self.listeners:
  234.             other.Update(self.colour, self.thickness)
  235.  
  236.     def clear(self, event):
  237.         print "clear"
  238.         self.dc.Clear()
  239.        
  240. # end of class drawFrame
  241.  
  242.  
  243. if __name__ == "__main__":
  244.     app = wx.PySimpleApp(0)
  245.     wx.InitAllImageHandlers()
  246.     drawFrame = drawFrame(None, -1, "")
  247.     app.SetTopWindow(drawFrame)
  248.     drawFrame.Show()
  249.     app.MainLoop()
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top