Package nMOLDYN :: Package GUI :: Module PreferencesDialog |
[hide private]
[frames] | no frames]

# Source Code for Module nMOLDYN.GUI.PreferencesDialog

      1  """This modules implements I{File-->Preferences} dialog.
      2
      3  Classes:
      4      * PreferencesDialog: creates I{File-->Preferences} dialog used to to set up nMOLDYN PREFERENCES variables.
      5  """
      6
      7
      8  # The python distribution modules
      9  import os
     10  import sys
     11
     12  # The Tcl/Tk modules
     13  from tkFileDialog import askopenfilename, asksaveasfilename, askdirectory
     14  from Tkinter import *
     15
     16  # The nMOLDYN modules
     17  from nMOLDYN.Core.Error import Error
     18  from nMOLDYN.Core.Config import loadConfigurationFile, saveConfigurationFile
     19  from nMOLDYN.Core.Logger import LogMessage
     20  from nMOLDYN.Core.Preferences import PREFERENCES
     21  from nMOLDYN.GUI.Widgets import *
     22
     23 -class dummy:
     24 -    def __init__(self):
     25          pass
     26
     27 -class PreferencesDialog(Toplevel):
     28      """Sets up a dialog used to change some PREFERENCES.
     29      """
     30
     31 -    def __init__(self, parent, title = None):
     32          """The constructor.
     33
     34          @param parent: the parent widget.
     35
     36          @param title: a string specifying the title of the dialog.
     37          @type title: string
     38          """
     39
     40          Toplevel.__init__(self, parent)
     41          self.transient(parent)
     42
     43          if title:
     44              self.title(title)
     45
     46          self.parent = parent
     47
     48          body = Frame(self)
     49          self.initial_focus = self.body(body)
     50          body.grid(row = 0, column = 0, sticky = EW)
     51
     52          self.buttonbox()
     53
     54          self.grab_set()
     55
     56          if not self.initial_focus:
     57              self.initial_focus = self
     58
     59          self.protocol("WM_DELETE_WINDOW", self.cancel)
     60
     61          self.resizable(width = NO, height = NO)
     62
     63          self.geometry("+%d+%d" % (parent.winfo_rootx()+50, parent.winfo_rooty()+50))
     64
     65          self.initial_focus.focus_set()
     66
     67          self.wait_window(self)
     68
     69 -    def body(self, master):
     70          """
     71          Create dialog body. Return widget that should have initial focus.
     72          """
     73
     74          settingsFrame = LabelFrame(master, text = 'Settings', bd = 2, relief = GROOVE)
     75          settingsFrame.grid(row = 0, column = 0, sticky = EW, padx = 3, pady = 3)
     76          settingsFrame.grid_columnconfigure(0, weight = 1)
     77
     78          self.preferences = {}
     79
     80          pagesFrame = Frame(settingsFrame)
     81          pagesFrame.grid(row = 0, column = 0, sticky = 'NEW', padx = 3, pady = 3)
     82
     83          pagesFrame.grid_columnconfigure(0, weight = 1)
     84          pagesFrame.grid_columnconfigure(1, weight = 1)
     85
     86          self.pages = ComboRadiobutton(pagesFrame,\
     87                                        frameLabel = "Preferences sections",\
     88                                        contents = ["File handling", "External programs", "Miscellaneous"],\
     89                                        layout = (3,1))
     90          [r.config(width = 16, command = self.changePage) for r in self.pages.radio]
     91          self.pages.grid(row = 0, column = 0, padx = 2, pady = 5, sticky = EW)
     92          self.pages.grid_columnconfigure(0, weight = 1)
     93
     94          self.preferencesFrame = Frame(settingsFrame, bd = 2, relief = GROOVE, width = 500, height = 450)
     95          self.preferencesFrame.grid(row = 0, column = 1, padx = 3, pady = 3, sticky = 'NEW')
     96          self.preferencesFrame.grid_propagate(0)
     97          self.preferencesFrame.grid_columnconfigure(0, weight = 1)
     98          self.preferencesFrame.grid_rowconfigure(10, weight = 1)
     99
    100          self.preferences['logfile_path'] = ComboFileBrowser(self.preferencesFrame,\
    101                                                              frameLabel = "Logfile path",\
    102                                                              tagName = 'preferences_logfile_path',\
    103                                                              contents = PREFERENCES.logfile_path,\
    104                                                              save = True,\
    105                                                              command = lambda k = 'logfile_path': self.openDirectory(k))
    106          self.preferences['logfile_path'].grid(row = 0, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    107          self.preferences['logfile_path'].grid_columnconfigure(0, weight = 1)
    108
    109          self.preferences['trajfile_path'] = ComboFileBrowser(self.preferencesFrame,\
    110                                                               frameLabel = "Trajectory file path",\
    111                                                               tagName = 'preferences_trajfile_path',\
    112                                                               contents = PREFERENCES.trajfile_path,\
    113                                                               save = True,\
    114                                                               command = lambda k = 'trajfile_path': self.openDirectory(k))
    115          self.preferences['trajfile_path'].grid(row = 1, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    116          self.preferences['trajfile_path'].grid_columnconfigure(0, weight = 1)
    117
    118          self.preferences['outputfile_path'] = ComboFileBrowser(self.preferencesFrame,\
    119                                                                 frameLabel = "Output file path",\
    120                                                                 tagName = 'preferences_outputfile_path',\
    121                                                                 contents = PREFERENCES.outputfile_path,\
    122                                                                 save = True,\
    123                                                                 command = lambda k = 'outputfile_path': self.openDirectory(k))
    124          self.preferences['outputfile_path'].grid(row = 2, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    125          self.preferences['outputfile_path'].grid_columnconfigure(0, weight = 1)
    126
    127          self.preferences['pyro_server_configfile'] = ComboFileBrowser(self.preferencesFrame,\
    128                                                                        frameLabel = "Pyroserver configuration file",\
    129                                                                        tagName = 'preferences_pyro_server_configfile',\
    130                                                                        contents = PREFERENCES.pyro_server_configfile,\
    131                                                                        save = True,\
    132                                                                        command = lambda k = 'pyro_server_configfile': self.openFilename(k))
    133          self.preferences['pyro_server_configfile'].grid(row = 3, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    134          self.preferences['pyro_server_configfile'].grid_columnconfigure(0, weight = 1)
    135
    136          self.preferences['acroread_path'] = ComboFileBrowser(self.preferencesFrame,\
    137                                                               frameLabel = "Acrobat reader path",\
    138                                                               tagName = 'preferences_acroread_path',\
    139                                                               contents = PREFERENCES.acroread_path,\
    140                                                               save = True,\
    141                                                               command = lambda k = 'acroread_path': self.openFilename(k))
    142
    143          self.preferences['vmd_path'] = ComboFileBrowser(self.preferencesFrame,\
    144                                                          frameLabel = "VMD molecular viewer path",\
    145                                                          tagName = 'preferences_vmd_path',\
    146                                                          contents = PREFERENCES.vmd_path,\
    147                                                          save = True,\
    148                                                          command = lambda k = 'vmd_path': self.openFilename(k))
    149
    150          self.preferences['ncdump_path'] = ComboFileBrowser(self.preferencesFrame,\
    151                                                             frameLabel = "ncdump path",\
    152                                                             tagName = 'preferences_ncdump_path',\
    153                                                             contents = PREFERENCES.ncdump_path,\
    154                                                             save = True,\
    155                                                             command = lambda k = 'ncdump_path': self.openFilename(k))
    156
    157          self.preferences['ncgen_path'] = ComboFileBrowser(self.preferencesFrame,\
    158                                                            frameLabel = "ncgen path",\
    159                                                            tagName = 'preferences_ncgen_path',\
    160                                                            contents = PREFERENCES.ncgen_path,\
    161                                                            save = True,\
    162                                                            command = lambda k = 'ncgen_path': self.openFilename(k))
    163
    164          self.preferences['progress_rate'] = ComboScale(self.preferencesFrame,\
    165                                                         frameLabel = "Progress rate",\
    166                                                         tagName = 'preferences_progress_rate',\
    167                                                         contents = [5,95,5])
    168          self.preferences['progress_rate'].setValue(PREFERENCES.progress_rate)
    169
    170          self.preferences['documentation_style'] = ComboRadiobutton(self.preferencesFrame,\
    171                                                                     frameLabel = "Documentation style",\
    172                                                                     tagName = 'preferences_documentation_style',\
    173                                                                     contents = ["html", "pdf"],\
    174                                                                     layout = (1,2))
    175          self.preferences['documentation_style'].setValue(PREFERENCES.documentation_style)
    176
    177          self.preferences['warning']  = ComboFrame(self.preferencesFrame,\
    178                                                    frameLabel = "Show warning",\
    179                                                    tagName = 'preferences_warning')
    180
    181          self.preferences['warning_ncdump']  = ComboRadiobutton(self.preferences['warning'],\
    182                                                                 frameLabel = "ncdump missing",\
    183                                                                 tagName = 'preferences_warning_ncdump',\
    184                                                                 contents = ['yes', 'no'],\
    185                                                                 layout = (1,2))
    186          self.preferences['warning_ncdump'].setValue(PREFERENCES.warning_ncdump.lower())
    187
    188          self.preferences['warning_ncgen']  = ComboRadiobutton(self.preferences['warning'],\
    189                                                                frameLabel = "ncgen missing",\
    190                                                                tagName = 'preferences_warning_ncgen',\
    191                                                                contents = ['yes', 'no'],\
    192                                                                layout = (1,2))
    193          self.preferences['warning_ncgen'].setValue(PREFERENCES.warning_ncgen.lower())
    194
    195          self.preferences['warning_vmd']  = ComboRadiobutton(self.preferences['warning'],\
    196                                                              frameLabel = "vmd missing",\
    197                                                              tagName = 'preferences_warning_vmd',\
    198                                                              contents = ['yes', 'no'],\
    199                                                              layout = (1,2))
    200          self.preferences['warning_vmd'].setValue(PREFERENCES.warning_vmd.lower())
    201
    202          self.preferences['warning_acroread']  = ComboRadiobutton(self.preferences['warning'],\
    203                                                                 frameLabel = "acrobat reader missing",\
    204                                                                 tagName = 'preferences_warning_acroread',\
    205                                                                 contents = ['yes', 'no'],\
    206                                                                 layout = (1,2))
    207          self.preferences['warning_acroread'].setValue(PREFERENCES.warning_acroread.lower())
    208
    209          self.preferences['warning_autoupdate']  = ComboRadiobutton(self.preferences['warning'],\
    210                                                                     frameLabel = "autoupdate at start up",\
    211                                                                     tagName = 'preferences_warning_vmd',\
    212                                                                     contents = ['yes', 'no'],\
    213                                                                     layout = (1,2))
    214          self.preferences['warning_autoupdate'].setValue(PREFERENCES.warning_autoupdate.lower())
    215
    216 -    def buttonbox(self):
    217          """
    218          Add standard button box.
    219          """
    220
    221          # The frame that contains the 'Cancel' and 'OK' buttons.
    222          box = LabelFrame(self, text = 'Actions', bd = 2, relief = GROOVE)
    223          box.grid(row = 1, column = 0, sticky = EW, padx = 3, pady = 3)
    224          box.grid_columnconfigure(0, weight = 1)
    225
    226          w = Button(box, text = "Cancel", width=10, command = self.cancel)
    227          w.grid(row = 0, column = 0, sticky = E)
    228          w = Button(box, text = "Save", width=10, command = self.savePreferences, default=ACTIVE)
    229          w.grid(row = 0, column = 1, sticky = E)
    230          w = Button(box, text = "Load", width=10, command = self.loadPreferences, default=ACTIVE)
    231          w.grid(row = 0, column = 2, sticky = E)
    232          w = Button(box, text = "OK", width=10, command = self.ok, default=ACTIVE)
    233          w.grid(row = 0, column = 3, sticky = E)
    234
    235          self.bind("<Return>", self.ok)
    236          self.bind("<Escape>", self.cancel)
    237
    238      # Standard button semantics.
    239 -    def ok(self, event = None):
    240
    241          if not self.validate():
    242              self.initial_focus.focus_set()
    243              return
    244
    245          self.withdraw()
    246          self.update_idletasks()
    247
    248          self.apply()
    249
    250          self.cancel()
    251
    252 -    def cancel(self, event=None):
    253
    254          # Put focus back to the parent window
    255          self.parent.focus_set()
    256          self.destroy()
    257
    258 -    def validate(self):
    259          return True
    260
    261 -    def apply(self):
    262          """
    263          This method will set the nMOLDYN configuration.
    264          """
    265
    266          # The nMOLDYN configuration variables are set to the value defined in the dialog.
    267          for k in self.preferences.keys():
    268              if hasattr(PREFERENCES, k):
    269                  if self.preferences[k].getValue() is None:
    270                      setattr(PREFERENCES, k, '')
    271                  else:
    272                      setattr(PREFERENCES, k, self.preferences[k].getValue())
    273
    274 -    def changePage(self):
    275
    276          page = self.pages.getValue()
    277
    278          for w in self.preferencesFrame.winfo_children():
    279              w.grid_forget()
    280
    281          if page == 'File handling':
    282              self.preferences['logfile_path'].grid(row = 0, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    283              self.preferences['logfile_path'].grid_columnconfigure(0, weight = 1)
    284
    285              self.preferences['trajfile_path'].grid(row = 1, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    286              self.preferences['trajfile_path'].grid_columnconfigure(0, weight = 1)
    287
    288              self.preferences['outputfile_path'].grid(row = 2, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    289              self.preferences['outputfile_path'].grid_columnconfigure(0, weight = 1)
    290
    291              self.preferences['pyro_server_configfile'].grid(row = 3, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    292              self.preferences['pyro_server_configfile'].grid_columnconfigure(0, weight = 1)
    293
    294          elif page == 'External programs':
    295              self.preferences['acroread_path'].grid(row = 0, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    296              self.preferences['acroread_path'].grid_columnconfigure(0, weight = 1)
    297
    298              self.preferences['vmd_path'].grid(row = 1, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    299              self.preferences['vmd_path'].grid_columnconfigure(0, weight = 1)
    300
    301              self.preferences['ncdump_path'].grid(row = 2, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    302              self.preferences['ncdump_path'].grid_columnconfigure(0, weight = 1)
    303
    304              self.preferences['ncgen_path'].grid(row = 3, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    305              self.preferences['ncgen_path'].grid_columnconfigure(0, weight = 1)
    306
    307          elif page == 'Miscellaneous':
    308              self.preferences['progress_rate'].grid(row = 0, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    309              self.preferences['progress_rate'].grid_columnconfigure(0, weight = 1)
    310
    311              self.preferences['documentation_style'].grid(row = 1, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    312              self.preferences['documentation_style'].grid_columnconfigure(0, weight = 1)
    313
    314              self.preferences['warning'].grid(row = 2, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    315              self.preferences['warning'].grid_columnconfigure(0, weight = 1)
    316              self.preferences['warning'].grid_rowconfigure(2, weight = 1)
    317
    318              self.preferences['warning_ncdump'].grid(row = 0, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    319              self.preferences['warning_ncdump'].grid_columnconfigure(0, weight = 1)
    320
    321              self.preferences['warning_ncgen'].grid(row = 1, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    322              self.preferences['warning_ncgen'].grid_columnconfigure(0, weight = 1)
    323
    324              self.preferences['warning_acroread'].grid(row = 2, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    325              self.preferences['warning_acroread'].grid_columnconfigure(0, weight = 1)
    326
    327              self.preferences['warning_vmd'].grid(row = 3, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    328              self.preferences['warning_vmd'].grid_columnconfigure(0, weight = 1)
    329
    330              self.preferences['warning_autoupdate'].grid(row = 4, column = 0, padx = 2, pady = 5, sticky = 'NEW')
    331              self.preferences['warning_autoupdate'].grid_columnconfigure(0, weight = 1)
    332
    333 -    def openDirectory(self, prefKey):
    334
    335          directory = askdirectory(parent = self, title = 'Select directory')
    336
    337          if not directory:
    338              return
    339
    340          self.preferences[prefKey].setValue(directory)
    341
    342 -    def openFilename(self, prefKey):
    343
    344          filename = askopenfilename(parent = self, title = 'Select filename')
    345
    346          if not filename:
    347              return
    348
    349          self.preferences[prefKey].setValue(filename)
    350
    351 -    def savePreferences(self):
    352          """
    353          This method will save the preferences defined in the configuration dialog into a file.
    354          """
    355
    356          try:
    357              # The Win32 config file directory.
    358              if sys.platform == 'win32':
    359                  cfgFileDir = os.path.join(os.environ['USERPROFILE'],'Application Data','nMOLDYN')
    360                  initialName = 'nMOLDYN.ini'
    361              elif sys.platform == 'darwin':
    362                  cfgFileDir = os.path.join(os.environ['HOME'],'Library','Preferences','nMOLDYN')
    363                  initialName = 'nMOLDYN.pref'
    364              else:
    365                  cfgFileDir = os.environ['HOME']
    366                  initialName = '.nMOLDYN'
    367          except KeyError:
    368              cfgFileDir = None
    369          else:
    370              if not os.path.exists(cfgFileDir):
    371                  os.makedirs(cfgFileDir)
    372
    373          filename = asksaveasfilename(parent = self,\
    374                                       title = 'Save nMOLDYN configuration file',\
    375                                       initialdir = cfgFileDir,\
    376                                       initialfile = initialName)
    377          if not filename:
    378              return
    379
    380          cfg = dummy()
    381
    382          setattr(cfg, 'logfile_path'          , self.preferences['logfile_path'].getValue())
    383          setattr(cfg, 'trajfile_path'         , self.preferences['trajfile_path'].getValue())
    384          setattr(cfg, 'pyro_server_configfile', self.preferences['pyro_server_configfile'].getValue())
    385          setattr(cfg, 'outputfile_path'       , self.preferences['outputfile_path'].getValue())
    386          setattr(cfg, 'acroread_path'         , self.preferences['acroread_path'].getValue())
    387          setattr(cfg, 'vmd_path'              , self.preferences['vmd_path'].getValue())
    388          setattr(cfg, 'ncdump_path'           , self.preferences['ncdump_path'].getValue())
    389          setattr(cfg, 'ncgen_path'            , self.preferences['ncgen_path'].getValue())
    390          setattr(cfg, 'progress_rate'         , self.preferences['progress_rate'].getValue())
    391          setattr(cfg, 'documentation_style'   , self.preferences['documentation_style'].getValue())
    392          setattr(cfg, 'warning_ncdump'        , self.preferences['warning_ncdump'].getValue())
    393          setattr(cfg, 'warning_ncgen'         , self.preferences['warning_ncgen'].getValue())
    394          setattr(cfg, 'warning_acroread'      , self.preferences['warning_acroread'].getValue())
    395          setattr(cfg, 'warning_vmd'           , self.preferences['warning_vmd'].getValue())
    396          setattr(cfg, 'warning_autoupdate'    , self.preferences['warning_autoupdate'].getValue())
    397
    398          saveConfigurationFile(cfgFilename = filename, config = cfg)
    399
    400 -    def loadPreferences(self):
    401          """
    402          This method will load a preferences file and display its contents into the configuration dialog..
    403          """
    404
    405          try:
    406              # The Win32 config file directory.
    407              if sys.platform == 'win32':
    408                  cfgFileDir = os.path.join(os.environ['USERPROFILE'],'Application Data','nMOLDYN')
    409                  initialName = 'nMOLDYN.ini'
    410              elif sys.platform == 'darwin':
    411                  cfgFileDir = os.path.join('Library','Preferences','nMOLDYN')
    412                  initialName = 'nMOLDYN.pref'
    413              # The POSIX config file directory.
    414              else:
    415                  cfgFileDir = os.environ['HOME']
    416                  initialName = '.nMOLDYN'
    417          except KeyError:
    418              cfgFileDir = None
    419
    420          filename = askopenfilename(parent = self,\
    421                                     title = 'Open nMOLDYN configuration file',\
    422                                     initialdir = cfgFileDir,\
    423                                     initialfile = initialName)
    424
    425          if not filename:
    426              return
    427
    428          cfg = dummy()
    429          loadConfigurationFile(cfgFilename = filename, currentCfg = cfg)
    430          # The configuration dialog is updated with the configuration file contents.
    431          for k in vars(cfg):
    432              self.preferences[k].setValue(getattr(cfg,k))
    433