Grammalecte  Check-in [f87c8f4b2d]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:[lo] options dialog: code clarification
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | lo
Files: files | file ages | folders
SHA3-256:f87c8f4b2d4eac128b46afd8af3adf0520c9efadbc9aeb233f9782ab3810c6ec
User & Date: olr 2019-07-16 11:03:50
Context
2019-07-16
14:50
[lo] update update.xml check-in: 3948cb7d11 user: olr tags: lo, trunk, v1.2.1
11:03
[lo] options dialog: code clarification check-in: f87c8f4b2d user: olr tags: lo, trunk
2019-07-15
23:45
[fr] ajustements check-in: 9beeddfd4b user: olr tags: fr, trunk
Changes

Changes to gc_core/py/oxt/Grammalecte.py.

28
29
30
31
32
33
34
35
36
37
38
39
40

41
42
43
44
45
46
47
48
49
...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
        self.ImplementationName = "org.openoffice.comp.pyuno.Lightproof." + gce.pkg
        self.SupportedServiceNames = (self.ServiceName, )
        self.locales = []
        for i in gce.locales:
            l = gce.locales[i]
            self.locales.append(Locale(l[0], l[1], l[2]))
        self.locales = tuple(self.locales)
        xContext = uno.getComponentContext()
        # debug
        #helpers.startConsole()
        # init
        gce.load("Writer", "nInt")
        # GC options

        # opt_handler.load(xContext)
        dOpt = Options.load(xContext)
        gce.setOptions(dOpt)
        # dictionaries options
        self.loadUserDictionaries()
        # underlining options
        self.setWriterUnderliningStyle()
        # store for results of big paragraphs
        self.dResult = {}
................................................................................
                return self.dResult[nHashedVal]
        # WORKAROUND ->>>

        xRes.nBehindEndOfSentencePosition = xRes.nStartOfNextSentencePosition

        try:
            xRes.aErrors = tuple(gce.parse(rText, rLocale.Country))

            # ->>> WORKAROUND
            if xRes.nStartOfNextSentencePosition > 3000:
                self.dResult[nHashedVal] = xRes
                self.nRes += 1
                if self.nRes > self.nMaxRes:
                    del self.dResult[self.lLastRes.popleft()]
                    self.nRes = self.nMaxRes
                self.lLastRes.append(nHashedVal)
            # END OF WORKAROUND

        except Exception as e:
            if sys.version_info.major == 3:
                traceback.print_exc()

        return xRes

    def ignoreRule (self, rid, aLocale):
        gce.ignoreRule(rid)

    def resetIgnoreRules (self):
        gce.resetIgnoreRules()
................................................................................

    # Grammalecte
    def getSpellChecker (self):
        return gce.getSpellChecker()

    def loadUserDictionaries (self):
        try:
            xSettingNode = helpers.getConfigSetting("/org.openoffice.Lightproof_grammalecte/Other/", False)
            xChild = xSettingNode.getByName("o_${lang}")
            if xChild.getPropertyValue("use_personal_dic"):
                sJSON = xChild.getPropertyValue("personal_dic")
                if sJSON:
                    oSpellChecker = gce.getSpellChecker();
                    oSpellChecker.setPersonalDictionary(json.loads(sJSON))
        except:
            traceback.print_exc()

    def setWriterUnderliningStyle (self):
        try:
            xSettingNode = helpers.getConfigSetting("/org.openoffice.Lightproof_grammalecte/Other/", False)
            xChild = xSettingNode.getByName("o_${lang}")
            sLineType = xChild.getPropertyValue("line_type")
            bMulticolor = bool(xChild.getPropertyValue("line_multicolor"))
            gce.setWriterUnderliningStyle(sLineType, bMulticolor)
        except:
            traceback.print_exc()


g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(Grammalecte, "org.openoffice.comp.pyuno.Lightproof."+gce.pkg, ("com.sun.star.linguistic2.Proofreader",),)

