util/FileManagementPanel.py
author Edouard Tisserant
Fri, 06 Jul 2012 21:00:43 +0200
changeset 787 f49875b9e946
parent 784 a1d970365e41
child 789 b1b1a8f85e2f
permissions -rw-r--r--
merge
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
     1
#!/usr/bin/env python
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
     3
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
     6
#
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
     8
#
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
     9
#See COPYING file for copyrights details.
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    10
#
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    15
#
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    19
#General Public License for more details.
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    20
#
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    24
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    25
import os
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    26
import shutil
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    27
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    28
import wx
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    29
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    30
from controls import EditorPanel
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    31
from utils.BitmapLibrary import GetBitmap
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    32
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    33
DRIVE, FOLDER, FILE = range(3)
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    34
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    35
FILTER = _("All files (*.*)|*.*|CSV files (*.csv)|*.csv")
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    36
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    37
def sort_folder(x, y):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    38
    if x[1] == y[1]:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    39
        return cmp(x[0], y[0])
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    40
    elif x[1] != FILE:
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    41
        return -1
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    42
    else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    43
        return 1
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    44
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    45
def splitpath(path):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    46
    head, tail = os.path.split(path)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    47
    if head == "":
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    48
        return [tail]
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    49
    elif tail == "":
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    50
        return splitpath(head)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    51
    return splitpath(head) + [tail]
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    52
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    53
class FolderTree(wx.Panel):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    54
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    55
    def __init__(self, parent, folder, filter, editable=True):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    56
        wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    57
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    58
        main_sizer = wx.BoxSizer(wx.VERTICAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    59
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    60
        self.Tree = wx.TreeCtrl(self, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    61
              style=wx.TR_HAS_BUTTONS|
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    62
                    wx.TR_SINGLE|
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    63
                    wx.SUNKEN_BORDER|
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    64
                    wx.TR_HIDE_ROOT|
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    65
                    wx.TR_LINES_AT_ROOT|
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    66
                    wx.TR_EDIT_LABELS)
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    67
        if wx.Platform == '__WXMSW__':
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    68
            self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnTreeItemExpanded, self.Tree)
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    69
            self.Tree.Bind(wx.EVT_LEFT_DOWN, self.OnTreeLeftDown)
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    70
        else:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    71
            self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnTreeItemExpanded, self.Tree)
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    72
        self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnTreeItemCollapsed, self.Tree)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    73
        self.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.OnTreeBeginLabelEdit, self.Tree)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    74
        self.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.OnTreeEndLabelEdit, self.Tree)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    75
        main_sizer.AddWindow(self.Tree, 1, flag=wx.GROW)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    76
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    77
        self.Filter = wx.ComboBox(self, style=wx.CB_READONLY)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    78
        self.Bind(wx.EVT_COMBOBOX, self.OnFilterChanged, self.Filter)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    79
        main_sizer.AddWindow(self.Filter, flag=wx.GROW)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    80
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    81
        self.SetSizer(main_sizer)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    82
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    83
        self.Folder = folder
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    84
        self.Editable = editable
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    85
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    86
        self.TreeImageList = wx.ImageList(16, 16)
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    87
        self.TreeImageDict = {}
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    88
        for item_type, bitmap in [(DRIVE, "tree_drive"),
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    89
                                  (FOLDER, "tree_folder"),
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    90
                                  (FILE, "tree_file")]:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
    91
            self.TreeImageDict[item_type] = self.TreeImageList.Add(GetBitmap(bitmap))
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    92
        self.Tree.SetImageList(self.TreeImageList)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    93
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    94
        self.Filters = {}
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    95
        filter_parts = filter.split("|")
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    96
        for idx in xrange(0, len(filter_parts), 2):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    97
            if filter_parts[idx + 1] == "*.*":
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    98
                self.Filters[filter_parts[idx]] = ""
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
    99
            else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   100
                self.Filters[filter_parts[idx]] = filter_parts[idx + 1].replace("*", "")
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   101
            self.Filter.Append(filter_parts[idx])
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   102
            if idx == 0:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   103
                self.Filter.SetStringSelection(filter_parts[idx])
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   104
            
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   105
        self.CurrentFilter = self.Filters[self.Filter.GetStringSelection()]
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   106
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   107
    def _GetFolderChildren(self, folderpath, recursive=True):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   108
        items = []
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   109
        if wx.Platform == '__WXMSW__' and folderpath == "/":
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   110
            for c in xrange(ord('a'), ord('z')):
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   111
                drive = os.path.join("%s:\\" % chr(c))
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   112
                if os.path.exists(drive):
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   113
                    items.append((drive, DRIVE, self._GetFolderChildren(drive, False)))
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   114
        else:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   115
            try:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   116
                files = os.listdir(folderpath)
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   117
            except:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   118
                return []
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   119
            for filename in files:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   120
                if not filename.startswith("."):
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   121
                    filepath = os.path.join(folderpath, filename)
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   122
                    if os.path.isdir(filepath):
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   123
                        if recursive:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   124
                            children = len(self._GetFolderChildren(filepath, False))
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   125
                        else:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   126
                            children = 0
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   127
                        items.append((filename, FOLDER, children))
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   128
                    elif (self.CurrentFilter == "" or 
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   129
                          os.path.splitext(filename)[1] == self.CurrentFilter):
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   130
                        items.append((filename, FILE, None))
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   131
        if recursive:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   132
            items.sort(sort_folder)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   133
        return items
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   134
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   135
    def GetTreeCtrl(self):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   136
        return self.Tree
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   137
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   138
    def RefreshTree(self):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   139
        root = self.Tree.GetRootItem()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   140
        if not root.IsOk():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   141
            root = self.Tree.AddRoot("")
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   142
        self.GenerateTreeBranch(root, self.Folder)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   143
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   144
    def GenerateTreeBranch(self, root, folderpath):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   145
        item, item_cookie = self.Tree.GetFirstChild(root)
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   146
        for idx, (filename, item_type, children) in enumerate(self._GetFolderChildren(folderpath)):
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   147
            if not item.IsOk():
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   148
                item = self.Tree.AppendItem(root, filename, self.TreeImageDict[item_type])
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   149
                if wx.Platform != '__WXMSW__':
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   150
                    item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   151
            elif self.Tree.GetItemText(item) != filename:
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   152
                item = self.Tree.InsertItemBefore(root, idx, filename, self.TreeImageDict[item_type])
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   153
            filepath = os.path.join(folderpath, filename)
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   154
            if item_type != FILE:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   155
                if self.Tree.IsExpanded(item):
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   156
                    self.GenerateTreeBranch(item, filepath)
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   157
                elif children > 0:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   158
                    self.Tree.SetItemHasChildren(item)
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   159
            item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   160
        to_delete = []
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   161
        while item.IsOk():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   162
            to_delete.append(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   163
            item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   164
        for item in to_delete:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   165
            self.Tree.Delete(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   166
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   167
    def ExpandItem(self, item):
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   168
        self.GenerateTreeBranch(item, self.GetPath(item))
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   169
        self.Tree.Expand(item)
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   170
    
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   171
    def OnTreeItemActivated(self, event):
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   172
        self.ExpandItem(event.GetItem())
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   173
        event.Skip()
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   174
    
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   175
    def OnTreeLeftDown(self, event):
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   176
        item, flags = self.Tree.HitTest(event.GetPosition())
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   177
        if flags & wx.TREE_HITTEST_ONITEMBUTTON and not self.Tree.IsExpanded(item):
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   178
            self.ExpandItem(item)
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   179
        else:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   180
            event.Skip()
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   181
    
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   182
    def OnTreeItemExpanded(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   183
        item = event.GetItem()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   184
        self.GenerateTreeBranch(item, self.GetPath(item))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   185
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   186
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   187
    def OnTreeItemCollapsed(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   188
        item = event.GetItem()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   189
        self.Tree.DeleteChildren(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   190
        self.Tree.SetItemHasChildren(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   191
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   192
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   193
    def OnTreeBeginLabelEdit(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   194
        item = event.GetItem()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   195
        if self.Editable and not self.Tree.ItemHasChildren(item):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   196
            event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   197
        else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   198
            event.Veto()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   199
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   200
    def OnTreeEndLabelEdit(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   201
        event.Veto()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   202
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   203
    def OnFilterChanged(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   204
        self.CurrentFilter = self.Filters[self.Filter.GetStringSelection()]
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   205
        self.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   206
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   207
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   208
    def _SelectItem(self, root, parts):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   209
        if len(parts) == 0:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   210
            self.Tree.SelectItem(root)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   211
        else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   212
            item, item_cookie = self.Tree.GetFirstChild(root)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   213
            while item.IsOk():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   214
                if self.Tree.GetItemText(item) == parts[0]:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   215
                    if (self.Tree.ItemHasChildren(item) and 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   216
                        not self.Tree.IsExpanded(item)):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   217
                        self.Tree.Expand(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   218
                        wx.CallAfter(self._SelectItem, item, parts[1:])
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   219
                    else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   220
                        self._SelectItem(item, parts[1:])
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   221
                    return
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   222
                item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   223
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   224
    def SetPath(self, path):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   225
        if path.startswith(self.Folder):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   226
            root = self.Tree.GetRootItem()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   227
            if root.IsOk():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   228
                relative_path = path.replace(os.path.join(self.Folder, ""), "")
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   229
                self._SelectItem(root, splitpath(relative_path))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   230
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   231
    def GetPath(self, item=None):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   232
        if item is None:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   233
            item = self.Tree.GetSelection()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   234
        if item.IsOk():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   235
            filepath = self.Tree.GetItemText(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   236
            parent = self.Tree.GetItemParent(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   237
            while parent.IsOk() and parent != self.Tree.GetRootItem():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   238
                filepath = os.path.join(self.Tree.GetItemText(parent), filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   239
                parent = self.Tree.GetItemParent(parent)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   240
            return os.path.join(self.Folder, filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   241
        return self.Folder
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   242
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   243
class FileManagementPanel(EditorPanel):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   244
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   245
    def _init_Editor(self, parent):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   246
        self.Editor = wx.Panel(parent)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   247
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   248
        main_sizer = wx.BoxSizer(wx.HORIZONTAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   249
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   250
        left_sizer = wx.BoxSizer(wx.VERTICAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   251
        main_sizer.AddSizer(left_sizer, 1, border=5, flag=wx.GROW|wx.ALL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   252
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   253
        managed_dir_label = wx.StaticText(self.Editor, label=self.TagName + ":")
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   254
        left_sizer.AddWindow(managed_dir_label, border=5, flag=wx.GROW|wx.BOTTOM)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   255
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   256
        self.ManagedDir = FolderTree(self.Editor, self.Folder, FILTER)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   257
        left_sizer.AddWindow(self.ManagedDir, 1, flag=wx.GROW)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   258
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   259
        managed_treectrl = self.ManagedDir.GetTreeCtrl()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   260
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemChanged, managed_treectrl)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   261
        if self.EnableDragNDrop:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   262
            self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, managed_treectrl)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   263
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   264
        button_sizer = wx.BoxSizer(wx.VERTICAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   265
        main_sizer.AddSizer(button_sizer, border=5, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   266
              flag=wx.ALL|wx.ALIGN_CENTER_VERTICAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   267
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   268
        for idx, (name, bitmap, help) in enumerate([
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   269
                ("DeleteButton", "remove_element", _("Remove file from left folder")),
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   270
                ("LeftCopyButton", "LeftCopy", _("Copy file from right folder to left")),
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   271
                ("RightCopyButton", "RightCopy", _("copy file from left folder to right")),
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   272
                ("EditButton", "edit", _("Edit file"))]):
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   273
            button = wx.lib.buttons.GenBitmapButton(self.Editor, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   274
                  bitmap=GetBitmap(bitmap), 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   275
                  size=wx.Size(28, 28), style=wx.NO_BORDER)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   276
            button.SetToolTipString(help)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   277
            setattr(self, name, button)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   278
            if idx > 0:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   279
                flag = wx.TOP
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   280
            else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   281
                flag = 0
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   282
            self.Bind(wx.EVT_BUTTON, getattr(self, "On" + name), button)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   283
            button_sizer.AddWindow(button, border=20, flag=flag)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   284
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   285
        right_sizer = wx.BoxSizer(wx.VERTICAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   286
        main_sizer.AddSizer(right_sizer, 1, border=5, flag=wx.GROW|wx.ALL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   287
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   288
        if wx.Platform == '__WXMSW__':
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   289
            system_dir_label = wx.StaticText(self.Editor, label=_("My Computer:"))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   290
        else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   291
            system_dir_label = wx.StaticText(self.Editor, label=_("Home Directory:"))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   292
        right_sizer.AddWindow(system_dir_label, border=5, flag=wx.GROW|wx.BOTTOM)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   293
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   294
        self.SystemDir = FolderTree(self.Editor, self.HomeDirectory, FILTER, False)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   295
        right_sizer.AddWindow(self.SystemDir, 1, flag=wx.GROW)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   296
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   297
        system_treectrl = self.SystemDir.GetTreeCtrl()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   298
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemChanged, system_treectrl)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   299
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   300
        self.Editor.SetSizer(main_sizer)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   301
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   302
    def __init__(self, parent, controler, name, folder, enable_dragndrop=False):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   303
        self.Folder = os.path.realpath(folder)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   304
        self.EnableDragNDrop = enable_dragndrop
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   305
        
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   306
        if wx.Platform == '__WXMSW__':
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   307
            self.HomeDirectory = "/"
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   308
        else:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   309
            self.HomeDirectory = os.path.expanduser("~")
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   310
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   311
        EditorPanel.__init__(self, parent, name, None, None)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   312
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   313
        self.Controler = controler
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   314
        
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   315
        self.EditableFileExtensions = []
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   316
        self.EditButton.Hide()
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   317
        
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   318
        self.SetIcon(GetBitmap("FOLDER"))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   319
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   320
    def __del__(self):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   321
        self.Controler.OnCloseEditor(self)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   322
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   323
    def GetTitle(self):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   324
        return self.TagName
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   325
    
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   326
    def SetEditableFileExtensions(self, extensions):
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   327
        self.EditableFileExtensions = extensions
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   328
        if len(self.EditableFileExtensions) > 0:
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   329
            self.EditButton.Show()
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   330
    
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   331
    def RefreshView(self):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   332
        self.ManagedDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   333
        self.SystemDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   334
        self.RefreshButtonsState()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   335
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   336
    def RefreshButtonsState(self):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   337
        managed_filepath = self.ManagedDir.GetPath()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   338
        system_filepath = self.SystemDir.GetPath()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   339
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   340
        self.DeleteButton.Enable(os.path.isfile(managed_filepath))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   341
        self.LeftCopyButton.Enable(os.path.isfile(system_filepath))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   342
        self.RightCopyButton.Enable(os.path.isfile(managed_filepath))
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   343
        if len(self.EditableFileExtensions) > 0:
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   344
            self.EditButton.Enable(
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   345
                os.path.isfile(managed_filepath) and
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   346
                os.path.splitext(managed_filepath)[1] in self.EditableFileExtensions)
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   347
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   348
    def OnTreeItemChanged(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   349
        self.RefreshButtonsState()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   350
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   351
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   352
    def OnDeleteButton(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   353
        filepath = self.ManagedDir.GetPath()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   354
        if os.path.isfile(filepath):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   355
            folder, filename = os.path.split(filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   356
            
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   357
            dialog = wx.MessageDialog(self, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   358
                  _("Do you really want to delete the file '%s'?") % filename, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   359
                  _("Delete File"), wx.YES_NO|wx.ICON_QUESTION)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   360
            remove = dialog.ShowModal() == wx.ID_YES
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   361
            dialog.Destroy()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   362
            
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   363
            if remove:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   364
                os.remove(filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   365
                self.ManagedDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   366
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   367
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   368
    def OnEditButton(self, event):
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   369
        filepath = self.ManagedDir.GetPath()
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   370
        if (os.path.isfile(filepath) and 
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   371
            os.path.splitext(filepath)[1] in self.EditableFileExtensions):
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   372
            self.Controler._OpenView(filepath)
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   373
        event.Skip()
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   374
        
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   375
    def CopyFile(self, src, dst):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   376
        if os.path.isfile(src):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   377
            src_folder, src_filename = os.path.split(src)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   378
            if os.path.isfile(dst):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   379
                dst_folder, dst_filename = os.path.split(dst)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   380
            else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   381
                dst_folder = dst
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   382
            
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   383
            dst_filepath = os.path.join(dst_folder, src_filename)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   384
            if os.path.isfile(dst_filepath):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   385
                dialog = wx.MessageDialog(self, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   386
                      _("The file '%s' already exist.\nDo you want to replace it?") % src_filename, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   387
                      _("Replace File"), wx.YES_NO|wx.ICON_QUESTION)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   388
                copy = dialog.ShowModal() == wx.ID_YES
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   389
                dialog.Destroy()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   390
            else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   391
                copy = True
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   392
                
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   393
            if copy:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   394
                shutil.copyfile(src, dst_filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   395
                return dst_filepath
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   396
        return None
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   397
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   398
    def OnLeftCopyButton(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   399
        filepath = self.CopyFile(self.SystemDir.GetPath(), self.ManagedDir.GetPath())
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   400
        if filepath is not None:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   401
            self.ManagedDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   402
            self.ManagedDir.SetPath(filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   403
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   404
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   405
    def OnRightCopyButton(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   406
        filepath = self.CopyFile(self.ManagedDir.GetPath(), self.SystemDir.GetPath())
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   407
        if filepath is not None:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   408
            self.SystemDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   409
            self.SystemDir.SetPath(filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   410
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   411
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   412
    def OnTreeBeginDrag(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   413
        filepath = self.ManagedDir.GetPath()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   414
        if os.path.isfile(filepath):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   415
            relative_filepath = filepath.replace(os.path.join(self.Folder, ""), "")
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   416
            data = wx.TextDataObject(str(("'%s'" % relative_filepath, "Constant")))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   417
            dragSource = wx.DropSource(self)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   418
            dragSource.SetData(data)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   419
            dragSource.DoDragDrop()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   420