controls/FolderTree.py
author Edouard Tisserant
Mon, 11 Mar 2013 19:17:56 +0900
changeset 972 659198075ce4
parent 951 7f9a9c47a317
child 1219 f5da9702685a
permissions -rw-r--r--
Redirect PyEval exceptions to logging
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     3
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     5
#based on the plcopen standard. 
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     6
#
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     7
#Copyright (C) 2013: Edouard TISSERANT and Laurent BESSARD
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     8
#
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    10
#
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    15
#
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    19
#General Public License for more details.
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    20
#
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    24
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    25
import os
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    26
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    27
import wx
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    28
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    29
from util.BitmapLibrary import GetBitmap
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    30
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    31
DRIVE, FOLDER, FILE = range(3)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    32
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    33
def sort_folder(x, y):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    34
    if x[1] == y[1]:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    35
        return cmp(x[0], y[0])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    36
    elif x[1] != FILE:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    37
        return -1
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    38
    else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    39
        return 1
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    40
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    41
def splitpath(path):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    42
    head, tail = os.path.split(path)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    43
    if head == "":
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    44
        return [tail]
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    45
    elif tail == "":
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    46
        return splitpath(head)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    47
    return splitpath(head) + [tail]
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    48
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    49
class FolderTree(wx.Panel):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    50
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    51
    def __init__(self, parent, folder, filter=None, editable=True):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    52
        wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    53
        
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    54
        main_sizer = wx.BoxSizer(wx.VERTICAL)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    55
        
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    56
        self.Tree = wx.TreeCtrl(self, 
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    57
              style=wx.TR_HAS_BUTTONS|
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    58
                    wx.TR_SINGLE|
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    59
                    wx.SUNKEN_BORDER|
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    60
                    wx.TR_HIDE_ROOT|
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    61
                    wx.TR_LINES_AT_ROOT|
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    62
                    wx.TR_EDIT_LABELS)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    63
        if wx.Platform == '__WXMSW__':
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    64
            self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnTreeItemExpanded, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    65
            self.Tree.Bind(wx.EVT_LEFT_DOWN, self.OnTreeLeftDown)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    66
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    67
            self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnTreeItemExpanded, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    68
        self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnTreeItemCollapsed, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    69
        self.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.OnTreeBeginLabelEdit, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    70
        self.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.OnTreeEndLabelEdit, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    71
        main_sizer.AddWindow(self.Tree, 1, flag=wx.GROW)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    72
        
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    73
        if filter is not None:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    74
            self.Filter = wx.ComboBox(self, style=wx.CB_READONLY)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    75
            self.Bind(wx.EVT_COMBOBOX, self.OnFilterChanged, self.Filter)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    76
            main_sizer.AddWindow(self.Filter, flag=wx.GROW)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    77
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    78
            self.Filter = None
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    79
        
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    80
        self.SetSizer(main_sizer)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    81
        
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    82
        self.Folder = folder
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    83
        self.Editable = editable
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    84
        
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    85
        self.TreeImageList = wx.ImageList(16, 16)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    86
        self.TreeImageDict = {}
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    87
        for item_type, bitmap in [(DRIVE, "tree_drive"),
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    88
                                  (FOLDER, "tree_folder"),
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    89
                                  (FILE, "tree_file")]:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    90
            self.TreeImageDict[item_type] = self.TreeImageList.Add(GetBitmap(bitmap))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    91
        self.Tree.SetImageList(self.TreeImageList)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    92
        
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    93
        self.Filters = {}
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    94
        if self.Filter is not None:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    95
            filter_parts = filter.split("|")
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    96
            for idx in xrange(0, len(filter_parts), 2):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    97
                if filter_parts[idx + 1] == "*.*":
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    98
                    self.Filters[filter_parts[idx]] = ""
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    99
                else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   100
                    self.Filters[filter_parts[idx]] = filter_parts[idx + 1].replace("*", "")
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   101
                self.Filter.Append(filter_parts[idx])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   102
                if idx == 0:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   103
                    self.Filter.SetStringSelection(filter_parts[idx])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   104
                
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   105
            self.CurrentFilter = self.Filters[self.Filter.GetStringSelection()]
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   106
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   107
            self.CurrentFilter = ""
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   108
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   109
    def _GetFolderChildren(self, folderpath, recursive=True):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   110
        items = []
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   111
        if wx.Platform == '__WXMSW__' and folderpath == "/":
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   112
            for c in xrange(ord('a'), ord('z')):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   113
                drive = os.path.join("%s:\\" % chr(c))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   114
                if os.path.exists(drive):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   115
                    items.append((drive, DRIVE, self._GetFolderChildren(drive, False)))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   116
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   117
            try:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   118
                files = os.listdir(folderpath)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   119
            except:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   120
                return []
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   121
            for filename in files:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   122
                if not filename.startswith("."):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   123
                    filepath = os.path.join(folderpath, filename)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   124
                    if os.path.isdir(filepath):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   125
                        if recursive:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   126
                            children = len(self._GetFolderChildren(filepath, False))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   127
                        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   128
                            children = 0
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   129
                        items.append((filename, FOLDER, children))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   130
                    elif (self.CurrentFilter == "" or 
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   131
                          os.path.splitext(filename)[1] == self.CurrentFilter):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   132
                        items.append((filename, FILE, None))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   133
        if recursive:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   134
            items.sort(sort_folder)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   135
        return items
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   136
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   137
    def SetFilter(self, filter):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   138
        self.CurrentFilter = filter
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   139
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   140
    def GetTreeCtrl(self):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   141
        return self.Tree
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   142
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   143
    def RefreshTree(self):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   144
        root = self.Tree.GetRootItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   145
        if not root.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   146
            root = self.Tree.AddRoot("")
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   147
        self.GenerateTreeBranch(root, self.Folder)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   148
        
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   149
    def GenerateTreeBranch(self, root, folderpath):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   150
        item, item_cookie = self.Tree.GetFirstChild(root)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   151
        for idx, (filename, item_type, children) in enumerate(self._GetFolderChildren(folderpath)):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   152
            if not item.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   153
                item = self.Tree.AppendItem(root, filename, self.TreeImageDict[item_type])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   154
                if wx.Platform != '__WXMSW__':
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   155
                    item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   156
            elif self.Tree.GetItemText(item) != filename:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   157
                item = self.Tree.InsertItemBefore(root, idx, filename, self.TreeImageDict[item_type])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   158
            filepath = os.path.join(folderpath, filename)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   159
            if item_type != FILE:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   160
                if self.Tree.IsExpanded(item):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   161
                    self.GenerateTreeBranch(item, filepath)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   162
                elif children > 0:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   163
                    self.Tree.SetItemHasChildren(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   164
            item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   165
        to_delete = []
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   166
        while item.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   167
            to_delete.append(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   168
            item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   169
        for item in to_delete:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   170
            self.Tree.Delete(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   171
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   172
    def ExpandItem(self, item):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   173
        self.GenerateTreeBranch(item, self.GetPath(item))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   174
        self.Tree.Expand(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   175
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   176
    def OnTreeItemActivated(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   177
        self.ExpandItem(event.GetItem())
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   178
        event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   179
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   180
    def OnTreeLeftDown(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   181
        item, flags = self.Tree.HitTest(event.GetPosition())
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   182
        if flags & wx.TREE_HITTEST_ONITEMBUTTON and not self.Tree.IsExpanded(item):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   183
            self.ExpandItem(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   184
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   185
            event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   186
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   187
    def OnTreeItemExpanded(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   188
        item = event.GetItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   189
        self.GenerateTreeBranch(item, self.GetPath(item))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   190
        event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   191
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   192
    def OnTreeItemCollapsed(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   193
        item = event.GetItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   194
        self.Tree.DeleteChildren(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   195
        self.Tree.SetItemHasChildren(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   196
        event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   197
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   198
    def OnTreeBeginLabelEdit(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   199
        item = event.GetItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   200
        if self.Editable and not self.Tree.ItemHasChildren(item):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   201
            event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   202
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   203
            event.Veto()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   204
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   205
    def OnTreeEndLabelEdit(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   206
        old_filepath = self.GetPath(event.GetItem())
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   207
        new_filepath = os.path.join(os.path.split(old_filepath)[0], event.GetLabel())
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   208
        if new_filepath != old_filepath:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   209
            if not os.path.exists(new_filepath):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   210
                os.rename(old_filepath, new_filepath)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   211
                event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   212
            else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   213
                message =  wx.MessageDialog(self, 
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   214
                    _("File '%s' already exists!") % event.GetLabel(), 
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   215
                    _("Error"), wx.OK|wx.ICON_ERROR)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   216
                message.ShowModal()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   217
                message.Destroy()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   218
                event.Veto()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   219
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   220
    def OnFilterChanged(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   221
        self.CurrentFilter = self.Filters[self.Filter.GetStringSelection()]
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   222
        self.RefreshTree()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   223
        event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   224
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   225
    def _SelectItem(self, root, parts):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   226
        if len(parts) == 0:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   227
            self.Tree.SelectItem(root)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   228
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   229
            item, item_cookie = self.Tree.GetFirstChild(root)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   230
            while item.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   231
                if self.Tree.GetItemText(item) == parts[0]:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   232
                    if (self.Tree.ItemHasChildren(item) and 
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   233
                        not self.Tree.IsExpanded(item)):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   234
                        self.Tree.Expand(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   235
                        wx.CallAfter(self._SelectItem, item, parts[1:])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   236
                    else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   237
                        self._SelectItem(item, parts[1:])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   238
                    return
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   239
                item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   240
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   241
    def SetPath(self, path):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   242
        if path.startswith(self.Folder):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   243
            root = self.Tree.GetRootItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   244
            if root.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   245
                relative_path = path.replace(os.path.join(self.Folder, ""), "")
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   246
                self._SelectItem(root, splitpath(relative_path))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   247
    
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   248
    def GetPath(self, item=None):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   249
        if item is None:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   250
            item = self.Tree.GetSelection()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   251
        if item.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   252
            filepath = self.Tree.GetItemText(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   253
            parent = self.Tree.GetItemParent(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   254
            while parent.IsOk() and parent != self.Tree.GetRootItem():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   255
                filepath = os.path.join(self.Tree.GetItemText(parent), filepath)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   256
                parent = self.Tree.GetItemParent(parent)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   257
            return os.path.join(self.Folder, filepath)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   258
        return self.Folder