|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2004 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the single application server and client. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt5.QtCore import pyqtSignal |
|
13 |
|
14 from Toolbox.SingleApplication import ( |
|
15 SingleApplicationClient, SingleApplicationServer |
|
16 ) |
|
17 |
|
18 ########################################################################### |
|
19 ## define some module global stuff |
|
20 ########################################################################### |
|
21 |
|
22 SAFile = "eric6_trpreviewer" |
|
23 |
|
24 # define the protocol tokens |
|
25 SALoadForm = 'LoadForm' |
|
26 SALoadTranslation = 'LoadTranslation' |
|
27 |
|
28 |
|
29 class TRSingleApplicationServer(SingleApplicationServer): |
|
30 """ |
|
31 Class implementing the single application server embedded within the |
|
32 Translations Previewer. |
|
33 |
|
34 @signal loadForm(str) emitted to load a form file |
|
35 @signal loadTranslation(str, bool) emitted to load a translation file |
|
36 """ |
|
37 loadForm = pyqtSignal(str) |
|
38 loadTranslation = pyqtSignal(str, bool) |
|
39 |
|
40 def __init__(self, parent): |
|
41 """ |
|
42 Constructor |
|
43 |
|
44 @param parent parent widget (QWidget) |
|
45 """ |
|
46 SingleApplicationServer.__init__(self, SAFile) |
|
47 |
|
48 self.parent = parent |
|
49 |
|
50 def handleCommand(self, command, arguments): |
|
51 """ |
|
52 Public slot to handle the command sent by the client. |
|
53 |
|
54 @param command command sent by the client |
|
55 @type str |
|
56 @param arguments list of command arguments |
|
57 @type list of str |
|
58 """ |
|
59 if command == SALoadForm: |
|
60 self.__saLoadForm(arguments) |
|
61 |
|
62 elif command == SALoadTranslation: |
|
63 self.__saLoadTranslation(arguments) |
|
64 |
|
65 def __saLoadForm(self, fnames): |
|
66 """ |
|
67 Private method used to handle the "Load Form" command. |
|
68 |
|
69 @param fnames filenames of the forms to be loaded (list of strings) |
|
70 """ |
|
71 for fname in fnames: |
|
72 self.loadForm.emit(fname) |
|
73 |
|
74 def __saLoadTranslation(self, fnames): |
|
75 """ |
|
76 Private method used to handle the "Load Translation" command. |
|
77 |
|
78 @param fnames filenames of the translations to be loaded |
|
79 (list of strings) |
|
80 """ |
|
81 first = True |
|
82 for fname in fnames: |
|
83 self.loadTranslation.emit(fname, first) |
|
84 first = False |
|
85 |
|
86 |
|
87 class TRSingleApplicationClient(SingleApplicationClient): |
|
88 """ |
|
89 Class implementing the single application client of the Translations |
|
90 Previewer. |
|
91 """ |
|
92 def __init__(self): |
|
93 """ |
|
94 Constructor |
|
95 """ |
|
96 SingleApplicationClient.__init__(self, SAFile) |
|
97 |
|
98 def processArgs(self, args): |
|
99 """ |
|
100 Public method to process the command line args passed to the UI. |
|
101 |
|
102 @param args list of files to open |
|
103 """ |
|
104 # no args, return |
|
105 if args is None: |
|
106 return |
|
107 |
|
108 uiFiles = [] |
|
109 qmFiles = [] |
|
110 |
|
111 for arg in args: |
|
112 ext = os.path.splitext(arg)[1] |
|
113 ext = ext.lower() |
|
114 |
|
115 if ext == '.ui': |
|
116 uiFiles.append(arg) |
|
117 elif ext == '.qm': |
|
118 qmFiles.append(arg) |
|
119 |
|
120 self.sendCommand(SALoadForm, uiFiles) |
|
121 self.sendCommand(SALoadTranslation, qmFiles) |
|
122 |
|
123 self.disconnect() |