diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1906dfc --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\Felix Gateru\\AppData\\Local\\Programs\\Python\\Python37\\python.exe" +} \ No newline at end of file diff --git a/__pycache__/constants.cpython-37.pyc b/__pycache__/constants.cpython-37.pyc new file mode 100644 index 0000000..9a8be7d Binary files /dev/null and b/__pycache__/constants.cpython-37.pyc differ diff --git a/__pycache__/controller.cpython-37.pyc b/__pycache__/controller.cpython-37.pyc new file mode 100644 index 0000000..b352217 Binary files /dev/null and b/__pycache__/controller.cpython-37.pyc differ diff --git a/__pycache__/drawingmodel.cpython-37.pyc b/__pycache__/drawingmodel.cpython-37.pyc new file mode 100644 index 0000000..98a8c5f Binary files /dev/null and b/__pycache__/drawingmodel.cpython-37.pyc differ diff --git a/__pycache__/drwingpanel.cpython-37.pyc b/__pycache__/drwingpanel.cpython-37.pyc new file mode 100644 index 0000000..e932290 Binary files /dev/null and b/__pycache__/drwingpanel.cpython-37.pyc differ diff --git a/__pycache__/frame.cpython-37.pyc b/__pycache__/frame.cpython-37.pyc new file mode 100644 index 0000000..0724933 Binary files /dev/null and b/__pycache__/frame.cpython-37.pyc differ diff --git a/__pycache__/menubar.cpython-37.pyc b/__pycache__/menubar.cpython-37.pyc new file mode 100644 index 0000000..559992b Binary files /dev/null and b/__pycache__/menubar.cpython-37.pyc differ diff --git a/__pycache__/toolbar.cpython-37.pyc b/__pycache__/toolbar.cpython-37.pyc new file mode 100644 index 0000000..e95b3d0 Binary files /dev/null and b/__pycache__/toolbar.cpython-37.pyc differ diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..f422ef7 --- /dev/null +++ b/constants.py @@ -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" + + + + + diff --git a/controller.py b/controller.py new file mode 100644 index 0000000..b1ef733 --- /dev/null +++ b/controller.py @@ -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) diff --git a/drawincontroller.py b/drawincontroller.py new file mode 100644 index 0000000..aef519b --- /dev/null +++ b/drawincontroller.py @@ -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() diff --git a/drawingmodel.py b/drawingmodel.py new file mode 100644 index 0000000..6dff108 --- /dev/null +++ b/drawingmodel.py @@ -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) \ No newline at end of file diff --git a/drwingpanel.py b/drwingpanel.py new file mode 100644 index 0000000..50694aa --- /dev/null +++ b/drwingpanel.py @@ -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) \ No newline at end of file diff --git a/figure.py b/figure.py new file mode 100644 index 0000000..dd90003 --- /dev/null +++ b/figure.py @@ -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) \ No newline at end of file diff --git a/frame.py b/frame.py new file mode 100644 index 0000000..80b6f60 --- /dev/null +++ b/frame.py @@ -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() diff --git a/menubar.py b/menubar.py new file mode 100644 index 0000000..45f3127 --- /dev/null +++ b/menubar.py @@ -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") + + diff --git a/pydrawapp.py b/pydrawapp.py new file mode 100644 index 0000000..20bc591 --- /dev/null +++ b/pydrawapp.py @@ -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() \ No newline at end of file diff --git a/toolbar.py b/toolbar.py new file mode 100644 index 0000000..729134b --- /dev/null +++ b/toolbar.py @@ -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() \ No newline at end of file