8 """ |
8 """ |
9 |
9 |
10 import os |
10 import os |
11 import sys |
11 import sys |
12 |
12 |
13 from PyQt6.QtCore import pyqtSignal, pyqtSlot |
13 from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl |
|
14 from PyQt6.QtGui import QDesktopServices, QImageReader |
14 from PyQt6.QtWidgets import ( |
15 from PyQt6.QtWidgets import ( |
15 QWidget, QHeaderView, QApplication, QTreeWidgetItem |
16 QWidget, QHeaderView, QApplication, QTreeWidgetItem |
16 ) |
17 ) |
17 |
18 |
18 from EricWidgets.EricPathPicker import EricPathPickerModes |
19 from EricWidgets.EricPathPicker import EricPathPickerModes |
32 filename and the pathname. The file will be opened upon a double click |
33 filename and the pathname. The file will be opened upon a double click |
33 onto the respective entry of the list or by pressing the open button. |
34 onto the respective entry of the list or by pressing the open button. |
34 |
35 |
35 @signal sourceFile(str) emitted to open a file in the editor |
36 @signal sourceFile(str) emitted to open a file in the editor |
36 @signal designerFile(str) emitted to open a Qt-Designer file |
37 @signal designerFile(str) emitted to open a Qt-Designer file |
|
38 @signal linguistFile(str) emitted to open a Qt-Linguist (*.ts) file |
|
39 @signal trpreview([str]) emitted to preview Qt-Linguist (*.qm) files |
|
40 @signal pixmapFile(str) emitted to open a pixmap file |
|
41 @signal svgFile(str) emitted to open a SVG file |
|
42 @signal umlFile(str) emitted to open an eric UML file |
37 """ |
43 """ |
38 sourceFile = pyqtSignal(str) |
44 sourceFile = pyqtSignal(str) |
39 designerFile = pyqtSignal(str) |
45 designerFile = pyqtSignal(str) |
|
46 linguistFile = pyqtSignal(str) |
|
47 trpreview = pyqtSignal(list) |
|
48 pixmapFile = pyqtSignal(str) |
|
49 svgFile = pyqtSignal(str) |
|
50 umlFile = pyqtSignal(str) |
40 |
51 |
41 def __init__(self, project, parent=None): |
52 def __init__(self, project, parent=None): |
42 """ |
53 """ |
43 Constructor |
54 Constructor |
44 |
55 |
65 self.findButton.setIcon(UI.PixmapCache.getIcon("find")) |
76 self.findButton.setIcon(UI.PixmapCache.getIcon("find")) |
66 |
77 |
67 self.openButton.setIcon(UI.PixmapCache.getIcon("open")) |
78 self.openButton.setIcon(UI.PixmapCache.getIcon("open")) |
68 self.openButton.clicked.connect(self.__openFile) |
79 self.openButton.clicked.connect(self.__openFile) |
69 |
80 |
70 self.project = project |
81 self.__project = project |
|
82 self.__project.projectOpened.connect(self.__projectOpened) |
|
83 self.__project.projectClosed.connect(self.__projectClosed) |
71 self.extsepLabel.setText(os.extsep) |
84 self.extsepLabel.setText(os.extsep) |
72 |
85 |
73 self.__shouldStop = False |
86 self.__shouldStop = False |
74 |
87 |
75 self.fileNameEdit.returnPressed.connect(self.__searchFile) |
88 self.fileNameEdit.returnPressed.connect(self.__searchFile) |
85 @pyqtSlot() |
98 @pyqtSlot() |
86 def __openFile(self, itm=None): |
99 def __openFile(self, itm=None): |
87 """ |
100 """ |
88 Private slot to open a file. |
101 Private slot to open a file. |
89 |
102 |
90 It emits the signal sourceFile or designerFile depending on the |
103 It emits a signal depending on the file extension. |
91 file extension. |
|
92 |
104 |
93 @param itm item to be opened |
105 @param itm item to be opened |
94 @type QTreeWidgetItem |
106 @type QTreeWidgetItem |
95 """ |
107 """ |
96 if itm is None: |
108 if itm is None: |
97 itm = self.fileList.currentItem() |
109 itm = self.fileList.currentItem() |
98 if itm is not None: |
110 if itm is not None: |
99 fileName = itm.text(0) |
111 fileName = itm.text(0) |
100 filePath = itm.text(1) |
112 filePath = itm.text(1) |
|
113 fileExt = os.path.splitext(fileName)[1] |
|
114 fullName = os.path.join(filePath, fileName) |
101 |
115 |
102 # TODO: add more extensions and use mimetype |
116 if fileExt == ".ui": |
103 # - *.ts, *.qm->*.ts |
117 self.designerFile.emit(fullName) |
104 # Utilities.MimeTypes.isTextFile(filename) |
118 elif fileExt == ".ts": |
105 if fileName.endswith('.ui'): |
119 self.linguistFile.emit(fullName) |
106 self.designerFile.emit(os.path.join(filePath, fileName)) |
120 elif fileExt == ".qm": |
|
121 self.trpreview.emit([fullName]) |
|
122 elif fileExt in (".egj", ".e5g"): |
|
123 self.umlFile.emit(fullName) |
|
124 elif fileExt == ".svg": |
|
125 self.svgFile.emit(fullName) |
|
126 elif fileExt[1:] in QImageReader.supportedImageFormats(): |
|
127 self.pixmapFile.emit(fullName) |
107 else: |
128 else: |
108 self.sourceFile.emit(os.path.join(filePath, fileName)) |
129 if Utilities.MimeTypes.isTextFile(fullName): |
|
130 self.sourceFile.emit(fullName) |
|
131 else: |
|
132 QDesktopServices.openUrl(QUrl(fullName)) |
109 |
133 |
110 @pyqtSlot() |
134 @pyqtSlot() |
111 def __searchFile(self): |
135 def __searchFile(self): |
112 """ |
136 """ |
113 Private slot to handle the search. |
137 Private slot to handle the search. |
133 self.searchDirCheckBox.isChecked() and |
157 self.searchDirCheckBox.isChecked() and |
134 self.searchDirPicker.text() != "" |
158 self.searchDirPicker.text() != "" |
135 ): |
159 ): |
136 searchPaths.append(self.searchDirPicker.text()) |
160 searchPaths.append(self.searchDirPicker.text()) |
137 if self.projectCheckBox.isChecked(): |
161 if self.projectCheckBox.isChecked(): |
138 searchPaths.append(self.project.ppath) |
162 searchPaths.append(self.__project.getProjectPath()) |
139 if self.syspathCheckBox.isChecked(): |
163 if self.syspathCheckBox.isChecked(): |
140 searchPaths.extend(sys.path) |
164 searchPaths.extend(sys.path) |
141 |
165 |
142 self.fileList.clear() |
166 self.fileList.clear() |
143 locations = {} |
167 locations = {} |
162 QTreeWidgetItem(self.fileList, [fn, fp]) |
186 QTreeWidgetItem(self.fileList, [fn, fp]) |
163 QApplication.processEvents() |
187 QApplication.processEvents() |
164 |
188 |
165 del locations |
189 del locations |
166 self.stopButton.setEnabled(False) |
190 self.stopButton.setEnabled(False) |
|
191 self.fileList.sortItems(self.fileList.sortColumn(), |
|
192 Qt.SortOrder.AscendingOrder) |
167 self.fileList.header().resizeSections( |
193 self.fileList.header().resizeSections( |
168 QHeaderView.ResizeMode.ResizeToContents) |
194 QHeaderView.ResizeMode.ResizeToContents) |
|
195 self.fileList.header().resizeSection(0, self.width() // 2) |
169 self.fileList.header().setStretchLastSection(True) |
196 self.fileList.header().setStretchLastSection(True) |
170 |
197 |
171 self.findStatusLabel.setText(self.tr( |
198 self.findStatusLabel.setText(self.tr( |
172 "%n file(s) found", "", self.fileList.topLevelItemCount())) |
199 "%n file(s) found", "", self.fileList.topLevelItemCount())) |
173 |
200 |
228 @type QTreeWidgetItem |
255 @type QTreeWidgetItem |
229 """ |
256 """ |
230 self.openButton.setEnabled(current is not None) |
257 self.openButton.setEnabled(current is not None) |
231 |
258 |
232 @pyqtSlot() |
259 @pyqtSlot() |
|
260 def __projectOpened(self): |
|
261 """ |
|
262 Private slot to handle a project being opened. |
|
263 """ |
|
264 self.projectCheckBox.setEnabled(True) |
|
265 self.projectCheckBox.setChecked(True) |
|
266 |
|
267 @pyqtSlot() |
|
268 def __projectClosed(self): |
|
269 """ |
|
270 Private slot to handle a project being closed. |
|
271 """ |
|
272 self.projectCheckBox.setEnabled(False) |
|
273 self.projectCheckBox.setChecked(False) |
|
274 |
|
275 @pyqtSlot() |
233 def activate(self): |
276 def activate(self): |
234 """ |
277 """ |
235 Public slot to enable/disable the project checkbox. |
278 Public slot to enable/disable the project checkbox. |
236 """ |
279 """ |
237 if self.project and self.project.isOpen(): |
|
238 self.projectCheckBox.setEnabled(True) |
|
239 self.projectCheckBox.setChecked(True) |
|
240 else: |
|
241 self.projectCheckBox.setEnabled(False) |
|
242 self.projectCheckBox.setChecked(False) |
|
243 |
|
244 self.fileNameEdit.selectAll() |
280 self.fileNameEdit.selectAll() |
245 self.fileNameEdit.setFocus() |
281 self.fileNameEdit.setFocus() |