Gnumed.timelinelib.wxgui.components.propertyeditors.iconeditor

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

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

      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 os.path
     20
     21  import wx
     22
     23  from timelinelib.wxgui.components.propertyeditors.baseeditor import BaseEditor
     24
     25
     26 -class FileToBitmapConverter(object):
     27
     28 -    def __init__(self):
     29          self.MAX_SIZE = (128, 128)
     30
     31 -    def convert(self, path):
     32          try:
     33              image = wx.EmptyImage(0, 0)
     34              success = image.LoadFile(path)
     35              # LoadFile will show error popup if not successful
     36              if success:
     37                  # Resize image if too large
     38                  (w, h) = image.GetSize()
     39                  (W, H) = self.MAX_SIZE
     40                  if w > W:
     41                      factor = float(W) / float(w)
     42                      w = w * factor
     43                      h = h * factor
     44                  if h > H:
     45                      factor = float(H) / float(h)
     46                      w = w * factor
     47                      h = h * factor
     48                  image = image.Scale(w, h, wx.IMAGE_QUALITY_HIGH)
     49                  return image.ConvertToBitmap()
     50          except:
     51              pass
     52
     53
     54 -class IconEditorGuiCreator(wx.Panel):
     55
     56 -    def __init__(self, parent):
     57          wx.Panel.__init__(self, parent)
     58
     59 -    def create_sizer(self):
     60          return wx.GridBagSizer(5, 5)
     61
     62 -    def create_controls(self):
     63          self.MAX_SIZE = (128, 128)
     64          self.img_icon = self._create_icon()
     65          description = self._create_description()
     66          btn_select = self._create_select_button()
     67          btn_clear = self._create_clear_button()
     68          return (description, btn_select, btn_clear, self.img_icon)
     69
     70 -    def put_controls_in_sizer(self, sizer, controls):
     71          description, btn_select, btn_clear, img_icon = controls
     72          sizer.Add(description, wx.GBPosition(0, 0), wx.GBSpan(1, 2))
     73          sizer.Add(btn_select, wx.GBPosition(1, 0), wx.GBSpan(1, 1))
     74          sizer.Add(btn_clear, wx.GBPosition(1, 1), wx.GBSpan(1, 1))
     75          sizer.Add(img_icon, wx.GBPosition(0, 2), wx.GBSpan(2, 1))
     76
     77 -    def _create_select_button(self):
     78          btn = wx.Button(self, wx.ID_OPEN)
     79          self.Bind(wx.EVT_BUTTON, self._btn_select_on_click, btn)
     80          return btn
     81
     82 -    def _create_clear_button(self):
     83          btn = wx.Button(self, wx.ID_CLEAR)
     84          self.Bind(wx.EVT_BUTTON, self._btn_clear_on_click, btn)
     85          return btn
     86
     87 -    def _create_description(self):
     88          label = _("Images will be scaled to fit inside a %ix%i box.")
     89          return wx.StaticText(self, label=label % self.MAX_SIZE)
     90
     91 -    def _create_icon(self):
     92          return wx.StaticBitmap(self, size=self.MAX_SIZE)
     93
     94
     95 -class IconEditor(BaseEditor, IconEditorGuiCreator):
     96
     97 -    def __init__(self, parent, editor, name=""):
     98          BaseEditor.__init__(self, parent, editor)
     99          IconEditorGuiCreator.__init__(self, parent)
    100          self.create_gui()
    101          self._initialize_data()
    102
    103 -    def get_data(self):
    104          return self.get_icon()
    105
    106 -    def set_data(self, data):
    107          self.set_icon(data)
    108
    109 -    def clear_data(self):
    110          self.set_icon(None)
    111
    112 -    def set_icon(self, bmp):
    113          self.bmp = bmp
    114          if self.bmp is None:
    115              self.img_icon.SetBitmap(wx.EmptyBitmap(1, 1))
    116          else:
    117              self.img_icon.SetBitmap(bmp)
    118          self.GetSizer().Layout()
    119
    120 -    def get_icon(self):
    121          return self.bmp
    122
    123 -    def _initialize_data(self):
    124          self.bmp = None
    125
    126 -    def _btn_select_on_click(self, evt):
    127          dialog = wx.FileDialog(self, message=_("Select Icon"),
    128                                 wildcard="*", style=wx.FD_OPEN)
    129          if dialog.ShowModal() == wx.ID_OK:
    130              try:
    131                  bitmap = FileToBitmapConverter().convert(dialog.GetPath())
    132                  self.set_icon(bitmap)
    133              except:
    134                  pass
    135          dialog.Destroy()
    136
    137 -    def _btn_clear_on_click(self, evt):
    138          self.set_icon(None)
    139