Coverage for src/robotide/ui/fileexplorerplugin.py: 70%

109 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:40 +0100

1# Copyright 2020- Robot Framework Foundation 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15import builtins 1ab

16import wx 1ab

17from wx.lib.agw import customtreectrl 1ab

18from wx.lib.agw.aui import GetManager 1ab

19 

20from ..controller.project import Project 1ab

21from ..pluginapi import Plugin 1ab

22from ..pluginapi.plugin import ActionInfo 1ab

23 

24_ = wx.GetTranslation # To keep linter/code analyser happy 1ab

25builtins.__dict__['_'] = wx.GetTranslation 1ab

26 

27 

28class FileExplorerPlugin(Plugin): 1ab

29 __doc__ = _("""Provides a tree view for Files and Folders. Opens selected item with mouse right-click.""") 1ab

30 

31 datafile = property(lambda self: self.get_selected_datafile()) 1ab

32 defaults = {"opened": True, 1ab

33 "docked": True, 

34 "own colors": False 

35 } 

36 

37 def __init__(self, application, controller=None): 1ab

38 Plugin.__init__(self, application, default_settings=self.defaults) 

39 self._app = application 

40 self.settings = self._app.settings.config_obj['Plugins']['File Explorer'] 

41 self._parent = wx.App.Get().GetTopWindow() 

42 self._filemgr = self.filemgr 

43 self._filemgr.SetThemeEnabled(True) 

44 self._mgr = GetManager(self._filemgr) 

45 self._controller = controller 

46 self._pane = None 

47 self._filetreectrl = None 

48 self.opened = self.settings['opened'] 

49 self.font = self._filemgr.GetFont() 

50 

51 def register_frame(self, parent=None): 1ab

52 if parent: 

53 self._parent = parent 

54 

55 if self._mgr.GetPane("file_manager") in self._mgr._panes: 

56 register = self._mgr.InsertPane 

57 else: 

58 register = self._mgr.AddPane 

59 

60 register(self._filemgr, wx.lib.agw.aui.AuiPaneInfo().Name("file_manager"). 

61 Caption(_("Files")).LeftDockable(True).CloseButton(False)) 

62 

63 self._mgr.Update() 

64 

65 def enable(self): 1ab

66 self.register_action(ActionInfo(_('View'), _('View File Explorer'), self.toggle_view, 

67 shortcut='F11', 

68 doc=_('Show File Explorer panel'), 

69 position=1)) 

70 self.show_file_explorer() 

71 if not self.opened: 71 ↛ exitline 71 didn't return from function 'enable' because the condition on line 71 was always true

72 self.close_tree() 

73 

74 def close_tree(self): 1ab

75 # self.save_setting('opened', False) 

76 self.opened = False 

77 self._mgr.DetachPane(self._filemgr) 

78 self._filemgr.Hide() 

79 self._mgr.Update() 

80 

81 def is_focused(self): 1ab

82 return self._filemgr.HasFocus() 

83 

84 def toggle_view(self, event): 1ab

85 __ = event 

86 self.save_setting('opened', not self.opened) 

87 if not self.opened: 

88 self.opened = True 

89 self.show_file_explorer() 

90 else: 

91 self.close_tree() 

92 

93 def show_file_explorer(self): 1ab

94 if not self._parent: 94 ↛ 95line 94 didn't jump to line 95 because the condition on line 94 was never true

95 self._parent = wx.App.Get().GetWindow() # self.frame 

96 if not self._filemgr: # This is not needed because file explorer is always created 96 ↛ 97line 96 didn't jump to line 97 because the condition on line 96 was never true

97 self._filemgr = FileExplorer(self._parent, self._controller) 

98 

99 self._pane = self._mgr.GetPane(self._filemgr) 

100 global_settings = self._app.settings.config_obj['General'] 

101 apply_global = global_settings['apply to panels'] 

102 use_own = self.settings['own colors'] 

103 if apply_global or not use_own: 103 ↛ 107line 103 didn't jump to line 107 because the condition on line 103 was always true

104 html_background = self.settings.get('background help', (240, 242, 80)) 

105 html_foreground = self.settings.get('foreground text', (7, 0, 70)) 

106 else: 

107 html_background = self.settings.get('background', (240, 242, 80)) 

108 html_foreground = self.settings.get('foreground', (7, 0, 70)) 

109 html_font_face = self.settings.get('font face', '') 

110 html_font_size = self.settings.get('font size', 11) 

111 self._filetreectrl = self._filemgr.GetTreeCtrl() 

112 self._filemgr.Show(True) 

113 self._filemgr.SetMinSize(wx.Size(200, 225)) 

114 self._mgr.DetachPane(self._filemgr) 

115 self._mgr.AddPane(self.filemgr, 

116 wx.lib.agw.aui.AuiPaneInfo().Name("file_manager"). 

117 Caption(_("Files")).LeftDockable(True). 

118 CloseButton(False)) 

119 self._filemgr.SetBackgroundStyle(wx.BG_STYLE_SYSTEM) 

120 self._filemgr.SetBackgroundColour(html_background) 

121 self._filemgr.SetForegroundColour(html_foreground) 

122 self.font = self._filemgr.GetFont() 

123 self.font.SetFaceName(html_font_face) 

124 self.font.SetPointSize(html_font_size) 

125 self._filemgr.SetFont(self.font) 

126 self._filemgr.Refresh() 

127 self._filetreectrl.SetBackgroundColour(html_background) 

128 self._filetreectrl.SetForegroundColour(html_foreground) 

129 self._filetreectrl.SetFont(self.font) 

130 self._filetreectrl.Refresh() 

131 self._filemgr.Raise() 

132 self._mgr.Update() 

133 self.update_tree() 

134 

135 def update_tree(self): 1ab

136 if not self._filemgr: 136 ↛ 137line 136 didn't jump to line 137 because the condition on line 136 was never true

137 return 

138 self._filemgr.update_tree() 

139 

140 

141class FileExplorer(wx.GenericDirCtrl): 1ab

142 

143 def __init__(self, parent, controller=None): 1ab

144 wx.GenericDirCtrl.__init__(self, parent, id=-1, size=(200, 225), style=wx.DIRCTRL_3D_INTERNAL) 

145 self._controller = controller 

146 self.SetThemeEnabled(True) 

147 self.Refresh() 

148 

149 def update_tree(self): 1ab

150 if isinstance(self._controller, Project): 150 ↛ exitline 150 didn't return from function 'update_tree' because the condition on line 150 was always true

151 if self._controller.data and len(self._controller.data.directory) > 1: 151 ↛ 152line 151 didn't jump to line 152 because the condition on line 151 was never true

152 self.SelectPath(self._controller.data.source) 

153 try: 

154 self.ExpandPath(self._controller.data.source) 

155 except Exception: 

156 pass 

157 self.Refresh() 

158 self.Update()