Package Gnumed :: Package timelinelib :: Package plugin :: Package plugins :: Package exporters :: Module exporttolist |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.plugin.plugins.exporters.exporttolist

      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
     21  from timelinelib.wxgui.utils import BORDER
     22  from timelinelib.plugin.pluginbase import PluginBase
     23  from timelinelib.plugin.factory import EXPORTER
     24  from timelinelib.wxgui.components.dialogbuttonssizers.dialogbuttonsclosesizer import DialogButtonsCloseSizer
     25  import wx.lib.mixins.listctrl as listmix
     26
     27
     28 -class ListExporter(PluginBase):
     29
     30 -    def service(self):
     31          return EXPORTER
     32
     33 -    def display_name(self):
     34          return _("Export to Listbox...")
     35
     36 -    def run(self, main_frame):
     37          self.db = main_frame.timeline
     38          dlg = ListboxDialog(self.display_name()[:-3])
     39          dlg.populate(self._get_events(main_frame))
     40          dlg.ShowModal()
     41          dlg.Destroy()
     42
     43 -    def _get_events(self, main_frame):
     44          visible_categories = self._get_visible_categories(main_frame)
     45          return [
     46              (
     47                  self.db.get_time_type().format_period(event.get_time_period()),
     48                  event.get_text()
     49              )
     50              for event
     51              in sorted(
     52                  self.db.get_all_events(),
     53                  key=lambda event: event.get_start_time()
     54              )
     55              if event.get_category() in visible_categories
     56          ]
     57
     58 -    def _get_visible_categories(self, main_frame):
     59          if main_frame.config.filtered_listbox_export:
     60              vp = main_frame.get_view_properties()
     61              return [cat for cat in self.db.get_categories() if vp.is_category_visible(cat)]
     62          else:
     63              return [cat for cat in self.db.get_categories()]
     64
     65
     66 -class ListboxDialog(wx.Dialog):
     67
     68 -    def __init__(self, title, parent=None, events=None):
     69          wx.Dialog.__init__(self, parent, title=title, style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
     70          self._create_gui()
     71          if events is not None:
     72              self.populate(events)
     73
     74 -    def populate(self, events):
     75          self.list.populate(events)
     76
     77 -    def _create_gui(self):
     78          self.list = TestListCtrl(self)
     79          self.btn_copy = self._create_copy_button()
     80          vbox = self._create_vbox(self.list, self.btn_copy, DialogButtonsCloseSizer(self))
     81          self.SetSizerAndFit(vbox)
     82
     83 -    def _create_copy_button(self):
     84          btn_copy = wx.Button(self, wx.ID_COPY)
     85          self.Bind(wx.EVT_BUTTON, self.on_copy, btn_copy)
     86          return btn_copy
     87
     88 -    def on_copy(self, evt):
     89          if wx.TheClipboard.Open():
     90              self._copy_text_to_clipboard()
     91          else:
     92              self.view.DisplayErrorMessage(_("Unable to copy to clipboard."))
     93
     94 -    def _copy_text_to_clipboard(self):
     95          obj = wx.TextDataObject(self.GetText())
     96          wx.TheClipboard.SetData(obj)
     97          wx.TheClipboard.Close()
     98
     99 -    def GetText(self):
    100          return self.list.GetText()
    101
    102 -    def _create_vbox(self, ctrl, btn_copy, btn_box):
    103          hbox = wx.BoxSizer(wx.HORIZONTAL)
    104          hbox.Add(btn_copy, 0, flag=wx.ALL | wx.EXPAND, border=BORDER)
    105          hbox.Add(btn_box, 1, flag=wx.ALL, border=BORDER)
    106
    107          vbox = wx.BoxSizer(wx.VERTICAL)
    108          vbox.Add(ctrl, 1, flag=wx.ALL | wx.EXPAND, border=BORDER)
    109          vbox.Add(hbox, 0, flag=wx.ALL | wx.EXPAND, border=BORDER)
    110          return vbox
    111
    112
    113 -class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    114
    115 -    def __init__(self, parent, pos=wx.DefaultPosition, size=(400, 400), style=wx.LC_REPORT):
    116          wx.ListCtrl.__init__(self, parent, wx.ID_ANY, pos, size, style)
    117          listmix.ListCtrlAutoWidthMixin.__init__(self)
    118
    119 -    def populate(self, items):
    120          import sys
    121          self.InsertColumn(0, _("Time period"))
    122          self.InsertColumn(1, _("Event"))
    123          for period, event in items:
    124              index = self.InsertStringItem(sys.maxint, period, 0)
    125              self.SetStringItem(index, 1, event)
    126          self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
    127          self.SetColumnWidth(1, wx.LIST_AUTOSIZE)
    128
    129 -    def GetText(self):
    130          collector = []
    131          for i in range(self.GetItemCount()):
    132              item = self.GetItem(i, 0)
    133              collector.append(item.GetText())
    134              collector.append("\t")
    135              item = self.GetItem(i, 1)
    136              collector.append(item.GetText())
    137              collector.append("\n")
    138          return "".join(collector)
    139