Grammalecte  Diff

Differences From Artifact [53d806905a]:

To Artifact [3573b30804]:


1
2
3
4
5
6
7
8

9
10
11
12
13
14

15
16
17
18
19
20
21

1
2
3
4
5
6

7

8
9



10
11
12
13
14
15
16
17
-






-
+
-


-
-
-
+







# -*- coding: utf8 -*-
# Text Formatter
# by Olivier R.
# License: MPL 2

import unohelper
import uno
import sys

import os
import traceback
import time

if sys.version_info.major == 3:
    import imp
import json

import tf_strings
import tf_options
import tf_tabrep
import helpers


264
265
266
267
268
269
270

271

272
273
274
275
276
277
278
260
261
262
263
264
265
266
267

268
269
270
271
272
273
274
275







+
-
+







        #self.bsel = self._addWidget('bsel', 'CheckBox', x2, self.xDialog.Height-40, nWidth-55, nHeight, Label = self.dUI.get('bsel', "#err"))
        self.bapply = self._addWidget('apply', 'Button', self.xDialog.Width-55, self.xDialog.Height-19, 50, 15, Label = self.dUI.get('apply', "#err"), \
                                      FontDescriptor = xFD2, TextColor = 0x004400)
        self.binfo = self._addWidget('info', 'Button', self.xDialog.Width-15, 0, 10, 9, Label = self.dUI.get('info', "#err"), \
                                     HelpText = self.dUI.get('infotitle', "#err"), FontDescriptor = xFDsmall, TextColor = 0x444444)

        # load configuration
        self.xGLOptionNode = helpers.getConfigSetting("/org.openoffice.Lightproof_${implname}/Other/", True)
        self._loadConfig()
        self._loadConfig("${lang}")

        ## container
        self.xContainer = self.xSvMgr.createInstanceWithContext('com.sun.star.awt.UnoControlDialog', self.ctx)
        self.xContainer.setModel(self.xDialog)
        self.xContainer.setVisible(False)
        self.xContainer.getControl('info').addActionListener(self)
        self.xContainer.getControl('info').setActionCommand('Info')
307
308
309
310
311
312
313
314

315
316
317
318
319
320
321
304
305
306
307
308
309
310

311
312
313
314
315
316
317
318







-
+







                else:
                    xDesktop = self.ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", self.ctx)
                    xElem = xDesktop.getCurrentComponent()

                    # Writer
                    if True:
                        # no selection
                        self._saveConfig()
                        self._saveConfig("${lang}")
                        self._replaceAll(xElem)
                    else:
                        # modify selected text only
                        pass
                        #xSelecList = xDoc.getCurrentSelection()
                        #xSel = xSelecList.getByIndex(0)

358
359
360
361
362
363
364
365

366
367
368
369
370
371






372
373

374
375
376



377
378
379
380
381
382
383








384
385


386
387
388
389
390


391
392
393
394
395


396
397
398

399
400

401
402
403
404
405
406
407
355
356
357
358
359
360
361

362
363





364
365
366
367
368
369
370
371
372
373


374
375
376







377
378
379
380
381
382
383
384
385

386
387
388
389
390


391
392
393
394
395


396
397



398


399
400
401
402
403
404
405
406







-
+

-
-
-
-
-
+
+
+
+
+
+


+

-
-
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+

-
+
+



-
-
+
+



-
-
+
+
-
-
-
+
-
-
+







                xWindow = xDoc.CurrentController.Frame.ContainerWindow
                MessageBox (xWindow, self.dUI.get('infomsg', "#err"), self.dUI.get('infotitle', "#err"))
            else:
                print("Wrong command: " + xActionEvent.ActionCommand)
        except:
            traceback.print_exc()

    def _loadConfig (self):
    def _loadConfig (self, sLang):
        try:
            if sys.version_info.major == 3:
                imp.reload(tf_options)
            else:
                reload(tf_options)
            self._setConfig(tf_options.dOpt)
            dOpt = tf_options.dDefaultOpt.copy()
            xChild = self.xGLOptionNode.getByName("o_"+sLang)
            sTFOptionsJSON = xChild.getPropertyValue("tf_options")
            if sTFOptionsJSON:
                dOpt.update(json.loads(sTFOptionsJSON))
            self._setConfig(dOpt)
        except:
            traceback.print_exc()
            self._setConfig(tf_options.dDefaultOpt)

    def _setConfig (self, d):
        try:
    def _setConfig (self, dOpt):
        for sKey, val in dOpt.items():
            try:
            for key, val in d.items():
                w = getattr(self, key)
                w.State = val
                if key in self.dCheckboxWidgets:
                    self._switchCheckBox(w)
        except:
            raise
                w = getattr(self, sKey)
                if w:
                    w.State = val
                    if sKey in self.dCheckboxWidgets:
                        self._switchCheckBox(w)
            except:
                traceback.print_exc()
                print("option", sKey, "not set.")

    def _saveConfig (self):
    def _saveConfig (self, sLang):
        "save options in LibreOffice profile"
        try:
            # create options dictionary
            dOpt = {}
            for key, lWidget in self.dCheckboxWidgets.items():
                w = getattr(self, key)
            for sKey, lWidget in self.dCheckboxWidgets.items():
                w = getattr(self, sKey)
                dOpt[w.Name] = w.State
                for w in lWidget:
                    dOpt[w.Name] = w.State
            # write file
            sExtPath = helpers.getAbsolutePathOf("/pythonpath/tf_options.py")
            # save option to LO profile as JSON string
            xChild = self.xGLOptionNode.getByName("o_"+sLang)
            if os.path.isfile(sExtPath):
                hOpt = open(sExtPath, "w")
                hOpt.write("dDefaultOpt = " + str(tf_options.dDefaultOpt) + "\n")
            xChild.setPropertyValue("tf_options", json.dumps(dOpt))
                hOpt.write("dOpt = " + str(dOpt))
                hOpt.close()
            self.xGLOptionNode.commitChanges()
        except:
            traceback.print_exc()

    def _switchCheckBox (self, wGroupCheckbox):
        for w in self.dCheckboxWidgets.get(wGroupCheckbox.Name, []):
            w.Enabled = wGroupCheckbox.State

684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
683
684
685
686
687
688
689

690
691
692
693
694
695
696







-







        try:
            for sPattern, sRepl, bRegex, bCaseSensitive in tf_tabrep.dTableRepl[sList]:
                n += self._replaceText(xElem, sPattern, sRepl, bRegex, bCaseSensitive)
        except:
            print("# Error with "+sList)
            traceback.print_exc()
        return n


    def _replaceText (self, xElem, sPattern, sRepl, bRegex, bCaseSensitive=False):
        try:
            xRD = xElem.createReplaceDescriptor()
            xRD.SearchString = sPattern
            xRD.ReplaceString = sRepl
            xRD.SearchRegularExpression = bRegex