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