#
# Copyright (c) Madrid 2008
# BIT, ETSI Telecomunicacion, UPM
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
import wx
from Section import Section
from Section import GlobalColors
#
# this class implements a viewer for images and
# the tools to draw a colored overlay for segmentation
#
class DrawingArea (wx.Window):
def __init__ (self, parent):
wx.Window.__init__(self, parent, size=(800,800))
self.set = None
self.current_image = None
self.sections = []
self.index = 0
self.Pen = wx.Pen(GlobalColors[self.index], 1, wx.SOLID)
self.pos = None
self.curLines = []
self.InitBuffer()
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_IDLE, self.OnIdle)
self.Bind(wx.EVT_RIGHT_DOWN, self.OnMouseRightDown)
self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
self.Bind(wx.EVT_LEFT_DCLICK, self.OnMouseLeftDClick)
# self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
def ResizeBuffer(self):
size = self.GetClientSize()
self.buffer = wx.EmptyBitmap(size.width, size.height)
self.InitBuffer()
def InitBuffer(self):
dc = wx.BufferedDC(None, self.buffer)
dc.Clear()
self.DrawCurrentBitmap(dc)
self.Drawsections(dc)
self.reInitBuffer = False
def OnSize(self, size):
self.ResizeBuffer()
def OnIdle(self, dummy):
if self.reInitBuffer:
self.InitBuffer()
self.Refresh(False)
def OnPaint(self, event):
dc=wx.BufferedPaintDC(self, self.buffer)
def OnMouseRightDown(self, event):
if len(self.curLines) > 0:
self.curLines = []
else:
if len(self.sections) > 0:
self.sections.pop()
self.index = self.index - 1
self.Pen = wx.Pen(GlobalColors[self.index], 1, wx.SOLID)
self.pos = None
self.reInitBuffer = True
self.Refresh(False)
def OnMouseLeftDClick(self, event):
if len(self.curLines) > 2:
self.sections.append(Section(self.index, self.curLines))
self.curLines = []
self.pos = None
self.reInitBuffer = True
self.index = self.index + 1
if self.index > 5:
self.index = 0
self.Pen = wx.Pen(GlobalColors[self.index], 1, wx.SOLID)
self.Refresh(False)
def OnMouseLeftDown(self, event):
dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
dc.SetPen(self.Pen)
if self.pos:
newPos = event.GetPositionTuple()
coords = self.pos + newPos
dc.DrawLine(*coords)
self.pos = newPos
else:
self.pos = event.GetPositionTuple()
dc.DrawPoint(self.pos[0], self.pos[1])
self.curLines.append(self.pos)
def OnMouseMotion(self, event):
if self.pos:
dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
self.DrawMotion(dc,event)
event.Skip()
def DrawCurrentBitmap(self, dc):
if self.current_image:
dc.DrawBitmap(self.current_image, 0,0, True)
def SetSlice(self, set):
self.set = set
self.sections = set.GetSections(self.GetClientSize())
self.Refresh(False)
def GetSlice():
self.set.SetSections(self.sections, self.GetClientSize())
def SetCurrentBitmap(self, image):
size = self.GetClientSize()
if image:
self.current_image = image.Scale(size.width, size.height).ConvertToBitmap()
self.InitBuffer()
self.Refresh(False)
def Drawsections(self, dc):
for s in self.sections:
s.Draw(dc)
def Clearsections(self):
self.sections = []
self.index = 0
self.Pen = wx.Pen(GlobalColors[self.index], 1, wx.SOLID)