Skip to content

Commit

Permalink
initial commit for pydrawapp
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgateru committed Aug 28, 2020
1 parent 79ac847 commit 14b08e8
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "C:\\Users\\Felix Gateru\\AppData\\Local\\Programs\\Python\\Python37\\python.exe"
}
Binary file added __pycache__/constants.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/controller.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/drawingmodel.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/drwingpanel.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/frame.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/menubar.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/toolbar.cpython-37.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

class PyDraw_Constants:
LINE_ID = 100
SQUARE_ID = 102
CIRCLE_ID = 103
TEXT_ID = 104

SQUARE_MODE = "square"
LINE_MODE = "line"
CIRCLE_MODE = "circle"
TEXT_MODE = "text"





53 changes: 53 additions & 0 deletions controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import wx
from constants import PyDraw_Constants

class PyDrawController:

def __init__(self, view):
self.view = view
self.mode = PyDraw_Constants.SQUARE_MODE

def set_circle_mode(self):
self.mode = PyDraw_Constants.CIRCLE_MODE

def set_line_mode(self):
self.mode = PyDraw_Constants.LINE_MODE

def set_square_mode(self):
self.mode = PyDraw_Constants.SQUARE_MODE

def set_text_mode(self):
self.mode = PyDraw_Constants.TEXT_MODE

def clear_drawing(self):
self.view.drawing_controller.clear()

def get_mode(self):
return self.mode

def command_menu_handler(self, command_event):
id = command_event.GetId()
if id == wx.ID_NEW:
print('Clear the drawing area')
self.clear_drawing()
elif id == wx.ID_OPEN:
print('Open a drawing file')
elif id == wx.ID_SAVE:
print('Save a drawing file')
elif id == wx.ID_EXIT:
print('Quite the application')
self.view.Close()
elif id == PyDraw_Constants.LINE_ID:
print('set drawing mode to line')
self.set_line_mode()
elif id == PyDraw_Constants.SQUARE_ID:
print('set drawing mode to square')
self.set_square_mode()
elif id == PyDraw_Constants.CIRCLE_ID:
print('set drawing mode to circle')
self.set_circle_mode()
elif id == PyDraw_Constants.TEXT_ID:
print('set drawing mode to Text')
self.set_text_mode()
else:
print('Unknown option', id)
27 changes: 27 additions & 0 deletions drawincontroller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from constants import PyDraw_Constants

class DrawingController:
def __init__(self, view, model, get_mode):
self.view = view
self.model = model
self.get_mode = get_mode

def on_mouse_click(self, mouse_event):
point = mouse_event.GetPosition()
self.add(self.get_mode(), point)

def add(self, mode, point, size=30):
if mode == PyDraw_Constants.SQUARE_MODE:
fig = Square(self.view, point, wx.Size(size, size))
elif mode == PyDraw_Constants.CIRCLE_MODE:
fig = Circle(self.view, point, size)
elif mode == PyDraw_Constants.TEXT_MODE:
fig = Text(self.view, point, size)
elif mode == PyDraw_Constants.LINE_MODE:
fig = Line(self.view, point, size)

self.model.add_figure(fig)

def clear(self):
self.model.clear_figures()
self.view.Refresh()
10 changes: 10 additions & 0 deletions drawingmodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

class DrawingModel:
def __init__(self):
self.contents = []

def clear_figures(self):
self.contents = []

def add_figure(self, figure):
self.contents.append(figure)
16 changes: 16 additions & 0 deletions drwingpanel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from drawingmodel import DrawingModel
import wx

class DrawingPanel(wx.Panel):
def __init__(self,parent, get_mode):
super().__init__(parent,-1)
self.SetBackgroundColor(wx.Color(255,255,255))
self.model = DrawingModel()
self.controller = DrawingController(self, self.model,get_mode)
self.Bind(wx.EVT_PAINT, self.on_paint)
self.Bind(wx.EVT_LEFT_DOWN,self.controller.on_mouse_click)

def on_paint(self, event):
dc =wx.PaintDC(self)
for figure in self.model.contents:
figure.on_paint(dc)
42 changes: 42 additions & 0 deletions figure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

class Figure(wx.Panel):
def __init__(self, parent, id=wx.ID_ANY, pos=None,size=None, style=wx.TAB_TRAVERSAL):
wx.Panel.__init__(self, parent, id=id, pos=pos,size=size, style=style)
self.point = pos
self.size = size

@abstractmethod
def on_paint(self, dc):
pass

class Square(Figure):
def __init__(self, parent, pos, size):
super().__init__(parent=parent, pos=pos, size=size)

def on_paint(self, dc):
dc.DrawRectangle(self.point, self.size)

class Circle(Figure):
def __init__(self, parent, pos, size):
super().__init__(parent=parent, pos=pos,size=wx.Size(size, size))
self.radius = (size - 10) / 2
self.circle_center = wx.Point(self.point.x +self.radius, self.point.y + self.radius)

