fileDialog.py (10469B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\control\fileDialog.py 2 import ctypes 3 import os 4 import blue 5 import localization 6 import utillib 7 from carbonui import uiconst 8 from carbonui.control.button import Button 9 from carbonui.control.combo import Combo 10 from carbonui.control.singlelineedits.singleLineEditText import SingleLineEditText 11 from carbonui.control.window import Window 12 from carbonui.primitives.container import Container 13 from carbonui.util.various_unsorted import SortListOfTuples 14 from eve.client.script.ui.control import eveIcon, eveScroll 15 from carbonui.button.group import ButtonGroup 16 from eve.client.script.ui.control.entries.generic import Generic 17 from eve.client.script.ui.control.entries.util import GetFromClass 18 from player_messaging.client.ui_message import prompt_player_fsd_dialog 19 SELECT_FILES = 0 20 SELECT_FOLDERS = 1 21 SELECT_FILESANDFOLDERS = 2 22 23 class FileDialogEntry(Generic): 24 __guid__ = 'listentry.FileDialog' 25 26 def Startup(self, *args): 27 Generic.Startup(self) 28 self.sr.icon = eveIcon.Icon(icon='res:/ui/Texture/WindowIcons/smallfolder.png', parent=self, pos=(6, 0, 16, 16), ignoreSize=1) 29 30 def Load(self, node): 31 Generic.Load(self, node) 32 if self.sr.node.isDir: 33 self.sr.icon.state = uiconst.UI_DISABLED 34 elif self.sr.icon: 35 self.sr.icon.state = uiconst.UI_HIDDEN 36 37 38 class FileDialog(Window): 39 __guid__ = 'form.FileDialog' 40 default_path = None 41 default_fileExtensions = None 42 default_multiSelect = False 43 default_selectionType = SELECT_FILES 44 default_fixedWidth = 430 45 default_fixedHeight = 400 46 _ok_button = None 47 48 def ApplyAttributes(self, attributes): 49 Window.ApplyAttributes(self, attributes) 50 path = attributes.path 51 fileExtensions = attributes.fileExtensions 52 multiSelect = attributes.multiSelect or self.default_multiSelect 53 selectionType = attributes.selectionType or self.default_selectionType 54 if not multiSelect: 55 caption = {SELECT_FILES: localization.GetByLabel('UI/Control/FileDialog/SelectFile'), 56 SELECT_FOLDERS: localization.GetByLabel('UI/Control/FileDialog/SelectFolder'), 57 SELECT_FILESANDFOLDERS: localization.GetByLabel('UI/Control/FileDialog/SelectFileOrfolder')}.get(selectionType) 58 else: 59 caption = {SELECT_FILES: localization.GetByLabel('UI/Control/FileDialog/SelectFiles'), 60 SELECT_FOLDERS: localization.GetByLabel('UI/Control/FileDialog/SelectFolders'), 61 SELECT_FILESANDFOLDERS: localization.GetByLabel('UI/Control/FileDialog/SelectFilesOrFolders')}.get(selectionType) 62 if fileExtensions: 63 caption = localization.GetByLabel('UI/Control/FileDialog/CaptionWithFileExtensions', caption=caption, fileExtensions=localization.formatters.FormatGenericList(fileExtensions)) 64 self.SetCaption(caption) 65 self.selectionType = selectionType 66 if fileExtensions: 67 self.fileExtensions = [ f.replace('.', '').lower() for f in fileExtensions ] 68 else: 69 self.fileExtensions = None 70 topCont = Container(name='topCont', parent=self.sr.main, align=uiconst.TOTOP, pos=(0, 0, 0, 40), padding=(0, 20, 0, 0)) 71 icon = eveIcon.Icon(icon='res:/ui/Texture/WindowIcons/smallfolder.png', parent=self.sr.main, pos=(3, 16, 28, 28), ignoreSize=1) 72 options = self.GetAvailableDrives() 73 self.sr.pathEdit = SingleLineEditText(name='currentLocationEdit', parent=topCont, pos=(38, 74 0, 75 290 if options else 350, 76 0), label=localization.GetByLabel('UI/Common/Location')) 77 self.sr.pathEdit.OnReturn = self.OnPathEnteredIntoEdit 78 if options: 79 self.sr.driveSelectCombo = Combo(parent=self.sr.main, label=localization.GetByLabel('UI/Control/FileDialog/Drive'), options=options, name='driveSelectCombo', select=None, callback=self.OnDriveSelected, pos=(340, 22, 0, 0), width=60, align=uiconst.TOPLEFT) 80 self.sr.standardBtns = ButtonGroup(parent=self.content, align=uiconst.TOBOTTOM) 81 self._ok_button = Button(parent=self.sr.standardBtns, label=localization.GetByLabel('UI/Common/Buttons/OK'), func=self.OnOK, args=()) 82 Button(parent=self.sr.standardBtns, label=localization.GetByLabel('UI/Common/Buttons/Cancel'), func=self.OnCancel, args=()) 83 self.SetOKButtonState(enabled=self.selectionType != SELECT_FILES) 84 scrollCont = Container(name='scrollCont', parent=self.sr.main, align=uiconst.TOALL, pos=(0, 0, 0, 0), padding=(0, 0, 6, 6)) 85 self.sr.scroll = eveScroll.Scroll(parent=scrollCont, name='fileFolderScroll', padLeft=5) 86 self.sr.scroll.multiSelect = multiSelect 87 self.sr.scroll.OnSelectionChange = self.VerifySelectedEnties 88 self.sr.scroll.Sort = lambda *args, **kw: None 89 if path is None or not os.path.isdir(path): 90 path = settings.user.ui.Get('fileDialogLastPath', None) 91 if path is None: 92 path = os.path.join(blue.sysinfo.GetUserDocumentsDirectory(), 'EVE') 93 if not os.path.isdir(path): 94 path = blue.sysinfo.GetUserDocumentsDirectory() 95 self.LoadDirToScroll(path) 96 97 def GetAvailableDrives(self): 98 try: 99 length = ctypes.windll.kernel32.GetLogicalDriveStringsA(0, '') 100 buffer = ctypes.create_string_buffer(length + 1) 101 length = ctypes.windll.kernel32.GetLogicalDriveStringsA(length, buffer) 102 drives = buffer.raw[:length].split('\x00') 103 except: 104 return [] 105 106 ret = [] 107 for i, d in enumerate(drives): 108 if d: 109 ret.append((d, i)) 110 111 return ret 112 113 def Confirm(self): 114 if len(self.GetSelected(multi=True)) > 0: 115 self.OnDblClick() 116 117 def OnDriveSelected(self, combo, drive, index): 118 if os.path.isdir(drive): 119 self.LoadDirToScroll(drive) 120 else: 121 prompt_player_fsd_dialog('FileDialogUnableToAccessDrive', {}, uiconst.OK) 122 123 def OnPathEnteredIntoEdit(self): 124 path = self.sr.pathEdit.GetValue() 125 if os.path.isdir(path): 126 self.LoadDirToScroll(path) 127 128 def OnOK(self): 129 selected = self.GetSelected() 130 self.result = self.GetReturnValue() 131 settings.user.ui.Set('fileDialogLastPath', self.lastEnteredPath) 132 self.SetModalResult(1) 133 134 def GetReturnValue(self): 135 selected = self.GetSelected(multi=True) 136 retval = utillib.KeyVal(files=[], folders=[]) 137 if len(selected) == 0 and self.selectionType != SELECT_FILES: 138 retval.folders.append(self.sr.pathEdit.GetValue()) 139 for s in selected: 140 if s.isDir: 141 if s.isFolderUp: 142 retval.folders.append(self.sr.pathEdit.GetValue()) 143 else: 144 retval.folders.append(s.filePath) 145 else: 146 retval.files.append(s.filePath) 147 148 return retval 149 150 def OnCancel(self): 151 self.Close() 152 153 def GetSelected(self, multi = False): 154 selected = self.sr.scroll.GetSelected() 155 if not selected: 156 return [] 157 elif not multi: 158 return selected[0] 159 else: 160 return selected 161 162 def OnDblClick(self, *args): 163 selected = self.GetSelected() 164 if selected.isDir: 165 self.LoadDirToScroll(selected.filePath) 166 self.sr.scroll.SelectNode(self.sr.scroll.GetNode(0)) 167 else: 168 self.OnOK() 169 170 def VerifySelectedEnties(self, *args): 171 selected = self.GetSelected(multi=True) 172 sm.ScatterEvent('OnFileDialogSelection', selected) 173 for s in selected: 174 if self._IsEntryInvalid(s): 175 self.SetOKButtonState(enabled=False) 176 return 177 178 self.SetOKButtonState(enabled=True) 179 180 def _IsEntryInvalid(self, entry): 181 return entry.isFolderUp and self.selectionType == SELECT_FILES or entry.isDir and self.selectionType == SELECT_FILES or not entry.isDir and self.selectionType == SELECT_FOLDERS 182 183 def SetOKButtonState(self, enabled): 184 if self._ok_button: 185 if enabled: 186 self._ok_button.Enable() 187 else: 188 self._ok_button.Disable() 189 190 def EntryShouldBeDisplayed(self, filePath, isDir): 191 if self.selectionType == SELECT_FOLDERS and not isDir: 192 return False 193 extension = os.path.splitext(filePath)[1].replace('.', '').lower() 194 if not isDir and self.fileExtensions and extension not in self.fileExtensions: 195 return False 196 return True 197 198 def LoadDirToScroll(self, path): 199 scrolllist = [] 200 path = os.path.abspath(path) 201 try: 202 fileList = os.listdir(path) 203 except: 204 prompt_player_fsd_dialog('FileDialogUnableToAccessFolder') 205 return 206 207 self.lastEnteredPath = path 208 fileList.append('..') 209 for f in fileList: 210 f = unicode(f) 211 data = utillib.KeyVal() 212 data.label = u'<t>%s' % f 213 data.hint = f 214 data.filePath = os.path.join(path, f) 215 data.isDir = os.path.isdir(data.filePath) 216 data.OnDblClick = self.OnDblClick 217 data.charIndex = f 218 if not self.EntryShouldBeDisplayed(f, data.isDir): 219 continue 220 sortBy = f.lower() 221 if data.isDir: 222 if f == '..': 223 sortBy = ' ' + sortBy 224 data.isFolderUp = True 225 else: 226 sortBy = ' ' + sortBy 227 scrolllist.append((sortBy, GetFromClass(FileDialogEntry, data=data))) 228 229 scrolllist = SortListOfTuples(scrolllist) 230 self.sr.scroll.Load(contentList=scrolllist, headers=['', localization.GetByLabel('UI/Common/Name')]) 231 self.sr.pathEdit.SetValue(path) 232 233 @classmethod 234 def _Select(cls, *args, **kwds): 235 wnd = cls.Open(*args, **kwds) 236 if wnd.ShowModal() == 1: 237 return wnd.result 238 else: 239 return None 240 241 @classmethod 242 def SelectFiles(cls, **kwds): 243 return cls._Select(selectionType=SELECT_FILES, **kwds) 244 245 @classmethod 246 def SelectFolders(cls, **kwds): 247 return cls._Select(selectionType=SELECT_FOLDERS, **kwds) 248 249 @classmethod 250 def SelectFilesAndFolders(cls, **kwds): 251 return cls._Select(selectionType=SELECT_FILESANDFOLDERS, **kwds)