[go: up one dir, main page]

Menu

[r868]: / Segtool / DrawingArea.py  Maximize  Restore  History

Download this file

146 lines (124 with data), 4.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#
# 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)