Grammalecte  Diff

Differences From Artifact [e0f5ecd431]:

To Artifact [baf12f1015]:


     1         -# -*- coding: utf8 -*-
     2      1   # Options Dialog
     3      2   # by Olivier R.
     4      3   # License: MPL 2
     5      4   
     6      5   import unohelper
     7      6   import uno
     8      7   import traceback
................................................................................
    15     14   
    16     15   try:
    17     16       import grammalecte.${lang} as gce
    18     17   except:
    19     18       traceback.print_exc()
    20     19   
    21     20   
    22         -options = {}
           21  +def loadOptions (sLang):
           22  +    "load options from Grammalecte and change them according to LibreOffice settings, returns a dictionary {option_name: boolean}"
           23  +    try:
           24  +        xNode = helpers.getConfigSetting("/org.openoffice.Lightproof_${implname}/Leaves", False)
           25  +        xChild = xNode.getByName(sLang)
           26  +        dOpt = gce.gc_options.getOptions("Writer")
           27  +        for sKey in dOpt:
           28  +            sValue = xChild.getPropertyValue(sKey)
           29  +            if sValue != '':
           30  +                dOpt[sKey] = bool(int(sValue))
           31  +        return dOpt
           32  +    except:
           33  +        print("# Error. Unable to load options of language:", sLang)
           34  +        traceback.print_exc()
           35  +        return gce.gc_options.getOptions("Writer")
    23     36   
    24     37   
    25         -def load (ctx):
           38  +def saveOptions (sLang, dOpt):
           39  +    "save options in LibreOffice profile"
    26     40       try:
    27         -        oGCO = GC_Options(ctx)
    28         -        oGCO.load("${lang}")
           41  +        xNode = helpers.getConfigSetting("/org.openoffice.Lightproof_${implname}/Leaves", True)
           42  +        xChild = xNode.getByName(sLang)
           43  +        for sKey, value in dOpt.items():
           44  +            xChild.setPropertyValue(sKey, value)
           45  +        xNode.commitChanges()
    29     46       except:
    30         -        print("# Error. Unable to load options of language: ${lang}")
    31         -    return options
           47  +        traceback.print_exc()
    32     48   
    33     49   
    34     50   class GC_Options (unohelper.Base, XActionListener):
           51  +
    35     52       def __init__ (self, ctx):
    36     53           self.ctx = ctx
    37     54           self.xSvMgr = self.ctx.ServiceManager
    38     55           self.xContainer = None
    39         -        #self.xNode = helpers.getConfigSetting("/org.openoffice.Lightproof_%s/Leaves"%pkg, True)
    40         -        self.xNode = helpers.getConfigSetting("/org.openoffice.Lightproof_grammalecte/Leaves", True)
    41         -        self.nSecret = 0
    42         -        
           56  +
    43     57       def _addWidget (self, name, wtype, x, y, w, h, **kwargs):
    44     58           xWidget = self.xDialog.createInstance('com.sun.star.awt.UnoControl%sModel' % wtype)
    45     59           xWidget.Name = name
    46     60           xWidget.PositionX = x
    47     61           xWidget.PositionY = y
    48     62           xWidget.Width = w
    49     63           xWidget.Height = h
................................................................................
    51     65               setattr(xWidget, k, w)
    52     66           self.xDialog.insertByName(name, xWidget)
    53     67           return xWidget
    54     68   
    55     69       def run (self, sUI):
    56     70           try:
    57     71               dUI = op_strings.getUI(sUI)
    58         -            dUI2 = gce.gc_options.getUI(sUI)
           72  +            dOptionUI = gce.gc_options.getUI(sUI)
    59     73   
    60     74               # fonts
    61     75               xFDTitle = uno.createUnoStruct("com.sun.star.awt.FontDescriptor")
    62     76               xFDTitle.Height = 9
    63     77               xFDTitle.Weight = uno.getConstantByName("com.sun.star.awt.FontWeight.BOLD")
    64     78               xFDTitle.Name = "Verdana"
    65     79   
