"Fossies" - the Fresh Open Source Software Archive

Member "sk1-2.0rc4/src/wal/listwidgets.py" (25 May 2019, 6739 Bytes) of package /linux/misc/sk1-2.0rc4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "listwidgets.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0rc3_vs_2.0rc4.

    1 # -*- coding: utf-8 -*-
    2 #
    3 #  Copyright (C) 2015-2018 by Igor E. Novikov
    4 #
    5 #  This program 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 #  This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 import wx
   19 import wx.lib.mixins.listctrl as listmix
   20 
   21 import const
   22 from mixins import WidgetMixin
   23 
   24 
   25 class SimpleList(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, WidgetMixin):
   26     data = []
   27     select_cmd = None
   28     activate_cmd = None
   29     alt_color = False
   30 
   31     def __init__(self, parent, data=None, border=True, header=False,
   32                  single_sel=True, virtual=False, alt_color=False,
   33                  even_color=None, odd_color=None,
   34                  on_select=None, on_activate=None):
   35         self.data = data or []
   36         self.alt_color = alt_color
   37         self.odd_color = odd_color or const.ODD_COLOR
   38         self.even_color = even_color or const.EVEN_COLOR
   39         style = wx.LC_REPORT | wx.LC_VRULES
   40         if not const.IS_WX3:
   41             style |= wx.BORDER_MASK if border else wx.NO_BORDER
   42         style = style | wx.LC_NO_HEADER if not header else style
   43         style = style | wx.LC_SINGLE_SEL if single_sel else style
   44         style = style | wx.LC_VIRTUAL if virtual else style
   45         wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=style)
   46         listmix.ListCtrlAutoWidthMixin.__init__(self)
   47         if self.data:
   48             self.update(self.data)
   49         if on_select:
   50             self.select_cmd = on_select
   51             self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_select, self)
   52         if on_activate:
   53             self.activate_cmd = on_activate
   54             self.Bind(wx.wx.EVT_LIST_ITEM_ACTIVATED, self.on_activate, self)
   55 
   56     def set_active(self, index):
   57         if len(self.data) - 1 >= index:
   58             self.Select(index, True)
   59 
   60     def clear_all(self):
   61         self.ClearAll()
   62 
   63     def set_column_width(self, index, width):
   64         self.SetColumnWidth(index, width)
   65 
   66     def update(self, data):
   67         self.DeleteAllItems()
   68         self.data = data
   69         if not self.GetColumnCount():
   70             self.set_columns()
   71         self.set_data(self.data, self.alt_color)
   72         self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
   73 
   74     def set_columns(self):
   75         self.InsertColumn(0, '')
   76 
   77     def set_data(self, data, alt_color=True):
   78         even = False
   79         i = 0
   80         for item in data:
   81             item = const.tr(item)
   82             self.Append([item])
   83             if alt_color:
   84                 list_item = self.GetItem(i)
   85                 if even:
   86                     list_item.SetBackgroundColour(self.even_color)
   87                 else:
   88                     list_item.SetBackgroundColour(self.odd_color)
   89                 self.SetItem(list_item)
   90                 even = not even
   91                 i += 1
   92 
   93     def on_select(self, *args):
   94         index = self.GetFocusedItem()
   95         ret = self.data[index] if index >= 0 else None
   96         self.select_cmd(ret)
   97 
   98     def on_activate(self, *args):
   99         index = self.GetFocusedItem()
  100         if not index < 0:
  101             self.activate_cmd(self.data[index])
  102 
  103     def get_selected(self):
  104         index = self.GetFocusedItem()
  105         return self.data[index] if not index < 0 else None
  106 
  107     def get_active(self):
  108         return self.GetFocusedItem()
  109 
  110 
  111 class ReportList(SimpleList):
  112     def __init__(self, parent, data=None, border=True, header=True,
  113                  single_sel=True, virtual=False, alt_color=True,
  114                  even_color=None, odd_color=None,
  115                  on_select=None, on_activate=None):
  116         data = data or []
  117         SimpleList.__init__(self, parent, data, border, header, single_sel,
  118                             virtual, alt_color, even_color, odd_color,
  119                             on_select, on_activate)
  120 
  121     def set_columns(self):
  122         for item in self.data[0]:
  123             index = self.data[0].index(item)
  124             self.InsertColumn(index, const.tr(item))
  125 
  126     def set_data(self, data, alt_color=True):
  127         even = False
  128         i = 0
  129         cols = len(data[0])
  130         subheader = any(isinstance(i, str) for i in data)
  131         for item in data[1:]:
  132             if isinstance(item, list):
  133                 list_item = [const.tr(label) for label in item]
  134             elif isinstance(item, str):
  135                 list_item = [const.tr(item), ] + ['', ] * (cols - 1)
  136             else:
  137                 continue
  138             self.Append(list_item)
  139             list_item = self.GetItem(i)
  140             if subheader:
  141                 if isinstance(item, str):
  142                     list_item.SetBackgroundColour(self.even_color)
  143                     self.SetItem(list_item)
  144             elif alt_color:
  145                 color = self.even_color if even else self.odd_color
  146                 list_item.SetBackgroundColour(color)
  147                 self.SetItem(list_item)
  148                 even = not even
  149             i += 1
  150 
  151     def on_select(self, *args):
  152         index = self.GetFocusedItem()
  153         ret = self.data[index + 1] if index >= 0 else None
  154         self.select_cmd(ret)
  155 
  156     def on_activate(self, *args):
  157         index = self.GetFocusedItem()
  158         if not index < 0:
  159             self.activate_cmd(self.data[index + 1])
  160 
  161     def get_selected(self):
  162         index = self.GetFocusedItem()
  163         return self.data[index + 1] if not index < 0 else None
  164 
  165 
  166 class VirtualList(SimpleList):
  167     def __init__(self, parent, data=None, border=True, header=True,
  168                  single_sel=True, virtual=True, alt_color=True,
  169                  even_color=None, odd_color=None,
  170                  on_select=None, on_activate=None):
  171         data = data or []
  172         SimpleList.__init__(self, parent, data, border, header, single_sel,
  173                             virtual, alt_color, even_color, odd_color,
  174                             on_select, on_activate)
  175 
  176     def OnGetItemText(self, item, col):
  177         return self.get_item_text(item, col)
  178 
  179     def get_item_text(self, item, col):
  180         """
  181         Callback method. Should return item text for specified column. 
  182         """
  183         return ''
  184 
  185     def OnGetItemImage(self, item):
  186         return self.get_item_image(item)
  187 
  188     def get_item_image(self, item):
  189         """
  190         Callback method. Should return item icon index or -1.
  191         """
  192         return -1