Changeset 3 for trunk

Show
Ignore:
Timestamp:
09/23/06 18:47:13 (2 years ago)
Author:
mjoc
Message:

Ability to activate/deactivate installed dictionaries so that only a
part of them would be shown in the menu at the time.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/config.py

    r1 r3  
    3131 
    3232 
     33class ActiveDictConfig(object): 
     34    """Config file manager for activated dictionaries. Operates with 
     35    names of dictionaries only.""" 
     36 
     37    def __init__(self): 
     38      self.filePath = os.path.join(info.LOCAL_HOME, "active.conf") 
     39 
     40      # If config file does not exist (this is the first time), 
     41      # set special attribute init=True to notify that 
     42      if not os.path.exists(self.filePath): 
     43          self.init = True 
     44      else: 
     45          self.init = False 
     46 
     47      self.dicts = [] 
     48 
     49 
     50    def load(self): 
     51        """Load list of active dictionaries.""" 
     52 
     53        try: 
     54            for line in open(self.filePath): 
     55                name = line.strip() 
     56                name = unicode(name, 'UTF-8') 
     57                self.dicts.append(name) 
     58        except IOError, e: 
     59            pass 
     60 
     61 
     62    def save(self): 
     63        """Save list of active dictionaries.""" 
     64 
     65        fd = open(self.filePath, 'w') 
     66        for d in self.dicts: 
     67            name = d.encode('UTF-8') 
     68            print >> fd, name 
     69        fd.close() 
     70 
     71 
     72    def enabled(self, name): 
     73        """Return True if this dictionary is enabled.""" 
     74 
     75        if name in self.dicts: 
     76            return True 
     77 
     78        return False 
     79 
     80 
     81    def add(self, name): 
     82        """Add new dictionary to the list.""" 
     83 
     84        if type(name) == str: 
     85            name = unicode(name, 'UTF-8') 
     86 
     87        if not name in self.dicts: 
     88            self.dicts.append(name) 
     89 
     90 
     91    def remove(self, name): 
     92        """Remove dictionary from the list.""" 
     93 
     94        if type(name) == str: 
     95            name = unicode(name, 'UTF-8') 
     96 
     97        if name in self.dicts: 
     98            self.dicts.remove(name) 
     99 
     100 
     101 
    33102class Configuration: 
    34103   """This class is used for reading and writing config file. 
     
    37106   def __init__(self): 
    38107      """Initialize default values""" 
     108 
     109      self.activedict = ActiveDictConfig() 
     110      self.activedict.load() 
    39111       
    40112      self.filePath = os.path.join(info.LOCAL_HOME, "opendict.xml") 
  • trunk/lib/gui/mainwin.py

    r1 r3  
    265265      self.menuDict = wxMenu() 
    266266 
    267       dictNames = [] 
     267      dicts = [] 
    268268      for dictionary in self.app.dictionaries.values(): 
    269          dictNames.append(dictionary.getName()) 
    270       dictNames.sort() 
    271        
    272       for name in dictNames: 
     269         dicts.append([dictionary.getName(), dictionary.getActive()]) 
     270      dicts.sort() 
     271       
     272      for name, active in dicts: 
     273         #if not self.app.config.activedict.enabled(name): 
     274         #    continue 
     275         if not active: 
     276             continue 
     277 
    273278         encoded = enc.toWX(name) 
    274279 
     
    10491054      EVT_MENU(self, unid, self.onDefault) 
    10501055       
    1051       self.menuDict.InsertItem(self.menuDict.GetMenuItemCount()-2, item) 
     1056      #self.menuDict.InsertItem(self.menuDict.GetMenuItemCount()-2, item) 
     1057      self.menuDict.InsertItem(0, item) 
     1058 
     1059 
     1060   def removeDictionary(self, name): 
     1061       """Remove dictionary from the menu""" 
     1062 
     1063       item = self.menuDict.FindItem(name) 
     1064       if item: 
     1065           self.menuDict.Delete(item) 
    10521066 
    10531067 
  • trunk/lib/gui/pluginwin.py

    r1 r3  
    11 
    22# OpenDict 
    3 # Copyright (c) 2003-2005 Martynas Jocius <mjoc@akl.lt> 
     3# Copyright (c) 2003-2006 Martynas Jocius <mjoc@akl.lt> 
    44# 
    55# This program is free software; you can redistribute it and/or modify 
     
    123123       # 
    124124       idDictList = wx.NewId() 
    125        self.installedList = DictListCtrl(panelInstalled, idDictList, 
     125       self.installedList = wx.CheckListBox(panelInstalled, idDictList, 
    126126                                         style=wx.LC_REPORT 
    127127                                         | wx.LC_SINGLE_SEL 
    128128                                         | wx.SUNKEN_BORDER) 
     129       self.Bind(wx.EVT_CHECKLISTBOX, self.onDictionaryChecked, 
     130            self.installedList) 
     131       self.Bind(wx.EVT_LISTBOX, self.onInstalledSelected, 
     132            self.installedList) 
    129133       vboxInstalled.Add(self.installedList, 1, wxALL | wxEXPAND, 1) 
    130134 
     
    155159       # Make columns 
    156160       # 
    157        self.installedList.InsertColumn(0, _("Name")) 
     161       #self.installedList.InsertColumn(0, _("Name")) 
    158162        
    159163       dictNames = self.installedDictionaries.keys() 
     
    162166       self.setInstalledDicts(dictNames) 
    163167 
    164        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onInstalledSelected, 
    165                  self.installedList) 
     168       #self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onInstalledSelected, 
     169       #          self.installedList) 
    166170 
    167171       self.Bind(wx.EVT_BUTTON, self.onRemove, self.buttonRemove) 
     
    181185 
    182186       # 
    183        # Installed list 
     187       # List of available dictionaries 
    184188       # 
    185189       idAvailList = wx.NewId() 
     
    306310 
    307311       return panelInfo 
     312 
     313 
     314   def onDictionaryChecked(self, event, *args): 
     315       index = event.GetSelection() 
     316       label = self.installedList.GetString(index) 
     317       if self.installedList.IsChecked(index): 
     318           self._addDictToMenu(label) 
     319           d = self.app.dictionaries.get(label) 
     320           d.setActive() 
     321       else: 
     322           self._removeDictFromMenu(label) 
     323           d = self.app.dictionaries.get(label) 
     324           d.setActive(active=False) 
     325       self.installedList.SetSelection(index) 
     326 
     327 
     328   def _removeDictFromMenu(self, name): 
     329       self.app.config.activedict.remove(name) 
     330       self.app.config.activedict.save() 
     331       self.app.window.removeDictionary(name) 
     332 
     333 
     334   def _addDictToMenu(self, name): 
     335       dict = None 
     336       for k, v in self.app.dictionaries.items(): 
     337           if k == name: 
     338               dict = v 
     339       if dict: 
     340           self.app.config.activedict.add(name) 
     341           self.app.config.activedict.save() 
     342           self.app.window.addDictionary(dict) 
    308343 
    309344 
     
    334369       """Called when list item is selected""" 
    335370        
    336        self.currentInstalledItemSelection = event.m_itemIndex 
     371       self.currentInstalledItemSelection = event.GetSelection() 
    337372       self.buttonRemove.Enable(1) 
    338373        
     
    343378       """Show information about selected dictionary""" 
    344379 
    345        dictName = self.installedList.GetItemText(\ 
     380       dictName = self.installedList.GetString(\ 
    346381          self.currentInstalledItemSelection) 
    347382 
     
    441476   def setInstalledDicts(self, dictNames): 
    442477       """Clear the list of installed dictionaries and set new items""" 
    443  
    444        self.installedList.DeleteAllItems() 
     478     
     479       for i in range(self.installedList.GetCount()): 
     480           self.installedList.Delete(i) 
    445481 
    446482       dictNames.sort() 
    447483       dictNames.reverse() 
    448         
     484       i = 0 
     485 
    449486       for dictionary in dictNames: 
    450            index = self.installedList.InsertStringItem(0, dictionary)           
    451            self.installedList.SetItemData(index, index+1) 
    452  
    453  
    454        if dictNames: 
    455            self.installedList.SetColumnWidth(0, wx.LIST_AUTOSIZE) 
    456        else: 
    457            self.installedList.SetColumnWidth(0, 200) 
     487           index = self.installedList.Insert(dictionary, i) 
     488           if self.app.dictionaries[dictionary].getActive(): 
     489               self.installedList.Check(i) 
     490           i += 1 
    458491 
    459492 
  • trunk/lib/gui/prefswin.py

    r1 r3  
    11# 
    22# OpenDict 
    3 # Copyright (c) 2003-2005 Martynas Jocius <mjoc@akl.lt> 
     3# Copyright (c) 2003-2006 Martynas Jocius <mjoc@akl.lt> 
    44# 
    55# This program is free software; you can redistribute it and/or modify 
     
    4848                   0, wxALIGN_CENTER_VERTICAL) 
    4949 
    50       dictNames = self.app.dictionaries.keys() 
     50      dictNames = [] 
     51      for name, d in self.app.dictionaries.items(): 
     52          print name, d.getActive() 
     53          if d.getActive(): 
     54              dictNames.append(name) 
    5155      dictNames.sort() 
    5256      dictNames.insert(0, "") 
  • trunk/lib/meta.py

    r1 r3  
    11# 
    22# OpenDict 
    3 # Copyright (c) 2005 Martynas Jocius <mjoc@akl.lt> 
     3# Copyright (c) 2005-2006 Martynas Jocius <mjoc@akl.lt> 
    44# 
    55# This program is free software; you can redistribute it and/or modify 
     
    7676class Dictionary: 
    7777    """Dictionary interface""" 
     78 
     79    active = True 
    7880 
    7981 
     
    154156 
    155157        return None 
     158 
     159 
     160    def getActive(self): 
     161        return self.active 
     162 
     163 
     164    def setActive(self, active=True): 
     165        self.active = active 
    156166     
    157167 
  • trunk/opendict.py

    r1 r3  
    162162 
    163163 
     164      for d in self.dictionaries.values(): 
     165          if not self.config.activedict.init: 
     166              if not self.config.activedict.enabled(d.getName()): 
     167                  d.setActive(active=False) 
     168          else: 
     169              # Fill up with names if not initialized yet 
     170              self.config.activedict.add(d.getName()) 
     171 
     172 
    164173      windowPos = (int(self.config.get('windowPosX')), 
    165174                                int(self.config.get('windowPosY')))