Hooke(GUI)
[hooke.git] / panels / commands.py
1 #!/usr/bin/env python\r
2 \r
3 '''\r
4 commands.py\r
5 \r
6 Commands and settings panel for Hooke.\r
7 \r
8 Copyright 2009 by Dr. Rolf Schmidt (Concordia University, Canada)\r
9 \r
10 This program is released under the GNU General Public License version 2.\r
11 '''\r
12 \r
13 from configobj import ConfigObj\r
14 import os.path\r
15 import wx\r
16 \r
17 import lib.libhooke as lh\r
18 \r
19 class Commands(wx.Panel):\r
20 \r
21     def __init__(self, parent):\r
22         # Use the WANTS_CHARS style so the panel doesn't eat the Return key.\r
23         wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS|wx.NO_BORDER, size=(160, 200))\r
24 \r
25         self.CommandsTree = wx.TreeCtrl(self, -1, wx.Point(0, 0), wx.Size(160, 250), wx.TR_DEFAULT_STYLE|wx.NO_BORDER|wx.TR_HIDE_ROOT)\r
26         imglist = wx.ImageList(16, 16, True, 2)\r
27         imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER, wx.Size(16, 16)))\r
28         imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_EXECUTABLE_FILE, wx.ART_OTHER, wx.Size(16, 16)))\r
29         self.CommandsTree.AssignImageList(imglist)\r
30         self.CommandsTree.AddRoot('Commands and Settings', 0)\r
31 \r
32         self.ExecuteButton = wx.Button(self, -1, 'Execute')\r
33 \r
34         sizer = wx.BoxSizer(wx.VERTICAL)\r
35         sizer.Add(self.CommandsTree, 1, wx.EXPAND)\r
36         sizer.Add(self.ExecuteButton, 0, wx.EXPAND)\r
37 \r
38         self.SetSizer(sizer)\r
39         sizer.Fit(self)\r
40 \r
41     def Initialize(self, plugins):\r
42         selected = None\r
43         tree_root = self.CommandsTree.GetRootItem()\r
44         path = lh.get_file_path('hooke.ini', ['config'])\r
45         config = ConfigObj()\r
46         if os.path.isfile(path):\r
47             config.filename = path\r
48             config.reload()\r
49             #get the selected command/plugin from the config file\r
50             command_str = config['command']['command']\r
51             module_str = config['command']['plugin']\r
52 \r
53             #sort the plugins into alphabetical order\r
54             plugins_list = [key for key, value in plugins.iteritems()]\r
55             plugins_list.sort()\r
56             for plugin in plugins_list:\r
57                 filename = ''.join([plugin, '.ini'])\r
58                 path = lh.get_file_path(filename, ['plugins'])\r
59                 config = ConfigObj()\r
60                 if os.path.isfile(path):\r
61                     config.filename = path\r
62                     config.reload()\r
63                     #append the ini file to the plugin\r
64                     plugin_root = self.CommandsTree.AppendItem(tree_root, plugin, 0, data=wx.TreeItemData(config))\r
65                 else:\r
66                     plugin_root = self.CommandsTree.AppendItem(tree_root, plugin, 0)\r
67                 #select the plugin according to the config file\r
68                 if plugin == module_str:\r
69                     selected = plugin_root\r
70 \r
71                 #add all commands to the tree\r
72                 for command in plugins[plugin]:\r
73                     command_label = command.replace('do_', '')\r
74                     #do not add the ini file to the command (we'll access the ini file of the plugin (ie parent) instead, see above)\r
75                     item = self.CommandsTree.AppendItem(plugin_root, command_label, 1)\r
76                     #select the command according to the config file\r
77                     if plugin == module_str and command_label == command_str:\r
78                         selected = item\r
79                         #e = wx.MouseEvent()\r
80                         #e.SetEventType(wx.EVT_LEFT_DOWN.typeId)\r
81                         #e.SetEventObject(self.CommandsTree)\r
82 \r
83                         ##e.SetSelection(page)\r
84                         #self.Parent.OnTreeCtrlCommandsLeftDown(e)\r
85                         #wx.PostEvent(self, e)\r
86 \r
87                         #self.CommandsTree.SelectItem(item, True)\r
88 \r
89                 self.CommandsTree.Expand(plugin_root)\r
90             #make sure the selected command/plugin is visible in the tree\r
91             if selected is not None:\r
92                 self.CommandsTree.SelectItem(selected, True)\r
93                 self.CommandsTree.EnsureVisible(selected)\r