src/eric7/EricWidgets/EricPathPickerDialog.py

branch
eric7
changeset 9238
a7cbf3d61498
parent 9221
bf71ee032bb4
child 9322
3f0fe9a79aa1
equal deleted inserted replaced
9237:03c714bd4ebf 9238:a7cbf3d61498
73 @param mode picker mode 73 @param mode picker mode
74 @type EricPathPickerModes 74 @type EricPathPickerModes
75 """ 75 """
76 self.__pathPicker.setMode(mode) 76 self.__pathPicker.setMode(mode)
77 77
78 def setPickerPath(self, path):
79 """
80 Public method to set the path of the path picker.
81
82 @param path path to be set
83 @type str
84 """
85 self.__pathPicker.setPath(path)
86
87 def setDefaultDirectory(self, directory):
88 """
89 Public method to set the default directory of the path picker.
90
91 @param directory default directory
92 @type str
93 """
94 self.__pathPicker.setDefaultDirectory(directory)
95
96 def setPickerFilters(self, filters): 78 def setPickerFilters(self, filters):
97 """ 79 """
98 Public method to set the filters of the path picker. 80 Public method to set the filters of the path picker.
99 81
100 Note: Multiple filters must be separated by ';;'. 82 Note: Multiple filters must be separated by ';;'.
102 @param filters string containing the file filters 84 @param filters string containing the file filters
103 @type str 85 @type str
104 """ 86 """
105 self.__pathPicker.setFilters(filters) 87 self.__pathPicker.setFilters(filters)
106 88
107 def getPath(self): 89 def setPickerPath(self, fpath):
108 """ 90 """
109 Public method to get the current path. 91 Public method to set the path of the path picker.
92
93 @param fpath path to be set
94 @type str or pathlib.Path
95 """
96 self.__pathPicker.setText(str(fpath))
97
98 def setDefaultDirectory(self, directory):
99 """
100 Public method to set the default directory of the path picker.
101
102 @param directory default directory
103 @type str or pathlib.Path
104 """
105 self.__pathPicker.setDefaultDirectory(str(directory))
106
107 def getText(self):
108 """
109 Public method to get the current path as text.
110 110
111 @return current path 111 @return current path
112 @rtype str 112 @rtype str
113 """ 113 """
114 return self.__pathPicker.text()
115
116 def getPath(self):
117 """
118 Public method to get the current path as a pathlib.Path object.
119
120 @return current path
121 @rtype pathlib.Path
122 """
114 return self.__pathPicker.path() 123 return self.__pathPicker.path()
115 124
116 125
117 def getPath( 126 def getStrPath(
118 parent, 127 parent,
119 title, 128 title,
120 label, 129 label,
121 mode=EricPathPickerModes.OPEN_FILE_MODE, 130 mode=EricPathPickerModes.OPEN_FILE_MODE,
122 path="", 131 strPath=None,
123 defaultDirectory="", 132 defaultDirectory=None,
124 filters=None, 133 filters=None,
125 ): 134 ):
126 """ 135 """
127 Function to get a file or directory path from the user. 136 Function to get a file or directory path from the user.
128 137
130 @type QWidget 139 @type QWidget
131 @param title title of the dialog 140 @param title title of the dialog
132 @type str 141 @type str
133 @param label text to be shown above the path picker 142 @param label text to be shown above the path picker
134 @type str 143 @type str
135 @param mode mode of the path picker 144 @param mode mode of the path picker (defaults to EricPathPickerModes.OPEN_FILE_MODE)
136 @type EricPathPickerModes 145 @type EricPathPickerModes (optional)
137 @param path initial path to be shown 146 @param strPath initial path to be shown (defaults to None)
138 @type str 147 @type str (optional)
139 @param defaultDirectory default directory of the path picker selection 148 @param defaultDirectory default directory of the path picker selection
140 dialog 149 dialog (defaults to None)
141 @type str 150 @type str (optional)
142 @param filters list of file filters 151 @param filters list of file filters (defaults to None)
143 @type list of str 152 @type list of str (optional)
144 @return tuple containing the entered path and a flag indicating that the 153 @return tuple containing the entered path and a flag indicating that the
145 user pressed the OK button 154 user pressed the OK button
146 @rtype tuple of (str, bool) 155 @rtype tuple of (str, bool)
147 """ 156 """
148 # step 1: setup of the dialog 157 # step 1: setup of the dialog
150 if title: 159 if title:
151 dlg.setTitle(title) 160 dlg.setTitle(title)
152 if label: 161 if label:
153 dlg.setLabelText(label) 162 dlg.setLabelText(label)
154 dlg.setPickerMode(mode) 163 dlg.setPickerMode(mode)
155 if path: 164 if strPath:
156 dlg.setPickerPath(path) 165 dlg.setPickerPath(strPath)
157 if defaultDirectory: 166 if defaultDirectory:
158 dlg.setDefaultDirectory(defaultDirectory) 167 dlg.setDefaultDirectory(defaultDirectory)
159 if filters is not None and len(filters) > 0: 168 if filters is not None and len(filters) > 0:
160 dlg.setPickerFilters(";;".join(filters)) 169 dlg.setPickerFilters(";;".join(filters))
161 170
162 # step 2: show the dialog and get the result 171 # step 2: show the dialog and get the result
163 if dlg.exec() == QDialog.DialogCode.Accepted: 172 if dlg.exec() == QDialog.DialogCode.Accepted:
164 ok = True 173 ok = True
165 path = dlg.getPath().strip() 174 fpath = dlg.getText().strip()
166 else: 175 else:
167 ok = False 176 ok = False
168 path = "" 177 fpath = ""
169 178
170 # step 3: return the result 179 # step 3: return the result
171 return path, ok 180 return fpath, ok
181
182
183 def getPath(
184 parent,
185 title,
186 label,
187 mode=EricPathPickerModes.OPEN_FILE_MODE,
188 pathlibPath=None,
189 defaultDirectory=None,
190 filters=None,
191 ):
192 """
193 Function to get a file or directory path from the user.
194
195 @param parent reference to the parent widget
196 @type QWidget
197 @param title title of the dialog
198 @type str
199 @param label text to be shown above the path picker
200 @type str
201 @param mode mode of the path picker (defaults to EricPathPickerModes.OPEN_FILE_MODE)
202 @type EricPathPickerModes (optional)
203 @param pathlibPath initial path to be shown (defaults to None)
204 @type pathlib.Path (optional)
205 @param defaultDirectory default directory of the path picker selection
206 dialog (defaults to None)
207 @type pathlib.Path (optional)
208 @param filters list of file filters (defaults to None)
209 @type list of str (optional)
210 @return tuple containing the entered path and a flag indicating that the
211 user pressed the OK button
212 @rtype tuple of (pathlib.Path, bool)
213 """
214 # step 1: setup of the dialog
215 dlg = EricPathPickerDialog(parent)
216 if title:
217 dlg.setTitle(title)
218 if label:
219 dlg.setLabelText(label)
220 dlg.setPickerMode(mode)
221 if pathlibPath:
222 dlg.setPickerPath(pathlibPath)
223 if defaultDirectory:
224 dlg.setDefaultDirectory(defaultDirectory)
225 if filters is not None and len(filters) > 0:
226 dlg.setPickerFilters(";;".join(filters))
227
228 # step 2: show the dialog and get the result
229 if dlg.exec() == QDialog.DialogCode.Accepted:
230 ok = True
231 fpath = dlg.getText().strip()
232 else:
233 ok = False
234 fpath = ""
235
236 # step 3: return the result
237 return fpath, ok

eric ide

mercurial