Skip to content

Commit 0d997d7

Browse files
authored
Merge pull request wxWidgets#1604 from wxWidgets/grid-updates
Add wrappers for the GridBlockCoords, GridBlocks, and GridBlockDiffResult
2 parents bf0d92f + 3eef67f commit 0d997d7

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

CHANGES.rst

+7
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ New and improved in this release:
9494
the Animation objects are created. See the AnimationCtrl.py sample in the demo
9595
for the various usage patterns (#1579)
9696

97+
* Added wrappers for the wx.grid.GridBlockCoords, wx.grid.GridBlocks, and
98+
wx.grid.GridBlockDiffResult classes, as well as associated new methods in the
99+
wx.grid.Grid class. These provide a new way to interact with blocks of
100+
selected cells, including an iterator interface in wx.grid.GridBlocks which
101+
should be a more efficient (time and memory) way to process large groups of
102+
selections.
103+
97104

98105

99106

docs/sphinx/itemToModuleMap.json

+4
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,9 @@
14341434
"GraphicsRenderer":"wx.",
14351435
"Grid":"wx.grid.",
14361436
"GridBagSizer":"wx.",
1437+
"GridBlockCoords":"wx.grid.",
1438+
"GridBlockDiffResult":"wx.grid.",
1439+
"GridBlocks":"wx.grid.",
14371440
"GridCellAttr":"wx.grid.",
14381441
"GridCellAttrProvider":"wx.grid.",
14391442
"GridCellAttrPtr":"wx.grid.",
@@ -7003,6 +7006,7 @@
70037006
"__DECLARE_EVT2":"wx.",
70047007
"byte":"wx.",
70057008
"date2pydate":"wx.",
7009+
"iterator":"wx.grid.GridBlocks.",
70067010
"operator!=":"wx.",
70077011
"operator*":"wx.",
70087012
"operator+":"wx.",

etg/grid.py

+99
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
# this script.
2020
ITEMS = [ 'wxGridCellCoords',
2121

22+
'wxGridBlockCoords',
23+
'wxGridBlockDiffResult',
24+
'wxGridBlocks',
25+
2226
'wxGridCellRenderer',
2327
'wxGridCellStringRenderer',
2428
'wxGridCellAutoWrapStringRenderer',
@@ -133,6 +137,101 @@ def run():
133137
getItemCopy=True))
134138

135139

