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