# g_ImplementationHelper.addImplementation( opt_handler.LightproofOptionsEventHandler, \
#     "org.openoffice.comp.pyuno.LightproofOptionsEventHandler." + gce.pkg, ("com.sun.star.awt.XContainerWindowEventHandler",),)







<





>
|
|







 







<









<
|
<
|
<







 







|











|













28
29
30
31
32
33
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
...
108
109
110
111
112
113
114

115
116
117
118
119
120
121
122
123

124

125

126
127
128
129
130
131
132
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
        self.ImplementationName = "org.openoffice.comp.pyuno.Lightproof." + gce.pkg
        self.SupportedServiceNames = (self.ServiceName, )
        self.locales = []
        for i in gce.locales:
            l = gce.locales[i]
            self.locales.append(Locale(l[0], l[1], l[2]))
        self.locales = tuple(self.locales)

        # debug
        #helpers.startConsole()
        # init
        gce.load("Writer", "nInt")
        # GC options
        #xContext = uno.getComponentContext()
        #opt_handler.load(xContext)
        dOpt = Options.loadOptions("${lang}")
        gce.setOptions(dOpt)
        # dictionaries options
        self.loadUserDictionaries()
        # underlining options
        self.setWriterUnderliningStyle()
        # store for results of big paragraphs
        self.dResult = {}
................................................................................
                return self.dResult[nHashedVal]
        # WORKAROUND ->>>

        xRes.nBehindEndOfSentencePosition = xRes.nStartOfNextSentencePosition

        try:
            xRes.aErrors = tuple(gce.parse(rText, rLocale.Country))

            # ->>> WORKAROUND
            if xRes.nStartOfNextSentencePosition > 3000:
                self.dResult[nHashedVal] = xRes
                self.nRes += 1
                if self.nRes > self.nMaxRes:
                    del self.dResult[self.lLastRes.popleft()]
                    self.nRes = self.nMaxRes
                self.lLastRes.append(nHashedVal)
            # END OF WORKAROUND

        except:

            traceback.print_exc()

        return xRes

    def ignoreRule (self, rid, aLocale):
        gce.ignoreRule(rid)

    def resetIgnoreRules (self):
        gce.resetIgnoreRules()
................................................................................

    # Grammalecte
    def getSpellChecker (self):
        return gce.getSpellChecker()

    def loadUserDictionaries (self):
        try:
            xSettingNode = helpers.getConfigSetting("/org.openoffice.Lightproof_${implname}/Other/", False)
            xChild = xSettingNode.getByName("o_${lang}")
            if xChild.getPropertyValue("use_personal_dic"):
                sJSON = xChild.getPropertyValue("personal_dic")
                if sJSON:
                    oSpellChecker = gce.getSpellChecker();
                    oSpellChecker.setPersonalDictionary(json.loads(sJSON))
        except:
            traceback.print_exc()

    def setWriterUnderliningStyle (self):
        try:
            xSettingNode = helpers.getConfigSetting("/org.openoffice.Lightproof_${implname}/Other/", False)
            xChild = xSettingNode.getByName("o_${lang}")
            sLineType = xChild.getPropertyValue("line_type")
            bMulticolor = bool(xChild.getPropertyValue("line_multicolor"))
            gce.setWriterUnderliningStyle(sLineType, bMulticolor)
        except:
            traceback.print_exc()


g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(Grammalecte, "org.openoffice.comp.pyuno.Lightproof."+gce.pkg, ("com.sun.star.linguistic2.Proofreader",),)

# g_ImplementationHelper.addImplementation( opt_handler.LightproofOptionsEventHandler, \
#     "org.openoffice.comp.pyuno.LightproofOptionsEventHandler." + gce.pkg, ("com.sun.star.awt.XContainerWindowEventHandler",),)

Changes to gc_core/py/oxt/Options.py.

1
2
3
4
5
6
7
8
..
15
16
17
18
19
20
21
22














23
24
25


26
27
28





29
30
31

32
33
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
..
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
..
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110



111
112
113
114
115
116
117
...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172


173
174
# -*- coding: utf8 -*-
# Options Dialog
# by Olivier R.
# License: MPL 2

