#coding=utf-8
'''
Created on 2013年12月13日
@author: hxp
'''
import wx
import SketchWindow
import cPickle
import os
class MenuSketchFrame(wx.Frame):
'''
classdocs
'''
def __init__(self):
'''
Constructor
'''
wx.Frame.__init__(self,None,-1,"menu sketch frame",size=(1000,600))
self.sketch=SketchWindow.SketchWindow(self)
self.sketch.Bind(wx.EVT_MOTION, self.onMove)
self.fileName="a.sketch"
self.wildcard="Sketch files (*.sketch)|*.sketch|All files (*.*)|*.*"
self.initStatuBar()
self.initMenuBar()
self.createPanel()
def initStatuBar(self):
self.statuBar=self.CreateStatusBar()
self.statuBar.SetFieldsCount(3)
self.statuBar.SetStatusWidths([-1,-2,-3])
def onMove(self,evt):
self.statuBar.SetStatusText(str(evt.GetPositionTuple()),0)
self.statuBar.SetStatusText("current lines %s"%str(len(self.sketch.curLine)),1)
self.statuBar.SetStatusText("total lines %s"%str(len(self.sketch.lines)),2)
evt.Skip()
def menuData(self):
return [("&File",(("&New","New file",self.onNewFile),("&Open","open file",self.onOpenFile),("&Save","save file",self.onSaveFile),("","","")
,("&Color",(("&Black","black",self.onColor,wx.ITEM_RADIO),("&Red","red",self.onColor,wx.ITEM_RADIO),("&Green","green",self.onColor,wx.ITEM_RADIO),("&Blue","blue",self.onColor,wx.ITEM_RADIO),("&Other","other",self.onOther))),
("","",""),
("&Quit","quit",self.onCloseWindow)))]
def initMenuBar(self):
menuBar=wx.MenuBar()
for menuData in self.menuData():
label=menuData[0]
menuItem=menuData[1]
menuBar.Append(self.createMenu(menuItem),label)
self.SetMenuBar(menuBar)
def createMenu(self,menuData):
menu=wx.Menu()
for item in menuData:
if len(item)==2:
label=item[0]
submenu=self.createMenu(item[1])
menu.AppendMenu(-1,label,submenu)
else:
self.createMenuItem(menu,*item)
return menu
def createMenuItem(self,menu,label,status,handler,kind=wx.ITEM_NORMAL):
if not label:
menu.AppendSeparator()
else:
menuItem=menu.Append(-1,label,status,kind)
self.Bind(wx.EVT_MENU,handler,menuItem)
def onNewFile(self,evt):
pass
def onOpenFile(self,evt):
dlg=wx.FileDialog(self,"open sketch file...",os.getcwd(),style=wx.OPEN,wildcard=self.wildcard)
if dlg.ShowModal()==wx.ID_OK:
self.fileName=dlg.GetPath()
self.readFile()
self.SetTitle(self.Title+"---"+self.fileName)
dlg.Destroy()
def readFile(self):
if self.fileName:
try:
f=open(self.fileName,"r")
data=cPickle.load(f)
f.close()
self.sketch.setLineData(data)
except:
wx.MessageBox("error","read file error")
def onSaveFile(self,evt):
if self.fileName:
data=self.sketch.getLineData()
f=open(self.fileName,"w")
print data
cPickle.dump(data, f)
f.close()
evt.Skip()
def onOther(self,evt):
dlg=wx.ColourDialog(self)
dlg.GetColourData().SetChooseFull(True)
if dlg.ShowModal()==wx.ID_OK:
self.sketch.setColor(dlg.GetColourData().GetColour())
dlg.Destroy()
evt.Skip()
def onColor(self,evt):
menuBar=self.GetMenuBar()
id=evt.GetId()
item=menuBar.FindItemById(id)
color=item.GetLabel()
self.sketch.setColor(color)
def onCloseWindow(self,evt):
pass
def createPanel(self):
controlPanel=ControlPanel(self,-1,self.sketch)
box=wx.BoxSizer(wx.HORIZONTAL)
box.Add(controlPanel,0,wx.EXPAND)
box.Add(self.sketch,0,wx.EXPAND)
self.SetSizer(box)
class ControlPanel(wx.Panel):
BMP_SIZE=16
BMP_BORDER=16
NUM_COLOR=4
SPACE=4
colorList=["black","yellow","red","green","blue","purple"]
maxThickness=16
def __init__(self,parent,id,sketch):
wx.Panel.__init__(self,parent,id,style=wx.RAISED_BORDER)
self.sketch=sketch
buttonSize=(self.BMP_SIZE+2*self.BMP_BORDER,self.BMP_SIZE+2*self.BMP_BORDER)
colorGrid=self.createColorGrid(parent,buttonSize)
thickGrid=self.createThickGrid(buttonSize)
self.layout(colorGrid,thickGrid)
def createColorGrid(self,parent,buttonSize):
self.colorMap={}
self.colorButton={}
colorGrid=wx.GridSizer(cols=self.NUM_COLOR,hgap=2,vgap=2)
for color in self.colorList:
b=wx.ToggleButton(self,-1,color,size=buttonSize)
self.Bind(wx.EVT_TOGGLEBUTTON, self.onColor, b)
colorGrid.Add(b,0)
self.colorMap[b.GetId()]=color
self.colorButton[color]=b
return colorGrid
def createThickGrid(self,buttonSize):
self.thicknessMap={}
self.thicknessButton={}
thicknessGrid=wx.GridSizer(cols=self.NUM_COLOR,hgap=2,vgap=2)
for thickness in range(1,self.maxThickness+1):
b=wx.ToggleButton(self,-1,str(thickness),size=buttonSize)
self.Bind(wx.EVT_TOGGLEBUTTON, self.onThickness, b)
thicknessGrid.Add(b,0)
self.thicknessMap[b.GetId()]=thickness
self.thicknessMap[thickness]=b
return thicknessGrid
def layout(self,colorGrid,thickGrid):
box=wx.BoxSizer(wx.VERTICAL)
box.Add(colorGrid,0,wx.ALL,self.SPACE)
box.Add(thickGrid,0,wx.ALL,self.SPACE)
self.SetSizer(box)
box.Fit(self)
def onColor(self,evt):
color=self.colorMap[evt.GetId()]
self.sketch.setColor(color)
evt.Skip()
def onThickness(self,evt):
thick=self.thicknessMap[evt.GetId()]
self.sketch.setThick(thick)
evt.Skip()
if __name__=="__main__":
app=wx.PySimpleApp()
frame=MenuSketchFrame()
frame.Show()
app.MainLoop()