................................................................................
    74     88               self.xDialog.Height = 400
    75     89               self.xDialog.Title = dUI.get('title', "#err")
    76     90   
    77     91               # build
    78     92               y = 0
    79     93               nWidth = self.xDialog.Width - 20
    80     94               nHeight = 10
    81         -            
    82         -            self.lxOptions = []
           95  +
           96  +            self.lOptionWidgets = []
    83     97   
    84     98               for t in gce.gc_options.lStructOpt:
    85     99                   x = 10
    86    100                   y += 10
    87         -                self._addWidget(t[0], 'FixedLine', x, y, nWidth, nHeight, Label = dUI2.get(t[0], "#err")[0], FontDescriptor= xFDTitle)
          101  +                self._addWidget(t[0], 'FixedLine', x, y, nWidth, nHeight, Label = dOptionUI.get(t[0], "#err")[0], FontDescriptor= xFDTitle)
    88    102                   y += 3
    89    103                   for lOptLine in t[1]:
    90    104                       x = 15
    91    105                       y += 10
    92    106                       n = len(lOptLine)
    93    107                       for sOpt in lOptLine:
    94         -                        w = self._addWidget(sOpt, 'CheckBox', x, y, nWidth/n, nHeight, State = options.get(sOpt, False), \
    95         -                                            Label = dUI2.get(sOpt, "#err")[0], HelpText = dUI2.get(sOpt, "#err")[1])
    96         -                        self.lxOptions.append(w)
          108  +                        w = self._addWidget(sOpt, 'CheckBox', x, y, nWidth/n, nHeight, \
          109  +                                            Label = dOptionUI.get(sOpt, "#err")[0], HelpText = dOptionUI.get(sOpt, "#err")[1])
          110  +                        self.lOptionWidgets.append(w)
    97    111                           x += nWidth / n
    98         -            
          112  +
    99    113               self.xDialog.Height = y + 40
   100    114   
   101    115               xWindowSize = helpers.getWindowSize()
   102    116               self.xDialog.PositionX = int((xWindowSize.Width / 2) - (self.xDialog.Width / 2))
   103    117               self.xDialog.PositionY = int((xWindowSize.Height / 2) - (self.xDialog.Height / 2))
   104    118   
   105         -            but0 = self._addWidget('default', 'Button', 10, self.xDialog.Height-20, 50, 14, \
   106         -                                   Label = dUI.get('default', "#err"), FontDescriptor = xFDBut, TextColor = 0x000044)
   107         -            but1 = self._addWidget('apply', 'Button', self.xDialog.Width-115, self.xDialog.Height-20, 50, 14, \
   108         -                                   Label = dUI.get('apply', "#err"), FontDescriptor = xFDBut, TextColor = 0x004400)
   109         -            but2 = self._addWidget('cancel', 'Button', self.xDialog.Width-60, self.xDialog.Height-20, 50, 14,
   110         -                                   Label = dUI.get('cancel', "#err"), FontDescriptor = xFDBut, TextColor = 0x440000)
          119  +            self._addWidget('default', 'Button', 10, self.xDialog.Height-20, 50, 14, \
          120  +                            Label = dUI.get('default', "#err"), FontDescriptor = xFDBut, TextColor = 0x000044)
          121  +            self._addWidget('apply', 'Button', self.xDialog.Width-115, self.xDialog.Height-20, 50, 14, \
          122  +                            Label = dUI.get('apply', "#err"), FontDescriptor = xFDBut, TextColor = 0x004400)
          123  +            self._addWidget('cancel', 'Button', self.xDialog.Width-60, self.xDialog.Height-20, 50, 14,
          124  +                            Label = dUI.get('cancel', "#err"), FontDescriptor = xFDBut, TextColor = 0x440000)
          125  +
          126  +            dOpt = loadOptions("${lang}")
          127  +            self._setWidgets(dOpt)
   111    128   
   112    129               # container
   113    130               self.xContainer = self.xSvMgr.createInstanceWithContext('com.sun.star.awt.UnoControlDialog', self.ctx)
   114    131               self.xContainer.setModel(self.xDialog)
   115    132               self.xContainer.getControl('default').addActionListener(self)
   116    133               self.xContainer.getControl('default').setActionCommand('Default')
   117    134               self.xContainer.getControl('apply').addActionListener(self)
................................................................................
   125    142           except:
   126    143               traceback.print_exc()
   127    144   
   128    145       # XActionListener
   129    146       def actionPerformed (self, xActionEvent):
   130    147           try:
   131    148               if xActionEvent.ActionCommand == 'Default':
   132         -                self._setDefault()
          149  +                self._setWidgets(gce.gc_options.getOptions("Writer"))
   133    150               elif xActionEvent.ActionCommand == 'Apply':
   134         -                self._save("fr")
          151  +                self._save("${lang}")
   135    152                   self.xContainer.endExecute()
   136    153               elif xActionEvent.ActionCommand == 'Cancel':
   137    154                   self.xContainer.endExecute()
   138    155               else:
   139    156                   print("Wrong command: " + xActionEvent.ActionCommand)
   140    157           except:
   141    158               traceback.print_exc()
   142    159   
   143         -    def _setDefault (self):
   144         -        dOpt = gce.gc_options.getOptions("Writer")
   145         -        for w in self.lxOptions:
          160  +    # Other
          161  +    def _setWidgets (self, dOpt):
          162  +        for w in self.lOptionWidgets:
   146    163               w.State = dOpt.get(w.Name, False)
   147    164   
   148         -    def load (self, sLang):
   149         -        try:
   150         -            xChild = self.xNode.getByName(sLang)
   151         -            dOpt = gce.gc_options.getOptions("Writer")
   152         -            for sKey in dOpt:
   153         -                sValue = xChild.getPropertyValue(sKey)
   154         -                if sValue == '':
   155         -                    if dOpt[sKey]:
   156         -                        sValue = 1
   157         -                    else:
   158         -                        sValue = 0
   159         -                options[sKey] = bool(int(sValue))
   160         -        except:
   161         -            traceback.print_exc()
   162         -
   163    165       def _save (self, sLang):
   164    166           try:
   165         -            xChild = self.xNode.getByName(sLang)
   166         -            for w in self.lxOptions:
   167         -                sKey = w.Name
   168         -                bValue = w.State
   169         -                xChild.setPropertyValue(sKey, str(bValue))
   170         -                options[sKey] = bValue
   171         -                gce.setOptions(options)
   172         -            self.xNode.commitChanges()
          167  +            saveOptions(sLang, { w.Name: str(w.State)  for w in self.lOptionWidgets })
          168  +            gce.setOptions({ w.Name: bool(w.State)  for w in self.lOptionWidgets })
   173    169           except:
   174    170               traceback.print_exc()