eric7/EricWidgets/EricPathPicker.py

branch
eric7
changeset 9152
8a68afaf1ba2
parent 9024
640778fcd474
equal deleted inserted replaced
9151:8c5296fe3056 9152:8a68afaf1ba2
7 Module implementing a path picker widget. 7 Module implementing a path picker widget.
8 """ 8 """
9 9
10 import enum 10 import enum
11 import os 11 import os
12 12 import pathlib
13 from PyQt6.QtCore import pyqtSignal, Qt, QFileInfo, QCoreApplication, QDir 13
14 from PyQt6.QtCore import pyqtSignal, Qt, QCoreApplication, QDir
14 from PyQt6.QtWidgets import ( 15 from PyQt6.QtWidgets import (
15 QWidget, QHBoxLayout, QToolButton, QSizePolicy, QLineEdit, QComboBox 16 QWidget, QHBoxLayout, QToolButton, QSizePolicy, QLineEdit, QComboBox
16 ) 17 )
17 18
18 from . import EricFileDialog 19 from . import EricFileDialog
118 self.setSizePolicy(QSizePolicy.Policy.Expanding, 119 self.setSizePolicy(QSizePolicy.Policy.Expanding,
119 QSizePolicy.Policy.Preferred) 120 QSizePolicy.Policy.Preferred)
120 121
121 self.__button.setEnabled(self.__mode != EricPathPickerModes.NO_MODE) 122 self.__button.setEnabled(self.__mode != EricPathPickerModes.NO_MODE)
122 123
123 def __pathEdited(self, path): 124 def __pathEdited(self, fpath):
124 """ 125 """
125 Private slot handling editing of the path. 126 Private slot handling editing of the path.
126 127
127 @param path current text of the path line edit 128 @param fpath current text of the path line edit
128 @type str 129 @type str
129 """ 130 """
130 if self._completer and not self._completer.popup().isVisible(): 131 if self._completer and not self._completer.popup().isVisible():
131 self._completer.setRootPath(QDir.toNativeSeparators(path)) 132 self._completer.setRootPath(QDir.toNativeSeparators(fpath))
132 133
133 def setMode(self, mode): 134 def setMode(self, mode):
134 """ 135 """
135 Public method to set the path picker mode. 136 Public method to set the path picker mode.
136 137
234 if self.__lineEditKind: 235 if self.__lineEditKind:
235 return self._editor.text() 236 return self._editor.text()
236 else: 237 else:
237 return self._editor.currentText() 238 return self._editor.currentText()
238 239
239 def setText(self, path, toNative=True): 240 def setText(self, fpath, toNative=True):
240 """ 241 """
241 Public method to set the current path. 242 Public method to set the current path.
242 243
243 @param path path to be set 244 @param fpath path to be set
244 @type str 245 @type str
245 @param toNative flag indicating to convert the path into 246 @param toNative flag indicating to convert the path into
246 a native format 247 a native format
247 @type bool 248 @type bool
248 """ 249 """
249 if self.__mode in ( 250 if self.__mode in (
250 EricPathPickerModes.OPEN_FILES_MODE, 251 EricPathPickerModes.OPEN_FILES_MODE,
251 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE, 252 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
252 ): 253 ):
253 self._setEditorText(path) 254 self._setEditorText(fpath)
254 else: 255 else:
255 if toNative: 256 if toNative:
256 path = QDir.toNativeSeparators(path) 257 fpath = QDir.toNativeSeparators(fpath)
257 self._setEditorText(path) 258 self._setEditorText(fpath)
258 if self._completer: 259 if self._completer:
259 self._completer.setRootPath(path) 260 self._completer.setRootPath(fpath)
260 261
261 def text(self, toNative=True): 262 def text(self, toNative=True):
262 """ 263 """
263 Public method to get the current path. 264 Public method to get the current path.
264 265
272 EricPathPickerModes.OPEN_FILES_MODE, 273 EricPathPickerModes.OPEN_FILES_MODE,
273 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE, 274 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
274 ): 275 ):
275 if toNative: 276 if toNative:
276 return ";".join( 277 return ";".join(
277 [QDir.toNativeSeparators(path) 278 [QDir.toNativeSeparators(fpath)
278 for path in self._editorText().split(";")]) 279 for fpath in self._editorText().split(";")])
279 else: 280 else:
280 return self._editorText() 281 return self._editorText()
281 else: 282 else:
282 if toNative: 283 if toNative:
283 return os.path.expanduser( 284 return os.path.expanduser(
284 QDir.toNativeSeparators(self._editorText())) 285 QDir.toNativeSeparators(self._editorText()))
285 else: 286 else:
286 return os.path.expanduser(self._editorText()) 287 return os.path.expanduser(self._editorText())
287 288
288 def setEditText(self, path, toNative=True): 289 def setEditText(self, fpath, toNative=True):
289 """ 290 """
290 Public method to set the current path. 291 Public method to set the current path.
291 292
292 @param path path to be set 293 @param fpath path to be set
293 @type str 294 @type str
294 @param toNative flag indicating to convert the path into 295 @param toNative flag indicating to convert the path into
295 a native format 296 a native format
296 @type bool 297 @type bool
297 """ 298 """
298 self.setText(path, toNative=toNative) 299 self.setText(fpath, toNative=toNative)
299 300
300 def currentText(self, toNative=True): 301 def currentText(self, toNative=True):
301 """ 302 """
302 Public method to get the current path. 303 Public method to get the current path.
303 304
307 @return current path 308 @return current path
308 @rtype str 309 @rtype str
309 """ 310 """
310 return self.text(toNative=toNative) 311 return self.text(toNative=toNative)
311 312
312 def setPath(self, path, toNative=True): 313 def setPath(self, fpath, toNative=True):
313 """ 314 """
314 Public method to set the current path. 315 Public method to set the current path.
315 316
316 @param path path to be set 317 @param fpath path to be set
317 @type str 318 @type str
318 @param toNative flag indicating to convert the path into 319 @param toNative flag indicating to convert the path into
319 a native format 320 a native format
320 @type bool 321 @type bool
321 """ 322 """
322 self.setText(path, toNative=toNative) 323 self.setText(fpath, toNative=toNative)
323 324
324 def path(self, toNative=True): 325 def path(self, toNative=True):
325 """ 326 """
326 Public method to get the current path. 327 Public method to get the current path.
327 328
539 if not os.path.isabs(directory) and self.__defaultDirectory: 540 if not os.path.isabs(directory) and self.__defaultDirectory:
540 directory = os.path.join(self.__defaultDirectory, directory) 541 directory = os.path.join(self.__defaultDirectory, directory)
541 directory = QDir.fromNativeSeparators(directory) 542 directory = QDir.fromNativeSeparators(directory)
542 543
543 if self.__mode == EricPathPickerModes.OPEN_FILE_MODE: 544 if self.__mode == EricPathPickerModes.OPEN_FILE_MODE:
544 path = EricFileDialog.getOpenFileName( 545 fpath = EricFileDialog.getOpenFileName(
545 self, 546 self,
546 windowTitle, 547 windowTitle,
547 directory, 548 directory,
548 self.__filters) 549 self.__filters)
549 path = QDir.toNativeSeparators(path) 550 fpath = QDir.toNativeSeparators(fpath)
550 elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE: 551 elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE:
551 paths = EricFileDialog.getOpenFileNames( 552 fpaths = EricFileDialog.getOpenFileNames(
552 self, 553 self,
553 windowTitle, 554 windowTitle,
554 directory, 555 directory,
555 self.__filters) 556 self.__filters)
556 path = ";".join([QDir.toNativeSeparators(path) 557 fpath = ";".join([QDir.toNativeSeparators(fpath)
557 for path in paths]) 558 for fpath in fpaths])
558 elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE: 559 elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE:
559 paths = EricFileDialog.getOpenFileAndDirNames( 560 fpaths = EricFileDialog.getOpenFileAndDirNames(
560 self, 561 self,
561 windowTitle, 562 windowTitle,
562 directory, 563 directory,
563 self.__filters) 564 self.__filters)
564 path = ";".join([QDir.toNativeSeparators(path) 565 fpath = ";".join([QDir.toNativeSeparators(fpath)
565 for path in paths]) 566 for fpath in fpaths])
566 elif self.__mode == EricPathPickerModes.SAVE_FILE_MODE: 567 elif self.__mode == EricPathPickerModes.SAVE_FILE_MODE:
567 path = EricFileDialog.getSaveFileName( 568 fpath = EricFileDialog.getSaveFileName(
568 self, 569 self,
569 windowTitle, 570 windowTitle,
570 directory, 571 directory,
571 self.__filters, 572 self.__filters,
572 EricFileDialog.DontConfirmOverwrite) 573 EricFileDialog.DontConfirmOverwrite)
573 path = QDir.toNativeSeparators(path) 574 fpath = QDir.toNativeSeparators(fpath)
574 elif ( 575 elif (
575 self.__mode == EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE 576 self.__mode == EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE
576 ): 577 ):
577 path, selectedFilter = EricFileDialog.getSaveFileNameAndFilter( 578 fpath, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
578 self, 579 self,
579 windowTitle, 580 windowTitle,
580 directory, 581 directory,
581 self.__filters, 582 self.__filters,
582 None, 583 None,
583 EricFileDialog.DontConfirmOverwrite) 584 EricFileDialog.DontConfirmOverwrite)
584 path = QDir.toNativeSeparators(path) 585 fpath = pathlib.Path(fpath)
585 if path: 586 if not fpath.suffix:
586 ext = QFileInfo(path).suffix() 587 ex = selectedFilter.split("(*")[1].split(")")[0]
587 if not ext: 588 if ex:
588 ex = selectedFilter.split("(*")[1].split(")")[0] 589 fpath = fpath.with_suffix(ex)
589 if ex:
590 path += ex
591 elif self.__mode == EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE: 590 elif self.__mode == EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE:
592 path = EricFileDialog.getSaveFileName( 591 fpath = EricFileDialog.getSaveFileName(
593 self, 592 self,
594 windowTitle, 593 windowTitle,
595 directory, 594 directory,
596 self.__filters) 595 self.__filters)
597 path = QDir.toNativeSeparators(path) 596 fpath = QDir.toNativeSeparators(fpath)
598 elif self.__mode == EricPathPickerModes.DIRECTORY_MODE: 597 elif self.__mode == EricPathPickerModes.DIRECTORY_MODE:
599 path = EricFileDialog.getExistingDirectory( 598 fpath = EricFileDialog.getExistingDirectory(
600 self, 599 self,
601 windowTitle, 600 windowTitle,
602 directory, 601 directory,
603 EricFileDialog.ShowDirsOnly) 602 EricFileDialog.ShowDirsOnly)
604 path = QDir.toNativeSeparators(path) 603 fpath = QDir.toNativeSeparators(fpath)
605 while path.endswith(os.sep): 604 while fpath.endswith(os.sep):
606 path = path[:-1] 605 fpath = fpath[:-1]
607 elif self.__mode == EricPathPickerModes.DIRECTORY_SHOW_FILES_MODE: 606 elif self.__mode == EricPathPickerModes.DIRECTORY_SHOW_FILES_MODE:
608 path = EricFileDialog.getExistingDirectory( 607 fpath = EricFileDialog.getExistingDirectory(
609 self, 608 self,
610 windowTitle, 609 windowTitle,
611 directory, 610 directory,
612 EricFileDialog.DontUseNativeDialog) 611 EricFileDialog.DontUseNativeDialog)
613 path = QDir.toNativeSeparators(path) 612 fpath = QDir.toNativeSeparators(fpath)
614 while path.endswith(os.sep): 613 while fpath.endswith(os.sep):
615 path = path[:-1] 614 fpath = fpath[:-1]
616 615
617 if path: 616 if fpath:
618 self._setEditorText(path) 617 self._setEditorText(str(fpath))
619 self.pathSelected.emit(path) 618 self.pathSelected.emit(str(fpath))
620 619
621 def setReadOnly(self, readOnly): 620 def setReadOnly(self, readOnly):
622 """ 621 """
623 Public method to set the path picker to read only mode. 622 Public method to set the path picker to read only mode.
624 623
654 @param pathsList list of paths to add 653 @param pathsList list of paths to add
655 @type list of str 654 @type list of str
656 """ 655 """
657 self._editor.addItems(pathsList) 656 self._editor.addItems(pathsList)
658 657
659 def addItem(self, path): 658 def addItem(self, fpath):
660 """ 659 """
661 Public method to add a paths to the current list. 660 Public method to add a paths to the current list.
662 661
663 @param path path to add 662 @param fpath path to add
664 @type str 663 @type str
665 """ 664 """
666 self._editor.addItem(path) 665 self._editor.addItem(fpath)
667 666
668 def setPathsList(self, pathsList): 667 def setPathsList(self, pathsList):
669 """ 668 """
670 Public method to set the paths list. 669 Public method to set the paths list.
671 670

eric ide

mercurial