Browse Source

Bugfix in the gridLayout repack mechanism

pull/99/head
Eugenio Parodi 3 years ago
parent
commit
a059db2b2f
  1. 48
      TermTk/TTkLayouts/gridlayout.py
  2. 1
      ttkDesigner/app/superobj/supercontrol.py
  3. 35
      ttkDesigner/app/superobj/superlayoutgrid.py

48
TermTk/TTkLayouts/gridlayout.py

@ -78,15 +78,20 @@ class TTkGridLayout(TTkLayout):
rows, cols = size
self._rows, self._cols = size
self._horSizes = [(0,0)]*self._cols
self._verSizes = [(0,0)]*self._rows
# remove extra rows
if rows < len(self._gridItems):
self._gridItems = self._gridItems[:rows]
self._verSizes = self._verSizes[:rows]
elif rows > len(self._gridItems):
self._gridItems += [None]*(rows-len(self._gridItems))
self._verSizes += [(0,0)]*(rows-len(self._verSizes))
# remove extra cols
if cols < len(self._gridItems):
self._horSizes = self._verSizes[:cols]
elif cols > len(self._gridItems):
self._horSizes += [(0,0)]*(cols-len(self._horSizes))
for gridRow in range(len(self._gridItems)):
if self._gridItems[gridRow] is None:
self._gridItems[gridRow] = [None]*(cols)
@ -123,27 +128,30 @@ class TTkGridLayout(TTkLayout):
return self._gridItems
def repack(self):
# retrieve the empty cols and remove empty rows
cmap = [False]*self._cols
for r in self._gridItems:
if not(any(r)):
rown=coln= -1
# remove empty rows
for r in reversed(range(self._rows)):
if not(any([self.itemAtPosition(r,c) for c in range(self._cols)])):
# the row is empty
self._gridItems.remove(r)
else:
cmap = [cc or (rr is not None) for cc,rr in zip(cmap,r)]
#remove empty rows
for c in [i for i,ii in enumerate(cmap) if not ii]:
self._gridItems.pop(r)
# Realign the rows
for rown,r in enumerate(self._gridItems):
for coln,w in enumerate(r):
if w: w._row = rown
self._reshapeGrid((rown+1,self._cols))
#remove empty cols
unusedCols = []
for c in range(self._cols):
if not(any([self.itemAtPosition(r,c) for r in range(self._rows)])):
unusedCols.append(c)
for c in reversed(unusedCols):
for r in self._gridItems:
r.remove(r[c])
# adapt the widgets definition to the new list
rown=coln = -1
r.pop(c)
# Realign the cols
for rown,r in enumerate(self._gridItems):
for coln,w in enumerate(r):
if w:
w._col = coln
w._row = rown
self._rows = rown+1
self._cols = coln+1
if w: w._col = coln
self._reshapeGrid((rown+1,coln+1))
self.update()
def insertColumn(self, col):

1
ttkDesigner/app/superobj/supercontrol.py

@ -77,6 +77,7 @@ class SuperControlWidget(ttk.TTkResizableFrame):
def keyEvent(self, evt):
if evt.type == ttk.TTkK.SpecialKey:
if evt.key in (ttk.TTkK.Key_Delete, ttk.TTkK.Key_Backspace) :
self._wid.parentWidget().removeSuperWidget(self._wid)
self._wid.close()
self.close()
self._wid.weModified.emit()

35
ttkDesigner/app/superobj/superlayoutgrid.py

@ -68,19 +68,14 @@ class SuperLayoutGrid(SuperLayout):
self.layout().insertColumn(self._pushCol)
elif self._direction == ttk.TTkK.VERTICAL:
self.layout().insertRow(self._pushRow)
self.layout().addWidget(sw, self._pushRow, self._pushCol)
self.layout().addWidget(sw, self._pushRow, self._pushCol,1,1)
def _processDragOver(self, x, y):
# cehck the closest edge
col, row, dir = 0,0,None
ret = None
w,h = self.size()
dist = w+h
# Retrieve a list of widths,heights
hSizes = []
vSizes = []
cmw = self.layout().columnMinWidth()
rmh = self.layout().rowMinHeight()
rows,cols = self.layout().gridSize()
if not rows or not cols: return col,row,dir,ret
@ -99,32 +94,34 @@ class SuperLayoutGrid(SuperLayout):
ret = (ix, iy, iw, ih)
else:
#Top
if (dd := y-iy <= dist) and (self._orientation & ttk.TTkK.VERTICAL):
dist = dd
if ((y==iy) and (self._orientation & ttk.TTkK.VERTICAL) and
( row==0 or
( row>0 and self.layout().itemAtPosition(row,col) != self.layout().itemAtPosition(row-1,col)))):
dir = ttk.TTkK.VERTICAL
if row>0 and self.layout().itemAtPosition(row-1,col):
ret = (ix, iy-1, iw, 2)
else:
ret = (ix, iy, iw, 1)
#Bottom
if (dd := iy+ih-y <= dist) and (self._orientation & ttk.TTkK.VERTICAL):
dist = dd
if ((iy+ih==y+1) and (self._orientation & ttk.TTkK.VERTICAL) and
self.layout().itemAtPosition(row,col) != self.layout().itemAtPosition(row+1,col)):
dir = ttk.TTkK.VERTICAL
if row<rows-1 and self.layout().itemAtPosition(row+1,col):
ret = (ix, iy+ih-1, iw, 2)
else:
ret = (ix, iy+ih-1, iw, 1)
#Left
if (dd := x-ix <= dist) and (self._orientation & ttk.TTkK.HORIZONTAL):
dist = dd
if ((x==ix) and (self._orientation & ttk.TTkK.HORIZONTAL) and
( col==0 or
( col>0 and self.layout().itemAtPosition(row,col) != self.layout().itemAtPosition(row,col-1)))):
dir = ttk.TTkK.HORIZONTAL
if col>0 and self.layout().itemAtPosition(row,col-1):
ret = (ix-1, iy, 2, ih)
else:
ret = (ix, iy, 1, ih)
#Right
if (dd := ix+iw-x <= dist) and (self._orientation & ttk.TTkK.HORIZONTAL):
dist = dd
if ((ix+iw==x+1) and (self._orientation & ttk.TTkK.HORIZONTAL) and
self.layout().itemAtPosition(row,col) != self.layout().itemAtPosition(row,col+1)):
dir = ttk.TTkK.HORIZONTAL
if col<cols-1 and self.layout().itemAtPosition(row,col+1):
ret = (ix+iw-1, iy, 2, ih)
@ -132,12 +129,14 @@ class SuperLayoutGrid(SuperLayout):
ret = (ix+iw-1, iy, 1, ih)
# If we are on the edge of the item push to the next spot
if dir == ttk.TTkK.HORIZONTAL and ix+iw-x == dist:
if dir == ttk.TTkK.HORIZONTAL and ix+iw==x+1:
col+=1
if dir == ttk.TTkK.VERTICAL and iy+ih-y == dist:
if dir == ttk.TTkK.VERTICAL and iy+ih==y+1:
row+=1
ttk.TTkLog.debug(f"{row=} {self._dragOver=}")
# ttk.TTkLog.debug(f"{horSizes=}")
# ttk.TTkLog.debug(f"{verSizes=}")
# ttk.TTkLog.debug(f"{row=} {col=} {dir=} {self._dragOver=}")
self.update()
return row, col, dir, ret

Loading…
Cancel
Save