author | Paul Beltyukov <beltyukov.p.a@gmail.com> |
Fri, 09 Sep 2016 11:47:00 +0500 | |
changeset 1527 | 642bae8e8607 |
parent 951 | 7f9a9c47a317 |
child 1571 | 486f94a8032c |
permissions | -rw-r--r-- |
782 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import os |
|
26 |
import shutil |
|
27 |
||
28 |
import wx |
|
951
7f9a9c47a317
Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
815
diff
changeset
|
29 |
import wx.lib.buttons |
782 | 30 |
|
814 | 31 |
from EditorPanel import EditorPanel |
32 |
from util.BitmapLibrary import GetBitmap |
|
951
7f9a9c47a317
Extracted FolderTree control from FileManagementPanel to be able to use it anywhere else
Laurent Bessard
parents:
815
diff
changeset
|
33 |
from controls import FolderTree |
783 | 34 |
|
782 | 35 |
FILTER = _("All files (*.*)|*.*|CSV files (*.csv)|*.csv") |
36 |
||
37 |
class FileManagementPanel(EditorPanel): |
|
38 |
||
39 |
def _init_Editor(self, parent): |
|
40 |
self.Editor = wx.Panel(parent) |
|
41 |
||
42 |
main_sizer = wx.BoxSizer(wx.HORIZONTAL) |
|
43 |
||
44 |
left_sizer = wx.BoxSizer(wx.VERTICAL) |
|
45 |
main_sizer.AddSizer(left_sizer, 1, border=5, flag=wx.GROW|wx.ALL) |
|
46 |
||
815
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
47 |
managed_dir_label = wx.StaticText(self.Editor, label=_(self.TagName) + ":") |
782 | 48 |
left_sizer.AddWindow(managed_dir_label, border=5, flag=wx.GROW|wx.BOTTOM) |
49 |
||
50 |
self.ManagedDir = FolderTree(self.Editor, self.Folder, FILTER) |
|
51 |
left_sizer.AddWindow(self.ManagedDir, 1, flag=wx.GROW) |
|
52 |
||
53 |
managed_treectrl = self.ManagedDir.GetTreeCtrl() |
|
54 |
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemChanged, managed_treectrl) |
|
55 |
if self.EnableDragNDrop: |
|
56 |
self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnTreeBeginDrag, managed_treectrl) |
|
57 |
||
58 |
button_sizer = wx.BoxSizer(wx.VERTICAL) |
|
59 |
main_sizer.AddSizer(button_sizer, border=5, |
|
60 |
flag=wx.ALL|wx.ALIGN_CENTER_VERTICAL) |
|
61 |
||
62 |
for idx, (name, bitmap, help) in enumerate([ |
|
63 |
("DeleteButton", "remove_element", _("Remove file from left folder")), |
|
64 |
("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
|
65 |
("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
|
66 |
("EditButton", "edit", _("Edit file"))]): |
782 | 67 |
button = wx.lib.buttons.GenBitmapButton(self.Editor, |
68 |
bitmap=GetBitmap(bitmap), |
|
69 |
size=wx.Size(28, 28), style=wx.NO_BORDER) |
|
70 |
button.SetToolTipString(help) |
|
71 |
setattr(self, name, button) |
|
72 |
if idx > 0: |
|
73 |
flag = wx.TOP |
|
74 |
else: |
|
75 |
flag = 0 |
|
76 |
self.Bind(wx.EVT_BUTTON, getattr(self, "On" + name), button) |
|
77 |
button_sizer.AddWindow(button, border=20, flag=flag) |
|
78 |
||
79 |
right_sizer = wx.BoxSizer(wx.VERTICAL) |
|
80 |
main_sizer.AddSizer(right_sizer, 1, border=5, flag=wx.GROW|wx.ALL) |
|
81 |
||
82 |
if wx.Platform == '__WXMSW__': |
|
83 |
system_dir_label = wx.StaticText(self.Editor, label=_("My Computer:")) |
|
84 |
else: |
|
85 |
system_dir_label = wx.StaticText(self.Editor, label=_("Home Directory:")) |
|
86 |
right_sizer.AddWindow(system_dir_label, border=5, flag=wx.GROW|wx.BOTTOM) |
|
87 |
||
88 |
self.SystemDir = FolderTree(self.Editor, self.HomeDirectory, FILTER, False) |
|
89 |
right_sizer.AddWindow(self.SystemDir, 1, flag=wx.GROW) |
|
90 |
||
91 |
system_treectrl = self.SystemDir.GetTreeCtrl() |
|
92 |
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeItemChanged, system_treectrl) |
|
93 |
||
94 |
self.Editor.SetSizer(main_sizer) |
|
95 |
||
96 |
def __init__(self, parent, controler, name, folder, enable_dragndrop=False): |
|
97 |
self.Folder = os.path.realpath(folder) |
|
98 |
self.EnableDragNDrop = enable_dragndrop |
|
783 | 99 |
|
100 |
if wx.Platform == '__WXMSW__': |
|
101 |
self.HomeDirectory = "/" |
|
102 |
else: |
|
103 |
self.HomeDirectory = os.path.expanduser("~") |
|
782 | 104 |
|
105 |
EditorPanel.__init__(self, parent, name, None, None) |
|
106 |
||
107 |
self.Controler = controler |
|
108 |
||
784
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
109 |
self.EditableFileExtensions = [] |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
110 |
self.EditButton.Hide() |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
111 |
|
782 | 112 |
self.SetIcon(GetBitmap("FOLDER")) |
113 |
||
114 |
def __del__(self): |
|
115 |
self.Controler.OnCloseEditor(self) |
|
116 |
||
117 |
def GetTitle(self): |
|
815
e4f24593a758
Adding support for extending internationalization to extensions
laurent
parents:
814
diff
changeset
|
118 |
return _(self.TagName) |
782 | 119 |
|
784
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
120 |
def SetEditableFileExtensions(self, extensions): |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
121 |
self.EditableFileExtensions = extensions |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
122 |
if len(self.EditableFileExtensions) > 0: |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
123 |
self.EditButton.Show() |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
124 |
|
782 | 125 |
def RefreshView(self): |
126 |
self.ManagedDir.RefreshTree() |
|
127 |
self.SystemDir.RefreshTree() |
|
128 |
self.RefreshButtonsState() |
|
129 |
||
130 |
def RefreshButtonsState(self): |
|
131 |
managed_filepath = self.ManagedDir.GetPath() |
|
132 |
system_filepath = self.SystemDir.GetPath() |
|
133 |
||
134 |
self.DeleteButton.Enable(os.path.isfile(managed_filepath)) |
|
135 |
self.LeftCopyButton.Enable(os.path.isfile(system_filepath)) |
|
136 |
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
|
137 |
if len(self.EditableFileExtensions) > 0: |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
138 |
self.EditButton.Enable( |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
139 |
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
|
140 |
os.path.splitext(managed_filepath)[1] in self.EditableFileExtensions) |
782 | 141 |
|
142 |
def OnTreeItemChanged(self, event): |
|
143 |
self.RefreshButtonsState() |
|
144 |
event.Skip() |
|
145 |
||
146 |
def OnDeleteButton(self, event): |
|
147 |
filepath = self.ManagedDir.GetPath() |
|
148 |
if os.path.isfile(filepath): |
|
149 |
folder, filename = os.path.split(filepath) |
|
150 |
||
151 |
dialog = wx.MessageDialog(self, |
|
152 |
_("Do you really want to delete the file '%s'?") % filename, |
|
153 |
_("Delete File"), wx.YES_NO|wx.ICON_QUESTION) |
|
154 |
remove = dialog.ShowModal() == wx.ID_YES |
|
155 |
dialog.Destroy() |
|
156 |
||
157 |
if remove: |
|
158 |
os.remove(filepath) |
|
159 |
self.ManagedDir.RefreshTree() |
|
160 |
event.Skip() |
|
161 |
||
784
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
162 |
def OnEditButton(self, event): |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
163 |
filepath = self.ManagedDir.GetPath() |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
164 |
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
|
165 |
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
|
166 |
self.Controler._OpenView(filepath + "::") |
784
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
167 |
event.Skip() |
a1d970365e41
Adding support for beremiz extensions to define custom file editors for project files
laurent
parents:
783
diff
changeset
|
168 |
|
782 | 169 |
def CopyFile(self, src, dst): |
170 |
if os.path.isfile(src): |
|
171 |
src_folder, src_filename = os.path.split(src) |
|
172 |
if os.path.isfile(dst): |
|
173 |
dst_folder, dst_filename = os.path.split(dst) |
|
174 |
else: |
|
175 |
dst_folder = dst |
|
176 |
||
177 |
dst_filepath = os.path.join(dst_folder, src_filename) |
|
178 |
if os.path.isfile(dst_filepath): |
|
179 |
dialog = wx.MessageDialog(self, |
|
180 |
_("The file '%s' already exist.\nDo you want to replace it?") % src_filename, |
|
181 |
_("Replace File"), wx.YES_NO|wx.ICON_QUESTION) |
|
182 |
copy = dialog.ShowModal() == wx.ID_YES |
|
183 |
dialog.Destroy() |
|
184 |
else: |
|
185 |
copy = True |
|
186 |
||
187 |
if copy: |
|
188 |
shutil.copyfile(src, dst_filepath) |
|
189 |
return dst_filepath |
|
190 |
return None |
|
191 |
||
192 |
def OnLeftCopyButton(self, event): |
|
193 |
filepath = self.CopyFile(self.SystemDir.GetPath(), self.ManagedDir.GetPath()) |
|
194 |
if filepath is not None: |
|
195 |
self.ManagedDir.RefreshTree() |
|
196 |
self.ManagedDir.SetPath(filepath) |
|
197 |
event.Skip() |
|
198 |
||
199 |
def OnRightCopyButton(self, event): |
|
200 |
filepath = self.CopyFile(self.ManagedDir.GetPath(), self.SystemDir.GetPath()) |
|
201 |
if filepath is not None: |
|
202 |
self.SystemDir.RefreshTree() |
|
203 |
self.SystemDir.SetPath(filepath) |
|
204 |
event.Skip() |
|
205 |
||
206 |
def OnTreeBeginDrag(self, event): |
|
207 |
filepath = self.ManagedDir.GetPath() |
|
208 |
if os.path.isfile(filepath): |
|
209 |
relative_filepath = filepath.replace(os.path.join(self.Folder, ""), "") |
|
210 |
data = wx.TextDataObject(str(("'%s'" % relative_filepath, "Constant"))) |
|
211 |
dragSource = wx.DropSource(self) |
|
212 |
dragSource.SetData(data) |
|
213 |
dragSource.DoDragDrop() |
|
214 |