140+
#-----------------------------------------------------------------
141+
c = module.find('wxGridBlockCoords')
142+
tools.addAutoProperties(c)
143+
144+
c.find('operator!').ignore()
145+
c.addCppMethod('int', '__bool__', '()', "return self->operator!();")
146+
147+
c.addCppMethod('PyObject*', 'Get', '()', """\
148+
wxPyThreadBlocker blocker;
149+
return sipBuildResult(0, "(iiii)", self->GetTopRow(), self->GetLeftCol(), self->GetBottomRow(), self->GetRightCol());
150+
""",
151+
pyArgsString="() -> (topRow, leftCol, bottomRow, rightCol)",
152+
briefDoc="Return the block coordinants as a tuple.")
153+
154+
c.addPyMethod('__str__', '(self)', 'return str(self.Get())')
155+
c.addPyMethod('__repr__', '(self)', 'return "GridBlockCoords"+str(self.Get())')
156+
157+
#-----------------------------------------------------------------
158+
c = module.find('wxGridBlockDiffResult')
159+
c.find('m_parts').ignore()
160+
161+
c.addCppMethod('PyObject*', '_getParts', '()',
162+
"""\
163+
wxPyThreadBlocker blocker;
164+
PyObject* ret = PyTuple_New(4);
165+
if (ret) {
166+
PyTuple_SET_ITEM(ret, 0, wxPyConstructObject(&self->m_parts[0], "wxGridBlockCoords", false));
167+
PyTuple_SET_ITEM(ret, 1, wxPyConstructObject(&self->m_parts[1], "wxGridBlockCoords", false));
168+
PyTuple_SET_ITEM(ret, 2, wxPyConstructObject(&self->m_parts[2], "wxGridBlockCoords", false));
169+
PyTuple_SET_ITEM(ret, 3, wxPyConstructObject(&self->m_parts[3], "wxGridBlockCoords", false));
170+
}
171+
return ret;
172+
""")
173+
c.addProperty('m_parts', '_getParts')
174+
175+
176+
#-----------------------------------------------------------------
177+
c = module.find('wxGridBlocks')
178+
c.addPrivateDefaultCtor()
179+
180+
c.addPyMethod('__iter__', '(self)',
181+
'return PyGridBlocksIterator(self)',
182+
"Returns a Python iterator for acessing the collection of grid blocks.")
183+
184+
# This class is the Python iterator that knows how to fetch blocks from the
185+
# wxGridBlocks object
186+
c.addPyCode("""\
187+
class PyGridBlocksIterator(object):
188+
"A Python iterator for GridBlocks objects"
189+
def __init__(self, blocks):
190+
self._blocks = blocks
191+
self._iterator = self._blocks.begin()
192+
193+
def __next__(self):
194+
if self._iterator == self._blocks.end():
195+
raise StopIteration
196+
obj = self._iterator._get()
197+
self._iterator = self._iterator._next()
198+
return obj
199+
""")
200+
201+
202+
# c.find('iterator').addCppMethod('wxGridBlocks::iterator', '_next', '()',
203+
# "return self->operator++();")
204+
# c.find('iterator').addCppMethod('const wxGridBlockCoords&', '_get', '()',
205+
# "return self->operator*();")
206+
207+
# TODO: Doing these the normal way (above) breaks because it tries to use just
208+
# "iterator" for the self param type, instead of wxGridBlocks::iterator.
209+
# That should be fixable, but until then just add these methods the manual
210+
# sip way.
211+
c.find('iterator').addItem(etgtools.WigCode("""\
212+
iterator& _next();
213+
%MethodCode
214+
PyErr_Clear();
215+
Py_BEGIN_ALLOW_THREADS
216+
sipRes = &sipCpp->operator++();
217+
Py_END_ALLOW_THREADS
218+
if (PyErr_Occurred()) return 0;
219+
%End
220+
221+
const wxGridBlockCoords& _get() const;
222+
%MethodCode
223+
PyErr_Clear();
224+
Py_BEGIN_ALLOW_THREADS
225+
sipRes = new ::wxGridBlockCoords(sipCpp->operator*());
226+
Py_END_ALLOW_THREADS
227+
if (PyErr_Occurred()) return 0;
228+
%End
229+
230+
bool __eq__(const iterator& it) const;
231+
bool __ne__(const iterator& it) const;
232+
"""))
233+
234+
136235
#-----------------------------------------------------------------
137236
c = module.find('wxGridSizesInfo')
138237
c.find('m_customSizes').ignore() # TODO: Add support for wxUnsignedToIntHashMap??

ext/wxWidgets

Submodule wxWidgets updated 162 files

unittests/test_grid.py

+15
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,21 @@ def test_grid45(self):
355355
assert tl.Get() == (1,1)
356356
assert br.Get() == (5,5)
357357

358+
def test_grid46(self):
359+
g = wx.grid.Grid(self.frame)
360+
g.CreateGrid(10,10)
361+
g.SelectBlock((1,1), (5,5))
362+
g.SelectBlock((6,5), (7,9), addToSelected=True)
363+
364+
blocks = g.GetSelectedBlocks()
365+
assert isinstance(blocks, wx.grid.GridBlocks)
366+
367+
count = 0
368+
for block in blocks:
369+
count += 1
370+
assert isinstance(block, wx.grid.GridBlockCoords)
371+
372+
assert count == 2
358373

359374
#---------------------------------------------------------------------------
360375

0 commit comments

Comments
 (0)