From: <sv...@ww...> - 2008-01-21 18:23:37
|
Author: nsmoooose Date: 2008-01-21 10:23:23 -0800 (Mon, 21 Jan 2008) New Revision: 2209 Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/FileCommand.py branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/OpenSelectedFileCommand.py Modified: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/CommandControlFactory.py branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/MainFrame.py branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/OpenCustomLayoutModelFileCommand.py branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/controls/ProjectTree.py Log: It is now possible to open files directly from the project tree. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=2209 Modified: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/CommandControlFactory.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/CommandControlFactory.py 2008-01-20 17:16:39 UTC (rev 2208) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/CommandControlFactory.py 2008-01-21 18:23:23 UTC (rev 2209) @@ -14,6 +14,9 @@ def GenerateMenuItems(self, parent, commands): menu = wx.Menu() for command in commands: + if command == None: + menu.AppendSeparator() + continue instance = command() controlId = self.controlIdGenerator.Generate() Modified: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/MainFrame.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/MainFrame.py 2008-01-20 17:16:39 UTC (rev 2208) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/MainFrame.py 2008-01-21 18:23:23 UTC (rev 2209) @@ -8,7 +8,7 @@ from controls.SceneWindow import SceneWindow from commands.MoveCameraToHomeCommand import MoveCameraToHomeCommand -from commands.OpenCustomLayoutModelFileCommand import OpenCustomLayoutModelFileCommand +from commands.OpenSelectedFileCommand import OpenSelectedFileCommand from commands.QuitCommand import QuitCommand class MainFrame(wx.Frame): @@ -37,9 +37,9 @@ # objects. controlFactory = CommandControlFactory(self.controlIdGenerator) - fileMenuCommands = [OpenCustomLayoutModelFileCommand, QuitCommand] + fileMenuCommands = [QuitCommand] viewMenuCommands = [MoveCameraToHomeCommand] - toolbarCommands = [OpenCustomLayoutModelFileCommand, MoveCameraToHomeCommand] + toolbarCommands = [OpenSelectedFileCommand, MoveCameraToHomeCommand] # Menu items. menuBar = wx.MenuBar() @@ -75,14 +75,19 @@ startPage.SetEditable(False) documentNotebook.AddPage(startPage, "Start") - # Add the tree control that displays all files in the project. + # Add the scene control that is responsible for the 3D + # scene. scenePage = SceneWindow(documentNotebook, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.BORDER_NONE, "GLCanvas", 0, wx.NullPalette) documentNotebook.AddPage(scenePage, "Scene") self.scene = scenePage - # Display the tree control with all files in this project. + # Display the tree control with all files in this project. Also + # register this instance so all command object can use it to + # access the selected file or directory. projectTreePage = ProjectTree(propertyNotebook) projectTreePage.SetRootDirectory(application.Configuration['LayoutApplication.DataDirectory']) + projectTreePage.SetOpenCommand(OpenSelectedFileCommand) + ProjectTree.Instance = projectTreePage propertyNotebook.AddPage(projectTreePage, "Project") Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/FileCommand.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/FileCommand.py (rev 0) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/FileCommand.py 2008-01-21 18:23:23 UTC (rev 2209) @@ -0,0 +1,10 @@ +#!/usr/bin/env python + +class FileCommand: + """Base class for all command objects that involves files.""" + + def SetFileName(self, fileName): + self.fileName = fileName + + def GetFileName(self): + return self.fileName Modified: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/OpenCustomLayoutModelFileCommand.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/OpenCustomLayoutModelFileCommand.py 2008-01-20 17:16:39 UTC (rev 2208) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/OpenCustomLayoutModelFileCommand.py 2008-01-21 18:23:23 UTC (rev 2209) @@ -2,8 +2,9 @@ import os.path import wx from csp.tools.layout2.scripts.data import DataTree +from FileCommand import FileCommand -class OpenCustomLayoutModelFileCommand: +class OpenCustomLayoutModelFileCommand(FileCommand): def GetCaption(self): return "Open CustomLayoutModel file" @@ -30,18 +31,8 @@ if topWindow == None: return - # Show the open file dialog. - # retreive the last directory used by this function. - lastDirectoryUsedkey = 'OpenCustomLayoutModelFileCommand.LastDirectory' - lastDirectoryUsed = application.Configuration.get(lastDirectoryUsedkey, '.') - openFileDialog = wx.FileDialog(topWindow, 'Open feature', lastDirectoryUsed, - '', 'XML files (*.xml)|*.xml|All files|*.*', wx.OPEN) - result = openFileDialog.ShowModal() - if result != wx.ID_OK: - return - fileName = openFileDialog.GetPath() - # Store the last directory used. - application.Configuration[lastDirectoryUsedkey] = os.path.dirname(fileName) + # Retreive the filename. It is set by the parent class. + fileName = self.GetFileName() # Get the current xml path settings. We must check that the file we # are loading is actually below this directory. Added: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/OpenSelectedFileCommand.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/OpenSelectedFileCommand.py (rev 0) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/commands/OpenSelectedFileCommand.py 2008-01-21 18:23:23 UTC (rev 2209) @@ -0,0 +1,51 @@ +#!/usr/bin/env python +import os.path +import wx +from csp.tools.layout2.scripts.data import DataTree +from csp.tools.layout2.scripts.ui.controls.ProjectTree import ProjectTree +from OpenCustomLayoutModelFileCommand import OpenCustomLayoutModelFileCommand + +class OpenSelectedFileCommand: + """Opens the selected file in the project tree of this + application. When this command is executed we are trying + to identify the type of file we are loading and then + executing the correct FileCommand.""" + + def GetCaption(self): + return "Open selected file in project" + + def GetToolTipText(self): + return "Open selected file in project" + + def GetToolBarImageName(self): + return "document-open.png" + + def Execute(self): + """Load a feature group or feature model from the specified file. The existing + graph, if any, will be discarded. If file represents a feature model, a + default feature group is created at the root of the new graph. + """ + + # Get the application object. This object is used to retreive the + # configuration object and the top window for this application. + application = wx.GetApp() + + # Get the top window for this application. The top window shall be the + # parent for the open file dialog. + topWindow = application.GetTopWindow() + if topWindow == None: + return + + if ProjectTree.Instance is None: + print('No project tree instance has been set') + return + + fileName = ProjectTree.Instance.GetSelectedFile() + if fileName is None: + return + + # Now it is time to identify the filetype of the selected file. + command = OpenCustomLayoutModelFileCommand() + command.SetFileName(fileName) + command.Execute() + Modified: branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/controls/ProjectTree.py =================================================================== --- branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/controls/ProjectTree.py 2008-01-20 17:16:39 UTC (rev 2208) +++ branches/layout_tool_improvements/csp/tools/layout2/scripts/ui/controls/ProjectTree.py 2008-01-21 18:23:23 UTC (rev 2209) @@ -2,19 +2,27 @@ import os import wx -from csp.tools.layout2.layout_module import * +class ProjectTree(wx.TreeCtrl): + """A tree control that provides information about all files + within the project directory. All folders and files above + the root directory will be hidden.""" -class ProjectTree(wx.TreeCtrl): + Instance = None + def __init__(self, parent): wx.TreeCtrl.__init__(self, parent, style=wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS|wx.TR_LINES_AT_ROOT) + + self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.on_ItemActivated) def SetRootDirectory(self, directory): + """ Sets the root directory that the tree should display. All subfolders + and files of this directory will be displayed within the tree.""" self.DeleteAllItems() # Remember the base directory so we can return # the selected file or folder. self.baseDirectory = directory - + self.nodeDictionary = {} # Add the base node that is the parent to all # nodes. @@ -30,10 +38,32 @@ for subDirectory in dirs: newNode = self.AppendItem(parentNode, subDirectory) self.nodeDictionary[os.path.join(root, subDirectory)] = newNode - for file in files: - if file.lower().endswith('.pyc'): - continue - self.AppendItem(newNode, file) + for file in files: + if file.lower().endswith('.pyc'): + continue + self.AppendItem(parentNode, file) def GetSelectedFile(self): - return None \ No newline at end of file + node = self.GetSelection() + if node.IsOk(): + items = [] + while node != self.GetRootItem(): + items.insert(0, self.GetItemText(node)) + node = self.GetItemParent(node) + + selection = self.baseDirectory + for item in items: + selection = os.path.join(selection, item) + return selection + + def SetOpenCommand(self, command): + self.openCommand = command + + def on_ItemActivated(self, event): + if self.openCommand is None: + print('No open command object set') + return + + if os.path.isfile(self.GetSelectedFile()): + command = self.openCommand() + command.Execute() |