Gnumed.timelinelib.wxgui.components.propertyeditors.hyperlinkeditor

Package Gnumed :: Package timelinelib :: Package wxgui :: Package components :: Package propertyeditors :: Module hyperlinkeditor |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.components.propertyeditors.hyperlinkeditor

      1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  import wx
     20  import webbrowser
     21
     22  from timelinelib.wxgui.components.propertyeditors.baseeditor import BaseEditor
     23
     24
     25 -class HyperlinkEditorGuiCreator(wx.Panel):
     26
     27 -    def __init__(self, parent):
     28          wx.Panel.__init__(self, parent)
     29
     30 -    def create_sizer(self):
     31          return wx.GridBagSizer(5, 5)
     32
     33 -    def create_controls(self):
     34          self.btn_add = self._create_id_button(wx.ID_ADD, self._btn_add_on_click)
     35          self.btn_clear = self._create_id_button(wx.ID_CLEAR, self._btn_clear_on_click)
     36          self.btn_append = self._create_button(_("&Append"), self._btn_append_on_click)
     37          self.btn_remove = self._create_button(_("&Remove"), self._btn_remove_on_click)
     38          self.btn_test = self._create_button(_("Te&st"), self._btn_test_on_click)
     39          self.url_panel = self._create_input_controls()
     40          return (self.btn_add, self.btn_clear, self.btn_test, self.url_panel)
     41
     42 -    def put_controls_in_sizer(self, sizer, controls):
     43          self.btn_add, self.btn_clear, self.btn_test, self.url_panel = controls
     44          box_sizer = wx.BoxSizer(wx.HORIZONTAL)
     45          box_sizer.Add(self.btn_add)
     46          box_sizer.Add(self.btn_clear)
     47          box_sizer.Add(self.btn_append)
     48          box_sizer.Add(self.btn_remove)
     49          box_sizer.Add(self.btn_test)
     50          sizer.Add(box_sizer, wx.GBPosition(0, 0), wx.GBSpan(1, 1))
     51          sizer.Add(self.url_panel, wx.GBPosition(1, 0), wx.GBSpan(4, 5), wx.EXPAND | wx.ALL)
     52          sizer.AddGrowableRow(1)
     53          sizer.AddGrowableCol(0)
     54
     55 -    def _create_id_button(self, wxid, handler):
     56          btn = wx.Button(self, wxid)
     57          self.Bind(wx.EVT_BUTTON, handler, btn)
     58          return btn
     59
     60 -    def _create_button(self, label, handler):
     61          btn = wx.Button(self, wx.ID_ANY, label)
     62          self.Bind(wx.EVT_BUTTON, handler, btn)
     63          return btn
     64
     65 -    def _create_input_controls(self):
     66          TEXT_WIDTH = 300
     67          panel = wx.Panel(self)
     68          label = wx.StaticText(panel, label=_("URL:"))
     69          self.data = wx.TextCtrl(panel, size=(TEXT_WIDTH, 20))
     70          self.list = wx.ListBox(panel, wx.ID_ANY, size=(TEXT_WIDTH, -1))
     71          sizer = wx.GridBagSizer(5, 10)
     72          sizer.Add(label, wx.GBPosition(0, 0), wx.GBSpan(1, 1))
     73          sizer.Add(self.data, wx.GBPosition(0, 1), wx.GBSpan(1, 9), wx.EXPAND | wx.ALL, 1)
     74          sizer.Add(self.list, wx.GBPosition(1, 1), wx.GBSpan(1, 9), wx.EXPAND | wx.ALL, 1)
     75          sizer.AddGrowableRow(1)
     76          sizer.AddGrowableCol(1)
     77          panel.SetSizerAndFit(sizer)
     78          self.Bind(wx.EVT_LISTBOX, self._lb_on_click, self.list)
     79          self.data.Bind(wx.EVT_SET_FOCUS, self._txt_on_focus)
     80          return panel
     81
     82
     83 -class HyperlinkEditor(BaseEditor, HyperlinkEditorGuiCreator):
     84
     85 -    def __init__(self, parent, editor, name=""):
     86          BaseEditor.__init__(self, parent, editor)
     87          HyperlinkEditorGuiCreator.__init__(self, parent)
     88          self.create_gui()
     89          self._set_visible(False)
     90
     91 -    def get_data(self):
     92          if self.url_visible:
     93              urls = [item for item in self.list.GetItems() if len(item.strip()) > 0]
     94              if len(urls) > 0:
     95                  return ";".join(urls)
     96              else:
     97                  return None
     98          else:
     99              return None
    100
    101 -    def set_data(self, data):
    102          if data is None:
    103              self._set_visible(False)
    104          else:
    105              self._set_visible(True)
    106              self.list.InsertItems(data.split(";"), 0)
    107
    108 -    def _btn_add_on_click(self, evt):
    109          self._set_visible(True)
    110
    111 -    def _btn_clear_on_click(self, evt):
    112          self.clear_data()
    113
    114 -    def _btn_append_on_click(self, evt):
    115          if len(self.data.GetValue().strip()) > 0:
    116              self.list.Append(self.data.GetValue())
    117              self.data.SetValue("")
    118              self._change_btn_visibility()
    119          self.data.SetFocus()
    120
    121 -    def _btn_remove_on_click(self, evt):
    122          try:
    123              self.list.Delete(self.list.GetSelection())
    124          except:
    125              pass
    126          self.data.SetFocus()
    127
    128 -    def _btn_test_on_click(self, evt):
    129          url = self.list.GetStringSelection()
    130          if len(url) > 0:
    131              webbrowser.open(url)
    132
    133 -    def _lb_on_click(self, evt):
    134          self._change_btn_visibility()
    135
    136 -    def _txt_on_focus(self, evt):
    137          self.list.DeselectAll()
    138          self._change_btn_visibility()
    139
    140 -    def clear_data(self):
    141          self._set_visible(False)
    142
    143 -    def _set_visible(self, value):
    144          self.url_visible = value
    145          self.url_panel.Show(self.url_visible)
    146          self.btn_add.Enable(not value)
    147          self.btn_clear.Enable(value)
    148          self.btn_append.Enable(value)
    149          self.GetSizer().Layout()
    150          self.data.SetFocus()
    151
    152 -    def _change_btn_visibility(self):
    153          if self.list.GetSelection() == wx.NOT_FOUND:
    154              self.btn_remove.Enable(False)
    155              self.btn_test.Enable(False)
    156              self.btn_append.Enable(True)
    157          else:
    158              self.btn_remove.Enable(True)
    159              self.btn_test.Enable(True)
    160              self.btn_append.Enable(False)
    161