src/eric7/EricWidgets/EricDirFileDialog.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2021 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to select files and directories simultaneously.
8 """
9
10 from PyQt6.QtCore import pyqtSlot, QItemSelection
11 from PyQt6.QtGui import QFileSystemModel
12 from PyQt6.QtWidgets import QFileDialog, QPushButton, QLineEdit, QTreeView
13
14
15 class EricDirFileDialog(QFileDialog):
16 """
17 Derived QFileDialog to select files and directories simultaneously.
18
19 For this purpose the none native file dialog is used.
20 """
21 def __init__(self, parent=None, caption="", directory="", filterStr=""):
22 """
23 Constructor
24
25 @param parent parent widget of the dialog
26 @type QWidget
27 @param caption window title of the dialog
28 @type str
29 @param directory working directory of the dialog
30 @type str
31 @param filterStr filter string for the dialog
32 @type str
33 """
34 self.__selectedFilesFolders = []
35
36 super().__init__(parent, caption, directory, filterStr)
37 self.setFileMode(QFileDialog.FileMode.ExistingFiles)
38
39 @pyqtSlot()
40 def exec(self):
41 """
42 Public slot to finalize initialization and start the event loop.
43
44 @return accepted or rejected
45 @rtype QDialog.DialogCode
46 """
47 self.__openBtn = self.findChildren(QPushButton)[0]
48 self.__fileNameEdit = self.findChild(QLineEdit)
49 self.directoryEntered.connect(self.on_directoryEntered)
50 self.__tree = self.findChild(QTreeView)
51 self.__tree.selectionModel().selectionChanged.connect(
52 self.on_selectionChanged)
53
54 return QFileDialog.exec(self)
55
56 @pyqtSlot()
57 def accept(self):
58 """
59 Public slot to update the list with the selected files and folders.
60 """
61 # Avoid to close the dialog if only return is pressed
62 if not self.__openBtn.isEnabled():
63 return
64
65 self.__selectedFilesFolders = [
66 x.data(QFileSystemModel.Roles.FilePathRole)
67 for x in self.__tree.selectionModel().selectedIndexes()
68 if x.column() == 0]
69
70 self.hide()
71
72 @pyqtSlot(str)
73 def on_directoryEntered(self, directory):
74 """
75 Private slot to reset selections if another directory was entered.
76
77 @param directory name of the directory entered
78 @type str
79 """
80 self.__tree.selectionModel().clear()
81 self.__fileNameEdit.clear()
82 self.__openBtn.setEnabled(False)
83
84 @pyqtSlot(QItemSelection, QItemSelection)
85 def on_selectionChanged(self, selected, deselected):
86 """
87 Private method to determine the selected files and folders and update
88 the line edit.
89
90 @param selected newly selected entries
91 @type QItemSelection
92 @param deselected deselected entries
93 @type QItemSelection
94 """
95 selectedItems = self.__tree.selectionModel().selectedIndexes()
96 if self.__tree.rootIndex() in selectedItems or selectedItems == []:
97 return
98
99 selectedFiles = [x.data(QFileSystemModel.Roles.FileNameRole)
100 for x in selectedItems if x.column() == 0]
101 enteredFiles = self.__fileNameEdit.text().split('"')
102 enteredFiles = [x.strip() for x in enteredFiles if x.strip()]
103
104 # Check if there is a directory in the selection. Then update the
105 # lineEdit.
106 for selectedFile in selectedFiles:
107 if selectedFile not in enteredFiles:
108 txt = '" "'.join(selectedFiles)
109 if len(selectedFiles) > 1:
110 txt = '"{0}"'.format(txt)
111 self.__fileNameEdit.setText(txt)
112 break
113
114 @staticmethod
115 def getOpenFileAndDirNames(parent=None, caption="", directory="",
116 filterStr="", options=None):
117 """
118 Static method to get the names of files and directories for opening it.
119
120 @param parent parent widget of the dialog
121 @type QWidget
122 @param caption window title of the dialog
123 @type str
124 @param directory working directory of the dialog
125 @type str
126 @param filterStr filter string for the dialog
127 @type str
128 @param options various options for the dialog
129 @type QFileDialog.Options
130 @return names of the selected files and folders
131 @rtype list of str
132 """
133 if options is None:
134 options = QFileDialog.Option(0)
135 options |= QFileDialog.Option.DontUseNativeDialog
136 dlg = EricDirFileDialog(parent, caption, directory, filterStr)
137 dlg.setOptions(options)
138 dlg.exec()
139
140 return dlg.__selectedFilesFolders

eric ide

mercurial