controls/LibraryPanel.py
changeset 714 131ea7f237b9
parent 680 b693147fb2c3
child 741 330f578e228d
equal deleted inserted replaced
713:95a0a427f3ef 714:131ea7f237b9
    22 #License along with this library; if not, write to the Free Software
    22 #License along with this library; if not, write to the Free Software
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    25 import wx
    25 import wx
    26 
    26 
       
    27 #-------------------------------------------------------------------------------
       
    28 #                                 Helpers
       
    29 #-------------------------------------------------------------------------------
       
    30 
    27 [CATEGORY, BLOCK] = range(2)
    31 [CATEGORY, BLOCK] = range(2)
    28 
    32 
    29 [ID_LIBRARYPANEL, ID_LIBRARYSEARCHCTRL, 
    33 #-------------------------------------------------------------------------------
    30  ID_LIBRARYTREE, ID_LIBRARYCOMMENT, 
    34 #                              Library Panel
    31 ] = [wx.NewId() for _init_ctrls in range(4)]
    35 #-------------------------------------------------------------------------------
    32 
    36 
    33 class LibraryPanel(wx.Panel):
    37 class LibraryPanel(wx.Panel):
    34     
    38     
    35     def _init_coll_MainSizer_Items(self, parent):
    39     def __init__(self, parent, enable_drag=False):
    36         parent.AddWindow(self.SearchCtrl, 0, border=0, flag=wx.GROW)
    40         wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
    37         parent.AddWindow(self.Tree, 0, border=0, flag=wx.GROW)
    41         
    38         parent.AddWindow(self.Comment, 0, border=0, flag=wx.GROW)
    42         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=0)
    39 
    43         main_sizer.AddGrowableCol(0)
    40     def _init_coll_MainSizer_Growables(self, parent):
    44         main_sizer.AddGrowableRow(1)
    41         parent.AddGrowableCol(0)
    45         
    42         parent.AddGrowableRow(1)
    46         self.SearchCtrl = wx.SearchCtrl(self)
    43     
       
    44     def _init_sizers(self):
       
    45         self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=0)
       
    46         
       
    47         self._init_coll_MainSizer_Growables(self.MainSizer)
       
    48         self._init_coll_MainSizer_Items(self.MainSizer)
       
    49         
       
    50         self.SetSizer(self.MainSizer)
       
    51     
       
    52     def _init_ctrls(self, prnt, enable_drag=False):
       
    53         wx.Panel.__init__(self, id=ID_LIBRARYPANEL,
       
    54                   name='LibraryPanel', parent=prnt,
       
    55                   pos=wx.Point(0, 0), size=wx.Size(0, 0), 
       
    56                   style=wx.TAB_TRAVERSAL)
       
    57         
       
    58         self.SearchCtrl = wx.SearchCtrl(id=ID_LIBRARYSEARCHCTRL, 
       
    59                   name='SearchCtrl', parent=self,
       
    60                   pos=wx.Point(0, 0), size=wx.Size(0, 28), style=0)
       
    61         self.SearchCtrl.ShowSearchButton(True)
    47         self.SearchCtrl.ShowSearchButton(True)
    62         self.Bind(wx.EVT_TEXT, self.OnSearchCtrlChanged,
    48         self.Bind(wx.EVT_TEXT, self.OnSearchCtrlChanged, self.SearchCtrl)
    63               id=ID_LIBRARYSEARCHCTRL)
    49         self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, 
    64         self.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, self.OnSearchButtonClick,
    50               self.OnSearchButtonClick, self.SearchCtrl)
    65               id=ID_LIBRARYSEARCHCTRL)
       
    66         search_textctrl = self.SearchCtrl.GetChildren()[0]
    51         search_textctrl = self.SearchCtrl.GetChildren()[0]
    67         search_textctrl.Bind(wx.EVT_CHAR, self.OnKeyDown)
    52         search_textctrl.Bind(wx.EVT_CHAR, self.OnKeyDown)
    68         
    53         main_sizer.AddWindow(self.SearchCtrl, flag=wx.GROW)
    69         self.Tree = wx.TreeCtrl(id=ID_LIBRARYTREE,
    54         
    70                   name='Tree', parent=self, 
    55         self.Tree = wx.TreeCtrl(self, 
    71                   pos=wx.Point(0, 0), size=wx.Size(0, 0),
    56               style=wx.TR_HAS_BUTTONS|
    72                   style=wx.TR_HAS_BUTTONS|wx.TR_SINGLE|wx.SUNKEN_BORDER|wx.TR_HIDE_ROOT|wx.TR_LINES_AT_ROOT)
    57                     wx.TR_SINGLE|
    73         self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemSelected,
    58                     wx.SUNKEN_BORDER|
    74               id=ID_LIBRARYTREE)
    59                     wx.TR_HIDE_ROOT|
       
    60                     wx.TR_LINES_AT_ROOT)
       
    61         self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemSelected, self.Tree)
    75         self.Tree.Bind(wx.EVT_CHAR, self.OnKeyDown)
    62         self.Tree.Bind(wx.EVT_CHAR, self.OnKeyDown)
    76         if enable_drag:
    63         if enable_drag:
    77             self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag,
    64             self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, self.Tree)
    78                   id=ID_LIBRARYTREE)
    65         main_sizer.AddWindow(self.Tree, flag=wx.GROW)
    79         
    66         
    80         self.Comment = wx.TextCtrl(id=ID_LIBRARYCOMMENT,
    67         self.Comment = wx.TextCtrl(self, size=wx.Size(0, 80), 
    81               name='Comment', parent=self, 
       
    82               pos=wx.Point(0, 0), size=wx.Size(0, 80), 
       
    83               style=wx.TE_READONLY|wx.TE_MULTILINE)
    68               style=wx.TE_READONLY|wx.TE_MULTILINE)
    84         
    69         main_sizer.AddWindow(self.Comment, flag=wx.GROW)
    85         self._init_sizers()
    70         
    86     
    71         self.SetSizer(main_sizer)
    87     def __init__(self, parent, enable_drag=False):
    72             
    88         self._init_ctrls(parent, enable_drag)
    73         self.Controller = None
    89         
       
    90         self.Controler = None
       
    91     
    74     
    92         self.BlockList = None
    75         self.BlockList = None
    93     
    76     
    94     def __del__(self):
    77     def __del__(self):
    95         self.Controler = None
    78         self.Controller = None
    96     
    79     
    97     def SetControler(self, controler):
    80     def SetController(self, controller):
    98         self.Controler = controler
    81         self.Controller = controller
    99     
    82     
   100     def SetBlockList(self, blocklist):
    83     def SetBlockList(self, blocklist):
   101         self.BlockList = blocklist
    84         self.BlockList = blocklist
   102         self.RefreshTree()
    85         self.RefreshTree()
   103     
    86     
   108         self.SearchCtrl.SetValue("")
    91         self.SearchCtrl.SetValue("")
   109         self.Tree.DeleteAllItems()
    92         self.Tree.DeleteAllItems()
   110         self.Comment.SetValue("")
    93         self.Comment.SetValue("")
   111     
    94     
   112     def RefreshTree(self):
    95     def RefreshTree(self):
   113         if self.Controler is not None:
    96         if self.Controller is not None:
   114             to_delete = []
    97             to_delete = []
   115             selected_name = None
    98             selected_name = None
   116             selected = self.Tree.GetSelection()
    99             selected = self.Tree.GetSelection()
   117             if selected.IsOk():
   100             if selected.IsOk():
   118                 selected_pydata = self.Tree.GetPyData(selected)
   101                 selected_pydata = self.Tree.GetPyData(selected)
   119                 if selected_pydata is not None and selected_pydata["type"] != CATEGORY:
   102                 if selected_pydata is not None and selected_pydata["type"] != CATEGORY:
   120                     selected_name = self.Tree.GetItemText(selected)
   103                     selected_name = self.Tree.GetItemText(selected)
   121             if self.BlockList is not None:
   104             if self.BlockList is not None:
   122                 blocktypes = self.BlockList
   105                 blocktypes = self.BlockList
   123             else:
   106             else:
   124                 blocktypes = self.Controler.GetBlockTypes()
   107                 blocktypes = self.Controller.GetBlockTypes()
   125             root = self.Tree.GetRootItem()
   108             root = self.Tree.GetRootItem()
   126             if not root.IsOk():
   109             if not root.IsOk():
   127                 root = self.Tree.AddRoot("")
   110                 root = self.Tree.AddRoot("")
   128             if wx.VERSION >= (2, 6, 0):
   111             category_item, root_cookie = self.Tree.GetFirstChild(root)
   129                 category_item, root_cookie = self.Tree.GetFirstChild(root)
       
   130             else:
       
   131                 category_item, root_cookie = self.Tree.GetFirstChild(root, 0)
       
   132             for category in blocktypes:
   112             for category in blocktypes:
   133                 category_name = category["name"]
   113                 category_name = category["name"]
   134                 if not category_item.IsOk():
   114                 if not category_item.IsOk():
   135                     category_item = self.Tree.AppendItem(root, _(category_name))
   115                     category_item = self.Tree.AppendItem(root, _(category_name))
   136                     if wx.Platform != '__WXMSW__':
   116                     if wx.Platform != '__WXMSW__':
   137                         category_item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
   117                         category_item, root_cookie = self.Tree.GetNextChild(root, root_cookie)
   138                 else:
   118                 else:
   139                     self.Tree.SetItemText(category_item, _(category_name))
   119                     self.Tree.SetItemText(category_item, _(category_name))
   140                 self.Tree.SetPyData(category_item, {"type" : CATEGORY})
   120                 self.Tree.SetPyData(category_item, {"type" : CATEGORY})
   141                 if wx.VERSION >= (2, 6, 0):
   121                 blocktype_item, category_cookie = self.Tree.GetFirstChild(category_item)
   142                     blocktype_item, category_cookie = self.Tree.GetFirstChild(category_item)
       
   143                 else:
       
   144                     blocktype_item, category_cookie = self.Tree.GetFirstChild(category_item, 0)        
       
   145                 for blocktype in category["list"]:
   122                 for blocktype in category["list"]:
   146                     if not blocktype_item.IsOk():
   123                     if not blocktype_item.IsOk():
   147                         blocktype_item = self.Tree.AppendItem(category_item, blocktype["name"])
   124                         blocktype_item = self.Tree.AppendItem(category_item, blocktype["name"])
   148                         if wx.Platform != '__WXMSW__':
   125                         if wx.Platform != '__WXMSW__':
   149                             blocktype_item, category_cookie = self.Tree.GetNextChild(category_item, category_cookie)
   126                             blocktype_item, category_cookie = self.Tree.GetNextChild(category_item, category_cookie)
   278     
   255     
   279     def OnTreeItemSelected(self, event):
   256     def OnTreeItemSelected(self, event):
   280         selected = event.GetItem()
   257         selected = event.GetItem()
   281         pydata = self.Tree.GetPyData(selected)
   258         pydata = self.Tree.GetPyData(selected)
   282         if pydata is not None and pydata["type"] != CATEGORY:
   259         if pydata is not None and pydata["type"] != CATEGORY:
   283             blocktype = self.Controler.GetBlockType(self.Tree.GetItemText(selected), pydata["inputs"])
   260             blocktype = self.Controller.GetBlockType(self.Tree.GetItemText(selected), pydata["inputs"])
   284             if blocktype:
   261             if blocktype:
   285                 comment = blocktype["comment"]
   262                 comment = blocktype["comment"]
   286                 self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
   263                 self.Comment.SetValue(_(comment) + blocktype.get("usage", ""))
   287             else:
   264             else:
   288                 self.Comment.SetValue("")
   265                 self.Comment.SetValue("")