Cleaning up hooke.ui.gui. Can initialize, but without most bindings.
[hooke.git] / hooke / ui / gui / panel / commands.py
1 #!/usr/bin/env python\r
2 \r
3 """Commands and settings panel for Hooke.\r
4 """\r
5 \r
6 import os.path\r
7 \r
8 import wx\r
9 \r
10 \r
11 class Tree (wx.TreeCtrl):\r
12     def __init__(self, commands, selected, *args, **kwargs):\r
13         super(Tree, self).__init__(*args, **kwargs)\r
14         imglist = wx.ImageList(width=16, height=16, mask=True, initialCount=2)\r
15         imglist.Add(wx.ArtProvider.GetBitmap(\r
16                 wx.ART_FOLDER, wx.ART_OTHER, wx.Size(16, 16)))\r
17         imglist.Add(wx.ArtProvider.GetBitmap(\r
18                 wx.ART_EXECUTABLE_FILE, wx.ART_OTHER, wx.Size(16, 16)))\r
19         self.AssignImageList(imglist)\r
20         self.image = {\r
21             'root': 0,\r
22             'plugin': 0,\r
23             'command': 1,\r
24             }\r
25         self.AddRoot(text='Commands and Settings', image=self.image['root'])\r
26 \r
27         self._setup_commands(commands, selected)\r
28 \r
29     def _setup_commands(self, commands, selected):\r
30         self._commands = commands\r
31         selected = None\r
32         tree_root = self.GetRootItem()\r
33 \r
34         plugin_roots = {}\r
35         plugins = sorted(set([c.plugin for c in commands]),\r
36                          key=lambda p:p.name)\r
37         for plugin in plugins:\r
38             plugin_roots[plugin.name] = self.AppendItem(\r
39                 parent=tree_root,\r
40                 text=plugin.name,\r
41                 image=self.image['plugin'],\r
42                 data=wx.TreeItemData(plugin))\r
43 \r
44         for command in sorted(commands, key=lambda c:c.name):\r
45             item = self.AppendItem(\r
46                 parent=plugin_roots[command.plugin.name],\r
47                 text=command.name,\r
48                 image=self.image['command'])\r
49             if command.name == selected:\r
50                 selected = item\r
51         for key,value in plugin_roots.items():\r
52             self.Expand(value)\r
53         #make sure the selected command/plugin is visible in the tree\r
54         if selected is not None:\r
55             self.SelectItem(selected, True)\r
56             self.EnsureVisible(selected)\r
57 \r
58 \r
59 class Commands (wx.Panel):\r
60     def __init__(self, commands, selected, *args, **kwargs):\r
61         super(Commands, self).__init__(*args, **kwargs)\r
62         self._c = {\r
63             'tree': Tree(\r
64                 commands=commands,\r
65                 selected=selected,\r
66                 parent=self,\r
67                 pos=wx.Point(0, 0),\r
68                 size=wx.Size(160, 250),\r
69                 style=wx.TR_DEFAULT_STYLE|wx.NO_BORDER|wx.TR_HIDE_ROOT),\r
70             'execute': wx.Button(self, label='Execute'),\r
71             }\r
72         sizer = wx.BoxSizer(wx.VERTICAL)\r
73         sizer.Add(self._c['execute'], 0, wx.EXPAND)\r
74         sizer.Add(self._c['tree'], 1, wx.EXPAND)\r
75         self.SetSizer(sizer)\r
76         sizer.Fit(self)\r