E5Gui/E5PathPicker.py

changeset 4613
c7951eb9d525
parent 4601
0017e7cb67ae
child 4620
49d9c6b58678
equal deleted inserted replaced
4612:c9982bcd00f8 4613:c7951eb9d525
38 SaveFileEnsureExtensionMode = 3 38 SaveFileEnsureExtensionMode = 3
39 DirectoryMode = 4 39 DirectoryMode = 4
40 CustomMode = 99 40 CustomMode = 99
41 NoMode = 100 41 NoMode = 100
42 42
43 # TODO: Refactor the classes using a base class with common functions 43
44 44 class E5PathPickerBase(QWidget):
45 45 """
46 class E5PathPicker(QWidget): 46 Class implementing the base of a path picker widget consisting of a
47 """ 47 line edit or combo box and a tool button to open a file dialog.
48 Class implementing a path picker widget consisting of a line edit and a
49 tool button to open a file dialog.
50 48
51 @signal textChanged(path) emitted when the entered path has changed 49 @signal textChanged(path) emitted when the entered path has changed
50 (line edit based widget)
51 @signal editTextChanged(path) emitted when the entered path has changed
52 (combo box based widget)
52 @signal pathSelected(path) emitted after a path has been selected via the 53 @signal pathSelected(path) emitted after a path has been selected via the
53 file dialog 54 file dialog
54 @signal aboutToShowPathPickerDialog emitted before the file dialog is shown 55 @signal aboutToShowPathPickerDialog emitted before the file dialog is shown
55 @signal pickerButtonClicked emitted when the picker button was pressed and 56 @signal pickerButtonClicked emitted when the picker button was pressed and
56 the widget mode is custom 57 the widget mode is custom
57 """ 58 """
58 DefaultMode = E5PathPickerModes.NoMode 59 DefaultMode = E5PathPickerModes.NoMode
59 60
60 textChanged = pyqtSignal(str) 61 textChanged = pyqtSignal(str)
62 editTextChanged = pyqtSignal(str)
61 pathSelected = pyqtSignal(str) 63 pathSelected = pyqtSignal(str)
62 aboutToShowPathPickerDialog = pyqtSignal() 64 aboutToShowPathPickerDialog = pyqtSignal()
63 pickerButtonClicked = pyqtSignal() 65 pickerButtonClicked = pyqtSignal()
64 66
65 def __init__(self, parent=None): 67 def __init__(self, parent=None, useLineEdit=True):
66 """ 68 """
67 Constructor 69 Constructor
68 70
69 @param parent reference to the parent widget 71 @param parent reference to the parent widget
70 @type QWidget 72 @type QWidget
71 """ 73 @param useLineEdit flag indicating the use of a line edit
72 super(E5PathPicker, self).__init__(parent) 74 @type bool
75 """
76 super(E5PathPickerBase, self).__init__(parent)
77
78 self.__lineEditKind = useLineEdit
73 79
74 self.__mode = E5PathPicker.DefaultMode 80 self.__mode = E5PathPicker.DefaultMode
75 self.__editorEnabled = True 81 self.__editorEnabled = True
76 82
77 self.__completer = None 83 self._completer = None
78 self.__filters = "" 84 self.__filters = ""
79 self.__defaultDirectory = "" 85 self.__defaultDirectory = ""
80 self.__windowTitle = "" 86 self.__windowTitle = ""
81 87
82 self.__layout = QHBoxLayout() 88 self.__layout = QHBoxLayout()
83 self.__layout.setSpacing(0) 89 self.__layout.setSpacing(0)
84 self.__layout.setContentsMargins(0, 0, 0, 0) 90 self.__layout.setContentsMargins(0, 0, 0, 0)
85 self.setLayout(self.__layout) 91 self.setLayout(self.__layout)
86 92
87 self.__editor = E5ClearableLineEdit(self, self.tr("Enter Path Name")) 93 if useLineEdit:
94 self._editor = E5ClearableLineEdit(
95 self, self.tr("Enter Path Name"))
96 else:
97 self._editor = E5ClearableComboBox(
98 self, self.tr("Enter Path Name"))
88 99
89 self.__button = QToolButton(self) 100 self.__button = QToolButton(self)
90 self.__button.setToolButtonStyle(Qt.ToolButtonIconOnly) 101 self.__button.setToolButtonStyle(Qt.ToolButtonIconOnly)
91 self.__button.setIcon(UI.PixmapCache.getIcon("open.png")) 102 self.__button.setIcon(UI.PixmapCache.getIcon("open.png"))
92 103
93 self.__layout.addWidget(self.__editor) 104 self.__layout.addWidget(self._editor)
94 self.__layout.addWidget(self.__button) 105 self.__layout.addWidget(self.__button)
95 106
96 self.__button.clicked.connect(self.__showPathPickerDialog) 107 self.__button.clicked.connect(self.__showPathPickerDialog)
97 self.__editor.textEdited.connect(self.__pathEdited) 108 if useLineEdit:
98 self.__editor.textChanged.connect(self.textChanged) 109 self._editor.textEdited.connect(self.__pathEdited)
99 110 self._editor.textChanged.connect(self.textChanged)
100 self.setFocusProxy(self.__editor) 111 else:
112 self._editor.editTextChanged.connect(self.editTextChanged)
113
114 self.setFocusProxy(self._editor)
101 self.setFocusPolicy(Qt.StrongFocus) 115 self.setFocusPolicy(Qt.StrongFocus)
102 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) 116 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
103 117
104 self.__button.setEnabled(self.__mode != E5PathPickerModes.NoMode) 118 self.__button.setEnabled(self.__mode != E5PathPickerModes.NoMode)
105 119
108 Private slot handling editing of the path. 122 Private slot handling editing of the path.
109 123
110 @param path current text of the path line edit 124 @param path current text of the path line edit
111 @type str 125 @type str
112 """ 126 """
113 if self.__completer and not self.__completer.popup().isVisible(): 127 if self._completer and not self._completer.popup().isVisible():
114 self.__completer.setRootPath(Utilities.toNativeSeparators(path)) 128 self._completer.setRootPath(Utilities.toNativeSeparators(path))
115 129
116 def setMode(self, mode): 130 def setMode(self, mode):
117 """ 131 """
118 Public method to set the path picker mode. 132 Public method to set the path picker mode.
119 133
123 assert mode in E5PathPickerModes 137 assert mode in E5PathPickerModes
124 138
125 oldMode = self.__mode 139 oldMode = self.__mode
126 self.__mode = mode 140 self.__mode = mode
127 141
128 if mode != oldMode or not self.__completer: 142 if mode != oldMode or (self.__lineEditKind and not self._completer):
129 if self.__completer: 143 if self.__lineEditKind and self._completer:
130 # Remove current completer 144 # Remove current completer
131 self.__editor.setCompleter(None) 145 self._editor.setCompleter(None)
132 self.__completer = None 146 self._completer = None
133 147
134 if mode != E5PathPickerModes.NoMode: 148 if mode != E5PathPickerModes.NoMode:
135 # Set a new completer 149 if self.__lineEditKind:
136 if mode == E5PathPickerModes.DirectoryMode: 150 # Set a new completer
137 self.__completer = E5DirCompleter(self.__editor) 151 if mode == E5PathPickerModes.DirectoryMode:
138 else: 152 self._completer = E5DirCompleter(self._editor)
139 self.__completer = E5FileCompleter(self.__editor) 153 else:
154 self._completer = E5FileCompleter(self._editor)
140 155
141 # set inactive text 156 # set inactive text
142 if mode == E5PathPickerModes.OpenFilesMode: 157 if mode == E5PathPickerModes.OpenFilesMode:
143 self.__editor.setInactiveText( 158 self._editor.setInactiveText(
144 self.tr("Enter Path Names separated by ';'")) 159 self.tr("Enter Path Names separated by ';'"))
145 else: 160 else:
146 self.__editor.setInactiveText( 161 self._editor.setInactiveText(
147 self.tr("Enter Path Name")) 162 self.tr("Enter Path Name"))
148 self.__button.setEnabled(self.__mode != E5PathPickerModes.NoMode) 163 self.__button.setEnabled(self.__mode != E5PathPickerModes.NoMode)
149 164
150 def mode(self): 165 def mode(self):
151 """ 166 """
174 """ 189 """
175 return self.__button.isEnabled() 190 return self.__button.isEnabled()
176 191
177 def clear(self): 192 def clear(self):
178 """ 193 """
194 Public method to clear the current path or list of paths.
195 """
196 self._editor.clear()
197
198 def clearEditText(self):
199 """
179 Public method to clear the current path. 200 Public method to clear the current path.
180 """ 201 """
181 self.__editor.clear() 202 if not self.__lineEditKind:
203 self._editor.clearEditText()
204
205 def _setEditorText(self, text):
206 """
207 Protected method to set the text of the editor.
208
209 @param text text to set
210 @type str
211 """
212 if self.__lineEditKind:
213 self._editor.setText(text)
214 else:
215 self._editor.setEditText(text)
216
217 def _editorText(self):
218 """
219 Protected method to get the text of the editor.
220
221 @return text of the editor
222 @rtype str
223 """
224 if self.__lineEditKind:
225 return self._editor.text()
226 else:
227 return self._editor.currentText()
182 228
183 def setText(self, path): 229 def setText(self, path):
184 """ 230 """
185 Public method to set the current path. 231 Public method to set the current path.
186 232
187 @param path path to be set 233 @param path path to be set
188 @type str 234 @type str
189 """ 235 """
190 if self.__mode == E5PathPickerModes.OpenFilesMode: 236 if self.__mode == E5PathPickerModes.OpenFilesMode:
191 self.__editor.setText(path) 237 self._setEditorText(path)
192 else: 238 else:
193 path = Utilities.toNativeSeparators(path) 239 path = Utilities.toNativeSeparators(path)
194 self.__editor.setText(path) 240 self._setEditorText(path)
195 if self.__completer: 241 if self._completer:
196 self.__completer.setRootPath(path) 242 self._completer.setRootPath(path)
197 243
198 def text(self): 244 def text(self):
199 """ 245 """
200 Public method to get the current path. 246 Public method to get the current path.
201 247
203 @rtype str 249 @rtype str
204 """ 250 """
205 if self.__mode == E5PathPickerModes.OpenFilesMode: 251 if self.__mode == E5PathPickerModes.OpenFilesMode:
206 return ";".join( 252 return ";".join(
207 [Utilities.toNativeSeparators(path) 253 [Utilities.toNativeSeparators(path)
208 for path in self.__editor.text().split(";")]) 254 for path in self._editorText().split(";")])
209 else: 255 else:
210 return os.path.expanduser( 256 return os.path.expanduser(
211 Utilities.toNativeSeparators(self.__editor.text())) 257 Utilities.toNativeSeparators(self._editorText()))
258
259 def setEditText(self, path):
260 """
261 Public method to set the current path.
262
263 @param path path to be set
264 @type str
265 """
266 self.setText(path)
267
268 def currentText(self):
269 """
270 Public method to get the current path.
271
272 @return current path
273 @rtype str
274 """
275 return self.text()
212 276
213 def setPath(self, path): 277 def setPath(self, path):
214 """ 278 """
215 Public method to set the current path. 279 Public method to set the current path.
216 280
245 Public method to get the first path of a list of entered paths. 309 Public method to get the first path of a list of entered paths.
246 310
247 @return first path 311 @return first path
248 @rtype str 312 @rtype str
249 """ 313 """
250 if self.__mode == E5PathPickerModes.OpenFilesMode: 314 self.paths()[0]
251 return self.path().split(";")[0]
252 else:
253 return self.path()
254 315
255 def lastPath(self): 316 def lastPath(self):
256 """ 317 """
257 Public method to get the last path of a list of entered paths. 318 Public method to get the last path of a list of entered paths.
258 319
259 @return first path 320 @return first path
260 @rtype str 321 @rtype str
261 """ 322 """
262 if self.__mode == E5PathPickerModes.OpenFilesMode: 323 self.paths()[-1]
263 return self.path().split(";")[-1]
264 else:
265 return self.path()
266 324
267 def setEditorEnabled(self, enable): 325 def setEditorEnabled(self, enable):
268 """ 326 """
269 Public method to set the path editor's enabled state. 327 Public method to set the path editor's enabled state.
270 328
271 @param enable flag indicating the enable state 329 @param enable flag indicating the enable state
272 @type bool 330 @type bool
273 """ 331 """
274 if enable != self.__editorEnabled: 332 if enable != self._editorEnabled:
275 self.__editorEnabled = enable 333 self._editorEnabled = enable
276 self.__editor.setEnabled(enable) 334 self._editor.setEnabled(enable)
277 335
278 def editorEnabled(self): 336 def editorEnabled(self):
279 """ 337 """
280 Public method to get the path editor's enabled state. 338 Public method to get the path editor's enabled state.
281 339
282 @return flag indicating the enabled state 340 @return flag indicating the enabled state
283 @rtype bool 341 @rtype bool
284 """ 342 """
285 return self.__editorEnabled 343 return self._editorEnabled
286 344
287 def setDefaultDirectory(self, directory): 345 def setDefaultDirectory(self, directory):
288 """ 346 """
289 Public method to set the default directory. 347 Public method to set the default directory.
290 348
345 Public method to set the name filters for the completer. 403 Public method to set the name filters for the completer.
346 404
347 @param filters list of file name filters 405 @param filters list of file name filters
348 @type list of str 406 @type list of str
349 """ 407 """
350 self.__editor.completer().model().setNameFilters(filters) 408 if self._completer:
409 self._completer.model().setNameFilters(filters)
351 410
352 def setButtonToolTip(self, tooltip): 411 def setButtonToolTip(self, tooltip):
353 """ 412 """
354 Public method to set the tool button tool tip. 413 Public method to set the tool button tool tip.
355 414
372 Public method to set the editor tool tip. 431 Public method to set the editor tool tip.
373 432
374 @param tooltip text to be set as a tool tip 433 @param tooltip text to be set as a tool tip
375 @type str 434 @type str
376 """ 435 """
377 self.__editor.setToolTip(tooltip) 436 self._editor.setToolTip(tooltip)
378 437
379 def editorToolTip(self): 438 def editorToolTip(self):
380 """ 439 """
381 Public method to get the editor tool tip. 440 Public method to get the editor tool tip.
382 441
383 @return tool tip text 442 @return tool tip text
384 @rtype str 443 @rtype str
385 """ 444 """
386 return self.__editor.toolTip() 445 return self._editor.toolTip()
387 446
388 def __showPathPickerDialog(self): 447 def __showPathPickerDialog(self):
389 """ 448 """
390 Private slot to show the path picker dialog. 449 Private slot to show the path picker dialog.
391 """ 450 """
409 E5PathPickerModes.SaveFileEnsureExtensionMode]: 468 E5PathPickerModes.SaveFileEnsureExtensionMode]:
410 windowTitle = self.tr("Choose a file to save") 469 windowTitle = self.tr("Choose a file to save")
411 elif self.__mode == E5PathPickerModes.DirectoryMode: 470 elif self.__mode == E5PathPickerModes.DirectoryMode:
412 windowTitle = self.tr("Choose a directory") 471 windowTitle = self.tr("Choose a directory")
413 472
414 directory = self.__editor.text() 473 directory = self._editorText()
415 if not directory and self.__defaultDirectory: 474 if not directory and self.__defaultDirectory:
416 directory = self.__defaultDirectory 475 directory = self.__defaultDirectory
417 if self.__mode == E5PathPickerModes.OpenFilesMode: 476 if self.__mode == E5PathPickerModes.OpenFilesMode:
418 directory = os.path.expanduser(directory.split(";")[0]) 477 directory = os.path.expanduser(directory.split(";")[0])
419 else: 478 else:
469 path = Utilities.toNativeSeparators(path) 528 path = Utilities.toNativeSeparators(path)
470 while path.endswith(os.sep): 529 while path.endswith(os.sep):
471 path = path[:-1] 530 path = path[:-1]
472 531
473 if path: 532 if path:
474 self.__editor.setText(path) 533 self._setEditorText(path)
475 self.pathSelected.emit(path) 534 self.pathSelected.emit(path)
476 535
477 536 ##################################################################
478 class E5ComboPathPicker(QWidget): 537 ## Methods below emulate some of the QComboBox API
538 ##################################################################
539
540 def addItems(self, pathsList):
541 """
542 Public method to add paths to the current list.
543
544 @param pathsList list of paths to add
545 @type list of str
546 """
547 self._editor.addItems(pathsList)
548
549 def addItem(self, path):
550 """
551 Public method to add a paths to the current list.
552
553 @param path path to add
554 @type str
555 """
556 self._editor.addItem(path)
557
558 def setPathsList(self, pathsList):
559 """
560 Public method to set the paths list.
561
562 @param pathsList list of paths
563 @type list of str
564 """
565 self.clear()
566 self.addItems(pathsList)
567
568 def setCurrentIndex(self, index):
569 """
570 Public slot to set the current index.
571
572 @param index index of the item to set current
573 @type int
574 """
575 self._editor.setCurrentIndex(index)
576
577 def setInsertPolicy(self, policy):
578 """
579 Public method to set the insertion policy of the combo box.
580
581 @param policy insertion policy
582 @type QComboBox.InsertPolicy
583 """
584 self._editor.setInsertPolicy(policy)
585
586 def setSizeAdjustPolicy(self, policy):
587 """
588 Public method to set the size adjust policy of the combo box.
589
590 @param policy size adjust policy
591 @type QComboBox.SizeAdjustPolicy
592 """
593 self._editor.setSizeAdjustPolicy(policy)
594
595
596 class E5PathPicker(E5PathPickerBase):
597 """
598 Class implementing a path picker widget consisting of a line edit and a
599 tool button to open a file dialog.
600
601 @signal textChanged(path) emitted when the entered path has changed
602 @signal pathSelected(path) emitted after a path has been selected via the
603 file dialog
604 @signal aboutToShowPathPickerDialog emitted before the file dialog is shown
605 @signal pickerButtonClicked emitted when the picker button was pressed and
606 the widget mode is custom
607 """
608 def __init__(self, parent=None):
609 """
610 Constructor
611
612 @param parent reference to the parent widget
613 @type QWidget
614 """
615 super(E5PathPicker, self).__init__(parent, useLineEdit=True)
616
617
618 class E5ComboPathPicker(E5PathPickerBase):
479 """ 619 """
480 Class implementing a path picker widget consisting of a combobox and a 620 Class implementing a path picker widget consisting of a combobox and a
481 tool button to open a file dialog. 621 tool button to open a file dialog.
482 622
483 @signal editTextChanged(path) emitted when the entered path has changed 623 @signal editTextChanged(path) emitted when the entered path has changed
485 file dialog 625 file dialog
486 @signal aboutToShowPathPickerDialog emitted before the file dialog is shown 626 @signal aboutToShowPathPickerDialog emitted before the file dialog is shown
487 @signal pickerButtonClicked emitted when the picker button was pressed and 627 @signal pickerButtonClicked emitted when the picker button was pressed and
488 the widget mode is custom 628 the widget mode is custom
489 """ 629 """
490 DefaultMode = E5PathPickerModes.NoMode
491
492 editTextChanged = pyqtSignal(str)
493 pathSelected = pyqtSignal(str)
494 aboutToShowPathPickerDialog = pyqtSignal()
495 pickerButtonClicked = pyqtSignal()
496
497 def __init__(self, parent=None): 630 def __init__(self, parent=None):
498 """ 631 """
499 Constructor 632 Constructor
500 633
501 @param parent reference to the parent widget 634 @param parent reference to the parent widget
502 @type QWidget 635 @type QWidget
503 """ 636 """
504 super(E5ComboPathPicker, self).__init__(parent) 637 super(E5ComboPathPicker, self).__init__(parent, useLineEdit=False)
505
506 self.__mode = E5PathPicker.DefaultMode
507 self.__editorEnabled = True
508
509 self.__filters = ""
510 self.__defaultDirectory = ""
511 self.__windowTitle = ""
512
513 self.__layout = QHBoxLayout()
514 self.__layout.setSpacing(0)
515 self.__layout.setContentsMargins(0, 0, 0, 0)
516 self.setLayout(self.__layout)
517
518 self.__editor = E5ClearableComboBox(self, self.tr("Enter Path Name"))
519
520 self.__button = QToolButton(self)
521 self.__button.setToolButtonStyle(Qt.ToolButtonIconOnly)
522 self.__button.setIcon(UI.PixmapCache.getIcon("open.png"))
523
524 self.__layout.addWidget(self.__editor)
525 self.__layout.addWidget(self.__button)
526
527 self.__button.clicked.connect(self.__showPathPickerDialog)
528 self.__editor.editTextChanged.connect(self.editTextChanged)
529
530 self.setFocusProxy(self.__editor)
531 self.setFocusPolicy(Qt.StrongFocus)
532 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
533
534 self.__button.setEnabled(self.__mode != E5PathPickerModes.NoMode)
535
536 def setMode(self, mode):
537 """
538 Public method to set the path picker mode.
539
540 @param mode picker mode
541 @type E5PathPickerModes
542 """
543 assert mode in E5PathPickerModes
544
545 oldMode = self.__mode
546 self.__mode = mode
547
548 if mode != oldMode:
549 if mode != E5PathPickerModes.NoMode:
550 # set inactive text
551 if mode == E5PathPickerModes.OpenFilesMode:
552 self.__editor.setInactiveText(
553 self.tr("Enter Path Names separated by ';'"))
554 else:
555 self.__editor.setInactiveText(
556 self.tr("Enter Path Name"))
557 self.__button.setEnabled(self.__mode != E5PathPickerModes.NoMode)
558
559 def mode(self):
560 """
561 Public method to get the path picker mode.
562
563 @return path picker mode
564 @rtype E5PathPickerModes
565 """
566 return self.__mode
567
568 def setPickerEnabled(self, enable):
569 """
570 Public method to set the enabled state of the file dialog button.
571
572 @param enable flag indicating the enabled state
573 @type bool
574 """
575 self.__button.setEnabled(enable)
576
577 def isPickerEnabled(self):
578 """
579 Public method to get the file dialog button enabled state.
580
581 @return flag indicating the enabled state
582 @rtype bool
583 """
584 return self.__button.isEnabled()
585
586 def clear(self):
587 """
588 Public method to clear the list of paths.
589 """
590 self.__editor.clear()
591
592 def clearEditText(self):
593 """
594 Public method to clear the current path.
595 """
596 self.__editor.clearEditText()
597
598 def setEditText(self, path):
599 """
600 Public method to set the current path.
601
602 @param path path to be set
603 @type str
604 """
605 if self.__mode == E5PathPickerModes.OpenFilesMode:
606 self.__editor.setEditText(path)
607 else:
608 self.__editor.setEditText(Utilities.toNativeSeparators(path))
609
610 def currentText(self):
611 """
612 Public method to get the current path.
613
614 @return current path
615 @rtype str
616 """
617 if self.__mode == E5PathPickerModes.OpenFilesMode:
618 return ";".join(
619 [Utilities.toNativeSeparators(path)
620 for path in self.__editor.currentText().split(";")])
621 else:
622 return os.path.expanduser(
623 Utilities.toNativeSeparators(self.__editor.currentText()))
624
625 def setPath(self, path):
626 """
627 Public method to set the current path.
628
629 @param path path to be set
630 @type str
631 """
632 self.setEditText(path)
633
634 def path(self):
635 """
636 Public method to get the current path.
637
638 @return current path
639 @rtype str
640 """
641 return self.currentText()
642
643 def paths(self):
644 """
645 Public method to get the list of entered paths.
646
647 @return entered paths
648 @rtype list of str
649 """
650 if self.__mode == E5PathPickerModes.OpenFilesMode:
651 return self.path().split(";")
652 else:
653 return [self.path()]
654
655 def firstPath(self):
656 """
657 Public method to get the first path of a list of entered paths.
658
659 @return first path
660 @rtype str
661 """
662 if self.__mode == E5PathPickerModes.OpenFilesMode:
663 return self.path().split(";")[0]
664 else:
665 return self.path()
666
667 def lastPath(self):
668 """
669 Public method to get the last path of a list of entered paths.
670
671 @return first path
672 @rtype str
673 """
674 if self.__mode == E5PathPickerModes.OpenFilesMode:
675 return self.path().split(";")[-1]
676 else:
677 return self.path()
678
679 def addItems(self, pathsList):
680 """
681 Public method to add paths to the current list.
682
683 @param pathsList list of paths to add
684 @type list of str
685 """
686 self.__editor.addItems(pathsList)
687
688 def addItem(self, path):
689 """
690 Public method to add a paths to the current list.
691
692 @param path path to add
693 @type str
694 """
695 self.__editor.addItem(path)
696
697 def setPathsList(self, pathsList):
698 """
699 Public method to set the paths list.
700
701 @param pathsList list of paths
702 @type list of str
703 """
704 self.clear()
705 self.addItems(pathsList)
706
707 def setCurrentIndex(self, index):
708 """
709 Public slot to set the current index.
710
711 @param index index of the item to set current
712 @type int
713 """
714 self.__editor.setCurrentIndex(index)
715
716 def setEditorEnabled(self, enable):
717 """
718 Public method to set the path editor's enabled state.
719
720 @param enable flag indicating the enable state
721 @type bool
722 """
723 if enable != self.__editorEnabled:
724 self.__editorEnabled = enable
725 self.__editor.setEnabled(enable)
726
727 def editorEnabled(self):
728 """
729 Public method to get the path editor's enabled state.
730
731 @return flag indicating the enabled state
732 @rtype bool
733 """
734 return self.__editorEnabled
735
736 def setDefaultDirectory(self, directory):
737 """
738 Public method to set the default directory.
739
740 @param directory default directory
741 @type str
742 """
743 self.__defaultDirectory = directory
744
745 def defaultDirectory(self):
746 """
747 Public method to get the default directory.
748
749 @return default directory
750 @rtype str
751 """
752 return self.__defaultDirectory
753
754 def setWindowTitle(self, title):
755 """
756 Public method to set the path picker dialog window title.
757
758 @param title window title
759 @type str
760 """
761 self.__windowTitle = title
762
763 def windowTitle(self):
764 """
765 Public method to get the path picker dialog's window title.
766
767 @return window title
768 @rtype str
769 """
770 return self.__windowTitle
771
772 def setFilters(self, filters):
773 """
774 Public method to set the filters for the path picker dialog.
775
776 Note: Multiple filters must be separated by ';;'.
777
778 @param filters string containing the file filters
779 @type str
780 """
781 self.__filters = filters
782
783 def filters(self):
784 """
785 Public methods to get the filter string.
786
787 @return filter string
788 @rtype str
789 """
790 return self.__filters
791
792 def setButtonToolTip(self, tooltip):
793 """
794 Public method to set the tool button tool tip.
795
796 @param tooltip text to be set as a tool tip
797 @type str
798 """
799 self.__button.setToolTip(tooltip)
800
801 def buttonToolTip(self):
802 """
803 Public method to get the tool button tool tip.
804
805 @return tool tip text
806 @rtype str
807 """
808 return self.__button.toolTip()
809
810 def setEditorToolTip(self, tooltip):
811 """
812 Public method to set the editor tool tip.
813
814 @param tooltip text to be set as a tool tip
815 @type str
816 """
817 self.__editor.setToolTip(tooltip)
818
819 def editorToolTip(self):
820 """
821 Public method to get the editor tool tip.
822
823 @return tool tip text
824 @rtype str
825 """
826 return self.__editor.toolTip()
827
828 def setInsertPolicy(self, policy):
829 """
830 Public method to set the insertion policy of the combo box.
831
832 @param policy insertion policy
833 @type QComboBox.InsertPolicy
834 """
835 self.__editor.setInsertPolicy(policy)
836
837 def setSizeAdjustPolicy(self, policy):
838 """
839 Public method to set the size adjust policy of the combo box.
840
841 @param policy size adjust policy
842 @type QComboBox.SizeAdjustPolicy
843 """
844 self.__editor.setSizeAdjustPolicy(policy)
845
846 def __showPathPickerDialog(self):
847 """
848 Private slot to show the path picker dialog.
849 """
850 if self.__mode == E5PathPickerModes.NoMode:
851 return
852
853 if self.__mode == E5PathPickerModes.CustomMode:
854 self.pickerButtonClicked.emit()
855 return
856
857 self.aboutToShowPathPickerDialog.emit()
858
859 windowTitle = self.__windowTitle
860 if not windowTitle:
861 if self.__mode == E5PathPickerModes.OpenFileMode:
862 windowTitle = self.tr("Choose a file to open")
863 elif self.__mode == E5PathPickerModes.OpenFilesMode:
864 windowTitle = self.tr("Choose files to open")
865 elif self.__mode == E5PathPickerModes.SaveFileMode:
866 windowTitle = self.tr("Choose a file to save")
867 elif self.__mode == E5PathPickerModes.DirectoryMode:
868 windowTitle = self.tr("Choose a directory")
869
870 directory = self.__editor.currentText()
871 if not directory and self.__defaultDirectory:
872 directory = self.__defaultDirectory
873 if self.__mode == E5PathPickerModes.OpenFilesMode:
874 directory = os.path.expanduser(directory.split(";")[0])
875 else:
876 directory = os.path.expanduser(directory)
877 if not os.path.isabs(directory) and self.__defaultDirectory:
878 directory = os.path.join(self.__defaultDirectory, directory)
879 directory = Utilities.fromNativeSeparators(directory)
880
881 if self.__mode == E5PathPickerModes.OpenFileMode:
882 path = E5FileDialog.getOpenFileName(
883 self,
884 windowTitle,
885 directory,
886 self.__filters)
887 path = Utilities.toNativeSeparators(path)
888 elif self.__mode == E5PathPickerModes.OpenFilesMode:
889 paths = E5FileDialog.getOpenFileNames(
890 self,
891 windowTitle,
892 directory,
893 self.__filters)
894 path = ";".join([Utilities.toNativeSeparators(path)
895 for path in paths])
896 elif self.__mode == E5PathPickerModes.SaveFileMode:
897 path = E5FileDialog.getSaveFileName(
898 self,
899 windowTitle,
900 directory,
901 self.__filters,
902 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
903 path = Utilities.toNativeSeparators(path)
904 elif self.__mode == E5PathPickerModes.SaveFileEnsureExtensionMode:
905 path, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
906 self,
907 windowTitle,
908 directory,
909 self.__filters,
910 None,
911 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
912 path = Utilities.toNativeSeparators(path)
913 if path:
914 ext = QFileInfo(path).suffix()
915 if not ext:
916 ex = selectedFilter.split("(*")[1].split(")")[0]
917 if ex:
918 path += ex
919 elif self.__mode == E5PathPickerModes.DirectoryMode:
920 path = E5FileDialog.getExistingDirectory(
921 self,
922 windowTitle,
923 directory,
924 E5FileDialog.Options(E5FileDialog.ShowDirsOnly))
925 path = Utilities.toNativeSeparators(path)
926 while path.endswith(os.sep):
927 path = path[:-1]
928
929 if path:
930 self.__editor.setEditText(path)
931 self.pathSelected.emit(path)

eric ide

mercurial