editors/FileManagementPanel.py
author laurent
Tue, 11 Sep 2012 19:13:42 +0200
changeset 826 098f822ef308
parent 815 e4f24593a758
child 951 7f9a9c47a317
permissions -rw-r--r--
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
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
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents: 801
diff changeset
    30
from EditorPanel import EditorPanel
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents: 801
diff changeset
    31
from util.BitmapLibrary import GetBitmap
782
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):
794
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   201
        old_filepath = self.GetPath(event.GetItem())
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   202
        new_filepath = os.path.join(os.path.split(old_filepath)[0], event.GetLabel())
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   203
        if new_filepath != old_filepath:
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   204
            if not os.path.exists(new_filepath):
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   205
                os.rename(old_filepath, new_filepath)
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   206
                event.Skip()
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   207
            else:
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   208
                message =  wx.MessageDialog(self, 
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   209
                    _("File '%s' already exists!") % event.GetLabel(), 
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   210
                    _("Error"), wx.OK|wx.ICON_ERROR)
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   211
                message.ShowModal()
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   212
                message.Destroy()
5bbf57905011 Fix bug while renaming project file in FileManagementPanel
laurent
parents: 789
diff changeset
   213
                event.Veto()
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   214
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   215
    def OnFilterChanged(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   216
        self.CurrentFilter = self.Filters[self.Filter.GetStringSelection()]
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   217
        self.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   218
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   219
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   220
    def _SelectItem(self, root, parts):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   221
        if len(parts) == 0:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   222
            self.Tree.SelectItem(root)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   223
        else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   224
            item, item_cookie = self.Tree.GetFirstChild(root)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   225
            while item.IsOk():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   226
                if self.Tree.GetItemText(item) == parts[0]:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   227
                    if (self.Tree.ItemHasChildren(item) and 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   228
                        not self.Tree.IsExpanded(item)):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   229
                        self.Tree.Expand(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   230
                        wx.CallAfter(self._SelectItem, item, parts[1:])
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   231
                    else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   232
                        self._SelectItem(item, parts[1:])
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   233
                    return
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   234
                item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   235
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   236
    def SetPath(self, path):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   237
        if path.startswith(self.Folder):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   238
            root = self.Tree.GetRootItem()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   239
            if root.IsOk():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   240
                relative_path = path.replace(os.path.join(self.Folder, ""), "")
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   241
                self._SelectItem(root, splitpath(relative_path))
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
    def GetPath(self, item=None):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   244
        if item is None:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   245
            item = self.Tree.GetSelection()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   246
        if item.IsOk():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   247
            filepath = self.Tree.GetItemText(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   248
            parent = self.Tree.GetItemParent(item)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   249
            while parent.IsOk() and parent != self.Tree.GetRootItem():
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   250
                filepath = os.path.join(self.Tree.GetItemText(parent), filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   251
                parent = self.Tree.GetItemParent(parent)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   252
            return os.path.join(self.Folder, filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   253
        return self.Folder
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   254
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   255
class FileManagementPanel(EditorPanel):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   256
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   257
    def _init_Editor(self, parent):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   258
        self.Editor = wx.Panel(parent)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   259
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   260
        main_sizer = wx.BoxSizer(wx.HORIZONTAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   261
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   262
        left_sizer = wx.BoxSizer(wx.VERTICAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   263
        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
   264
        
815
e4f24593a758 Adding support for extending internationalization to extensions
laurent
parents: 814
diff changeset
   265
        managed_dir_label = wx.StaticText(self.Editor, label=_(self.TagName) + ":")
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   266
        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
   267
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   268
        self.ManagedDir = FolderTree(self.Editor, self.Folder, FILTER)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   269
        left_sizer.AddWindow(self.ManagedDir, 1, flag=wx.GROW)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   270
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   271
        managed_treectrl = self.ManagedDir.GetTreeCtrl()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   272
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemChanged, managed_treectrl)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   273
        if self.EnableDragNDrop:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   274
            self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, managed_treectrl)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   275
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   276
        button_sizer = wx.BoxSizer(wx.VERTICAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   277
        main_sizer.AddSizer(button_sizer, border=5, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   278
              flag=wx.ALL|wx.ALIGN_CENTER_VERTICAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   279
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   280
        for idx, (name, bitmap, help) in enumerate([
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   281
                ("DeleteButton", "remove_element", _("Remove file from left folder")),
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   282
                ("LeftCopyButton", "LeftCopy", _("Copy file from right folder to left")),
801
435e49e80832 Update list of messages to be translated for internationalization and french translations
laurent
parents: 794
diff changeset
   283
                ("RightCopyButton", "RightCopy", _("Copy file from left folder to right")),
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   284
                ("EditButton", "edit", _("Edit file"))]):
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   285
            button = wx.lib.buttons.GenBitmapButton(self.Editor, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   286
                  bitmap=GetBitmap(bitmap), 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   287
                  size=wx.Size(28, 28), style=wx.NO_BORDER)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   288
            button.SetToolTipString(help)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   289
            setattr(self, name, button)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   290
            if idx > 0:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   291
                flag = wx.TOP
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   292
            else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   293
                flag = 0
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   294
            self.Bind(wx.EVT_BUTTON, getattr(self, "On" + name), button)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   295
            button_sizer.AddWindow(button, border=20, flag=flag)
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
        right_sizer = wx.BoxSizer(wx.VERTICAL)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   298
        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
   299
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   300
        if wx.Platform == '__WXMSW__':
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   301
            system_dir_label = wx.StaticText(self.Editor, label=_("My Computer:"))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   302
        else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   303
            system_dir_label = wx.StaticText(self.Editor, label=_("Home Directory:"))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   304
        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
   305
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   306
        self.SystemDir = FolderTree(self.Editor, self.HomeDirectory, FILTER, False)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   307
        right_sizer.AddWindow(self.SystemDir, 1, flag=wx.GROW)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   308
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   309
        system_treectrl = self.SystemDir.GetTreeCtrl()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   310
        self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemChanged, system_treectrl)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   311
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   312
        self.Editor.SetSizer(main_sizer)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   313
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   314
    def __init__(self, parent, controler, name, folder, enable_dragndrop=False):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   315
        self.Folder = os.path.realpath(folder)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   316
        self.EnableDragNDrop = enable_dragndrop
783
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   317
        
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   318
        if wx.Platform == '__WXMSW__':
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   319
            self.HomeDirectory = "/"
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   320
        else:
f5cea1a6851e Fixing file explorer for Windows
laurent
parents: 782
diff changeset
   321
            self.HomeDirectory = os.path.expanduser("~")
782
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
        EditorPanel.__init__(self, parent, name, None, None)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   324
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   325
        self.Controler = controler
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   326
        
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   327
        self.EditableFileExtensions = []
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   328
        self.EditButton.Hide()
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   329
        
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   330
        self.SetIcon(GetBitmap("FOLDER"))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   331
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   332
    def __del__(self):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   333
        self.Controler.OnCloseEditor(self)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   334
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   335
    def GetTitle(self):
815
e4f24593a758 Adding support for extending internationalization to extensions
laurent
parents: 814
diff changeset
   336
        return _(self.TagName)
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   337
    
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   338
    def SetEditableFileExtensions(self, extensions):
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   339
        self.EditableFileExtensions = extensions
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   340
        if len(self.EditableFileExtensions) > 0:
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   341
            self.EditButton.Show()
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   342
    
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   343
    def RefreshView(self):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   344
        self.ManagedDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   345
        self.SystemDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   346
        self.RefreshButtonsState()
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 RefreshButtonsState(self):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   349
        managed_filepath = self.ManagedDir.GetPath()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   350
        system_filepath = self.SystemDir.GetPath()
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
        self.DeleteButton.Enable(os.path.isfile(managed_filepath))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   353
        self.LeftCopyButton.Enable(os.path.isfile(system_filepath))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   354
        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
   355
        if len(self.EditableFileExtensions) > 0:
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   356
            self.EditButton.Enable(
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   357
                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
   358
                os.path.splitext(managed_filepath)[1] in self.EditableFileExtensions)
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   359
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   360
    def OnTreeItemChanged(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   361
        self.RefreshButtonsState()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   362
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   363
        
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   364
    def OnDeleteButton(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   365
        filepath = self.ManagedDir.GetPath()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   366
        if os.path.isfile(filepath):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   367
            folder, filename = os.path.split(filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   368
            
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   369
            dialog = wx.MessageDialog(self, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   370
                  _("Do you really want to delete the file '%s'?") % filename, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   371
                  _("Delete File"), wx.YES_NO|wx.ICON_QUESTION)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   372
            remove = dialog.ShowModal() == wx.ID_YES
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   373
            dialog.Destroy()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   374
            
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   375
            if remove:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   376
                os.remove(filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   377
                self.ManagedDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   378
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   379
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   380
    def OnEditButton(self, event):
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   381
        filepath = self.ManagedDir.GetPath()
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   382
        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
   383
            os.path.splitext(filepath)[1] in self.EditableFileExtensions):
789
b1b1a8f85e2f Adding support for saving editor choosen when editing project file for reloading project layout
laurent
parents: 784
diff changeset
   384
            self.Controler._OpenView(filepath + "::")
784
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   385
        event.Skip()
a1d970365e41 Adding support for beremiz extensions to define custom file editors for project files
laurent
parents: 783
diff changeset
   386
        
782
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   387
    def CopyFile(self, src, dst):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   388
        if os.path.isfile(src):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   389
            src_folder, src_filename = os.path.split(src)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   390
            if os.path.isfile(dst):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   391
                dst_folder, dst_filename = os.path.split(dst)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   392
            else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   393
                dst_folder = dst
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   394
            
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   395
            dst_filepath = os.path.join(dst_folder, src_filename)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   396
            if os.path.isfile(dst_filepath):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   397
                dialog = wx.MessageDialog(self, 
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   398
                      _("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
   399
                      _("Replace File"), wx.YES_NO|wx.ICON_QUESTION)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   400
                copy = dialog.ShowModal() == wx.ID_YES
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   401
                dialog.Destroy()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   402
            else:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   403
                copy = True
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
            if copy:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   406
                shutil.copyfile(src, dst_filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   407
                return dst_filepath
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   408
        return None
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   409
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   410
    def OnLeftCopyButton(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   411
        filepath = self.CopyFile(self.SystemDir.GetPath(), self.ManagedDir.GetPath())
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   412
        if filepath is not None:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   413
            self.ManagedDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   414
            self.ManagedDir.SetPath(filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   415
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   416
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   417
    def OnRightCopyButton(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   418
        filepath = self.CopyFile(self.ManagedDir.GetPath(), self.SystemDir.GetPath())
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   419
        if filepath is not None:
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   420
            self.SystemDir.RefreshTree()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   421
            self.SystemDir.SetPath(filepath)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   422
        event.Skip()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   423
    
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   424
    def OnTreeBeginDrag(self, event):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   425
        filepath = self.ManagedDir.GetPath()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   426
        if os.path.isfile(filepath):
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   427
            relative_filepath = filepath.replace(os.path.join(self.Folder, ""), "")
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   428
            data = wx.TextDataObject(str(("'%s'" % relative_filepath, "Constant")))
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   429
            dragSource = wx.DropSource(self)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   430
            dragSource.SetData(data)
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   431
            dragSource.DoDragDrop()
6f0e10085df9 Adding support for file explorer for project files
laurent
parents:
diff changeset
   432