import unohelper
import uno
import traceback
................................................................................

try:
    import grammalecte.${lang} as gce
except:
    traceback.print_exc()


options = {}
















def load (ctx):


    try:
        oGCO = GC_Options(ctx)
        oGCO.load("${lang}")





    except:
        print("# Error. Unable to load options of language: ${lang}")
    return options



class GC_Options (unohelper.Base, XActionListener):

    def __init__ (self, ctx):
        self.ctx = ctx
        self.xSvMgr = self.ctx.ServiceManager
        self.xContainer = None
        #self.xNode = helpers.getConfigSetting("/org.openoffice.Lightproof_%s/Leaves"%pkg, True)
        self.xNode = helpers.getConfigSetting("/org.openoffice.Lightproof_grammalecte/Leaves", True)
        self.nSecret = 0
        
    def _addWidget (self, name, wtype, x, y, w, h, **kwargs):
        xWidget = self.xDialog.createInstance('com.sun.star.awt.UnoControl%sModel' % wtype)
        xWidget.Name = name
        xWidget.PositionX = x
        xWidget.PositionY = y
        xWidget.Width = w
        xWidget.Height = h
................................................................................
            setattr(xWidget, k, w)
        self.xDialog.insertByName(name, xWidget)
        return xWidget

    def run (self, sUI):
        try:
            dUI = op_strings.getUI(sUI)
            dUI2 = gce.gc_options.getUI(sUI)

            # fonts
            xFDTitle = uno.createUnoStruct("com.sun.star.awt.FontDescriptor")
            xFDTitle.Height = 9
            xFDTitle.Weight = uno.getConstantByName("com.sun.star.awt.FontWeight.BOLD")
            xFDTitle.Name = "Verdana"

................................................................................
            self.xDialog.Height = 400
            self.xDialog.Title = dUI.get('title', "#err")

            # build
            y = 0
            nWidth = self.xDialog.Width - 20
            nHeight = 10
            
            self.lxOptions = []

            for t in gce.gc_options.lStructOpt:
                x = 10
                y += 10
                self._addWidget(t[0], 'FixedLine', x, y, nWidth, nHeight, Label = dUI2.get(t[0], "#err")[0], FontDescriptor= xFDTitle)
                y += 3
                for lOptLine in t[1]:
                    x = 15
                    y += 10
                    n = len(lOptLine)
                    for sOpt in lOptLine:
                        w = self._addWidget(sOpt, 'CheckBox', x, y, nWidth/n, nHeight, State = options.get(sOpt, False), \
                                            Label = dUI2.get(sOpt, "#err")[0], HelpText = dUI2.get(sOpt, "#err")[1])
                        self.lxOptions.append(w)
                        x += nWidth / n
            
            self.xDialog.Height = y + 40

            xWindowSize = helpers.getWindowSize()
            self.xDialog.PositionX = int((xWindowSize.Width / 2) - (self.xDialog.Width / 2))
            self.xDialog.PositionY = int((xWindowSize.Height / 2) - (self.xDialog.Height / 2))

            but0 = self._addWidget('default', 'Button', 10, self.xDialog.Height-20, 50, 14, \
                                   Label = dUI.get('default', "#err"), FontDescriptor = xFDBut, TextColor = 0x000044)
            but1 = self._addWidget('apply', 'Button', self.xDialog.Width-115, self.xDialog.Height-20, 50, 14, \
                                   Label = dUI.get('apply', "#err"), FontDescriptor = xFDBut, TextColor = 0x004400)
            but2 = self._addWidget('cancel', 'Button', self.xDialog.Width-60, self.xDialog.Height-20, 50, 14,
                                   Label = dUI.get('cancel', "#err"), FontDescriptor = xFDBut, TextColor = 0x440000)




            # container
            self.xContainer = self.xSvMgr.createInstanceWithContext('com.sun.star.awt.UnoControlDialog', self.ctx)
            self.xContainer.setModel(self.xDialog)
            self.xContainer.getControl('default').addActionListener(self)
            self.xContainer.getControl('default').setActionCommand('Default')
            self.xContainer.getControl('apply').addActionListener(self)
