Grammalecte  Check-in [ac50c72aa8]

Overview
Comment:[lo] text formatter editor: save, import, export
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | lo
Files: files | file ages | folders
SHA3-256: ac50c72aa844add44ece9f5b9f9e13c30860a86234a807c539224df69ddcc2e3
User & Date: olr on 2020-12-16 17:54:28
Other Links: manifest | tags
Context
2020-12-16
22:49
[fr] ajustements check-in: 3db565e018 user: olr tags: trunk, fr
17:54
[lo] text formatter editor: save, import, export check-in: ac50c72aa8 user: olr tags: trunk, lo
2020-12-15
16:33
[lo] Text formatter editor (draft) check-in: c46f9e162f user: olr tags: trunk, new_feature, lo
Changes

Modified gc_core/py/oxt/OptionsDialog.xcs from [c4636de168] to [f223fd00f0].

31
32
33
34
35
36
37

38
39
40
41
42
43
44
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45







+







            <prop oor:name="personal_dic" oor:type="xs:string"><value></value></prop>
            <!-- graphic options -->
            <prop oor:name="line_multicolor" oor:type="xs:int"><value>1</value></prop>
            <prop oor:name="line_type" oor:type="xs:string"><value>WAVE</value></prop>
            <!-- misc options -->
            <prop oor:name="start_console" oor:type="xs:int"><value>0</value></prop>
            <prop oor:name="tf_options" oor:type="xs:string"><value></value></prop>
            <prop oor:name="tfe_rules" oor:type="xs:string"><value></value></prop>
        </group>
    </templates>

    <component>
        <group oor:name="Leaves">
            <node-ref oor:name="${lang}" oor:node-type="${lang}" />
        </group>

Modified gc_lang/fr/oxt/TextFormatter/TextFormatterEditor.py from [5352d7d61f] to [5d4886d661].

1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19

20
21
22

23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

20
21
22

23
24
25
26
27
28
29
30








+










-
+


-
+







# Text Formatter Editor
# by Olivier R.
# License: MPL 2

import unohelper
import uno
import traceback
import platform
import os
import json
import re

import helpers
import tfe_strings as ui
import grammalecte.graphspell as sc

from com.sun.star.awt import XActionListener
from com.sun.star.awt.grid import XGridSelectionListener

from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_YES_NO_CANCEL
# BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
# DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE
from com.sun.star.awt.MessageBoxType import INFOBOX, ERRORBOX # MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX
from com.sun.star.awt.MessageBoxType import INFOBOX, ERRORBOX, QUERYBOX # MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX


def MessageBox (xDocument, sMsg, sTitle, nBoxType=INFOBOX, nBoxButtons=BUTTONS_OK):
    xParentWin = xDocument.CurrentController.Frame.ContainerWindow
    ctx = uno.getComponentContext()
    xToolkit = ctx.ServiceManager.createInstanceWithContext("com.sun.star.awt.Toolkit", ctx)
    xMsgBox = xToolkit.createMessageBox(xParentWin, nBoxType, nBoxButtons, sTitle, sMsg)
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
175
176
177
178




179
180
181
182
183
184
185
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190







-
-
-
-
-
+
+
+
+
+



















-
-
-
-
-
+
+
+
+
+