def on_paint(self, dc):
dc.DrawCircle(pt=self.circle_center,radius=self.radius)

class Line(Figure):
def __init__(self, parent, pos, size):
super().__init__(parent=parent, pos=pos,size=wx.Size(size, size))
self.end_point = wx.Point(self.point.x + size,self.point.y + size)

def on_paint(self, dc):
dc.DrawLine(pt1=self.point, pt2=self.end_point)


class Text(Figure):
def __init__(self, parent, pos, size):
super().__init__(parent=parent, pos=pos,size=wx.Size(size, size))

def on_paint(self, dc):
dc.DrawText(text='Text', pt=self.point)
23 changes: 23 additions & 0 deletions frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import wx
from controller import PyDrawController
from drwingpanel import DrawingPanel
from menubar import PyDrawMenuBar
from toolbar import PyDrawToolBar

class App_Frame(wx.Frame):
def __init__(self, title):
super().__init__(parent=None,title=title,size=(300,300))

self.controller = PyDrawController(self)
self.vertical_box_sizer = wx.BoxSizer(wx.VERTICAL)

self.SetSizer(self.vertical_box_sizer)

self.SetMenuBar(PyDrawMenuBar())

self.vertical_box_sizer.Add((PyDrawToolBar(self),wx.ID_ANY,wx.EXPAND | wx.ALL, 20 ))
self.drawing_panel = DrawingPanel(self, self.controller.get_mode)
self.drawing_controller = self.drawing_panel.controller
self.vertical_box_sizer.Add(self.drawing_panel,wx.ID_ANY,wx.EXPAND | wx.ALL)
self.Bind(wx.EVT_MENU, self.controller.command_menu_handler)
self.Center()
40 changes: 40 additions & 0 deletions menubar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import wx
from constants import PyDraw_Constants

class PyDrawMenuBar(wx.MenuBar):

def __init__(self):
super().__init__()
fileMenu = wx.Menu()
newMenuItem = wx.MenuItem(fileMenu,wx.ID_NEW, text="New",kind=wx.ITEM_NORMAL)
newMenuItem.SetBitmap(wx.Bitmap("new.gif"))
fileMenu.Append(newMenuItem)

openMenuItem = wx.MenuItem(fileMenu,wx.ID_OPEN, text="Open",kind=wx.ITEM_NORMAL)
openMenuItem.SetBitmap(wx.Bitmap("open.gif"))
fileMenu.Append(openMenuItem)

saveMenuItem = wx.MenuItem(fileMenu,wx.ID_SAVE, text="Save",kind=wx.ITEM_NORMAL)
saveMenuItem.SetBitmap(wx.Bitmap("save.gif"))
fileMenu.Append(saveMenuItem)

fileMenu.AppendSeparator()
quit = wx.MenuItem(fileMenu,wx.ID_EXIT,'&Quit\tCtrl+Q')

fileMenu.Append(quit)
self.Append(fileMenu, '&File')

drawingMenu = wx.Menu()
lineMenuItem = wx.MenuItem(drawingMenu,PyDraw_Constants.LINE_ID, text="Line", kind=wx.ITEM_NORMAL)
drawingMenu.Append(lineMenuItem)
squareMenuItem = wx.MenuItem(drawingMenu,PyDraw_Constants.SQUARE_ID, text="Square", kind=wx.ITEM_NORMAL)
drawingMenu.Append(squareMenuItem)
circleMenuItem = wx.MenuItem(drawingMenu,PyDraw_Constants.CIRCLE_ID, text="Circle", kind=wx.ITEM_NORMAL)
drawingMenu.Append(circleMenuItem)
textMenuItem = wx.MenuItem(drawingMenu,PyDraw_Constants.TEXT_ID, text="Text", kind=wx.ITEM_NORMAL)

drawingMenu.Append(textMenuItem)

self.Append(drawingMenu,"&Drawing")


14 changes: 14 additions & 0 deletions pydrawapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from frame import App_Frame
import wx

class PyDrawApp(wx.App):
def OnInit(self):
""" Initialise the GUI display"""
frame = App_Frame(title='PyDraw')
frame.Show()
return True


# Run the GUI application
app = PyDrawApp()
app.MainLoop()
9 changes: 9 additions & 0 deletions toolbar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import wx

class PyDrawToolBar(wx.ToolBar):
def __init__(self,parent):
super().__init__(parent)
self.AddTool(toolId=wx.ID_NEW, label="New",bitmap=wx.Bitmap("new.gif"), shortHelp='Open drawing',kind=wx.ITEM_NORMAL)
self.AddTool(toolId=wx.ID_OPEN, label="Open",bitmap=wx.Bitmap("load.gif"), shortHelp='Open drawing',kind=wx.ITEM_NORMAL)
self.AddTool(toolId=wx.ID_SAVE, label="Save",bitmap=wx.Bitmap("save.gif"), shortHelp="Save drawing", kind=wx.ITEM_NORMAL)
self.Realize()

0 comments on commit 14b08e8

Please sign in to comment.