class CircuitDropTarget(wx.TextDropTarget): def __init__(self, editor): wx.TextDropTarget.__init__(self) self._editor = editor self._model = Model() #FIXME use OnDragOver() to check if there can a module be placed def OnDropText(self, x, y, uri): if not self._model.checkURI(uri): raise Exception("Unknown module %s"%uri) if not re.match("^mod://",uri): raise Exception("Can only place modules here!") (x,y) = self._editor._convertCoords( (x,y) ) self._editor.loadModule(uri, x, y)
class eDevModuleEditor(ScrolledPanel, eDevEditorInterface): _d_txt = None def __init__(self, parent, ID, uri): ScrolledPanel.__init__(self, parent, ID) eDevEditorInterface.__init__(self, parent, False, uri) self._controller = Controller() self._model = Model() self._mainframe = self._controller.getMainFrame() self._component_manager = ComponentManager() self._moduletree = self._component_manager.getComponent("modeditor").getModuleTree() self._notebook = self._controller.getNotebook() if uri == "mod://": txt = '<?xml version="1.0"?><Module version="1.0"/>' title = "unsaved" else: txt = self._model.openURI(uri) title = getModuleName(uri) self.setTitle(title) self._d_doc = xml.dom.minidom.parseString(txt) vert_box = wx.BoxSizer(wx.VERTICAL) self._d_basic_data = eDevModuleEditorBasic(self, -1, self._d_doc) self._d_require = eDevModuleEditorRequire(self, -1, self._d_doc) self._d_provide = eDevModuleEditorProvide(self, -1, self._d_doc) self.Bind(Events.EVT_MODIFIED, self.OnModified, self._d_basic_data) self.Bind(Events.EVT_MODIFIED, self.OnModified, self._d_require) self.Bind(Events.EVT_MODIFIED, self.OnModified, self._d_provide) vert_box.Add(self._d_basic_data, 0, wx.EXPAND|wx.ALL, 15) vert_box.Add(self._d_require, 0, wx.EXPAND|wx.ALL, 15) vert_box.Add(self._d_provide, 0, wx.EXPAND|wx.ALL, 15) self.SetSizer(vert_box) self.SetAutoLayout(1) self.SetupScrolling() def OnModified(self, evt): #print "Something was modified" self.setModified() event = Events.PageModifiedEvent(Events._event_modified, self.GetId()) event.SetPage(self) self.GetEventHandler().ProcessEvent(event) evt.Skip() self._updateMainFrame() def OnSelected(self): self._updateMainFrame() def _updateMainFrame(self): self._mainframe.bindCopy() self._mainframe.bindCut() self._mainframe.bindPaste() self._mainframe.bindRedo() self._mainframe.bindUndo() self._mainframe.bindSaveAs(self.OnSaveAs) if self.isModified() and self.getURI() != "mod://": self._mainframe.bindSave(self.OnSave) else: self._mainframe.bindSave() def _toXML(self): impl = xml.dom.minidom.getDOMImplementation() dom = impl.createDocument(None, "Module", None) dom.documentElement.setAttribute("version","1.0") node_list = self._d_basic_data.ToXML(dom) node_list += self._d_require.ToXML(dom) node_list += self._d_provide.ToXML(dom) for node in node_list: dom.documentElement.appendChild(node) return dom.toprettyxml(" ") def OnSave(self, evt=None): if self.getURI() == "mod://": return txt = self._toXML() self._controller.DocumentSave(self.getURI(),txt) self.setModified(False) self._updateMainFrame() def OnSaveAs(self, evt=None): selected = False while not selected: dlg = Dialogs.eDevSaveModuleAsDialog(self, -1) if dlg.ShowModal() != wx.ID_OK: return uri = "mod://%s"%dlg.getSelection() dlg.Destroy() if self._model.checkURI(uri): # FIXME Override? continue selected = True if not self._model.checkURI(uri): self._controller.DocumentSave(uri, self._toXML()) self._moduletree.addURI(uri) else: self._controller.DocumentSave(uri, self._toXML()) self.setURI(uri) self.setTitle( getModuleName(uri) )
class PythonEditor(stc.StyledTextCtrl, EditorInterface): def __init__(self, parent, ID, uri): stc.StyledTextCtrl.__init__(self, parent, ID, style=0) if uri == "zip://": modified = True title = "unsaved" else: modified = False (aname, title) = splitPyFile(uri) EditorInterface.__init__(self, parent, False, uri) self.setTitle(title) self._d_config = Config() self._d_controller = Controller() self._logger = self._d_controller.getLogger() self._d_mainframe = self._d_controller.getMainFrame() self._d_model = Model() self._d_notebook = self._d_controller.getNotebook() self._pyedit_component = ComponentManager().getComponent("pyeditor") self._archive_tree = self._pyedit_component.getArchiveTree() self.SetLexer(stc.STC_LEX_PYTHON) self.SetKeyWords(0, " ".join(keyword.kwlist)) # config: font = self._d_config.getEditorFont() fsize = self._d_config.getEditorFontSize() sdfsize = self._d_config.getEditorSecondFontSize() faces = {"times": font, "mono": font, "helv": font, "other": font, "size": fsize, "size2": sdfsize} self.SetTabWidth(self._d_config.getEditorTabSpace()) self.SetUseTabs(not self._d_config.getEditorExpandTab()) self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces) self.StyleClearAll() # Reset all to be like the default self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces) self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces) self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, "fore:#FFFFFF,back:#0000FF,bold") self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold") self.StyleSetSpec(stc.STC_P_DEFAULT, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_NUMBER, "fore:#007F7F,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_STRING, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_WORD, "fore:#00007F,bold,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#7F0000,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#7F0000,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#0000FF,bold,underline,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#007F7F,bold,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_OPERATOR, "bold,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_IDENTIFIER, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_COMMENTBLOCK, "fore:#7F7F7F,size:%(size)d" % faces) self.StyleSetSpec(stc.STC_P_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces) self.SetCaretForeground("BLUE") if isPyFileURI(uri): text = self._d_model.openURI(uri) else: text = "" self.SetText(text) self.EmptyUndoBuffer() self.Colourise(0, -1) self.Bind(stc.EVT_STC_CHANGE, self.OnModified) def OnModified(self, evt): if self.isModified(): return self.setModified() event = Events.PageModifiedEvent(Events._event_page_modified, self.GetId()) event.SetPage(self) self.GetEventHandler().ProcessEvent(event) self._updateMainFrame() # evt.Skip() def OnSelected(self, evt=None): self._updateMainFrame() def _updateMainFrame(self): self._d_mainframe.bindRedo() self._d_mainframe.bindUndo() self._d_mainframe.bindSave() self._d_mainframe.bindSaveAs() self._d_mainframe.bindCopy() self._d_mainframe.bindCut() self._d_mainframe.bindPaste() if self.CanRedo(): self._d_mainframe.bindRedo(self.OnRedo) if self.CanUndo(): self._d_mainframe.bindUndo(self.OnUndo) self._d_mainframe.bindCopy(self.OnCopy) self._d_mainframe.bindCut(self.OnCut) if self.CanPaste(): self._d_mainframe.bindPaste(self.OnPaste) self._d_mainframe.bindSaveAs(self.OnSaveAs) if self.isModified() and self.getURI() != "zip://": self._d_mainframe.bindSave(self.OnSave) def OnSaveAs(self, evt=None): selected = False while not selected: dlg = Dialogs.eDevSaveAsDialog(self, -1) if dlg.ShowModal() != wx.ID_OK: return uri = "zip://%s/%s" % dlg.getSelection() dlg.Destroy() if self._d_model.checkURI(uri): # FIXME Override? continue selected = True if not self._d_model.checkURI(uri): self._d_controller.DocumentSave(uri, self.GetText()) self._archive_tree.addURI(uri) else: self._d_controller.DocumentSave(uri, self.GetText()) self.setURI(uri) self._logger.debug("rename to " + uri) self._d_notebook.setPageTitleByURI(uri, getPyFile(uri)) def OnSave(self, evt=None): txt = self.GetText() try: self._d_controller.DocumentSave(self.getURI(), txt) self.setModified(False) finally: self._updateMainFrame() def OnCopy(self, evt=None): self.Copy() self._updateMainFrame() def OnCut(self, evt=None): self.Cut() self._updateMainFrame() def OnPaste(self, evt=None): self.Paste() self._updateMainFrame() def OnRedo(self, evt=None): self.Redo() self._updateMainFrame() def OnUndo(self, evt=None): self.Undo() self._updateMainFrame()
class SaveAsDialog(wx.Dialog): def __init__(self, parent, ID): wx.Dialog.__init__(self,parent, ID, title="Save circuit as") self._model = Model() box = wx.BoxSizer(wx.VERTICAL) dlg_txt = """<font size="14">Enter new (full) circuit name.</font> For example "class.subclass.name". A class or circuit name can only contain a-z, A-Z, 0-9 and _.""" txt = StaticFancyText(self, -1, dlg_txt) box.Add(txt, 0, wx.ALL, 10) self._name = wx.TextCtrl(self, -1) box.Add(self._name, 1, wx.EXPAND|wx.ALIGN_CENTER|wx.LEFT|wx.RIGHT|wx.BOTTOM, 20) bbox = self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL) box.Add(bbox, 1, wx.ALIGN_CENTER|wx.ALL, 10) self.SetSizer(box) box.Fit(self) self.Bind(wx.EVT_BUTTON, self.OnOk, id=wx.ID_OK) def validate(self, name): if not re.match("^[\w\_]+[\.[\w\_]+]*$", name): return False return True def circuitExists(self, name): uri = "circ://"+"/".join( name.split(".") ) if self._model.checkURI(uri): return True return False def getSelection(self): # FIXME maybe we will return the uri return "circ://"+"/".join(self._name.GetValue().split(".")) def OnOk(self, evt): name = self._name.GetValue() if not self.validate(name): wx.MessageBox("Invalid circuit name.\n"+ "The name should only constists of [a-zA-Z0-9_] "+ "seperated by dots!", "Invalid circuit name", wx.OK|wx.ICON_ERROR, self, -1) return if self.circuitExists(name): ret = wx.MessageBox("There exists allready a circuit named %s. Override?"%name, "Override?", wx.YES|wx.NO|wx.ICON_QUESTION, self, -1) if ret == wx.NO: return self.EndModal(wx.ID_OK)
class ArchiveTree(wx.TreeCtrl): def __init__(self, parent, ID): wx.TreeCtrl.__init__(self, parent, ID, size=wx.Size(-1,-1), style=wx.TR_HAS_BUTTONS|wx.TR_HIDE_ROOT|wx.TR_NO_LINES|wx.TR_FULL_ROW_HIGHLIGHT) self._d_imgs = wx.ImageList(16,16) self._bmp_class = self._d_imgs.Add( getArchiveBitmap() ) self._bmp_class_open = self._d_imgs.Add( getArchiveBitmap() ) self._bmp_module = self._d_imgs.Add( getPythonBitmap() ) self.SetImageList(self._d_imgs) self._d_model = Model() self._d_controller = Controller() self._d_mainframe = self._d_controller.getMainFrame() self._d_root = self.AddRoot("root") self.SetPyData(self._d_root, (None,None)) self._logger = self._d_controller.getLogger() #Add all archives: archives = self._d_model.openURI("zip://") for archive_uri in archives: uri_list = self._d_model.openURI(archive_uri) archive_name = getArchive(archive_uri) arch_item = self.AppendItem(self._d_root, archive_name) self.SetPyData(arch_item, ("Archive", archive_uri) ) self.SetItemImage(arch_item, self._bmp_class, wx.TreeItemIcon_Normal) self.SetItemImage(arch_item, self._bmp_class_open, wx.TreeItemIcon_Expanded) for file_uri in uri_list: file_name = getPyFile(file_uri) file_item = self.AppendItem(arch_item, file_name) self.SetPyData(file_item, ("File", file_uri)) self.SetItemImage(file_item, self._bmp_module, wx.TreeItemIcon_Normal) self.SetItemImage(file_item, self._bmp_module, wx.TreeItemIcon_Expanded) # FIXME self._d_mainframe.bindNewArch(self.NewArchive) # Events: self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelection) self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivate) self.Bind(wx.EVT_SET_FOCUS, self.OnFocus) def removeURI(self, uri): item = self.getItemByURI(uri,self._d_root) if not item: raise Exception("Item %s not found"%uri) self.Delete(item) def addURI(self, uri): if self.getItemByURI(uri, self._d_root): return if isPyFileURI(uri): (ar, py) = splitPyFile(uri) item = self.getItemByURI("zip://"+ar, self._d_root) fitem = self.AppendItem(item, py) self.SetPyData(fitem, ("File", uri)) self.SetItemImage(fitem, self._bmp_module, wx.TreeItemIcon_Normal) self.SetItemImage(fitem, self._bmp_module, wx.TreeItemIcon_Expanded) elif isArchiveURI(uri): path = getArchive(uri) item = self.AppendItem(self._d_root, path) self.SetPyData(item, ("Archive",uri)) self.SetItemImage(item, self._bmp_class, wx.TreeItemIcon_Normal) self.SetItemImage(item, self._bmp_class_open, wx.TreeItemIcon_Expanded) else: raise Exception("Unkonw URI %s"%uri) def getItemByURI(self, uri, item): (typ, iuri) = self.GetPyData(item) if iuri==uri: return item (citem, cookie) = self.GetFirstChild(item) while citem: ret = self.getItemByURI(uri, citem) if ret != None: return ret (citem, cookie) = self.GetNextChild(item,cookie) # # CONTROLLER PART # def OnFocus(self, evt): self._updateMainFrame() evt.Skip() def OnSelection(self, event): item = event.GetItem() self._updateMainFrame(item) event.Skip() def _updateMainFrame(self, item=None): if not item: item = self.GetSelection() self._d_mainframe.bindNew() self._d_mainframe.bindOpen() self._d_mainframe.bindDelete() if not item: return (typ, uri) = self.GetPyData(item) if typ is "File": self._d_mainframe.bindOpen(self.OpenFile, "&Open file") self._d_mainframe.bindDelete(self.DeleteFile, "&Delete file") elif typ is "Archive": self._d_mainframe.bindNew(self.NewFile, "&New file") self._d_mainframe.bindOpen(self.OpenArchive, "&Open archive") self._d_mainframe.bindDelete(self.DeleteArchive, "&Delete archive") def OnActivate(self, event): item = event.GetItem() (typ, uri) = self.GetPyData(item) if typ == "File": self._d_controller.DocumentOpen(uri) event.Skip() def OpenFile(self, evt=None): item = self.GetSelection() if not item: return (typ, uri) = self.GetPyData(item) if not typ == "File": return self._d_controller.DocumentOpen(uri) def DeleteFile(self, evt=None): item = self.GetSelection() if not item: self._logger.warning("No item selected to delete") return (typ, uri) = self.GetPyData(item) if not typ == "File": return try: self._d_controller.DocumentDelete(uri) self.removeURI(uri) except: self._logger.exception("Unable to remove file %s"%uri) def NewFile(self, evt=None): self._d_controller.DocumentOpen("zip://") def OpenArchive(self, evt=None): item = self.GetSelection() if not item: return (typ, uri) = self.GetPyData(item) if not typ=="Archive": return self.Expand(item) def NewArchive(self, evt=None): selected = False while not selected: dlg = Dialogs.eDevNewArchiveDialog(self, -1) if dlg.ShowModal() == wx.ID_CANCEL: return uri = dlg.getSelection() dlg.Destroy() if self._d_model.checkURI(uri): # FIXME ask for overwrite continue selected = True self._d_model.saveURI(uri,"") self.addURI(uri) def DeleteArchive(self, evt=None): item = self.GetSelection() (typ, uri) = self.GetPyData(item) py_list = self._d_model.openURI(uri) for py in py_list: self._d_controller.DocumentDelete(py) self._d_model.deleteURI(uri) self.Delete(item)