+
+
+
+







        # Add
        self._addWidget("new_entry", 'FixedLine', nX, nY1, nWidth, nHeight, Label = ui.get("new_entry"), FontDescriptor = xFDTitle)

        self._addWidget('newnamelbl', 'FixedText', nX, nY1+10, 60, nHeight, Label = ui.get("name"))
        self._addWidget('newreplacelbl', 'FixedText', nX+65, nY1+10, 130, nHeight, Label = ui.get("replace"))
        self._addWidget('newbylbl', 'FixedText', nX+200, nY1+10, 100, nHeight, Label = ui.get("by"))

        self.xNewname = self._addWidget('newname', 'Edit', nX, nY1+19, 60, 10, FontDescriptor = xFDMono)
        self.xNewreplace = self._addWidget('newreplace', 'Edit', nX+65, nY1+19, 130, 10, FontDescriptor = xFDMono)
        self.xNewby = self._addWidget('newby', 'Edit', nX+200, nY1+19, 100, 10, FontDescriptor = xFDMono)
        self.xNewregex = self._addWidget('newregex', 'CheckBox', nX+305, nY1+21, 35, nHeight, Label = ui.get("regex"), HelpText=ui.get("regex_help"))
        self.xNewcasesens = self._addWidget('newcasesens', 'CheckBox', nX+340, nY1+21, 40, nHeight, Label = ui.get("casesens"), HelpText=ui.get("casesens_help"), State=True)
        self.xNewname = self._addWidget('newname', 'Edit', nX, nY1+20, 60, 10, FontDescriptor = xFDMono)
        self.xNewreplace = self._addWidget('newreplace', 'Edit', nX+65, nY1+20, 130, 10, FontDescriptor = xFDMono)
        self.xNewby = self._addWidget('newby', 'Edit', nX+200, nY1+20, 100, 10, FontDescriptor = xFDMono)
        self.xNewregex = self._addWidget('newregex', 'CheckBox', nX+305, nY1+22, 35, nHeight, Label = ui.get("regex"), HelpText=ui.get("regex_help"))
        self.xNewcasesens = self._addWidget('newcasesens', 'CheckBox', nX+340, nY1+22, 40, nHeight, Label = ui.get("casesens"), HelpText=ui.get("casesens_help"), State=True)

        self._addWidget('add', 'Button', self.xDialog.Width-50, nY1+31, 40, 11, Label = ui.get('add'))

        lColumns = [
            {"Title": ui.get("name"),     "ColumnWidth": 80},
            {"Title": ui.get("replace"),  "ColumnWidth": 140},
            {"Title": ui.get("by"),       "ColumnWidth": 140},
            {"Title": ui.get("regex"),    "ColumnWidth": 60},
            {"Title": ui.get("casesens"), "ColumnWidth": 60},
        ]
        self.xGridModel = self._addGrid("list_grid", nX, nY1+45, nWidth, 150, lColumns)

        # Modify
        self._addWidget("edit_entry", 'FixedLine', nX, nY2, nWidth, nHeight, Label = ui.get("edit_entry"), FontDescriptor = xFDTitle)

        self._addWidget('editnamelbl', 'FixedText', nX, nY2+10, 60, nHeight, Label = ui.get("name"))
        self._addWidget('editreplacelbl', 'FixedText', nX+65, nY2+10, 130, nHeight, Label = ui.get("replace"))
        self._addWidget('editbylbl', 'FixedText', nX+200, nY2+10, 100, nHeight, Label = ui.get("by"))

        self.xEditname = self._addWidget('editname', 'Edit', nX, nY2+19, 60, 10, FontDescriptor = xFDMono, Enabled = False)
        self.xEditreplace = self._addWidget('editreplace', 'Edit', nX+65, nY2+19, 130, 10, FontDescriptor = xFDMono, Enabled = False)
        self.xEditby = self._addWidget('editby', 'Edit', nX+200, nY2+19, 100, 10, FontDescriptor = xFDMono, Enabled = False)
        self.xEditregex = self._addWidget('editregex', 'CheckBox', nX+305, nY2+21, 35, nHeight, Label = ui.get("regex"), HelpText=ui.get("regex_help"), Enabled = False)
        self.xEditcasesens = self._addWidget('editcasesens', 'CheckBox', nX+340, nY2+21, 40, nHeight, Label = ui.get("casesens"), HelpText=ui.get("casesens_help"), Enabled = False)
        self.xEditname = self._addWidget('editname', 'Edit', nX, nY2+20, 60, 10, FontDescriptor = xFDMono, Enabled = False)
        self.xEditreplace = self._addWidget('editreplace', 'Edit', nX+65, nY2+20, 130, 10, FontDescriptor = xFDMono, Enabled = False)
        self.xEditby = self._addWidget('editby', 'Edit', nX+200, nY2+20, 100, 10, FontDescriptor = xFDMono, Enabled = False)
        self.xEditregex = self._addWidget('editregex', 'CheckBox', nX+305, nY2+22, 35, nHeight, Label = ui.get("regex"), HelpText=ui.get("regex_help"), Enabled = False)
        self.xEditcasesens = self._addWidget('editcasesens', 'CheckBox', nX+340, nY2+22, 40, nHeight, Label = ui.get("casesens"), HelpText=ui.get("casesens_help"), Enabled = False)

        self.xDeleteButton = self._addWidget('delete', 'Button', nX, nY2+31, 40, 11, Label = ui.get('delete'), TextColor = 0xAA0000, Enabled = False)
        self.xApplyButton = self._addWidget('apply', 'Button', nX + (self.xDialog.Width/2)-20, nY2+31, 40, 11, Label = ui.get('apply'), HelpText="apply_help", TextColor = 0x0000AA, Enabled = False)
        self.xModifyButton = self._addWidget('modify', 'Button', self.xDialog.Width-50, nY2+31, 40, 11, Label = ui.get('modify'), TextColor = 0x00AA00, Enabled = False)

        # import, export, save, close
        self._addWidget("buttons_line", 'FixedLine', nX, self.xDialog.Height-35, nWidth, nHeight)
        self._addWidget('import', 'Button', nX, self.xDialog.Height-25, 50, 14, Label = ui.get('import'), FontDescriptor = xFDTitle, TextColor = 0x0000AA)
        self._addWidget('export', 'Button', nX+60, self.xDialog.Height-25, 50, 14, Label = ui.get('export'), FontDescriptor = xFDTitle, TextColor = 0x00AA00)
        self._addWidget('save', 'Button', self.xDialog.Width-120, self.xDialog.Height-25, 50, 14, Label = ui.get('save'), FontDescriptor = xFDTitle, TextColor = 0x00AA00)
        self._addWidget('close', 'Button', self.xDialog.Width-60, self.xDialog.Height-25, 50, 14, Label = ui.get('close'), FontDescriptor = xFDTitle, TextColor = 0xAA0000)

        # data
        self.dRules = {}
        self.iSelectedRow = -1


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

        # container
        self.xContainer = self.xSvMgr.createInstanceWithContext('com.sun.star.awt.UnoControlDialog', self.ctx)
        self.xContainer.setModel(self.xDialog)
        self.xGridControl = self.xContainer.getControl('list_grid')
        self.xGridControl.addSelectionListener(self)
