]>
Commit | Line | Data |
---|---|---|
1f780e48 RD |
1 | #---------------------------------------------------------------------------- |
2 | # Name: UICommon.py | |
3 | # Purpose: Shared UI stuff | |
4 | # | |
5 | # Author: Matt Fryer | |
6 | # | |
7 | # Created: 3/10/05 | |
8 | # CVS-ID: $Id$ | |
9 | # Copyright: (c) 2005 ActiveGrid, Inc. | |
10 | # License: wxWindows License | |
11 | #---------------------------------------------------------------------------- | |
12 | ||
13 | import os | |
14 | import os.path | |
15 | import wx | |
16 | import ProjectEditor | |
17 | _ = wx.GetTranslation | |
18 | ||
19 | def CreateDirectoryControl( parent, fileLabel, dirLabel, fileExtension, startingName="", startingDirectory=""): | |
20 | ||
21 | nameControl = wx.TextCtrl(parent, -1, startingName, size=(-1,-1)) | |
22 | nameLabelText = wx.StaticText(parent, -1, fileLabel) | |
23 | dirLabelText = wx.StaticText(parent, -1, dirLabel) | |
24 | dirControl = wx.TextCtrl(parent, -1, startingDirectory, size=(-1,-1)) | |
25 | dirControl.SetToolTipString(startingDirectory) | |
26 | button = wx.Button(parent, -1, _("Browse..."), size=(60,-1)) | |
27 | ||
28 | def OnFindDirClick(event): | |
29 | name = "" | |
30 | nameCtrlValue = nameControl.GetValue() | |
31 | if nameCtrlValue: | |
32 | root, ext = os.path.splitext( nameCtrlValue ) | |
33 | if ext == '.' + fileExtension: | |
34 | name = nameCtrlValue | |
35 | else: | |
36 | name = _("%s.%s") % (nameCtrlValue, fileExtension) | |
37 | path = wx.FileSelector(_("Choose a filename and directory"), | |
38 | "", | |
39 | "%s" % name, | |
40 | wildcard=_("*.%s") % fileExtension , | |
41 | flags=wx.SAVE, | |
42 | parent=parent) | |
43 | ||
44 | if path: | |
45 | dir, filename = os.path.split(path) | |
46 | dirControl.SetValue(dir) | |
47 | dirControl.SetToolTipString(dir) | |
48 | nameControl.SetValue(filename) | |
49 | ||
50 | parent.Bind(wx.EVT_BUTTON, OnFindDirClick, button) | |
51 | ||
52 | def Validate(allowOverwriteOnPrompt=False): | |
53 | if nameControl.GetValue() == "": | |
54 | wx.MessageBox(_("Please provide a filename."), _("Provide a Filename")) | |
55 | return False | |
56 | if nameControl.GetValue().find(' ') != -1: | |
57 | wx.MessageBox(_("Please provide a filename that does not contains spaces."), _("Spaces in Filename")) | |
58 | return False | |
26ee3a06 RD |
59 | if not os.path.exists(dirControl.GetValue()): |
60 | wx.MessageBox(_("That directory does not exist. Please choose an existing directory."), _("Provide a Valid Directory")) | |
61 | return False | |
62 | ||
1f780e48 RD |
63 | filePath = os.path.join(dirControl.GetValue(), MakeNameEndInExtension(nameControl.GetValue(), "." + fileExtension)) |
64 | if os.path.exists(filePath): | |
65 | if allowOverwriteOnPrompt: | |
66 | res = wx.MessageBox(_("That file already exists. Would you like to overwrite it."), "File Exists", style=wx.YES_NO|wx.NO_DEFAULT) | |
67 | return (res == wx.YES) | |
68 | else: | |
69 | wx.MessageBox(_("That file already exists. Please choose a different name."), "File Exists") | |
70 | return False | |
71 | return True | |
72 | HALF_SPACE = 5 | |
73 | flexGridSizer = wx.FlexGridSizer(cols = 3, vgap = HALF_SPACE, hgap = HALF_SPACE) | |
74 | flexGridSizer.AddGrowableCol(1,1) | |
75 | flexGridSizer.Add(nameLabelText, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT|wx.TOP|wx.RIGHT, HALF_SPACE) | |
76 | flexGridSizer.Add(nameControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND) | |
77 | flexGridSizer.Add(button, flag=wx.ALIGN_RIGHT|wx.LEFT, border=HALF_SPACE) | |
78 | ||
79 | flexGridSizer.Add(dirLabelText, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT|wx.TOP|wx.RIGHT, border=HALF_SPACE) | |
80 | flexGridSizer.Add(dirControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border=HALF_SPACE) | |
81 | flexGridSizer.Add(wx.StaticText(parent, -1, ""), 0) | |
82 | return nameControl, dirControl, flexGridSizer, Validate | |
83 | ||
84 | def AddFilesToCurrentProject(paths, save=False): | |
85 | projectService = wx.GetApp().GetService(ProjectEditor.ProjectService) | |
86 | if projectService: | |
87 | projectDocument = projectService.GetCurrentProject() | |
88 | if projectDocument: | |
89 | files = projectDocument.GetFiles() | |
90 | for path in paths: | |
91 | if path in files: | |
92 | paths.remove(path) | |
93 | if paths: | |
94 | projectDocument.GetCommandProcessor().Submit(ProjectEditor.ProjectAddFilesCommand(projectDocument, paths)) | |
b792147d | 95 | projectDocument.GetFirstView().DoSelectFiles([paths[0]]) |
1f780e48 RD |
96 | if save: |
97 | projectDocument.OnSaveDocument(projectDocument.GetFilename()) | |
98 | ||
99 | def MakeNameEndInExtension(name, extension): | |
100 | if not name: | |
101 | return name | |
102 | root, ext = os.path.splitext(name) | |
103 | if ext == extension: | |
104 | return name | |
105 | else: | |
106 | return name + extension | |
107 | ||
108 | # Lame | |
109 | def PluralName(name): | |
110 | if not name: | |
111 | return name | |
112 | if name.endswith('us'): | |
113 | return name[0:-2] + 'ii' | |
114 | elif name.endswith('s'): | |
115 | return name | |
116 | elif name.endswith('y'): | |
117 | return name[0:-1] + 'ies' | |
118 | else: | |
119 | return name + 's' | |
bbf7159c | 120 |