How can I manage which cells are selected or visible

In a grid control, one or more cells can be selected by the user. In wxPython, there are several methods that allow you to manipulate the group of selected cells. You can also locate the exact cell containing the cursor, and the exact screen position of any specific cell in the grid.

At any given time, the selection in a grid control can be zero or more of the following:

■ a collection of individually selected cells

■ a collection of selected rows

■ a collection of select columns

■ a collection of selected blocks of cells

The user can select more than one group of cells by control- or command-clicking on cells, row or column labels, or dragging the mouse across cells. To determine whether there are any cells selected in the grid, use the method IsSelection() that returns True if there is a current selection in the grid of any kind. You can query whether any specific cell is in a current selection by using the method

IsInSelection(row, col), that returns True if the cell at the given coordinates is currently selected.

Table 14.2 displays several methods that allow you to get the current selection returned to you in different ways.

Table 14.2 Methods to return the set of currently selected cells

Method

Return Value

GetSelectedCells()

Returns a Python list of all the cells that were individually selected. Each item in the list is a tuple of (row, col) values.

GetSelectedCols()

Returns a Python list of the indexes of the columns that were selected by clicking the column labels.

GetSelectedRows()

Returns a Python list of the indexes of the rows that were selected by clicking the row labels.

GetSelectionBlockTopLeft()

Returns a Python list, each element of which is a (row, col) tuple of the top left corner of a currently selected rectangle.

GetSelectionBlockBottomRight()

Returns a Python list, each element of which is a (row, col) tuple of the bottom right corner of a currently selected rectangle.

There are also several methods for setting or modifying the selection. The first is ClearSelection() which removes any current selection. After this method is called, IsSelection() returns False. You can do the opposite action—make all the cells selected—with the method SelectAll(). You can select an entire column or row with the methods SelectCol(col, addToSelected=False) and Select-Row(row, addToSelected=False). In both cases, the first argument is the index of the row or column to select. If the addToSelected argument is True, all other currently selected cells remain selected and the row or column is added to the existing selection (simulating a control or command-click). If the addToSelected argument is False then all the other selected cells are deselected and the new row or column replaces them as the selection. You can similarly add a rectangular block with the method SelectBlock(topRow, leftCol, bottomRow, rightCol, addToSelected=False), where the first four arguments are the corners of the rectangle, and addToSelected behaves as in the previous methods.

You can tell whether a particular cell is visible in the current display by using the method IsVisible(row, col, wholeCellVisible=True). The method returns True if the cell at the given row and col attributes is currently displayed onscreen, as opposed to being in the hidden part of a scrolled container. If wholeCell-Visible is True, the entire cell must be visible for the method to return True, if the parameter is False, any part of the cell being visible is good enough. Conversely, the method MakeCellVisible(row, col) ensures that the cell at the given coordinates is visible with a minimal amount of scrolling.

In addition to the selected cells, the grid control also has a cursor cell that represents the cell with the current user focus. You can determine the current position of the cursor with the methods GetGridCursorCol() and GetGridCursorRow(), that return the appropriate integer index. You can place the cursor explicitly with the method SetGridCursor(row, col). Not only does this method move the cursor, it implicitly calls MakeCellVisible on the new cursor location.

Table 14.3 describes grid control methods that help convert between grid coordinates and display coordinates.

Table 14.3 Coordinate conversion methods

Method

Description

BlockToDeviceRect (topLeft, bottomRight)

The topLeft and bottomRight parameters are cell coordinates—in wxPython, pass them as (row, col) tuples. The return value is a wx.Rect in device pixel coordinates of the rectangle bounded by the given grid coordinates. If necessary, the rectangle is clipped to the size of the grid window.

CellToRect(row, col)

Returns a wx.Rect with the coordinates relative to the container for the cell at grid coordinates (row, col) .

XToCol(x)

Returns the integer index of the column containing the given x coordinate in relation to the container. If there is no column at that coordinate, returns wx.NOT_FOUND.

XToEdgeOfCol(x)

Returns the integer index of the column whose right edge is closest to the given x coordinate. If there is no such column, returns wx.not_found.

YToRow(y)

Returns the integer index of the row containing the given y coordinate. If there is no such row, returns wx.not_found.

YToEdgeOfRow(y)

Returns the row whose bottom edge is closest to the given y coordinate. If there is no such row, returns wx.not_found.

You could use these to convert the location of a mouse click to the grid cell that contains the click.

Was this article helpful?

0 0

Post a comment