295
296
297
298
299
300
301
302

303
304
305
306
307
308
309
300
301
302
303
304
305
306

307
308
309
310
311
312
313
314







-
+







        xGridDataModel.addRow(xGridDataModel.RowCount + 1, self._getValuesForRow(sRuleName))
        self._clearAddFields()

    def _getValuesForRow (self, sRuleName):
        return (sRuleName, self.dRules[sRuleName]["sReplace"], self.dRules[sRuleName]["sBy"], str(self.dRules[sRuleName]["bRegex"]), str(self.dRules[sRuleName]["bCaseSens"]))

    def _checkRuleName (self, sRuleName):
        return re.match(r"\w[\w_#.,;!?-]*", sRuleName)
        return re.search(r"^\w[\w_#.,;!?-]*$", sRuleName)

    def modifyRule (self):
        if not self._checkRuleName(self.xEditname.Text):
            MessageBox(self.xDocument, ui.get("name_error"), ui.get("name_error_title"), ERRORBOX)
            return
        sRuleName = self.xEditname.Text
        if self.iSelectedRow < 0 or not sRuleName or not self.xEditreplace.Text:
340
341
342
343
344
345
346









347



348
349
350




351



352

353






























354















355
356
357
358
359
360
361
345
346
347
348
349
350
351
352
353
354
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

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428







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



+
+
+
+
-
+
+
+

+

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







            traceback.print_exc()

    @_waitPointer
    def apply (self):
        pass

    def loadRules (self):
        try:
            xChild = self.xGLOptionNode.getByName("o_${lang}")
            sTFEditorOptions = xChild.getPropertyValue("tfe_rules")
            if not sTFEditorOptions:
                return
            self.dRules = json.loads(sTFEditorOptions)
            xGridDataModel = self.xGridModel.GridDataModel
            for sRuleName in self.dRules:
                xGridDataModel.addRow(xGridDataModel.RowCount + 1, self._getValuesForRow(sRuleName))
        pass
        except:
            sMessage = traceback.format_exc()
            MessageBox(self.xDocument, sMessage, ui.get('error'), ERRORBOX)

    @_waitPointer
    def saveRules (self):
        try:
            xChild = self.xGLOptionNode.getByName("o_${lang}")
            xChild.setPropertyValue("tfe_rules", json.dumps(self.dRules))
            self.xGLOptionNode.commitChanges()
        pass
        except:
            sMessage = traceback.format_exc()
            MessageBox(self.xDocument, sMessage, ui.get('error'), ERRORBOX)

    @_waitPointer
    def importRules (self):
        spfImported = ""
        try:
            xFilePicker = self.xSvMgr.createInstanceWithContext('com.sun.star.ui.dialogs.FilePicker', self.ctx)  # other possibility: com.sun.star.ui.dialogs.SystemFilePicker
            xFilePicker.initialize([uno.getConstantByName("com.sun.star.ui.dialogs.TemplateDescription.FILEOPEN_SIMPLE")]) # seems useless
            xFilePicker.appendFilter("Supported files", "*.json")
            xFilePicker.setDefaultName("grammalecte_tf_trans_rules.json") # useless, doesn’t work
            xFilePicker.setDisplayDirectory("")
            xFilePicker.setMultiSelectionMode(False)
            nResult = xFilePicker.execute()
            if nResult == 1:
                # lFile = xFilePicker.getSelectedFiles()
                lFile = xFilePicker.getFiles()
                #print(lFile)
                spfImported = lFile[0][5:].lstrip("/") # remove file://
                if platform.system() != "Windows":
                    spfImported = "/" + spfImported
        except:
            sMessage = traceback.format_exc()
            MessageBox(self.xDocument, sMessage, ui.get('error'), ERRORBOX)
            return
        if not spfImported or not os.path.isfile(spfImported):
            sMessage = ui.get('file_not_found') + "<" + spfImported + ">"
            MessageBox(self.xDocument, sMessage, ui.get('error'), ERRORBOX)
            return
        try:
            with open(spfImported, "r", encoding="utf-8") as hFile:
                dImportedRules = json.load(hFile)
        except:
            sMessage = traceback.format_exc()
            MessageBox(self.xDocument, sMessage, ui.get('error'), ERRORBOX)
        pass
        else:
            nButton = MessageBox(self.xDocument, ui.get('import_question'), ui.get('import_title'), QUERYBOX, BUTTONS_YES_NO_CANCEL)
            if nButton == 0: # cancel
                return
            xGridDataModel = self.xGridModel.GridDataModel
            if nButton == 2: # yes
                self.dRules.update(dImportedRules)
                self.xGridModel.GridDataModel.removeAllRows()
                for sRuleName in self.dRules:
                    xGridDataModel.addRow(xGridDataModel.RowCount + 1, self._getValuesForRow(sRuleName))
            else: # 3 = no
                for sRuleName, dValues in dImportedRules.items():
                    if not sRuleName in self.dRules:
                        self.dRules[sRuleName] = dValues
                        xGridDataModel.addRow(xGridDataModel.RowCount + 1, self._getValuesForRow(sRuleName))

    def exportRules (self):
        if not self.dRules:
            return
        sText = json.dumps(self.dRules, ensure_ascii=False)
        try:
            xFilePicker = self.xSvMgr.createInstanceWithContext('com.sun.star.ui.dialogs.FilePicker', self.ctx)  # other possibility: com.sun.star.ui.dialogs.SystemFilePicker
