src/eric7/EricWidgets/EricPathPicker.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9238
a7cbf3d61498
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
11 import os 11 import os
12 import pathlib 12 import pathlib
13 13
14 from PyQt6.QtCore import pyqtSignal, Qt, QCoreApplication, QDir 14 from PyQt6.QtCore import pyqtSignal, Qt, QCoreApplication, QDir
15 from PyQt6.QtWidgets import ( 15 from PyQt6.QtWidgets import (
16 QWidget, QHBoxLayout, QToolButton, QSizePolicy, QLineEdit, QComboBox 16 QWidget,
17 QHBoxLayout,
18 QToolButton,
19 QSizePolicy,
20 QLineEdit,
21 QComboBox,
17 ) 22 )
18 23
19 from . import EricFileDialog 24 from . import EricFileDialog
20 from .EricCompleters import EricFileCompleter, EricDirCompleter 25 from .EricCompleters import EricFileCompleter, EricDirCompleter
21 26
24 29
25 class EricPathPickerModes(enum.Enum): 30 class EricPathPickerModes(enum.Enum):
26 """ 31 """
27 Class implementing the path picker modes. 32 Class implementing the path picker modes.
28 """ 33 """
34
29 OPEN_FILE_MODE = 0 35 OPEN_FILE_MODE = 0
30 OPEN_FILES_MODE = 1 36 OPEN_FILES_MODE = 1
31 SAVE_FILE_MODE = 2 37 SAVE_FILE_MODE = 2
32 SAVE_FILE_ENSURE_EXTENSION_MODE = 3 38 SAVE_FILE_ENSURE_EXTENSION_MODE = 3
33 SAVE_FILE_OVERWRITE_MODE = 4 39 SAVE_FILE_OVERWRITE_MODE = 4
40 46
41 class EricPathPickerBase(QWidget): 47 class EricPathPickerBase(QWidget):
42 """ 48 """
43 Class implementing the base of a path picker widget consisting of a 49 Class implementing the base of a path picker widget consisting of a
44 line edit or combo box and a tool button to open a file dialog. 50 line edit or combo box and a tool button to open a file dialog.
45 51
46 @signal textChanged(path) emitted when the entered path has changed 52 @signal textChanged(path) emitted when the entered path has changed
47 (line edit based widget) 53 (line edit based widget)
48 @signal editTextChanged(path) emitted when the entered path has changed 54 @signal editTextChanged(path) emitted when the entered path has changed
49 (combo box based widget) 55 (combo box based widget)
50 @signal pathSelected(path) emitted after a path has been selected via the 56 @signal pathSelected(path) emitted after a path has been selected via the
51 file dialog 57 file dialog
52 @signal aboutToShowPathPickerDialog emitted before the file dialog is shown 58 @signal aboutToShowPathPickerDialog emitted before the file dialog is shown
53 @signal pickerButtonClicked emitted when the picker button was pressed and 59 @signal pickerButtonClicked emitted when the picker button was pressed and
54 the widget mode is custom 60 the widget mode is custom
55 """ 61 """
62
56 DefaultMode = EricPathPickerModes.NO_MODE 63 DefaultMode = EricPathPickerModes.NO_MODE
57 64
58 textChanged = pyqtSignal(str) 65 textChanged = pyqtSignal(str)
59 editTextChanged = pyqtSignal(str) 66 editTextChanged = pyqtSignal(str)
60 pathSelected = pyqtSignal(str) 67 pathSelected = pyqtSignal(str)
61 aboutToShowPathPickerDialog = pyqtSignal() 68 aboutToShowPathPickerDialog = pyqtSignal()
62 pickerButtonClicked = pyqtSignal() 69 pickerButtonClicked = pyqtSignal()
63 70
64 def __init__(self, parent=None, useLineEdit=True): 71 def __init__(self, parent=None, useLineEdit=True):
65 """ 72 """
66 Constructor 73 Constructor
67 74
68 @param parent reference to the parent widget 75 @param parent reference to the parent widget
69 @type QWidget 76 @type QWidget
70 @param useLineEdit flag indicating the use of a line edit 77 @param useLineEdit flag indicating the use of a line edit
71 @type bool 78 @type bool
72 """ 79 """
73 super().__init__(parent) 80 super().__init__(parent)
74 81
75 self.__lineEditKind = useLineEdit 82 self.__lineEditKind = useLineEdit
76 83
77 self.__mode = EricPathPicker.DefaultMode 84 self.__mode = EricPathPicker.DefaultMode
78 self.__editorEnabled = True 85 self.__editorEnabled = True
79 86
80 self._completer = None 87 self._completer = None
81 self.__filters = "" 88 self.__filters = ""
82 self.__defaultDirectory = "" 89 self.__defaultDirectory = ""
83 self.__windowTitle = "" 90 self.__windowTitle = ""
84 91
85 self.__layout = QHBoxLayout(self) 92 self.__layout = QHBoxLayout(self)
86 self.__layout.setSpacing(0) 93 self.__layout.setSpacing(0)
87 self.__layout.setContentsMargins(0, 0, 0, 0) 94 self.__layout.setContentsMargins(0, 0, 0, 0)
88 self.setLayout(self.__layout) 95 self.setLayout(self.__layout)
89 96
90 if useLineEdit: 97 if useLineEdit:
91 self._editor = QLineEdit(self) 98 self._editor = QLineEdit(self)
92 self._editor.setPlaceholderText(QCoreApplication.translate( 99 self._editor.setPlaceholderText(
93 "EricPathPickerBase", "Enter Path Name")) 100 QCoreApplication.translate("EricPathPickerBase", "Enter Path Name")
101 )
94 self._editor.setClearButtonEnabled(True) 102 self._editor.setClearButtonEnabled(True)
95 else: 103 else:
96 self._editor = QComboBox(self) 104 self._editor = QComboBox(self)
97 self._editor.setEditable(True) 105 self._editor.setEditable(True)
98 self._editor.lineEdit().setPlaceholderText( 106 self._editor.lineEdit().setPlaceholderText(
99 QCoreApplication.translate( 107 QCoreApplication.translate("EricPathPickerBase", "Enter Path Name")
100 "EricPathPickerBase", "Enter Path Name")) 108 )
101 self._editor.lineEdit().setClearButtonEnabled(True) 109 self._editor.lineEdit().setClearButtonEnabled(True)
102 110
103 self.__button = QToolButton(self) 111 self.__button = QToolButton(self)
104 self.__button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly) 112 self.__button.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
105 self.__button.setIcon(UI.PixmapCache.getIcon("open")) 113 self.__button.setIcon(UI.PixmapCache.getIcon("open"))
106 114
107 self.__layout.addWidget(self._editor) 115 self.__layout.addWidget(self._editor)
108 self.__layout.addWidget(self.__button) 116 self.__layout.addWidget(self.__button)
109 117
110 self.__button.clicked.connect(self.__showPathPickerDialog) 118 self.__button.clicked.connect(self.__showPathPickerDialog)
111 if useLineEdit: 119 if useLineEdit:
112 self._editor.textEdited.connect(self.__pathEdited) 120 self._editor.textEdited.connect(self.__pathEdited)
113 self._editor.textChanged.connect(self.textChanged) 121 self._editor.textChanged.connect(self.textChanged)
114 else: 122 else:
115 self._editor.editTextChanged.connect(self.editTextChanged) 123 self._editor.editTextChanged.connect(self.editTextChanged)
116 124
117 self.setFocusProxy(self._editor) 125 self.setFocusProxy(self._editor)
118 self.setFocusPolicy(Qt.FocusPolicy.StrongFocus) 126 self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
119 self.setSizePolicy(QSizePolicy.Policy.Expanding, 127 self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred)
120 QSizePolicy.Policy.Preferred) 128
121
122 self.__button.setEnabled(self.__mode != EricPathPickerModes.NO_MODE) 129 self.__button.setEnabled(self.__mode != EricPathPickerModes.NO_MODE)
123 130
124 def __pathEdited(self, fpath): 131 def __pathEdited(self, fpath):
125 """ 132 """
126 Private slot handling editing of the path. 133 Private slot handling editing of the path.
127 134
128 @param fpath current text of the path line edit 135 @param fpath current text of the path line edit
129 @type str 136 @type str
130 """ 137 """
131 if self._completer and not self._completer.popup().isVisible(): 138 if self._completer and not self._completer.popup().isVisible():
132 self._completer.setRootPath(QDir.toNativeSeparators(fpath)) 139 self._completer.setRootPath(QDir.toNativeSeparators(fpath))
133 140
134 def setMode(self, mode): 141 def setMode(self, mode):
135 """ 142 """
136 Public method to set the path picker mode. 143 Public method to set the path picker mode.
137 144
138 @param mode picker mode 145 @param mode picker mode
139 @type EricPathPickerModes 146 @type EricPathPickerModes
140 @exception ValueError raised to indicate a bad parameter value 147 @exception ValueError raised to indicate a bad parameter value
141 """ 148 """
142 if mode not in EricPathPickerModes: 149 if mode not in EricPathPickerModes:
143 raise ValueError("Bad value for 'mode' parameter.") 150 raise ValueError("Bad value for 'mode' parameter.")
144 151
145 oldMode = self.__mode 152 oldMode = self.__mode
146 self.__mode = mode 153 self.__mode = mode
147 154
148 if mode != oldMode or (self.__lineEditKind and not self._completer): 155 if mode != oldMode or (self.__lineEditKind and not self._completer):
149 if self.__lineEditKind and self._completer: 156 if self.__lineEditKind and self._completer:
150 # Remove current completer 157 # Remove current completer
151 self._editor.setCompleter(None) 158 self._editor.setCompleter(None)
152 self._completer = None 159 self._completer = None
153 160
154 if mode != EricPathPickerModes.NO_MODE: 161 if mode != EricPathPickerModes.NO_MODE:
155 if self.__lineEditKind: 162 if self.__lineEditKind:
156 # Set a new completer 163 # Set a new completer
157 if mode == EricPathPickerModes.DIRECTORY_MODE: 164 if mode == EricPathPickerModes.DIRECTORY_MODE:
158 self._completer = EricDirCompleter(self._editor) 165 self._completer = EricDirCompleter(self._editor)
159 else: 166 else:
160 self._completer = EricFileCompleter(self._editor) 167 self._completer = EricFileCompleter(self._editor)
161 168
162 # set inactive text 169 # set inactive text
163 if mode in ( 170 if mode in (
164 EricPathPickerModes.OPEN_FILES_MODE, 171 EricPathPickerModes.OPEN_FILES_MODE,
165 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE, 172 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
166 ): 173 ):
167 self._editor.setPlaceholderText( 174 self._editor.setPlaceholderText(
168 self.tr("Enter Path Names separated by ';'")) 175 self.tr("Enter Path Names separated by ';'")
176 )
169 else: 177 else:
170 self._editor.setPlaceholderText( 178 self._editor.setPlaceholderText(self.tr("Enter Path Name"))
171 self.tr("Enter Path Name"))
172 self.__button.setEnabled(self.__mode != EricPathPickerModes.NO_MODE) 179 self.__button.setEnabled(self.__mode != EricPathPickerModes.NO_MODE)
173 180
174 def mode(self): 181 def mode(self):
175 """ 182 """
176 Public method to get the path picker mode. 183 Public method to get the path picker mode.
177 184
178 @return path picker mode 185 @return path picker mode
179 @rtype EricPathPickerModes 186 @rtype EricPathPickerModes
180 """ 187 """
181 return self.__mode 188 return self.__mode
182 189
183 def setPickerEnabled(self, enable): 190 def setPickerEnabled(self, enable):
184 """ 191 """
185 Public method to set the enabled state of the file dialog button. 192 Public method to set the enabled state of the file dialog button.
186 193
187 @param enable flag indicating the enabled state 194 @param enable flag indicating the enabled state
188 @type bool 195 @type bool
189 """ 196 """
190 self.__button.setEnabled(enable) 197 self.__button.setEnabled(enable)
191 198
192 def isPickerEnabled(self): 199 def isPickerEnabled(self):
193 """ 200 """
194 Public method to get the file dialog button enabled state. 201 Public method to get the file dialog button enabled state.
195 202
196 @return flag indicating the enabled state 203 @return flag indicating the enabled state
197 @rtype bool 204 @rtype bool
198 """ 205 """
199 return self.__button.isEnabled() 206 return self.__button.isEnabled()
200 207
201 def clear(self): 208 def clear(self):
202 """ 209 """
203 Public method to clear the current path or list of paths. 210 Public method to clear the current path or list of paths.
204 """ 211 """
205 self._editor.clear() 212 self._editor.clear()
206 213
207 def clearEditText(self): 214 def clearEditText(self):
208 """ 215 """
209 Public method to clear the current path. 216 Public method to clear the current path.
210 """ 217 """
211 if not self.__lineEditKind: 218 if not self.__lineEditKind:
212 self._editor.clearEditText() 219 self._editor.clearEditText()
213 220
214 def _setEditorText(self, text): 221 def _setEditorText(self, text):
215 """ 222 """
216 Protected method to set the text of the editor. 223 Protected method to set the text of the editor.
217 224
218 @param text text to set 225 @param text text to set
219 @type str 226 @type str
220 """ 227 """
221 if self.__lineEditKind: 228 if self.__lineEditKind:
222 self._editor.setText(text) 229 self._editor.setText(text)
223 else: 230 else:
224 self._editor.setEditText(text) 231 self._editor.setEditText(text)
225 if text and self._editor.findText(text) == -1: 232 if text and self._editor.findText(text) == -1:
226 self._editor.insertItem(0, text) 233 self._editor.insertItem(0, text)
227 234
228 def _editorText(self): 235 def _editorText(self):
229 """ 236 """
230 Protected method to get the text of the editor. 237 Protected method to get the text of the editor.
231 238
232 @return text of the editor 239 @return text of the editor
233 @rtype str 240 @rtype str
234 """ 241 """
235 if self.__lineEditKind: 242 if self.__lineEditKind:
236 return self._editor.text() 243 return self._editor.text()
237 else: 244 else:
238 return self._editor.currentText() 245 return self._editor.currentText()
239 246
240 def setText(self, fpath, toNative=True): 247 def setText(self, fpath, toNative=True):
241 """ 248 """
242 Public method to set the current path. 249 Public method to set the current path.
243 250
244 @param fpath path to be set 251 @param fpath path to be set
245 @type str 252 @type str
246 @param toNative flag indicating to convert the path into 253 @param toNative flag indicating to convert the path into
247 a native format 254 a native format
248 @type bool 255 @type bool
256 if toNative: 263 if toNative:
257 fpath = QDir.toNativeSeparators(fpath) 264 fpath = QDir.toNativeSeparators(fpath)
258 self._setEditorText(fpath) 265 self._setEditorText(fpath)
259 if self._completer: 266 if self._completer:
260 self._completer.setRootPath(fpath) 267 self._completer.setRootPath(fpath)
261 268
262 def text(self, toNative=True): 269 def text(self, toNative=True):
263 """ 270 """
264 Public method to get the current path. 271 Public method to get the current path.
265 272
266 @param toNative flag indicating to convert the path into 273 @param toNative flag indicating to convert the path into
267 a native format 274 a native format
268 @type bool 275 @type bool
269 @return current path 276 @return current path
270 @rtype str 277 @rtype str
273 EricPathPickerModes.OPEN_FILES_MODE, 280 EricPathPickerModes.OPEN_FILES_MODE,
274 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE, 281 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
275 ): 282 ):
276 if toNative: 283 if toNative:
277 return ";".join( 284 return ";".join(
278 [QDir.toNativeSeparators(fpath) 285 [
279 for fpath in self._editorText().split(";")]) 286 QDir.toNativeSeparators(fpath)
287 for fpath in self._editorText().split(";")
288 ]
289 )
280 else: 290 else:
281 return self._editorText() 291 return self._editorText()
282 else: 292 else:
283 if toNative: 293 if toNative:
284 return os.path.expanduser( 294 return os.path.expanduser(QDir.toNativeSeparators(self._editorText()))
285 QDir.toNativeSeparators(self._editorText()))
286 else: 295 else:
287 return os.path.expanduser(self._editorText()) 296 return os.path.expanduser(self._editorText())
288 297
289 def setEditText(self, fpath, toNative=True): 298 def setEditText(self, fpath, toNative=True):
290 """ 299 """
291 Public method to set the current path. 300 Public method to set the current path.
292 301
293 @param fpath path to be set 302 @param fpath path to be set
294 @type str 303 @type str
295 @param toNative flag indicating to convert the path into 304 @param toNative flag indicating to convert the path into
296 a native format 305 a native format
297 @type bool 306 @type bool
298 """ 307 """
299 self.setText(fpath, toNative=toNative) 308 self.setText(fpath, toNative=toNative)
300 309
301 def currentText(self, toNative=True): 310 def currentText(self, toNative=True):
302 """ 311 """
303 Public method to get the current path. 312 Public method to get the current path.
304 313
305 @param toNative flag indicating to convert the path into 314 @param toNative flag indicating to convert the path into
306 a native format 315 a native format
307 @type bool 316 @type bool
308 @return current path 317 @return current path
309 @rtype str 318 @rtype str
310 """ 319 """
311 return self.text(toNative=toNative) 320 return self.text(toNative=toNative)
312 321
313 def setPath(self, fpath, toNative=True): 322 def setPath(self, fpath, toNative=True):
314 """ 323 """
315 Public method to set the current path. 324 Public method to set the current path.
316 325
317 @param fpath path to be set 326 @param fpath path to be set
318 @type str 327 @type str
319 @param toNative flag indicating to convert the path into 328 @param toNative flag indicating to convert the path into
320 a native format 329 a native format
321 @type bool 330 @type bool
322 """ 331 """
323 self.setText(fpath, toNative=toNative) 332 self.setText(fpath, toNative=toNative)
324 333
325 def path(self, toNative=True): 334 def path(self, toNative=True):
326 """ 335 """
327 Public method to get the current path. 336 Public method to get the current path.
328 337
329 @param toNative flag indicating to convert the path into 338 @param toNative flag indicating to convert the path into
330 a native format 339 a native format
331 @type bool 340 @type bool
332 @return current path 341 @return current path
333 @rtype str 342 @rtype str
334 """ 343 """
335 return self.text(toNative=toNative) 344 return self.text(toNative=toNative)
336 345
337 def paths(self, toNative=True): 346 def paths(self, toNative=True):
338 """ 347 """
339 Public method to get the list of entered paths. 348 Public method to get the list of entered paths.
340 349
341 @param toNative flag indicating to convert the path into 350 @param toNative flag indicating to convert the path into
342 a native format 351 a native format
343 @type bool 352 @type bool
344 @return entered paths 353 @return entered paths
345 @rtype list of str 354 @rtype list of str
349 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE, 358 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
350 ): 359 ):
351 return self.path(toNative=toNative).split(";") 360 return self.path(toNative=toNative).split(";")
352 else: 361 else:
353 return [self.path(toNative=toNative)] 362 return [self.path(toNative=toNative)]
354 363
355 def firstPath(self, toNative=True): 364 def firstPath(self, toNative=True):
356 """ 365 """
357 Public method to get the first path of a list of entered paths. 366 Public method to get the first path of a list of entered paths.
358 367
359 @param toNative flag indicating to convert the path into 368 @param toNative flag indicating to convert the path into
360 a native format 369 a native format
361 @type bool 370 @type bool
362 @return first path 371 @return first path
363 @rtype str 372 @rtype str
364 """ 373 """
365 return self.paths(toNative=toNative)[0] 374 return self.paths(toNative=toNative)[0]
366 375
367 def lastPath(self, toNative=True): 376 def lastPath(self, toNative=True):
368 """ 377 """
369 Public method to get the last path of a list of entered paths. 378 Public method to get the last path of a list of entered paths.
370 379
371 @param toNative flag indicating to convert the path into 380 @param toNative flag indicating to convert the path into
372 a native format 381 a native format
373 @type bool 382 @type bool
374 @return last path 383 @return last path
375 @rtype str 384 @rtype str
376 """ 385 """
377 return self.paths(toNative=toNative)[-1] 386 return self.paths(toNative=toNative)[-1]
378 387
379 def setEditorEnabled(self, enable): 388 def setEditorEnabled(self, enable):
380 """ 389 """
381 Public method to set the path editor's enabled state. 390 Public method to set the path editor's enabled state.
382 391
383 @param enable flag indicating the enable state 392 @param enable flag indicating the enable state
384 @type bool 393 @type bool
385 """ 394 """
386 if enable != self._editorEnabled: 395 if enable != self._editorEnabled:
387 self._editorEnabled = enable 396 self._editorEnabled = enable
388 self._editor.setEnabled(enable) 397 self._editor.setEnabled(enable)
389 398
390 def editorEnabled(self): 399 def editorEnabled(self):
391 """ 400 """
392 Public method to get the path editor's enabled state. 401 Public method to get the path editor's enabled state.
393 402
394 @return flag indicating the enabled state 403 @return flag indicating the enabled state
395 @rtype bool 404 @rtype bool
396 """ 405 """
397 return self._editorEnabled 406 return self._editorEnabled
398 407
399 def setDefaultDirectory(self, directory): 408 def setDefaultDirectory(self, directory):
400 """ 409 """
401 Public method to set the default directory. 410 Public method to set the default directory.
402 411
403 @param directory default directory 412 @param directory default directory
404 @type str 413 @type str
405 """ 414 """
406 self.__defaultDirectory = directory 415 self.__defaultDirectory = directory
407 416
408 def defaultDirectory(self): 417 def defaultDirectory(self):
409 """ 418 """
410 Public method to get the default directory. 419 Public method to get the default directory.
411 420
412 @return default directory 421 @return default directory
413 @rtype str 422 @rtype str
414 """ 423 """
415 return self.__defaultDirectory 424 return self.__defaultDirectory
416 425
417 def setWindowTitle(self, title): 426 def setWindowTitle(self, title):
418 """ 427 """
419 Public method to set the path picker dialog window title. 428 Public method to set the path picker dialog window title.
420 429
421 @param title window title 430 @param title window title
422 @type str 431 @type str
423 """ 432 """
424 self.__windowTitle = title 433 self.__windowTitle = title
425 434
426 def windowTitle(self): 435 def windowTitle(self):
427 """ 436 """
428 Public method to get the path picker dialog's window title. 437 Public method to get the path picker dialog's window title.
429 438
430 @return window title 439 @return window title
431 @rtype str 440 @rtype str
432 """ 441 """
433 return self.__windowTitle 442 return self.__windowTitle
434 443
435 def setFilters(self, filters): 444 def setFilters(self, filters):
436 """ 445 """
437 Public method to set the filters for the path picker dialog. 446 Public method to set the filters for the path picker dialog.
438 447
439 Note: Multiple filters must be separated by ';;'. 448 Note: Multiple filters must be separated by ';;'.
440 449
441 @param filters string containing the file filters 450 @param filters string containing the file filters
442 @type str 451 @type str
443 """ 452 """
444 self.__filters = filters 453 self.__filters = filters
445 454
446 def filters(self): 455 def filters(self):
447 """ 456 """
448 Public methods to get the filter string. 457 Public methods to get the filter string.
449 458
450 @return filter string 459 @return filter string
451 @rtype str 460 @rtype str
452 """ 461 """
453 return self.__filters 462 return self.__filters
454 463
455 def setNameFilters(self, filters): 464 def setNameFilters(self, filters):
456 """ 465 """
457 Public method to set the name filters for the completer. 466 Public method to set the name filters for the completer.
458 467
459 @param filters list of file name filters 468 @param filters list of file name filters
460 @type list of str 469 @type list of str
461 """ 470 """
462 if self._completer: 471 if self._completer:
463 self._completer.model().setNameFilters(filters) 472 self._completer.model().setNameFilters(filters)
464 473
465 def setButtonToolTip(self, tooltip): 474 def setButtonToolTip(self, tooltip):
466 """ 475 """
467 Public method to set the tool button tool tip. 476 Public method to set the tool button tool tip.
468 477
469 @param tooltip text to be set as a tool tip 478 @param tooltip text to be set as a tool tip
470 @type str 479 @type str
471 """ 480 """
472 self.__button.setToolTip(tooltip) 481 self.__button.setToolTip(tooltip)
473 482
474 def buttonToolTip(self): 483 def buttonToolTip(self):
475 """ 484 """
476 Public method to get the tool button tool tip. 485 Public method to get the tool button tool tip.
477 486
478 @return tool tip text 487 @return tool tip text
479 @rtype str 488 @rtype str
480 """ 489 """
481 return self.__button.toolTip() 490 return self.__button.toolTip()
482 491
483 def setEditorToolTip(self, tooltip): 492 def setEditorToolTip(self, tooltip):
484 """ 493 """
485 Public method to set the editor tool tip. 494 Public method to set the editor tool tip.
486 495
487 @param tooltip text to be set as a tool tip 496 @param tooltip text to be set as a tool tip
488 @type str 497 @type str
489 """ 498 """
490 self._editor.setToolTip(tooltip) 499 self._editor.setToolTip(tooltip)
491 500
492 def editorToolTip(self): 501 def editorToolTip(self):
493 """ 502 """
494 Public method to get the editor tool tip. 503 Public method to get the editor tool tip.
495 504
496 @return tool tip text 505 @return tool tip text
497 @rtype str 506 @rtype str
498 """ 507 """
499 return self._editor.toolTip() 508 return self._editor.toolTip()
500 509
501 def __showPathPickerDialog(self): 510 def __showPathPickerDialog(self):
502 """ 511 """
503 Private slot to show the path picker dialog. 512 Private slot to show the path picker dialog.
504 """ 513 """
505 if self.__mode == EricPathPickerModes.NO_MODE: 514 if self.__mode == EricPathPickerModes.NO_MODE:
506 return 515 return
507 516
508 if self.__mode == EricPathPickerModes.CUSTOM_MODE: 517 if self.__mode == EricPathPickerModes.CUSTOM_MODE:
509 self.pickerButtonClicked.emit() 518 self.pickerButtonClicked.emit()
510 return 519 return
511 520
512 self.aboutToShowPathPickerDialog.emit() 521 self.aboutToShowPathPickerDialog.emit()
513 522
514 windowTitle = self.__windowTitle 523 windowTitle = self.__windowTitle
515 if not windowTitle: 524 if not windowTitle:
516 if self.__mode == EricPathPickerModes.OPEN_FILE_MODE: 525 if self.__mode == EricPathPickerModes.OPEN_FILE_MODE:
517 windowTitle = self.tr("Choose a file to open") 526 windowTitle = self.tr("Choose a file to open")
518 elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE: 527 elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE:
519 windowTitle = self.tr("Choose files to open") 528 windowTitle = self.tr("Choose files to open")
520 elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE: 529 elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE:
521 windowTitle = self.tr("Choose files and directories") 530 windowTitle = self.tr("Choose files and directories")
522 elif self.__mode in [ 531 elif self.__mode in [
523 EricPathPickerModes.SAVE_FILE_MODE, 532 EricPathPickerModes.SAVE_FILE_MODE,
524 EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE, 533 EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE,
525 EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE]: 534 EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE,
535 ]:
526 windowTitle = self.tr("Choose a file to save") 536 windowTitle = self.tr("Choose a file to save")
527 elif self.__mode == EricPathPickerModes.DIRECTORY_MODE: 537 elif self.__mode == EricPathPickerModes.DIRECTORY_MODE:
528 windowTitle = self.tr("Choose a directory") 538 windowTitle = self.tr("Choose a directory")
529 539
530 directory = self._editorText() 540 directory = self._editorText()
531 if not directory and self.__defaultDirectory: 541 if not directory and self.__defaultDirectory:
532 directory = self.__defaultDirectory 542 directory = self.__defaultDirectory
533 directory = ( 543 directory = (
534 os.path.expanduser(directory.split(";")[0]) 544 os.path.expanduser(directory.split(";")[0])
535 if self.__mode in ( 545 if self.__mode
546 in (
536 EricPathPickerModes.OPEN_FILES_MODE, 547 EricPathPickerModes.OPEN_FILES_MODE,
537 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE) else 548 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
538 os.path.expanduser(directory) 549 )
550 else os.path.expanduser(directory)
539 ) 551 )
540 if not os.path.isabs(directory) and self.__defaultDirectory: 552 if not os.path.isabs(directory) and self.__defaultDirectory:
541 directory = os.path.join(self.__defaultDirectory, directory) 553 directory = os.path.join(self.__defaultDirectory, directory)
542 directory = QDir.fromNativeSeparators(directory) 554 directory = QDir.fromNativeSeparators(directory)
543 555
544 if self.__mode == EricPathPickerModes.OPEN_FILE_MODE: 556 if self.__mode == EricPathPickerModes.OPEN_FILE_MODE:
545 fpath = EricFileDialog.getOpenFileName( 557 fpath = EricFileDialog.getOpenFileName(
546 self, 558 self, windowTitle, directory, self.__filters
547 windowTitle, 559 )
548 directory,
549 self.__filters)
550 fpath = QDir.toNativeSeparators(fpath) 560 fpath = QDir.toNativeSeparators(fpath)
551 elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE: 561 elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE:
552 fpaths = EricFileDialog.getOpenFileNames( 562 fpaths = EricFileDialog.getOpenFileNames(
553 self, 563 self, windowTitle, directory, self.__filters
554 windowTitle, 564 )
555 directory, 565 fpath = ";".join([QDir.toNativeSeparators(fpath) for fpath in fpaths])
556 self.__filters)
557 fpath = ";".join([QDir.toNativeSeparators(fpath)
558 for fpath in fpaths])
559 elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE: 566 elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE:
560 fpaths = EricFileDialog.getOpenFileAndDirNames( 567 fpaths = EricFileDialog.getOpenFileAndDirNames(
561 self, 568 self, windowTitle, directory, self.__filters
562 windowTitle, 569 )
563 directory, 570 fpath = ";".join([QDir.toNativeSeparators(fpath) for fpath in fpaths])
564 self.__filters)
565 fpath = ";".join([QDir.toNativeSeparators(fpath)
566 for fpath in fpaths])
567 elif self.__mode == EricPathPickerModes.SAVE_FILE_MODE: 571 elif self.__mode == EricPathPickerModes.SAVE_FILE_MODE:
568 fpath = EricFileDialog.getSaveFileName( 572 fpath = EricFileDialog.getSaveFileName(
569 self, 573 self,
570 windowTitle, 574 windowTitle,
571 directory, 575 directory,
572 self.__filters, 576 self.__filters,
573 EricFileDialog.DontConfirmOverwrite) 577 EricFileDialog.DontConfirmOverwrite,
578 )
574 fpath = QDir.toNativeSeparators(fpath) 579 fpath = QDir.toNativeSeparators(fpath)
575 elif ( 580 elif self.__mode == EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE:
576 self.__mode == EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE
577 ):
578 fpath, selectedFilter = EricFileDialog.getSaveFileNameAndFilter( 581 fpath, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
579 self, 582 self,
580 windowTitle, 583 windowTitle,
581 directory, 584 directory,
582 self.__filters, 585 self.__filters,
583 None, 586 None,
584 EricFileDialog.DontConfirmOverwrite) 587 EricFileDialog.DontConfirmOverwrite,
588 )
585 fpath = pathlib.Path(fpath) 589 fpath = pathlib.Path(fpath)
586 if not fpath.suffix: 590 if not fpath.suffix:
587 ex = selectedFilter.split("(*")[1].split(")")[0] 591 ex = selectedFilter.split("(*")[1].split(")")[0]
588 if ex: 592 if ex:
589 fpath = fpath.with_suffix(ex) 593 fpath = fpath.with_suffix(ex)
590 elif self.__mode == EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE: 594 elif self.__mode == EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE:
591 fpath = EricFileDialog.getSaveFileName( 595 fpath = EricFileDialog.getSaveFileName(
592 self, 596 self, windowTitle, directory, self.__filters
593 windowTitle, 597 )
594 directory,
595 self.__filters)
596 fpath = QDir.toNativeSeparators(fpath) 598 fpath = QDir.toNativeSeparators(fpath)
597 elif self.__mode == EricPathPickerModes.DIRECTORY_MODE: 599 elif self.__mode == EricPathPickerModes.DIRECTORY_MODE:
598 fpath = EricFileDialog.getExistingDirectory( 600 fpath = EricFileDialog.getExistingDirectory(
599 self, 601 self, windowTitle, directory, EricFileDialog.ShowDirsOnly
600 windowTitle, 602 )
601 directory,
602 EricFileDialog.ShowDirsOnly)
603 fpath = QDir.toNativeSeparators(fpath) 603 fpath = QDir.toNativeSeparators(fpath)
604 while fpath.endswith(os.sep): 604 while fpath.endswith(os.sep):
605 fpath = fpath[:-1] 605 fpath = fpath[:-1]
606 elif self.__mode == EricPathPickerModes.DIRECTORY_SHOW_FILES_MODE: 606 elif self.__mode == EricPathPickerModes.DIRECTORY_SHOW_FILES_MODE:
607 fpath = EricFileDialog.getExistingDirectory( 607 fpath = EricFileDialog.getExistingDirectory(
608 self, 608 self, windowTitle, directory, EricFileDialog.DontUseNativeDialog
609 windowTitle, 609 )
610 directory,
611 EricFileDialog.DontUseNativeDialog)
612 fpath = QDir.toNativeSeparators(fpath) 610 fpath = QDir.toNativeSeparators(fpath)
613 while fpath.endswith(os.sep): 611 while fpath.endswith(os.sep):
614 fpath = fpath[:-1] 612 fpath = fpath[:-1]
615 613
616 if fpath: 614 if fpath:
617 self._setEditorText(str(fpath)) 615 self._setEditorText(str(fpath))
618 self.pathSelected.emit(str(fpath)) 616 self.pathSelected.emit(str(fpath))
619 617
620 def setReadOnly(self, readOnly): 618 def setReadOnly(self, readOnly):
621 """ 619 """
622 Public method to set the path picker to read only mode. 620 Public method to set the path picker to read only mode.
623 621
624 @param readOnly flag indicating read only mode 622 @param readOnly flag indicating read only mode
625 @type bool 623 @type bool
626 """ 624 """
627 try: 625 try:
628 self._editor.setReadOnly(readOnly) 626 self._editor.setReadOnly(readOnly)
629 except AttributeError: 627 except AttributeError:
630 self._editor.setEditable(not readOnly) 628 self._editor.setEditable(not readOnly)
631 self.setPickerEnabled(not readOnly) 629 self.setPickerEnabled(not readOnly)
632 630
633 def isReadOnly(self): 631 def isReadOnly(self):
634 """ 632 """
635 Public method to check the path picker for read only mode. 633 Public method to check the path picker for read only mode.
636 634
637 @return flg indicating read only mode 635 @return flg indicating read only mode
638 @rtype bool 636 @rtype bool
639 """ 637 """
640 try: 638 try:
641 return self._editor.isReadOnly() 639 return self._editor.isReadOnly()
642 except AttributeError: 640 except AttributeError:
643 return not self._editor.isEditable() 641 return not self._editor.isEditable()
644 642
645 ################################################################## 643 ##################################################################
646 ## Methods below emulate some of the QComboBox API 644 ## Methods below emulate some of the QComboBox API
647 ################################################################## 645 ##################################################################
648 646
649 def addItems(self, pathsList): 647 def addItems(self, pathsList):
650 """ 648 """
651 Public method to add paths to the current list. 649 Public method to add paths to the current list.
652 650
653 @param pathsList list of paths to add 651 @param pathsList list of paths to add
654 @type list of str 652 @type list of str
655 """ 653 """
656 self._editor.addItems(pathsList) 654 self._editor.addItems(pathsList)
657 655
658 def addItem(self, fpath): 656 def addItem(self, fpath):
659 """ 657 """
660 Public method to add a paths to the current list. 658 Public method to add a paths to the current list.
661 659
662 @param fpath path to add 660 @param fpath path to add
663 @type str 661 @type str
664 """ 662 """
665 self._editor.addItem(fpath) 663 self._editor.addItem(fpath)
666 664
667 def setPathsList(self, pathsList): 665 def setPathsList(self, pathsList):
668 """ 666 """
669 Public method to set the paths list. 667 Public method to set the paths list.
670 668
671 @param pathsList list of paths 669 @param pathsList list of paths
672 @type list of str 670 @type list of str
673 """ 671 """
674 self.clear() 672 self.clear()
675 self.addItems(pathsList) 673 self.addItems(pathsList)
676 674
677 def setCurrentIndex(self, index): 675 def setCurrentIndex(self, index):
678 """ 676 """
679 Public slot to set the current index. 677 Public slot to set the current index.
680 678
681 @param index index of the item to set current 679 @param index index of the item to set current
682 @type int 680 @type int
683 """ 681 """
684 self._editor.setCurrentIndex(index) 682 self._editor.setCurrentIndex(index)
685 683
686 def setCurrentText(self, text): 684 def setCurrentText(self, text):
687 """ 685 """
688 Public slot to set the current text. 686 Public slot to set the current text.
689 687
690 @param text text of the item to set current 688 @param text text of the item to set current
691 @type str 689 @type str
692 """ 690 """
693 self._editor.setCurrentText(text) 691 self._editor.setCurrentText(text)
694 692
695 def setInsertPolicy(self, policy): 693 def setInsertPolicy(self, policy):
696 """ 694 """
697 Public method to set the insertion policy of the combo box. 695 Public method to set the insertion policy of the combo box.
698 696
699 @param policy insertion policy 697 @param policy insertion policy
700 @type QComboBox.InsertPolicy 698 @type QComboBox.InsertPolicy
701 """ 699 """
702 self._editor.setInsertPolicy(policy) 700 self._editor.setInsertPolicy(policy)
703 701
704 def setSizeAdjustPolicy(self, policy): 702 def setSizeAdjustPolicy(self, policy):
705 """ 703 """
706 Public method to set the size adjust policy of the combo box. 704 Public method to set the size adjust policy of the combo box.
707 705
708 @param policy size adjust policy 706 @param policy size adjust policy
709 @type QComboBox.SizeAdjustPolicy 707 @type QComboBox.SizeAdjustPolicy
710 """ 708 """
711 self._editor.setSizeAdjustPolicy(policy) 709 self._editor.setSizeAdjustPolicy(policy)
712 710
714 class EricPathPicker(EricPathPickerBase): 712 class EricPathPicker(EricPathPickerBase):
715 """ 713 """
716 Class implementing a path picker widget consisting of a line edit and a 714 Class implementing a path picker widget consisting of a line edit and a
717 tool button to open a file dialog. 715 tool button to open a file dialog.
718 """ 716 """
717
719 def __init__(self, parent=None): 718 def __init__(self, parent=None):
720 """ 719 """
721 Constructor 720 Constructor
722 721
723 @param parent reference to the parent widget 722 @param parent reference to the parent widget
724 @type QWidget 723 @type QWidget
725 """ 724 """
726 super().__init__(parent, useLineEdit=True) 725 super().__init__(parent, useLineEdit=True)
727 726
729 class EricComboPathPicker(EricPathPickerBase): 728 class EricComboPathPicker(EricPathPickerBase):
730 """ 729 """
731 Class implementing a path picker widget consisting of a combobox and a 730 Class implementing a path picker widget consisting of a combobox and a
732 tool button to open a file dialog. 731 tool button to open a file dialog.
733 """ 732 """
733
734 def __init__(self, parent=None): 734 def __init__(self, parent=None):
735 """ 735 """
736 Constructor 736 Constructor
737 737
738 @param parent reference to the parent widget 738 @param parent reference to the parent widget
739 @type QWidget 739 @type QWidget
740 """ 740 """
741 super().__init__(parent, useLineEdit=False) 741 super().__init__(parent, useLineEdit=False)
742 742
743 def getPathItems(self): 743 def getPathItems(self):
744 """ 744 """
745 Public method to get the list of remembered paths. 745 Public method to get the list of remembered paths.
746 746
747 @return list od remembered paths 747 @return list od remembered paths
748 @rtype list of str 748 @rtype list of str
749 """ 749 """
750 paths = [] 750 paths = []
751 for index in range(self._editor.count()): 751 for index in range(self._editor.count()):

eric ide

mercurial