#!/usr/bin/env python
modules = {'DrawingArea' : [0, '', 'none://DrawingArea.py' ],
'Section' : [0, '', 'none://Section.py' ]
}
import wx
import os
from lxml import etree
from Section import *
from DrawingArea import DrawingArea
class MainWindow (wx.Frame):
def __init__ (self, parent, id, title):
wx.Frame.__init__(self,parent,id, title, size=(600,600))
self.CreateMenu()
self.CreateToolbar()
self.CreateDisplay()
self.nimages = 0
self.current_slice = None
self.current_set = None
self.current_set_name = None
def CreateMenu(self):
menuBar = wx.MenuBar()
Filemenu = wx.Menu()
FileOpenSet = Filemenu.Append(wx.ID_NEW, "&New Set")
FileOpenSet = Filemenu.Append(wx.ID_OPEN, "&Open Set")
FileSaveSet = Filemenu.Append(wx.ID_SAVE, "&Save Set")
FileSaveSetAs = Filemenu.Append(wx.ID_SAVEAS, "Save Set &As")
Filemenu.AppendSeparator()
FileOpenImage = Filemenu.Append(-1, "Import &Images")
Filemenu.AppendSeparator()
FileExit = Filemenu.Append(wx.ID_EXIT, "E&xit")
menuBar.Append(Filemenu, "&File")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnFileExit, FileExit)
self.Bind(wx.EVT_MENU, self.OnFileOpenImage, FileOpenImage)
self.Bind(wx.EVT_MENU, self.OnFileOpenSet, FileOpenSet)
self.Bind(wx.EVT_MENU, self.OnFileSaveSet, FileSaveSet)
self.Bind(wx.EVT_MENU, self.OnFileSaveSetAs, FileSaveSetAs)
def CreateToolbar(self):
ap = wx.ArtProvider()
toolbar = self.CreateToolBar()
toolbar.AddSimpleTool(wx.ID_NEW, ap.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR), \
"New", "Create a new working set")
toolbar.AddSimpleTool(wx.ID_OPEN, ap.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR),\
"Open", "Open a new working set")
toolbar.AddSimpleTool(wx.ID_SAVE, ap.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR), \
"Save", "Save the current working set")
toolbar.AddSimpleTool(wx.ID_SAVEAS, ap.GetBitmap(wx.ART_FILE_SAVE_AS, wx.ART_TOOLBAR), \
"Save", "Save the current working set")
toolbar.AddSeparator()
toolbar.AddSimpleTool(wx.ID_EXIT, ap.GetBitmap(wx.ART_QUIT, wx.ART_TOOLBAR),\
"Exit", "Exit program")
toolbar.Realize()
def CreateDisplay(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.ImageNumSelect = wx.Slider(self, 0, 0, 0, 1, \
style=wx.SL_LABELS|wx.SL_VERTICAL)
self.ImageNumSelect.SetPageSize(1)
self.ImageNumSelect.SetMinSize((16,512))
sizer.Add(self.ImageNumSelect, flag=wx.ALIGN_LEFT)
self.Bind(wx.EVT_SLIDER, self.OnChangeImage, self.ImageNumSelect)
self.da = DrawingArea(self)
self.da.SetMinSize((800,800))
sizer.Add(self.da, flag=wx.ALIGN_LEFT)
self.SetSizer(sizer)
self.Fit()
def OnChangeImage(self, event):
image_nr = self.ImageNumSelect.GetValue()
if image_nr < self.nimages:
self.current_slice = self.current_set.GetFrame(image_nr)
self.da.SetSlice( self.current_slice )
def OnFileExit(self, event):
self.Close(True)
def OnFileOpenImage(self, event):
wildcard = "PNG Images (*.png)|*.png|All Files (*.*)|*.*"
flags = wx.OPEN | wx.MULTIPLE
files = self.FileOpen("Open Image files", wildcard, True)
if not files:
return
self.current_set = WorkSet(images=files)
nimages = self.current_set.GetFrameNumber()
self.ImageNumSelect.SetRange(0, nimages - 1)
if nimages > 0:
self.da.SetCurrentBitmap(self.current_set.GetFrame(0))
def FileOpen(self, message, wildcard, multiple):
if multiple:
flags = wx.OPEN |wx.MULTIPLE
else:
flags = wx.OPEN
dialog = wx.FileDialog(self, message, os.getcwd(),"", wildcard, flags)
if dialog.ShowModal() == wx.ID_OK:
if multiple:
files = dialog.GetPaths()
else:
files = dialog.GetPath()
else:
files = None
dialog.Destroy()
return files
def OnFileOpenSet(self, event):
wildcard = "Workset (*.set)|*.set|All Files (*.*)|*.*"
file = self.FileOpen("Open Working set", wildcard, False)
print file
def SaveSet(self):
data = self.current_set.Save()
file = open(self.current_set_name, "w")
file.write(data)
file.close()
def OnFileSaveSet(self, event):
if not self.current_set_name:
self.OnFileSaveSetAs(self, event)
else:
self.SaveSet()
def OnFileSaveSetAs(self, event):
wildcard = "Workset Images (*.set)|*.set"
flags = wx.SAVE
dialog = wx.FileDialog(self, "Save Workset", os.getcwd(),"", wildcard, wx.SAVE)
if dialog.ShowModal() == wx.ID_OK:
self.current_set_name = dialog.GetPath()
if self.current_set[-4:0]!= ".set":
self.current_set_name = self.current_set + ".set"
self.SaveSet()
dialog.Destroy()
class App(wx.App):
def OnInit(self):
self.frame = MainWindow(parent=None, id=-1, title='Segment')
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app = App()
app.MainLoop()