372
373
374
375
376
377
378
379

439
440
441
442
443
444
445

446







-
+
                if platform.system() != "Windows":
                    spfExported = "/" + spfExported
                #spfExported = os.path.join(os.path.expanduser("~"), "fr.personal.json")
                with open(spfExported, "w", encoding="utf-8") as hDst:
                    hDst.write(sText)
        except:
            sMessage = traceback.format_exc()
            MessageBox(self.xDocument, sMessage, self.ui.get('export_title'), ERRORBOX)
            MessageBox(self.xDocument, sMessage, ui.get('error'), ERRORBOX)

Modified gc_lang/fr/oxt/TextFormatter/tfe_strings.py from [17e244691b] to [bda797a9d2].

1
2
3
4
5
6
7
8
9


10
11
12
13
14
15
16
1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17








-
+
+







# Strings for Text Formatter Editor


sUI = "fr"


def selectLang (sLang):
    global sUI
    sUI = sLang  if sLang in dStrings  else "fr"
    if sLang in dStrings:
        sUI = sLang


def get (sMsgCode):
    try:
        return dStrings[sUI].get(sMsgCode, sMsgCode)
    except:
        return "#error"
52
53
54
55
56
57
58






59

60
61
62
63
64
65
66
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74







+
+
+
+
+
+

+







        "add_name_error_title": "Nom déjà utilisé",

        "delete_name_error": "Vous avez modifié le nom de la règle. Veuillez resélectionner la règle que vous voulez supprimer, et cliquez sur ‹Supprimer›.",
        "delete_name_error_title": "Nom de la règle à supprimer douteux",

        "modify_name_error": "Une autre règle porte déjà ce nom. Veuillez modifier le nom de la règle.",
        "modify_name_error_title": "Nouveau nom déjà utilisé par une autre règle",

        "import_question": "Voulez-vous que les règles importées écrasent celles existantes si elles possèdent un nom identique ?",
        "import_title": "Importation d’un fichier de règles de transformation",

        "error": "Erreur",
        "file_not_found": "Fichier introuvable : ",
    },

    "en": {
        "title": "Grammalecte · Editor for custom transformations",

        "name": "Rule name",
        "replace": "Replace",
        "by": "by",
        "regex": "Regex",
92
93
94
95
96
97
98






99
100
101
102
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116







+
+
+
+
+
+




        "add_name_error_title": "Rule name already used",

        "delete_name_error": "You have modified the name of the rule. Please, select again the rule you want to delete and click on ‹Delete›.",
        "delete_name_error_title": "Dubious rule name to delete",

        "modify_name_error": "This rule name is already used by another rule. Please, modify the rule name.",
        "modify_name_error_title": "New rule name already used by another rule",

        "import_question": "Do you want that imported rules replace existing ones if they have the same name?",
        "import_title": "Importation of a transformation rules file",

        "error": "Error",
        "file_not_found": "File not found: ",
    }
}