................................................................................
        except:
            traceback.print_exc()

    # XActionListener
    def actionPerformed (self, xActionEvent):
        try:
            if xActionEvent.ActionCommand == 'Default':
                self._setDefault()
            elif xActionEvent.ActionCommand == 'Apply':
                self._save("fr")
                self.xContainer.endExecute()
            elif xActionEvent.ActionCommand == 'Cancel':
                self.xContainer.endExecute()
            else:
                print("Wrong command: " + xActionEvent.ActionCommand)
        except:
            traceback.print_exc()

    def _setDefault (self):
        dOpt = gce.gc_options.getOptions("Writer")
        for w in self.lxOptions:
            w.State = dOpt.get(w.Name, False)

    def load (self, sLang):
        try:
            xChild = self.xNode.getByName(sLang)
            dOpt = gce.gc_options.getOptions("Writer")
            for sKey in dOpt:
                sValue = xChild.getPropertyValue(sKey)
                if sValue == '':
                    if dOpt[sKey]:
                        sValue = 1
                    else:
                        sValue = 0
                options[sKey] = bool(int(sValue))
        except:
            traceback.print_exc()

    def _save (self, sLang):
        try:
            xChild = self.xNode.getByName(sLang)
            for w in self.lxOptions:
                sKey = w.Name
                bValue = w.State
                xChild.setPropertyValue(sKey, str(bValue))
                options[sKey] = bValue
                gce.setOptions(options)
            self.xNode.commitChanges()


        except:
            traceback.print_exc()
<







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>


<
>
>

<
<
>
>
>
>
>

<
<
>



>




<
<
<
|







 







|







 







|
|




|






|
|
|

|






|
|
|
|
|
|
>
>
>







 







|

|








|
|
|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<


<
<
<
<
<
<
<
<
>
>



1
2
3
4
5
6
7
..
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

38
39
40


41
42
43
44
45
46


47
48
49
50
51
52
53
54
55



56
57
58
59
60
61
62
63
..
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
..
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164















165
166








167
168
169
170

# Options Dialog
# by Olivier R.
# License: MPL 2

import unohelper
import uno
import traceback
................................................................................

try:
    import grammalecte.${lang} as gce
except:
    traceback.print_exc()


def loadOptions (sLang):
    "load options from Grammalecte and change them according to LibreOffice settings, returns a dictionary {option_name: boolean}"
    try:
        xNode = helpers.getConfigSetting("/org.openoffice.Lightproof_${implname}/Leaves", False)
        xChild = xNode.getByName(sLang)
        dOpt = gce.gc_options.getOptions("Writer")
        for sKey in dOpt:
            sValue = xChild.getPropertyValue(sKey)
            if sValue != '':
                dOpt[sKey] = bool(int(sValue))
        return dOpt
    except:
        print("# Error. Unable to load options of language:", sLang)
        traceback.print_exc()
        return gce.gc_options.getOptions("Writer")



def saveOptions (sLang, dOpt):
    "save options in LibreOffice profile"
    try:


        xNode = helpers.getConfigSetting("/org.openoffice.Lightproof_${implname}/Leaves", True)
        xChild = xNode.getByName(sLang)
        for sKey, value in dOpt.items():
            xChild.setPropertyValue(sKey, value)
        xNode.commitChanges()
    except:


        traceback.print_exc()


class GC_Options (unohelper.Base, XActionListener):

    def __init__ (self, ctx):
        self.ctx = ctx
        self.xSvMgr = self.ctx.ServiceManager
        self.xContainer = None




    def _addWidget (self, name, wtype, x, y, w, h, **kwargs):
        xWidget = self.xDialog.createInstance('com.sun.star.awt.UnoControl%sModel' % wtype)
        xWidget.Name = name
        xWidget.PositionX = x
        xWidget.PositionY = y
        xWidget.Width = w
        xWidget.Height = h
