controls/FolderTree.py
author Edouard Tisserant
Fri, 27 Apr 2018 16:32:53 +0200
changeset 2000 9fa2f8ede5d6
parent 1881 091005ec69c4
child 2432 dbc065a2f7a5
permissions -rw-r--r--
Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
     3
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
     6
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
     7
# Copyright (C) 2013: Edouard TISSERANT and Laurent BESSARD
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
     8
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
     9
# See COPYING file for copyrights details.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    10
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    11
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    12
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    13
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    14
# of the License, or (at your option) any later version.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    15
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    16
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    19
# GNU General Public License for more details.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    20
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    21
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    22
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1219
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    24
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
    25
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
    26
from __future__ import absolute_import
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    27
import os
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    28
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    29
import wx
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    30
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    31
from util.BitmapLibrary import GetBitmap
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    32
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    33
DRIVE, FOLDER, FILE = range(3)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    34
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    35
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    36
def sort_folder(x, y):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    37
    if x[1] == y[1]:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    38
        return cmp(x[0], y[0])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    39
    elif x[1] != FILE:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    40
        return -1
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    41
    else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    42
        return 1
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    43
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    44
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    45
def splitpath(path):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    46
    head, tail = os.path.split(path)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    47
    if head == "":
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    48
        return [tail]
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    49
    elif tail == "":
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    50
        return splitpath(head)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    51
    return splitpath(head) + [tail]
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    52
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    53
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    54
class FolderTree(wx.Panel):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    55
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    56
    def __init__(self, parent, folder, filter=None, editable=True):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    57
        wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    58
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    59
        main_sizer = wx.BoxSizer(wx.VERTICAL)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    60
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    61
        self.Tree = wx.TreeCtrl(self,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
    62
                                style=(wx.TR_HAS_BUTTONS |
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
    63
                                       wx.TR_SINGLE |
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
    64
                                       wx.SUNKEN_BORDER |
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
    65
                                       wx.TR_HIDE_ROOT |
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
    66
                                       wx.TR_LINES_AT_ROOT |
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
    67
                                       wx.TR_EDIT_LABELS))
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    68
        if wx.Platform == '__WXMSW__':
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    69
            self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnTreeItemExpanded, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    70
            self.Tree.Bind(wx.EVT_LEFT_DOWN, self.OnTreeLeftDown)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    71
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    72
            self.Bind(wx.EVT_TREE_ITEM_EXPANDED, self.OnTreeItemExpanded, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    73
        self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnTreeItemCollapsed, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    74
        self.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.OnTreeBeginLabelEdit, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    75
        self.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.OnTreeEndLabelEdit, self.Tree)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    76
        main_sizer.AddWindow(self.Tree, 1, flag=wx.GROW)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    77
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    78
        if filter is not None:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    79
            self.Filter = wx.ComboBox(self, style=wx.CB_READONLY)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    80
            self.Bind(wx.EVT_COMBOBOX, self.OnFilterChanged, self.Filter)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    81
            main_sizer.AddWindow(self.Filter, flag=wx.GROW)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    82
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    83
            self.Filter = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    84
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    85
        self.SetSizer(main_sizer)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    86
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    87
        self.Folder = folder
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    88
        self.Editable = editable
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    89
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    90
        self.TreeImageList = wx.ImageList(16, 16)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    91
        self.TreeImageDict = {}
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    92
        for item_type, bitmap in [(DRIVE, "tree_drive"),
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    93
                                  (FOLDER, "tree_folder"),
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    94
                                  (FILE, "tree_file")]:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    95
            self.TreeImageDict[item_type] = self.TreeImageList.Add(GetBitmap(bitmap))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    96
        self.Tree.SetImageList(self.TreeImageList)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
    97
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    98
        self.Filters = {}
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
    99
        if self.Filter is not None:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   100
            filter_parts = filter.split("|")
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   101
            for idx in xrange(0, len(filter_parts), 2):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   102
                if filter_parts[idx + 1] == "*.*":
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   103
                    self.Filters[filter_parts[idx]] = ""
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   104
                else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   105
                    self.Filters[filter_parts[idx]] = filter_parts[idx + 1].replace("*", "")
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   106
                self.Filter.Append(filter_parts[idx])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   107
                if idx == 0:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   108
                    self.Filter.SetStringSelection(filter_parts[idx])
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   109
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   110
            self.CurrentFilter = self.Filters[self.Filter.GetStringSelection()]
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   111
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   112
            self.CurrentFilter = ""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   113
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   114
    def _GetFolderChildren(self, folderpath, recursive=True):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   115
        items = []
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   116
        if wx.Platform == '__WXMSW__' and folderpath == "/":
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   117
            for c in xrange(ord('a'), ord('z')):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   118
                drive = os.path.join("%s:\\" % chr(c))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   119
                if os.path.exists(drive):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   120
                    items.append((drive, DRIVE, self._GetFolderChildren(drive, False)))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   121
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   122
            try:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   123
                files = os.listdir(folderpath)
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
   124
            except Exception:
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   125
                return []
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   126
            for filename in files:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   127
                if not filename.startswith("."):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   128
                    filepath = os.path.join(folderpath, filename)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   129
                    if os.path.isdir(filepath):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   130
                        if recursive:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   131
                            children = len(self._GetFolderChildren(filepath, False))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   132
                        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   133
                            children = 0
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   134
                        items.append((filename, FOLDER, children))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   135
                    elif (self.CurrentFilter == "" or
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   136
                          os.path.splitext(filename)[1] == self.CurrentFilter):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   137
                        items.append((filename, FILE, None))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   138
        if recursive:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   139
            items.sort(sort_folder)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   140
        return items
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   141
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   142
    def SetFilter(self, filter):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   143
        self.CurrentFilter = filter
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   144
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   145
    def GetTreeCtrl(self):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   146
        return self.Tree
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   147
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   148
    def RefreshTree(self):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   149
        root = self.Tree.GetRootItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   150
        if not root.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   151
            root = self.Tree.AddRoot("")
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   152
        self.GenerateTreeBranch(root, self.Folder)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   153
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   154
    def GenerateTreeBranch(self, root, folderpath):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   155
        item, item_cookie = self.Tree.GetFirstChild(root)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   156
        for idx, (filename, item_type, children) in enumerate(self._GetFolderChildren(folderpath)):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   157
            if not item.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   158
                item = self.Tree.AppendItem(root, filename, self.TreeImageDict[item_type])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   159
                if wx.Platform != '__WXMSW__':
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   160
                    item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   161
            elif self.Tree.GetItemText(item) != filename:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   162
                item = self.Tree.InsertItemBefore(root, idx, filename, self.TreeImageDict[item_type])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   163
            filepath = os.path.join(folderpath, filename)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   164
            if item_type != FILE:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   165
                if self.Tree.IsExpanded(item):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   166
                    self.GenerateTreeBranch(item, filepath)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   167
                elif children > 0:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   168
                    self.Tree.SetItemHasChildren(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   169
            item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   170
        to_delete = []
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   171
        while item.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   172
            to_delete.append(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   173
            item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   174
        for item in to_delete:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   175
            self.Tree.Delete(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   176
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   177
    def ExpandItem(self, item):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   178
        self.GenerateTreeBranch(item, self.GetPath(item))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   179
        self.Tree.Expand(item)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   180
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   181
    def OnTreeItemActivated(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   182
        self.ExpandItem(event.GetItem())
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   183
        event.Skip()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   184
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   185
    def OnTreeLeftDown(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   186
        item, flags = self.Tree.HitTest(event.GetPosition())
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   187
        if flags & wx.TREE_HITTEST_ONITEMBUTTON and not self.Tree.IsExpanded(item):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   188
            self.ExpandItem(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   189
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   190
            event.Skip()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   191
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   192
    def OnTreeItemExpanded(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   193
        item = event.GetItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   194
        self.GenerateTreeBranch(item, self.GetPath(item))
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   195
        event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   196
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   197
    def OnTreeItemCollapsed(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   198
        item = event.GetItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   199
        self.Tree.DeleteChildren(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   200
        self.Tree.SetItemHasChildren(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   201
        event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   202
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   203
    def OnTreeBeginLabelEdit(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   204
        item = event.GetItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   205
        if self.Editable and not self.Tree.ItemHasChildren(item):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   206
            event.Skip()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   207
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   208
            event.Veto()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   209
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   210
    def OnTreeEndLabelEdit(self, event):
1219
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   211
        new_name = event.GetLabel()
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   212
        if new_name != "":
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   213
            old_filepath = self.GetPath(event.GetItem())
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   214
            new_filepath = os.path.join(os.path.split(old_filepath)[0], new_name)
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   215
            if new_filepath != old_filepath:
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   216
                if not os.path.exists(new_filepath):
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   217
                    os.rename(old_filepath, new_filepath)
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   218
                    event.Skip()
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   219
                else:
1758
845ca626db09 clean-up: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1754
diff changeset
   220
                    message = wx.MessageDialog(self,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
   221
                                               _("File '%s' already exists!") % new_name,
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
   222
                                               _("Error"), wx.OK | wx.ICON_ERROR)
1219
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   223
                    message.ShowModal()
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   224
                    message.Destroy()
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   225
                    event.Veto()
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   226
        else:
f5da9702685a Fixed bug when editing file name and close editor without modification
Laurent Bessard
parents: 951
diff changeset
   227
            event.Skip()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   228
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   229
    def OnFilterChanged(self, event):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   230
        self.CurrentFilter = self.Filters[self.Filter.GetStringSelection()]
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   231
        self.RefreshTree()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   232
        event.Skip()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   233
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   234
    def _SelectItem(self, root, parts):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   235
        if len(parts) == 0:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   236
            self.Tree.SelectItem(root)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   237
        else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   238
            item, item_cookie = self.Tree.GetFirstChild(root)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   239
            while item.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   240
                if self.Tree.GetItemText(item) == parts[0]:
1766
c1e5b9f19483 clean-up: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1758
diff changeset
   241
                    if self.Tree.ItemHasChildren(item) and \
c1e5b9f19483 clean-up: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1758
diff changeset
   242
                       not self.Tree.IsExpanded(item):
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   243
                        self.Tree.Expand(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   244
                        wx.CallAfter(self._SelectItem, item, parts[1:])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   245
                    else:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   246
                        self._SelectItem(item, parts[1:])
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   247
                    return
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   248
                item, item_cookie = self.Tree.GetNextChild(root, item_cookie)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   249
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   250
    def SetPath(self, path):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   251
        if path.startswith(self.Folder):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   252
            root = self.Tree.GetRootItem()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   253
            if root.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   254
                relative_path = path.replace(os.path.join(self.Folder, ""), "")
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   255
                self._SelectItem(root, splitpath(relative_path))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   256
951
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   257
    def GetPath(self, item=None):
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   258
        if item is None:
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   259
            item = self.Tree.GetSelection()
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   260
        if item.IsOk():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   261
            filepath = self.Tree.GetItemText(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   262
            parent = self.Tree.GetItemParent(item)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   263
            while parent.IsOk() and parent != self.Tree.GetRootItem():
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   264
                filepath = os.path.join(self.Tree.GetItemText(parent), filepath)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   265
                parent = self.Tree.GetItemParent(parent)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   266
            return os.path.join(self.Folder, filepath)
7f9a9c47a317 Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
diff changeset
   267
        return self.Folder