File size: 11,923 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
# ex: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:

#
#  Author(s):	Chris Abidin, Andrew LaMora
#  Created on: 2004-04-30
#
#  (C) Copyright 2004-2026 Johns Hopkins University (JHU), All Rights Reserved.

# --- begin cisst license - do not edit ---
# 
# This software is provided "as is" under an open source license, with
# no warranty.  The complete license can be found in license.txt and
# http://www.cisst.org/cisst/license.txt.
# 
# --- end cisst license ---

# ireListCtrlPanel.py

import wx, sys, string, os
import wx.lib.mixins.listctrl as listmix
import ireImages

if sys.version_info.major == 2:
    from types import StringTypes
else:
    StringTypes = str

class ireListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    def __init__(self, parent, ID, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
        listmix.ListCtrlAutoWidthMixin.__init__(self)

# Class:  ireListCtrlPanel
#
# This class implements the panel with a list control that
# is used for the register contents, scope variables, and
# command history.
#
# It is a little complicated by the fact that the command history
# is a single column (list of strings), whereas the other
# displays consist of 2 columns (list of tuples of strings).
# 
# The ColumnSorterMixin allows the columns to be sorted by clicking
# on the column header.  For the command history, we set the
# itemDataMap so that the sort is done by the command index
# (which starts at 0 for the first command).  The result is that
# the command history can be sorted by chronological or reverse-
# chronological order, which is more sensible than sorting the
# commands alphabetically.  The columns for the register contents
# and the scope variables are sorted alphabetically.
#
# There are two methods for adding items to the list control:
#
#    AddIndexedItem:  this is used to add a single string
#                     (e.g., command) and set the itemDataMap
#                     to sort by (chronological) index.
#
#    AddTupleItem:    this is used to add a tuple of strings
#                     (the tuple size should correspond to the
#                     number of columns) and set the itemDataMap
#                     to sort the columns alphabetically.
#
# There are other implementation options, such as merging them
# into a single method, with a flag to indicate how to handle the
# sorting.  Also, note that currently there is no method that
# accepts a single string and sorts alphabetically or 
# that accepts a tuple and sorts chronologically, but these
# are not needed.
#
# The ireListCtrlPanel can also be a source for drag-and-drop
# operations.  This is enabled by calling the EnableDropSource
# method.  The drag-and-drop operation is initiated by the
# left mouse button click (see OnLeftDown event handler).

class ireListCtrlPanel(wx.Panel, listmix.ColumnSorterMixin):

    dropSource = None

    def __init__(self, parent, Label, ColumnTitles=[], Data=[]):
        wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)

        tID = wx.NewId()

        # Create an image list for the up and down arrows.
        # For Windows, we also create a blank image as the first entry;
        # otherwise, every line in the list starts with a small up arrow
        # (this seems to be a wxWidgets bug on Windows).
        self.il = wx.ImageList(16, 16)
        if sys.platform == 'win32':
            self.blank = self.il.Add(wx.Bitmap(16,16,1), wx.Bitmap(16,16,1))
        up_bitmap = ireImages.getSmallUpArrowBitmap()
        down_bitmap = ireImages.getSmallDnArrowBitmap()
        self.sm_up = self.il.Add(up_bitmap)
        self.sm_dn = self.il.Add(down_bitmap)

        self.list = ireListCtrl(self, tID,
                                 style=wx.LC_REPORT 
                                 | wx.SUNKEN_BORDER
                                 | wx.LC_EDIT_LABELS
                                 #| wxLC_NO_HEADER
                                 #| wxLC_VRULES | wxLC_HRULES
                                 )

        self.numColumns = len(ColumnTitles)
        self.SetBackgroundColour(wx.BLUE)

        self.Text = wx.StaticText(self, -1, Label, wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_LEFT)
        self.Text.SetForegroundColour(wx.WHITE)
        Font = wx.Font(9, wx.FONTFAMILY_ROMAN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL)
        self.Text.SetFont(Font)

        self.list.SetImageList(self.il, wx.IMAGE_LIST_SMALL)

        # self.itemDataMap is needed for ColumnSorterMixin
        self.itemDataMap = {}

        self.PopulateList(ColumnTitles, Data)

        # Now that the list exists we can init the other base class,
        # see wxPython/lib/mixins/listctrl.py
        listmix.ColumnSorterMixin.__init__(self, self.numColumns)

        # Start with list sorted in ascending order
        self.SortListItems(0)

        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_LIST_DELETE_ITEM, self.OnItemDelete, self.list)
        self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginEdit, self.list)

        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)
        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list)
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list)

        self.list.Bind(wx.EVT_LEFT_DCLICK, self.OnDoubleClick)

        # for Drag-and-Drop (only need OnLeftDown)
        self.list.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
        self.list.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

    def EnableDropSource(self):
        self.dropSource = wx.DropSource(self)


    #------------------------------------------------------
    # Populate the list on launch
    #------------------------------------------------------
    def PopulateList(self, ColumnTitles, Data):
        for x in range(self.numColumns):
            self.list.InsertColumn(x, ColumnTitles[x])
            self.list.SetColumnWidth(x, wx.LIST_AUTOSIZE)
        for line in Data:
            if isinstance(line, StringTypes):
               self.AddIndexedItem(line)
            elif isinstance(line, tuple):
               self.AddTupleItem(line)


    #------------------------------------------------------
    # Add a tuple of strings to the list control.
    #
    # This is called for adding the scope variables and register
    # contents.
    #
    # The itemDataMap is updated so that the column sort is
    # based on the strings displayed in the selected column
    # (i.e., in alphabetical or reverse-alphabetical order).
    #------------------------------------------------------
    def AddTupleItem(self, Data):
        Index = self.list.GetItemCount()
        if isinstance(Data, tuple) and len(Data) > 0 and isinstance(Data[0], StringTypes):
            self.list.InsertItem(Index, Data[0])
            for i in range(1, len(Data)):
                self.list.SetItem(Index, i, Data[i])
            # Following two lines are needed for ColumnSorterMixin
            self.list.SetItemData(Index, Index)
            self.itemDataMap.update( {Index:Data} )
        else:
            print('ireListCtrlPanel.AddTupleItem: invalid data: ')
            print( Data)

    #------------------------------------------------------
    # Add a single string to the list control.
    #
    # This is called for adding commands to the command history.
    #
    # The itemDataMap is updated so that the sort is based
    # on the index, rather than the command.  Thus, it is
    # only possible to sort commands in chronological or
    # reverse-chronological order.
    #
    #------------------------------------------------------
    def AddIndexedItem(self, Data):
        Index = self.list.GetItemCount()
        if isinstance(Data, StringTypes):
            try:
               self.list.InsertItem(Index, Data)
               # Following two lines are needed for ColumnSorterMixin
               self.list.SetItemData(Index, Index)
               self.itemDataMap.update( {Index:(Index,)} )
               return Index
            except Exception as e:
               print('AddIndexedItem exception:', e)
        else:
            print('ireListCtrlPanel.AddIndexedItem: invalid data: ')
            print(Data)
        return -1


    
    #------------------------------------------------------
    # Sort the items in the list control.  It calls the
    # SortListItems method in the ColumnSorterMixin.
    # If no parameters are specified, the sort is done
    # using the current sorting parameters.  Otherwise,
    # the column to be sorted (and the sort order) can
    # be specified.
    # 
    # This method should be called after items are added to
    # the list.
    #------------------------------------------------------
    def SortList(self, col = -1, ascending = 1):
        self.SortListItems(col, ascending)

    #------------------------------------------------------
    # Clear items from the list control
    #------------------------------------------------------
    def ClearItems(self):
        self.list.DeleteAllItems()
        self.itemDataMap = {}

    #------------------------------------------------------
    # Get all items from the list control.  The items
    # are returned in their indexed order (i.e., independent
    # of the current sorting status).
    #------------------------------------------------------
    def GetAllItems(self):
        Data = list(range(self.list.GetItemCount()))
        for x in range(self.list.GetItemCount()):
           Data[self.list.GetItemData(x)] = self.list.GetItemText(x)
        return Data
    
    #------------------------------------------------------
    # Return an instance of the current list
    # Used by the ColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
    #------------------------------------------------------ 
    def GetListCtrl(self):
        return self.list

    # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
    def GetSortImages(self):
        return (self.sm_dn, self.sm_up)

    #------------------------------------------------------
    # Get Header text
    #------------------------------------------------------
    def getColumnText(self, index, col):
        item = self.list.GetItem(index, col)
        return item.GetText()


    #------------------------------------------------------
    # Get drag and drop data:  This function returns a string
    # that is formed by joining all lines of text that have
    # been selected.  This simplifies the drag and drop
    # implementation because wx.TextDataObject and
    # wx.TextDropTarget can be used.
    #------------------------------------------------------
    def GetDragAndDropData(self):
        Data = ''
        Index = self.list.GetFirstSelected()

        while Index != -1:
            Data.append( self.getColumnText(Index, 0) )
            Index = self.list.GetNextSelected(Index)

        return Data.join(os.linesep)

    # Left mouse button pressed
    def OnLeftDown(self, event):
        if self.dropSource:
            data = wx.TextDataObject(self.GetDragAndDropData())
            self.dropSource.SetData(data)
            self.dropSource.DoDragDrop(wx.Drag_AllowMove)
        event.Skip()


    # Left mouse button released
    def OnLeftUp(self, event):
        event.Skip()


    def OnBeginEdit(self, event):
        event.Veto()


    def OnItemDelete(self, event):
        event.Veto()


    def OnItemActivated(self, event):
        event.Skip()


    def OnItemSelected(self, event):
        event.Skip()


    def OnColClick(self, event):
        event.Skip()


    def OnDoubleClick(self, event):
        event.Skip()


    def OnSize(self, event):
        w,h = self.GetClientSize().Get()
        self.Text.SetSize(2, 0, w-2, 15)
        self.list.SetSize(0, 15, w, h-15)