................................................................................
            setattr(xWidget, k, w)
        self.xDialog.insertByName(name, xWidget)
        return xWidget

    def run (self, sUI):
        try:
            dUI = op_strings.getUI(sUI)
            dOptionUI = gce.gc_options.getUI(sUI)

            # fonts
            xFDTitle = uno.createUnoStruct("com.sun.star.awt.FontDescriptor")
            xFDTitle.Height = 9
            xFDTitle.Weight = uno.getConstantByName("com.sun.star.awt.FontWeight.BOLD")
            xFDTitle.Name = "Verdana"

................................................................................
            self.xDialog.Height = 400
            self.xDialog.Title = dUI.get('title', "#err")

            # build
            y = 0
            nWidth = self.xDialog.Width - 20
            nHeight = 10

            self.lOptionWidgets = []

            for t in gce.gc_options.lStructOpt:
                x = 10
                y += 10
                self._addWidget(t[0], 'FixedLine', x, y, nWidth, nHeight, Label = dOptionUI.get(t[0], "#err")[0], FontDescriptor= xFDTitle)
                y += 3
                for lOptLine in t[1]:
                    x = 15
                    y += 10
                    n = len(lOptLine)
                    for sOpt in lOptLine:
                        w = self._addWidget(sOpt, 'CheckBox', x, y, nWidth/n, nHeight, \
                                            Label = dOptionUI.get(sOpt, "#err")[0], HelpText = dOptionUI.get(sOpt, "#err")[1])
                        self.lOptionWidgets.append(w)
                        x += nWidth / n

            self.xDialog.Height = y + 40

            xWindowSize = helpers.getWindowSize()
            self.xDialog.PositionX = int((xWindowSize.Width / 2) - (self.xDialog.Width / 2))
            self.xDialog.PositionY = int((xWindowSize.Height / 2) - (self.xDialog.Height / 2))

            self._addWidget('default', 'Button', 10, self.xDialog.Height-20, 50, 14, \
                            Label = dUI.get('default', "#err"), FontDescriptor = xFDBut, TextColor = 0x000044)
            self._addWidget('apply', 'Button', self.xDialog.Width-115, self.xDialog.Height-20, 50, 14, \
                            Label = dUI.get('apply', "#err"), FontDescriptor = xFDBut, TextColor = 0x004400)
            self._addWidget('cancel', 'Button', self.xDialog.Width-60, self.xDialog.Height-20, 50, 14,
                            Label = dUI.get('cancel', "#err"), FontDescriptor = xFDBut, TextColor = 0x440000)

            dOpt = loadOptions("${lang}")
            self._setWidgets(dOpt)

            # container
            self.xContainer = self.xSvMgr.createInstanceWithContext('com.sun.star.awt.UnoControlDialog', self.ctx)
            self.xContainer.setModel(self.xDialog)
            self.xContainer.getControl('default').addActionListener(self)
            self.xContainer.getControl('default').setActionCommand('Default')
            self.xContainer.getControl('apply').addActionListener(self)
................................................................................
        except:
            traceback.print_exc()

    # XActionListener
    def actionPerformed (self, xActionEvent):
        try:
            if xActionEvent.ActionCommand == 'Default':
                self._setWidgets(gce.gc_options.getOptions("Writer"))
            elif xActionEvent.ActionCommand == 'Apply':
                self._save("${lang}")
                self.xContainer.endExecute()
            elif xActionEvent.ActionCommand == 'Cancel':
                self.xContainer.endExecute()
            else:
                print("Wrong command: " + xActionEvent.ActionCommand)
        except:
            traceback.print_exc()

    # Other
    def _setWidgets (self, dOpt):
        for w in self.lOptionWidgets:
            w.State = dOpt.get(w.Name, False)
















    def _save (self, sLang):
        try:








            saveOptions(sLang, { w.Name: str(w.State)  for w in self.lOptionWidgets })
            gce.setOptions({ w.Name: bool(w.State)  for w in self.lOptionWidgets })
        except:
            traceback.print_exc()