views:

217

answers:

2

I'm rewriting this post to clarify some things and provide a full class definition for the Virtual List I'm having trouble with. The class is defined like so:

from wx import ListCtrl, LC_REPORT, LC_VIRTUAL, LC_HRULES, LC_VRULES, \
  EVT_LIST_COL_CLICK, EVT_LIST_CACHE_HINT, EVT_LIST_COL_RIGHT_CLICK, \
  ImageList, IMAGE_LIST_SMALL, Menu, MenuItem, NewId, ITEM_CHECK, Frame, \
  EVT_MENU

class VirtualList(ListCtrl):
  def __init__(self, parent, datasource = None,
               style = LC_REPORT | LC_VIRTUAL | LC_HRULES | LC_VRULES):
    ListCtrl.__init__(self, parent, style = style)

    self.columns = []
    self.il = ImageList(16, 16)

    self.Bind(EVT_LIST_CACHE_HINT, self.CheckCache)
    self.Bind(EVT_LIST_COL_CLICK, self.OnSort)

    if datasource is not None:
      self.datasource = datasource
      self.Bind(EVT_LIST_COL_RIGHT_CLICK, self.ShowAvailableColumns)

      self.datasource.list = self

      self.Populate()

  def SetDatasource(self, datasource):
    self.datasource = datasource

  def CheckCache(self, event):
    self.datasource.UpdateCache(event.GetCacheFrom(), event.GetCacheTo())

  def OnGetItemText(self, item, col):
    return self.datasource.GetItem(item, self.columns[col])

  def OnGetItemImage(self, item):
    return self.datasource.GetImg(item)

  def OnSort(self, event):
    self.datasource.SortByColumn(self.columns[event.Column])
    self.Refresh()

  def UpdateCount(self):
    self.SetItemCount(self.datasource.GetCount())

  def Populate(self):
    self.UpdateCount()

    self.datasource.MakeImgList(self.il)

    self.SetImageList(self.il, IMAGE_LIST_SMALL)

    self.ShowColumns()

  def ShowColumns(self):
    for col, (text, visible) in enumerate(self.datasource.GetColumnHeaders()):
      if visible:
        self.columns.append(text)
        self.InsertColumn(col, text, width = -2)

  def Filter(self, filter):
    self.datasource.Filter(filter)

    self.UpdateCount()

    self.Refresh()

  def ShowAvailableColumns(self, evt):
    colMenu = Menu()

    self.id2item = {}

    for idx, (text, visible) in enumerate(self.datasource.columns):
      id = NewId()

      self.id2item[id] = (idx, visible, text)

      item = MenuItem(colMenu, id, text, kind = ITEM_CHECK)
      colMenu.AppendItem(item)

      EVT_MENU(colMenu, id, self.ColumnToggle)

      item.Check(visible)

    Frame(self, -1).PopupMenu(colMenu)

    colMenu.Destroy()

  def ColumnToggle(self, evt):
    toggled = self.id2item[evt.GetId()]

    if toggled[1]:
      idx = self.columns.index(toggled[2])

      self.datasource.columns[toggled[0]] = (self.datasource.columns[toggled[0]][0], False)

      self.DeleteColumn(idx)

      self.columns.pop(idx)

    else:
      self.datasource.columns[toggled[0]] = (self.datasource.columns[toggled[0]][0], True)

      idx = self.datasource.GetColumnHeaders().index((toggled[2], True))

      self.columns.insert(idx, toggled[2])

      self.InsertColumn(idx, toggled[2], width = -2)

    self.datasource.SaveColumns()

I've added functions that allow for Column Toggling which facilitate my description of the issue I'm encountering. On the 3rd instance of this class in my application the Column at Index 1 will not display String values. Integer values are displayed properly. If I add print statements to my OnGetItemText method the values show up in my console properly. This behavior is not present in the first two instances of this class, and my class does not contain any type checking code with respect to value display.

It was suggested by someone on the wxPython users' group that I create a standalone sample that demonstrates this issue if I can. I'm working on that, but have not yet had time to create a sample that does not rely on database access. Any suggestions or advice would be most appreciated. I'm tearing my hair out on this one.

A: 

Are you building on the wxPython demo code for virtual list controls? There are a couple of bookkeeping things you need to do, like set the ItemCount property.

One comment about your OnGetItemText method: Since there's no other return statement, it will return None if data is None, so your test has no effect.

How about return data or "" instead?

Ryan Ginstrom
The test for `if data is not None:` was meaningful prior to a refactor. Previously data was a dictionary object and I attempted to return the value from data. If data was None then I got a key error. I've changed the methods so I'm not passing as much data around and that makes the check extraneous. I appreciate you pointing that out.My full class does set the ItemCount properly. I omitted parts of the class definition to save space.
g.d.d.c
A: 

There's a problem with the native object in Windows. If GetImg returns None instead of -1 the list has a problem with column 1 for some reason. That from Robin over on the Google Group post for this issue.

g.d.d.c