#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os import dabo dabo.ui.loadUI("wx") from dabo.dLocalize import _ import dabo.dEvents as dEvents import components from components.ClassDesigner import ClassDesigner from components.CxnEditor import CxnEditorPage from components.MenuDesigner import MenuDesigner from components.ReportDesigner import ReportDesigner from components.TextEditor import TextEditorPage class ProjectTree(dabo.ui.dTreeView): def beforeInit(self): self.controller = None def onContextMenu(self, evt): nd = self.getNodeUnderMouse() if not nd: return # Full path is in the ToolTipText pth = nd.ToolTipText if os.path.isfile(pth): self._context = dict(node=nd, filepath=pth) pop = self.createContextMenu(pth) self.showContextMenu(pop) def createContextMenu(self, pth): pop = dabo.ui.dMenu() pop.append(_("Edit"), OnHit=self.onEditFile) return pop def onEditFile(self, evt): if not self._context: return nd = self._context["node"] pth = self._context["filepath"] self.controller.editFile(pth) class StudioToolBar(dabo.ui.dToolBar): """Overrides the _appendInsertButton() method, so that we get actual Dabo dBitmapButton or dToggleButton controls instead of the default wx.ToolBarItem buttons that are difficult to work with. """ def _appendInsertButton(self, pos, caption, pic, bindfunc, toggle, tip, help, *args, **kwargs): buttonClass = {True: dabo.ui.dToggleButton, False: dabo.ui.dBitmapButton}[toggle] if not toggle: kwargs["AutoSize"] = True ctl = buttonClass(self, Caption=caption, Picture=pic, ToolTipText=tip, *args, **kwargs) # Don't show the caption if there is an image def _dyncap(obj): if obj.Picture: return "" else: return obj._caption ctl.DynamicCaption = (_dyncap, ctl) return self.insertControl(pos, ctl, bindfunc=bindfunc) class StudioForm(dabo.ui.dDockForm): def initProperties(self): self.Caption = _("Dabo Developer Studio") self.Orientation = "h" def afterInit(self): treePnl = self.projectTreePanel = self.addPanel(Caption=_("Projects"), Docked=True, DockSide="Left", ShowPinButton=True) sz = self.projectTreePanel.Sizer = dabo.ui.dSizer("v") btn = dabo.ui.dButton(treePnl, Caption="Open Project", OnHit=self.onOpenProject) sz.append(btn, halign="center", border=30) self.tree = ProjectTree(treePnl) self.tree.controller = self sz.append1x(self.tree) self.toolbar = self.addPanel(Toolbar=True, Docked=True, DockSide="Top", ShowCaption=False, ShowGripper=True, BottomDockable=False, LeftDockable=False, RightDockable=False) self.toolbar.Sizer = dabo.ui.dSizer("h", DefaultSpacing=10) # Automatically add child controls to the toolbar sizer self.toolbar.bindEvent(dEvents.ChildBorn, self.onToolbarItemAdded) cp = self.CenterPanel csz = cp.Sizer = dabo.ui.dSizer("v") # self.toolbar = self.ToolBar = StudioToolBar(self) # csz.append(self.toolbar) self._toolSets = {None: []} self._currentToolBar = None dabo.ui.setAfter(self, "CurrentToolBar", None) self.pgfEditors = dabo.ui.dDockTabs(self.CenterPanel, PageCount=0) csz.append1x(self.pgfEditors) # Enable the various parts of the IDE. self.registerComponents() self.registerPlugins() self._currentProject = None # See if there is a saved project proj = self.PreferenceManager.lastOpenedProject print "LAST", proj if isinstance(proj, basestring) and os.path.exists(proj): self.openProject(proj) # Make sure that all components are visible dabo.ui.callAfter(self._showAllPanels) def onToolbarItemAdded(self, evt): self.toolbar.Sizer.append(evt.child) def _showAllPanels(self): self.toolbar.Visible = self.projectTreePanel.Visible = True self.toolbar.Show() self._refreshState() def registerComponents(self): import types comps = [getattr(components, comp) for comp in dir(components) if isinstance(getattr(components, comp), types.ModuleType)] for comp in comps: try: comp.register(self) except AttributeError, e: print "ERR", e dabo.errorLog.write(_("Component '%s' lacks a 'register()' method.") % comp) def registerPlugins(self): pass def setToolBarGroup(self, key, grp): self._toolSets[key] = grp def editFile(self, pth): if pth.endswith(".py"): pg = self.pgfEditors.appendPage(TextEditorPage) pg.openFile(pth) self.pgfEditors.SelectedPage = pg self.CurrentToolBar = "TextEditor" elif pth.endswith(".cnxml"): pg = self.pgfEditors.appendPage(CxnEditorPage) pg.openFile(pth) self.pgfEditors.SelectedPage = pg self.CurrentToolBar = "CxnEditor" def onOpenProject(self, evt): pjd = dabo.ui.getDirectory(_("Select Project Base")) if pjd: self.openProject(pjd) def openProject(self, proj): self.Application._currentProject = proj self.tree.makeDirTree(proj, ignored=["*pyc", "*~"], expand=False) self.tree.getRootNode().Expanded = True def _getCurrentToolBar(self): return self._currentToolBar def _setCurrentToolBar(self, val): if self._constructed(): if val == self._currentToolBar: return if val not in self._toolSets: dabo.errorLog.write(_("Invalid toolbar specified: '%s'") % val) print self._toolSets.keys() return self._currentToolBar = val sz = self.toolbar.Sizer curr = self._toolSets[self._currentToolBar] for itm in self.toolbar.Children: itm.Visible = (itm in curr) self.toolbar.layout() else: self._properties["CurrentToolBar"] = val CurrentToolBar = property(_getCurrentToolBar, _setCurrentToolBar, None, _("Name of the current toolbar to display. Changing this will change the buttons that are visible in the editing area. (str)")) class StudioApp(dabo.dApp): def afterFinish(self): # Save the current project being edited. pm = self.PreferenceManager print "CURR", self._currentProject if self._currentProject is None: pm.removePref(lastOpenedProject) else: pm.lastOpenedProject = self._currentProject def main(): app = StudioApp() app.BasePrefKey = "DeveloperStudio" app.MainFormClass = StudioForm app.start() if __name__ == "__main__": main()