eric7/E5Gui/EricFileDialog.py

branch
eric7
changeset 8356
68ec9c3d4de5
parent 8319
ea11a3948f40
equal deleted inserted replaced
8355:8a7677a63c8d 8356:68ec9c3d4de5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing alternative functions for the QFileDialog static methods
8 to cope with distributor's usage of KDE wrapper dialogs for Qt file dialogs.
9 """
10
11 from PyQt6.QtWidgets import QFileDialog
12
13 import Globals
14
15 ##Options = QFileDialog.Options
16 Option = QFileDialog.Option
17
18 ShowDirsOnly = QFileDialog.Option.ShowDirsOnly
19 DontResolveSymlinks = QFileDialog.Option.DontResolveSymlinks
20 DontConfirmOverwrite = QFileDialog.Option.DontConfirmOverwrite
21 DontUseNativeDialog = QFileDialog.Option.DontUseNativeDialog
22 ReadOnly = QFileDialog.Option.ReadOnly
23 HideNameFilterDetails = QFileDialog.Option.HideNameFilterDetails
24 ##DontUseSheet = QFileDialog.Option.DontUseSheet
25 DontUseCustomDirectoryIcons = QFileDialog.Option.DontUseCustomDirectoryIcons
26
27
28 def __reorderFilter(filterStr, initialFilter=""):
29 """
30 Private function to reorder the file filter to cope with a KDE issue
31 introduced by distributor's usage of KDE file dialogs.
32
33 @param filterStr Qt file filter (string)
34 @param initialFilter initial filter (string)
35 @return the rearranged Qt file filter (string)
36 """
37 if initialFilter and not Globals.isMacPlatform():
38 fileFilters = filterStr.split(';;')
39 if len(fileFilters) < 10 and initialFilter in fileFilters:
40 fileFilters.remove(initialFilter)
41 fileFilters.insert(0, initialFilter)
42 return ";;".join(fileFilters)
43 else:
44 return filterStr
45
46
47 def getOpenFileName(parent=None, caption="", directory="",
48 filterStr="", options=None):
49 """
50 Module function to get the name of a file for opening it.
51
52 @param parent parent widget of the dialog (QWidget)
53 @param caption window title of the dialog (string)
54 @param directory working directory of the dialog (string)
55 @param filterStr filter string for the dialog (string)
56 @param options various options for the dialog (QFileDialog.Options)
57 @return name of file to be opened (string)
58 """
59 if options is None:
60 options = QFileDialog.Option(0)
61 if Globals.isLinuxPlatform():
62 options |= QFileDialog.Option.DontUseNativeDialog
63 return QFileDialog.getOpenFileName(
64 parent, caption, directory, filterStr, "", options)[0]
65
66
67 def getOpenFileNameAndFilter(parent=None, caption="", directory="",
68 filterStr="", initialFilter="",
69 options=None):
70 """
71 Module function to get the name of a file for opening it and the selected
72 file name filter.
73
74 @param parent parent widget of the dialog (QWidget)
75 @param caption window title of the dialog (string)
76 @param directory working directory of the dialog (string)
77 @param filterStr filter string for the dialog (string)
78 @param initialFilter initial filter for the dialog (string)
79 @param options various options for the dialog (QFileDialog.Options)
80 @return name of file to be opened and selected filter (string, string)
81 """
82 if options is None:
83 options = QFileDialog.Option(0)
84 if Globals.isLinuxPlatform():
85 options |= QFileDialog.Option.DontUseNativeDialog
86 newfilter = __reorderFilter(filterStr, initialFilter)
87 return QFileDialog.getOpenFileName(
88 parent, caption, directory, newfilter, initialFilter, options)
89
90
91 def getOpenFileNames(parent=None, caption="", directory="",
92 filterStr="", options=None):
93 """
94 Module function to get a list of names of files for opening.
95
96 @param parent parent widget of the dialog (QWidget)
97 @param caption window title of the dialog (string)
98 @param directory working directory of the dialog (string)
99 @param filterStr filter string for the dialog (string)
100 @param options various options for the dialog (QFileDialog.Options)
101 @return list of file names to be opened (list of string)
102 """
103 if options is None:
104 options = QFileDialog.Option(0)
105 if Globals.isLinuxPlatform():
106 options |= QFileDialog.Option.DontUseNativeDialog
107 return QFileDialog.getOpenFileNames(
108 parent, caption, directory, filterStr, "", options)[0]
109
110
111 def getOpenFileNamesAndFilter(parent=None, caption="", directory="",
112 filterStr="", initialFilter="",
113 options=None):
114 """
115 Module function to get a list of names of files for opening and the
116 selected file name filter.
117
118 @param parent parent widget of the dialog (QWidget)
119 @param caption window title of the dialog (string)
120 @param directory working directory of the dialog (string)
121 @param filterStr filter string for the dialog (string)
122 @param initialFilter initial filter for the dialog (string)
123 @param options various options for the dialog (QFileDialog.Options)
124 @return list of file names to be opened and selected filter
125 (list of string, string)
126 """
127 if options is None:
128 options = QFileDialog.Option(0)
129 if Globals.isLinuxPlatform():
130 options |= QFileDialog.Option.DontUseNativeDialog
131 newfilter = __reorderFilter(filterStr, initialFilter)
132 return QFileDialog.getOpenFileNames(
133 parent, caption, directory, newfilter, initialFilter, options)
134
135
136 def getSaveFileName(parent=None, caption="", directory="",
137 filterStr="", options=None):
138 """
139 Module function to get the name of a file for saving it.
140
141 @param parent parent widget of the dialog (QWidget)
142 @param caption window title of the dialog (string)
143 @param directory working directory of the dialog (string)
144 @param filterStr filter string for the dialog (string)
145 @param options various options for the dialog (QFileDialog.Options)
146 @return name of file to be saved (string)
147 """
148 if options is None:
149 options = QFileDialog.Option(0)
150 if Globals.isLinuxPlatform():
151 options |= QFileDialog.Option.DontUseNativeDialog
152 return QFileDialog.getSaveFileName(
153 parent, caption, directory, filterStr, "", options)[0]
154
155
156 def getSaveFileNameAndFilter(parent=None, caption="", directory="",
157 filterStr="", initialFilter="",
158 options=None):
159 """
160 Module function to get the name of a file for saving it and the selected
161 file name filter.
162
163 @param parent parent widget of the dialog (QWidget)
164 @param caption window title of the dialog (string)
165 @param directory working directory of the dialog (string)
166 @param filterStr filter string for the dialog (string)
167 @param initialFilter initial filter for the dialog (string)
168 @param options various options for the dialog (QFileDialog.Options)
169 @return name of file to be saved and selected filter (string, string)
170 """
171 if options is None:
172 options = QFileDialog.Option(0)
173 if Globals.isLinuxPlatform():
174 options |= QFileDialog.Option.DontUseNativeDialog
175 newfilter = __reorderFilter(filterStr, initialFilter)
176 return QFileDialog.getSaveFileName(
177 parent, caption, directory, newfilter, initialFilter, options)
178
179
180 def getExistingDirectory(parent=None, caption="",
181 directory="",
182 options=QFileDialog.Option.ShowDirsOnly):
183 """
184 Module function to get the name of a directory.
185
186 @param parent parent widget of the dialog (QWidget)
187 @param caption window title of the dialog (string)
188 @param directory working directory of the dialog (string)
189 @param options various options for the dialog (QFileDialog.Options)
190 @return name of selected directory (string)
191 """
192 if options is None:
193 options = QFileDialog.Option(0)
194 if Globals.isLinuxPlatform():
195 options |= QFileDialog.Option.DontUseNativeDialog
196 return QFileDialog.getExistingDirectory(parent, caption, directory,
197 options)

eric ide

mercurial