Package Gnumed :: Package wxpython :: Module gmCfgWidgets |
[frames] | no frames]

# Source Code for Module Gnumed.wxpython.gmCfgWidgets

      1  """GNUmed configuration related widgets.
      2  """
      3  #================================================================
      4  __version__ = '$Revision: 1.4 $'
      5  __author__ = 'karsten.hilbert@gmx.net'
      6  __license__ = 'GPL v2 or later (details at http://www.gnu.org)'
      7
      8  # stdlib
      9  import logging, sys
     10
     11
     12  # 3rd party
     13  import wx
     14
     15
     16  # GNUmed
     17  if __name__ == '__main__':
     18          sys.path.insert(0, '../../')
     19  from Gnumed.pycommon import gmCfg
     20  from Gnumed.pycommon import gmNetworkTools
     21  from Gnumed.pycommon import gmTools
     22  from Gnumed.pycommon import gmDispatcher
     23  from Gnumed.pycommon import gmCfg2
     24  from Gnumed.pycommon import gmWorkerThread
     25  from Gnumed.pycommon import gmPG2
     26  from Gnumed.business import gmPraxis
     27  from Gnumed.wxpython import gmGuiHelpers
     28  from Gnumed.wxpython import gmListWidgets
     29
     30
     31  _log = logging.getLogger('gm.ui')
     32  _log.info(__version__)
     33
     34  #==============================================================================
     35 -def _get_update_status():
     36
     37          dbcfg = gmCfg.cCfgSQL()
     38          url = dbcfg.get2 (
     39                  option = 'horstspace.update.url',
     40                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
     41                  bias = 'workplace',
     42                  default = 'http://www.gnumed.de/downloads/gnumed-versions.txt'
     43          )
     44          consider_latest_branch = bool(dbcfg.get2 (
     45                  option = 'horstspace.update.consider_latest_branch',
     46                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
     47                  bias = 'workplace',
     48                  default = True
     49          ))
     50
     51          _cfg = gmCfg2.gmCfgData()
     52          update_found, msg = gmNetworkTools.check_for_update (
     53                  url = url,
     54                  current_branch = _cfg.get(option = 'client_branch'),
     55                  current_version = _cfg.get(option = 'client_version'),
     56                  consider_latest_branch = consider_latest_branch
     57          )
     58
     59          return update_found, msg
     60
     61  #------------------------------------------------------------------------------
     62 -def _signal_update_status(status):
     63
     64          update_found, msg = status
     65          if update_found is False:
     66                  _cfg = gmCfg2.gmCfgData()
     67                  gmDispatcher.send(signal = 'statustext', msg = _('Your client (%s) is up to date.') % _cfg.get(option = 'client_version'))
     68                  return
     69
     70          gmGuiHelpers.gm_show_info (
     71                  msg,
     72                  _('Checking for client updates')
     73          )
     74
     75  #------------------------------------------------------------------------------
     76 -def _async_signal_update_status(status):
     77          gmPG2.discard_pooled_connection()
     78          wx.CallAfter(_signal_update_status, status)
     79
     80  #------------------------------------------------------------------------------
     81 -def check_for_updates(do_async=False):
     82
     83          if do_async:
     84                  gmWorkerThread.execute_in_worker_thread (
     85                          payload_function = _get_update_status,
     86                          payload_kwargs = None,
     87                          completion_callback = _async_signal_update_status,
     88                          worker_name = 'UpdChk'
     89                  )
     90                  return
     91
     92          _signal_update_status(_get_update_status())
     93
     94  #================================================================
     95 -def list_configuration(parent=None):
     96
     97          if parent is None:
     98                  parent = wx.GetApp().GetTopWindow()
     99
    100          #---------------
    101          def refresh(lctrl):
    102                  opts = gmCfg.get_all_options(order_by = 'owner, workplace, option')
    103
    104                  items = [ [
    105                          o['owner'],
    106                          o['workplace'],
    107                          o['option'],
    108                          o['value'],
    109                          o['type'],
    110                          gmTools.coalesce(o['description'], '')
    111                  ] for o in opts ]
    112                  lctrl.set_string_items(items)
    113                  lctrl.set_data(opts)
    114          #---------------
    115          def tooltip(item):
    116                  return (
    117                          '%s %s (#%s) %s\n'
    118                          '\n'
    119                          ' %s @ %s\n'
    120                          '\n'
    121                          ' %s: %s\n'
    122                          '%s'
    123                  ) % (
    124                          gmTools.u_box_horiz_single * 3,
    125                          item['option'],
    126                          item['pk_cfg_item'],
    127                          gmTools.u_box_horiz_single * 3,
    128                          item['owner'],
    129                          item['workplace'],
    130                          item['type'],
    131                          gmTools.wrap(
    132                                  text = item['value'],
    133                                  width = 40,
    134                                  subsequent_indent = ' ' * 8
    135                          ),
    136                          gmTools.wrap (
    137                                  text = gmTools.coalesce(item['description'], '', '\n%s'),
    138                                  width = 40,
    139                                  initial_indent = ' ',
    140                                  subsequent_indent = ' '
    141                          )
    142                  )
    143          #---------------
    144          def delete(item):
    145                  delete_it = gmGuiHelpers.gm_show_question (
    146                          aTitle = _('Deleting option'),
    147                          aMessage = '%s\n\n%s %s (#%s) %s\n\n%s\n\n%s' % (
    148                                  tooltip(item),
    149                                  gmTools.u_box_horiz_single * 3,
    150                                  item['option'],
    151                                  item['pk_cfg_item'],
    152                                  gmTools.u_box_horiz_single * 3,
    153                                  _('Do you really want to delete this option ?'),
    154                                  _('(GNUmed will re-create options as needed.)')
    155                          )
    156                  )
    157                  if not delete_it:
    158                          return False
    159
    160                  from Gnumed.wxpython.gmAuthWidgets import get_dbowner_connection
    161                  conn = get_dbowner_connection(procedure = _('Deleting option'))
    162                  if conn is None:
    163                          gmDispatcher.send(signal = 'statustext', msg = _('Cannot connect as database owner. Unable to delete option.'))
    164                          return False
    165
    166                  cfg = gmCfg.cCfgSQL()
    167                  cfg.delete(conn = conn, pk_option = item['pk_cfg_item'])
    168                  return True
    169          #---------------
    170          gmListWidgets.get_choices_from_list (
    171                  parent = parent,
    172                  msg = _('All configured options currently in the database.'),
    173                  caption = _('All configured options'),
    174                  columns = [ _('User'), _('Workplace'), _('Option'), _('Value'), _('Type'), _('Description') ],
    175                  refresh_callback = refresh,
    176                  delete_callback = delete,
    177                  ignore_OK_button = True,
    178                  list_tooltip_callback = tooltip
    179          )
    180
    181  #================================================================
    182 -def configure_string_from_list_option(parent=None, message=None, option=None, bias='user', default_value='', choices=None, columns=None, data=None, caption=None):
    183
    184          dbcfg = gmCfg.cCfgSQL()
    185
    186          current_value = dbcfg.get2 (
    187                  option = option,
    188                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    189                  bias = bias,
    190                  default = default_value
    191          )
    192
    193          if parent is None:
    194                  parent = wx.GetApp().GetTopWindow()
    195
    196          if caption is None:
    197                  caption = _('Configuration')
    198
    199          selections = None
    200          if current_value is not None:
    201                  try:
    202                          selections = [choices.index(current_value)]
    203                  except ValueError:
    204                          pass
    205
    206          choice = gmListWidgets.get_choices_from_list (
    207                  parent = parent,
    208                  msg = message,
    209                  caption = caption,
    210                  choices = choices,
    211                  columns = columns,
    212                  data = data,
    213                  selections = selections,
    214                  single_selection = True,
    215                  can_return_empty = False
    216          )
    217
    218          # aborted
    219          if choice is None:
    220                  return
    221
    222          # same value selected again
    223          if choice == current_value:
    224                  return
    225
    226          dbcfg = gmCfg.cCfgSQL()
    227          dbcfg.set (
    228                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    229                  option = option,
    230                  value = choice
    231          )
    232
    233          return
    234
    235  #================================================================
    236 -def configure_list_from_list_option(parent=None, message=None, option=None, bias='user', default_value=None, choices=None, columns=None, data=None, caption=None, picks=None):
    237
    238          if default_value is None:
    239                  default_value = []
    240
    241          dbcfg = gmCfg.cCfgSQL()
    242
    243          current_value = dbcfg.get2 (
    244                  option = option,
    245                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    246                  bias = bias,
    247                  default = default_value
    248          )
    249
    250          if parent is None:
    251                  parent = wx.GetApp().GetTopWindow()
    252
    253          if caption is None:
    254                  caption = _('Configuration')
    255
    256          # setup item picker
    257          picker = gmListWidgets.cItemPickerDlg(parent, -1, msg = message)
    258          picker.set_columns(columns)
    259          picker.set_choices(choices)
    260          picker.set_picks(picks)
    261          result = picker.ShowModal()
    262          if result == wx.ID_CANCEL:
    263                  picker.DestroyLater()
    264                  return
    265
    266          picks = picker.get_picks()
    267          picker.DestroyLater()
    268
    269          dbcfg.set (
    270                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    271                  option = option,
    272                  value = picks
    273          )
    274
    275          return
    276
    277  #================================================================
    278 -def configure_string_option(parent=None, message=None, option=None, bias='user', default_value='', validator=None):
    279
    280          dbcfg = gmCfg.cCfgSQL()
    281
    282          current_value = dbcfg.get2 (
    283                  option = option,
    284                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    285                  bias = bias,
    286                  default = default_value
    287          )
    288
    289          if current_value is not None:
    290                  current_value = '%s' % current_value
    291
    292          if parent is None:
    293                  parent = wx.GetApp().GetTopWindow()
    294
    295          while True:
    296                  dlg = wx.TextEntryDialog (
    297                          parent,
    298                          message,
    299                          caption = _('Configuration'),
    300                          value = gmTools.coalesce(current_value, ''),
    301                          style = wx.OK | wx.CANCEL | wx.CENTRE
    302                  )
    303                  result = dlg.ShowModal()
    304                  if result == wx.ID_CANCEL:
    305                          dlg.DestroyLater()
    306                          return None
    307
    308                  user_val = dlg.GetValue().strip()
    309                  dlg.DestroyLater()
    310
    311                  if user_val == current_value:
    312                          return user_val
    313
    314                  validated, user_val = validator(user_val)
    315                  if validated:
    316                          break
    317
    318                  gmDispatcher.send (
    319                          signal = 'statustext',
    320                          msg = _('Value [%s] not valid for option <%s>.') % (user_val, option),
    321                          beep = True
    322                  )
    323
    324          dbcfg = gmCfg.cCfgSQL()
    325          dbcfg.set (
    326                  workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    327                  option = option,
    328                  value = user_val
    329          )
    330
    331          return user_val
    332
    333  #================================================================
    334 -def configure_boolean_option(parent=None, question=None, option=None, button_tooltips=None):
    335
    336          if parent is None:
    337                  parent = wx.GetApp().GetTopWindow()
    338
    339          tooltips = [
    340                  _('Set "%s" to <True>.') % option,
    341                  _('Set "%s" to <False>.') % option,
    342                  _('Abort the dialog and do not change the current setting.')
    343          ]
    344          if button_tooltips is not None:
    345                  for idx in range(len(button_tooltips)):
    346                          tooltips[idx] = button_tooltips[idx]
    347
    348          dlg = gmGuiHelpers.c3ButtonQuestionDlg (
    349                  parent,
    350                  -1,
    351                  caption = _('Configuration'),
    352                  question = question,
    353                  button_defs = [
    354                          {'label': _('Yes'), 'tooltip': tooltips[0]},
    355                          {'label': _('No'), 'tooltip': tooltips[1]},
    356                          {'label': _('Cancel'), 'tooltip': tooltips[2], 'default': True}
    357                  ]
    358          )
    359
    360          decision = dlg.ShowModal()
    361          dbcfg = gmCfg.cCfgSQL()
    362          if decision == wx.ID_YES:
    363                  dbcfg.set (
    364                          workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    365                          option = option,
    366                          value = True
    367                  )
    368          elif decision == wx.ID_NO:
    369                  dbcfg.set (
    370                          workplace = gmPraxis.gmCurrentPraxisBranch().active_workplace,
    371                          option = option,
    372                          value = False
    373                  )
    374
    375          return
    376
    377  #================================================================
    378  if __name__ == '__main__':
    379
    380          from Gnumed.pycommon import gmI18N
    381          gmI18N.activate_locale()
    382          gmI18N.install_domain()
    383
    384          if len(sys.argv) < 2:
    385                  sys.exit()
    386
    387          if sys.argv[1] != 'test':
    388                  sys.exit()
    389
    390          check_for_updates()
    391