25 |
25 |
26 |
26 |
27 def usage(appinfo, optlen=12): |
27 def usage(appinfo, optlen=12): |
28 """ |
28 """ |
29 Module function to show the usage information. |
29 Module function to show the usage information. |
30 |
30 |
31 @param appinfo dictionary describing the application |
31 @param appinfo dictionary describing the application |
32 @param optlen length of the field for the commandline option (integer) |
32 @param optlen length of the field for the commandline option (integer) |
33 """ |
33 """ |
34 options = [ |
34 options = [ |
35 ("--version", "show the program's version number and exit"), |
35 ("--version", "show the program's version number and exit"), |
36 ("-h, --help", "show this help message and exit") |
36 ("-h, --help", "show this help message and exit"), |
37 ] |
37 ] |
38 options.extend(appinfo["options"]) |
38 options.extend(appinfo["options"]) |
39 |
39 |
40 print("""\n""" |
40 print( |
41 """Usage: {bin} [OPTIONS] {arg}\n""" |
41 """\n""" |
42 """\n""" |
42 """Usage: {bin} [OPTIONS] {arg}\n""" |
43 """{name} - {description}\n""" |
43 """\n""" |
44 """\n""" |
44 """{name} - {description}\n""" |
45 """Options:""".format(**appinfo)) |
45 """\n""" |
|
46 """Options:""".format(**appinfo) |
|
47 ) |
46 for opt in options: |
48 for opt in options: |
47 print(" {0} {1}".format(opt[0].ljust(optlen), opt[1])) |
49 print(" {0} {1}".format(opt[0].ljust(optlen), opt[1])) |
48 sys.exit(0) |
50 sys.exit(0) |
49 |
51 |
50 |
52 |
51 def version(appinfo): |
53 def version(appinfo): |
52 """ |
54 """ |
53 Module function to show the version information. |
55 Module function to show the version information. |
54 |
56 |
55 @param appinfo dictionary describing the application |
57 @param appinfo dictionary describing the application |
56 """ |
58 """ |
57 print("""\n""" |
59 print( |
58 """{name} {version}\n""" |
60 """\n""" |
59 """\n""" |
61 """{name} {version}\n""" |
60 """{description}\n""" |
62 """\n""" |
61 """\n""" |
63 """{description}\n""" |
62 """Copyright (c) 2002 - 2022 Detlev Offenbach""" |
64 """\n""" |
63 """ <detlev@die-offenbachs.de>\n""" |
65 """Copyright (c) 2002 - 2022 Detlev Offenbach""" |
64 """This is free software; see LICENSE.GPL3 for copying""" |
66 """ <detlev@die-offenbachs.de>\n""" |
65 """ conditions.\n""" |
67 """This is free software; see LICENSE.GPL3 for copying""" |
66 """There is NO warranty; not even for MERCHANTABILITY or FITNESS""" |
68 """ conditions.\n""" |
67 """ FOR A\n""" |
69 """There is NO warranty; not even for MERCHANTABILITY or FITNESS""" |
68 """PARTICULAR PURPOSE.""".format(**appinfo)) |
70 """ FOR A\n""" |
|
71 """PARTICULAR PURPOSE.""".format(**appinfo) |
|
72 ) |
69 sys.exit(0) |
73 sys.exit(0) |
70 |
74 |
71 |
75 |
72 def handleArgs(argv, appinfo): |
76 def handleArgs(argv, appinfo): |
73 """ |
77 """ |
74 Module function to handle the always present commandline options. |
78 Module function to handle the always present commandline options. |
75 |
79 |
76 @param argv list of commandline parameters (list of strings) |
80 @param argv list of commandline parameters (list of strings) |
77 @param appinfo dictionary describing the application |
81 @param appinfo dictionary describing the application |
78 @return index of the '--' option (integer). This is used to tell |
82 @return index of the '--' option (integer). This is used to tell |
79 the application, that all additional options don't belong to |
83 the application, that all additional options don't belong to |
80 the application. |
84 the application. |
81 """ |
85 """ |
82 ddindex = 30000 # arbitrarily large number |
86 ddindex = 30000 # arbitrarily large number |
83 args = { |
87 args = {"--version": version, "--help": usage, "-h": usage} |
84 "--version": version, |
88 if "--" in argv: |
85 "--help": usage, |
|
86 "-h": usage |
|
87 } |
|
88 if '--' in argv: |
|
89 ddindex = argv.index("--") |
89 ddindex = argv.index("--") |
90 for a in args: |
90 for a in args: |
91 if a in argv and argv.index(a) < ddindex: |
91 if a in argv and argv.index(a) < ddindex: |
92 args[a](appinfo) |
92 args[a](appinfo) |
93 return ddindex |
93 return ddindex |
105 trans = QTranslator(None) |
105 trans = QTranslator(None) |
106 for directory in dirs: |
106 for directory in dirs: |
107 loaded = trans.load(tn, directory) |
107 loaded = trans.load(tn, directory) |
108 if loaded: |
108 if loaded: |
109 return (trans, True) |
109 return (trans, True) |
110 |
110 |
111 print("Warning: translation file '" + tn + "'could not be loaded.") |
111 print("Warning: translation file '" + tn + "'could not be loaded.") |
112 print("Using default.") |
112 print("Using default.") |
113 return (None, False) |
113 return (None, False) |
114 |
114 |
115 |
115 |
116 def initializeResourceSearchPath(application): |
116 def initializeResourceSearchPath(application): |
117 """ |
117 """ |
118 Module function to initialize the default mime source factory. |
118 Module function to initialize the default mime source factory. |
119 |
119 |
120 @param application reference to the application object |
120 @param application reference to the application object |
121 @type EricApplication |
121 @type EricApplication |
122 """ |
122 """ |
123 import Preferences |
123 import Preferences |
124 |
124 |
125 defaultIconPaths = getDefaultIconPaths(application) |
125 defaultIconPaths = getDefaultIconPaths(application) |
126 iconPaths = Preferences.getIcons("Path") |
126 iconPaths = Preferences.getIcons("Path") |
127 for iconPath in iconPaths: |
127 for iconPath in iconPaths: |
128 if iconPath: |
128 if iconPath: |
129 UI.PixmapCache.addSearchPath(iconPath) |
129 UI.PixmapCache.addSearchPath(iconPath) |
133 |
133 |
134 |
134 |
135 def getDefaultIconPaths(application): |
135 def getDefaultIconPaths(application): |
136 """ |
136 """ |
137 Module function to determine the default icon paths. |
137 Module function to determine the default icon paths. |
138 |
138 |
139 @param application reference to the application object |
139 @param application reference to the application object |
140 @type EricApplication |
140 @type EricApplication |
141 @return list of default icon paths |
141 @return list of default icon paths |
142 @rtype list of str |
142 @rtype list of str |
143 """ |
143 """ |
144 import Preferences |
144 import Preferences |
145 |
145 |
146 defaultIconsPath = Preferences.getIcons("DefaultIconsPath") |
146 defaultIconsPath = Preferences.getIcons("DefaultIconsPath") |
147 if defaultIconsPath == "automatic": |
147 if defaultIconsPath == "automatic": |
148 if application.usesDarkPalette(): |
148 if application.usesDarkPalette(): |
149 # dark desktop |
149 # dark desktop |
150 defaultIconsPath = "breeze-dark" |
150 defaultIconsPath = "breeze-dark" |
151 else: |
151 else: |
152 # light desktop |
152 # light desktop |
153 defaultIconsPath = "breeze-light" |
153 defaultIconsPath = "breeze-light" |
154 |
154 |
155 return [ |
155 return [ |
156 os.path.join(getConfig('ericIconDir'), defaultIconsPath), |
156 os.path.join(getConfig("ericIconDir"), defaultIconsPath), |
157 os.path.join(getConfig('ericIconDir'), defaultIconsPath, "languages"), |
157 os.path.join(getConfig("ericIconDir"), defaultIconsPath, "languages"), |
158 ] |
158 ] |
159 |
159 |
160 |
160 |
161 def setLibraryPaths(): |
161 def setLibraryPaths(): |
162 """ |
162 """ |
164 """ |
164 """ |
165 libPaths = ( |
165 libPaths = ( |
166 os.path.join(Globals.getPyQt6ModulesDirectory(), "plugins"), |
166 os.path.join(Globals.getPyQt6ModulesDirectory(), "plugins"), |
167 os.path.join(Globals.getPyQt6ModulesDirectory(), "Qt6", "plugins"), |
167 os.path.join(Globals.getPyQt6ModulesDirectory(), "Qt6", "plugins"), |
168 ) |
168 ) |
169 |
169 |
170 libraryPaths = QApplication.libraryPaths() |
170 libraryPaths = QApplication.libraryPaths() |
171 for libPath in libPaths: |
171 for libPath in libPaths: |
172 if os.path.exists(libPath): |
172 if os.path.exists(libPath): |
173 libPath = QDir.fromNativeSeparators(libPath) |
173 libPath = QDir.fromNativeSeparators(libPath) |
174 if libPath not in libraryPaths: |
174 if libPath not in libraryPaths: |
175 libraryPaths.insert(0, libPath) |
175 libraryPaths.insert(0, libPath) |
176 QApplication.setLibraryPaths(libraryPaths) |
176 QApplication.setLibraryPaths(libraryPaths) |
177 |
177 |
|
178 |
178 # the translator must not be deleted, therefore we save them here |
179 # the translator must not be deleted, therefore we save them here |
179 loaded_translators = {} |
180 loaded_translators = {} |
180 |
181 |
181 |
182 |
182 def loadTranslators(qtTransDir, app, translationFiles=()): |
183 def loadTranslators(qtTransDir, app, translationFiles=()): |
183 """ |
184 """ |
184 Module function to load all required translations. |
185 Module function to load all required translations. |
185 |
186 |
186 @param qtTransDir directory of the Qt translations files (string) |
187 @param qtTransDir directory of the Qt translations files (string) |
187 @param app reference to the application object (QApplication) |
188 @param app reference to the application object (QApplication) |
188 @param translationFiles tuple of additional translations to |
189 @param translationFiles tuple of additional translations to |
189 be loaded (tuple of strings) |
190 be loaded (tuple of strings) |
190 @return the requested locale (string) |
191 @return the requested locale (string) |
191 """ |
192 """ |
192 import Preferences |
193 import Preferences |
193 |
194 |
194 global loaded_translators |
195 global loaded_translators |
195 |
196 |
196 translations = ( |
197 translations = ( |
197 "qt", "qt_help", "qtbase", "qtmultimedia", "qtserialport", |
198 "qt", |
198 "qtwebengine", "qtwebsockets", "eric7" |
199 "qt_help", |
|
200 "qtbase", |
|
201 "qtmultimedia", |
|
202 "qtserialport", |
|
203 "qtwebengine", |
|
204 "qtwebsockets", |
|
205 "eric7", |
199 ) + translationFiles |
206 ) + translationFiles |
200 loc = Preferences.getUILanguage() |
207 loc = Preferences.getUILanguage() |
201 if loc is None: |
208 if loc is None: |
202 return "" |
209 return "" |
203 |
210 |
204 if loc == "System": |
211 if loc == "System": |
205 loc = QLocale.system().name() |
212 loc = QLocale.system().name() |
206 if loc != "C": |
213 if loc != "C": |
207 dirs = [getConfig('ericTranslationsDir'), Globals.getConfigDir()] |
214 dirs = [getConfig("ericTranslationsDir"), Globals.getConfigDir()] |
208 if qtTransDir is not None: |
215 if qtTransDir is not None: |
209 dirs.append(qtTransDir) |
216 dirs.append(qtTransDir) |
210 |
217 |
211 loca = loc |
218 loca = loc |
212 for tf in ["{0}_{1}".format(tr, loc) for tr in translations]: |
219 for tf in ["{0}_{1}".format(tr, loc) for tr in translations]: |
221 else: |
228 else: |
222 loc = None |
229 loc = None |
223 return loc |
230 return loc |
224 |
231 |
225 |
232 |
226 def simpleAppStartup(argv, appinfo, mwFactory, quitOnLastWindowClosed=True, |
233 def simpleAppStartup( |
227 app=None, raiseIt=True, installErrorHandler=False): |
234 argv, |
|
235 appinfo, |
|
236 mwFactory, |
|
237 quitOnLastWindowClosed=True, |
|
238 app=None, |
|
239 raiseIt=True, |
|
240 installErrorHandler=False, |
|
241 ): |
228 """ |
242 """ |
229 Module function to start up an application that doesn't need a specialized |
243 Module function to start up an application that doesn't need a specialized |
230 start up. |
244 start up. |
231 |
245 |
232 This function is used by all of eric's helper programs. |
246 This function is used by all of eric's helper programs. |
233 |
247 |
234 @param argv list of commandline parameters (list of strings) |
248 @param argv list of commandline parameters (list of strings) |
235 @param appinfo dictionary describing the application |
249 @param appinfo dictionary describing the application |
236 @param mwFactory factory function generating the main widget. This |
250 @param mwFactory factory function generating the main widget. This |
237 function must accept the following parameter. |
251 function must accept the following parameter. |
238 <dl> |
252 <dl> |
247 @param installErrorHandler flag indicating to install an error |
261 @param installErrorHandler flag indicating to install an error |
248 handler dialog (boolean) |
262 handler dialog (boolean) |
249 @return exit result (integer) |
263 @return exit result (integer) |
250 """ |
264 """ |
251 global application |
265 global application |
252 |
266 |
253 if "__PYVENV_LAUNCHER__" in os.environ: |
267 if "__PYVENV_LAUNCHER__" in os.environ: |
254 del os.environ["__PYVENV_LAUNCHER__"] |
268 del os.environ["__PYVENV_LAUNCHER__"] |
255 |
269 |
256 handleArgs(argv, appinfo) |
270 handleArgs(argv, appinfo) |
257 if app is None: |
271 if app is None: |
258 # set the library paths for plugins |
272 # set the library paths for plugins |
259 setLibraryPaths() |
273 setLibraryPaths() |
260 app = EricApplication(argv) |
274 app = EricApplication(argv) |
261 application = app |
275 application = app |
262 app.setQuitOnLastWindowClosed(quitOnLastWindowClosed) |
276 app.setQuitOnLastWindowClosed(quitOnLastWindowClosed) |
263 |
277 |
264 # the following code depends upon a valid application object |
278 # the following code depends upon a valid application object |
265 import Preferences |
279 import Preferences |
266 |
280 |
267 # set the application style sheet |
281 # set the application style sheet |
268 app.setStyleSheetFile(Preferences.getUI("StyleSheet")) |
282 app.setStyleSheetFile(Preferences.getUI("StyleSheet")) |
269 |
283 |
270 initializeResourceSearchPath(app) |
284 initializeResourceSearchPath(app) |
271 QApplication.setWindowIcon(UI.PixmapCache.getIcon("eric")) |
285 QApplication.setWindowIcon(UI.PixmapCache.getIcon("eric")) |
272 |
286 |
273 qtTransDir = Preferences.getQtTranslationsDir() |
287 qtTransDir = Preferences.getQtTranslationsDir() |
274 if not qtTransDir: |
288 if not qtTransDir: |
275 qtTransDir = QLibraryInfo.path( |
289 qtTransDir = QLibraryInfo.path(QLibraryInfo.LibraryPath.TranslationsPath) |
276 QLibraryInfo.LibraryPath.TranslationsPath) |
|
277 loadTranslators(qtTransDir, app, ("qscintilla",)) |
290 loadTranslators(qtTransDir, app, ("qscintilla",)) |
278 # qscintilla needed for web browser |
291 # qscintilla needed for web browser |
279 |
292 |
280 w = mwFactory(argv) |
293 w = mwFactory(argv) |
281 if w is None: |
294 if w is None: |
282 return 100 |
295 return 100 |
283 |
296 |
284 if quitOnLastWindowClosed: |
297 if quitOnLastWindowClosed: |
285 app.lastWindowClosed.connect(app.quit) |
298 app.lastWindowClosed.connect(app.quit) |
286 w.show() |
299 w.show() |
287 if raiseIt: |
300 if raiseIt: |
288 w.raise_() |
301 w.raise_() |
289 |
302 |
290 if installErrorHandler: |
303 if installErrorHandler: |
291 # generate a graphical error handler |
304 # generate a graphical error handler |
292 from EricWidgets import EricErrorMessage |
305 from EricWidgets import EricErrorMessage |
|
306 |
293 eMsg = EricErrorMessage.qtHandler() |
307 eMsg = EricErrorMessage.qtHandler() |
294 eMsg.setMinimumSize(600, 400) |
308 eMsg.setMinimumSize(600, 400) |
295 |
309 |
296 return app.exec() |
310 return app.exec() |
|
311 |
297 |
312 |
298 # |
313 # |
299 # eflag: noqa = M801 |
314 # eflag: noqa = M801 |