Skip to content

Commit

Permalink
Fixes for delete_row() in GridBox implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
ejeschke committed Aug 8, 2023
1 parent 67148e3 commit cf22146
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 70 deletions.
70 changes: 37 additions & 33 deletions ginga/gtk3w/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,14 +1886,15 @@ def add_widget(self, child, row, col, stretch=0):
self.make_callback('widget-added', child)

def remove(self, child, delete=False):
super().remove(child, delete=delete)

# need to delete the child from self.tbl
children = list(self.tbl.values())
keys = list(self.tbl.keys())
idx = children.index(child)
key = keys[idx]
del self.tbl[key]

super().remove(child, delete=delete)
if child in children:
keys = list(self.tbl.keys())
idx = children.index(child)
key = keys[idx]
del self.tbl[key]

def get_widget_at_cell(self, row, col):
return self.tbl[(row, col)]
Expand All @@ -1902,26 +1903,27 @@ def insert_row(self, index, widgets):
if len(widgets) != self.num_cols:
raise ValueError("Number of widgets ({}) != number of columns ({})".format(len(widgets), self.num_cols))

self.num_rows += 1
self.resize_grid(self.num_rows, self.num_cols)
self.resize_grid(self.num_rows + 1, self.num_cols)

xoptions = (Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK)
yoptions = (Gtk.AttachOptions.FILL | Gtk.AttachOptions.SHRINK)

# handle case where user inserts row before the end of the gridbox
if index < self.num_rows - 1:
if index < self.num_rows:
# shift key/value pairs down to make the row empty at index
for i in range(self.num_rows - 2, index - 1, -1):
for i in range(self.num_rows - 1, index - 1, -1):
for j in range(self.num_cols):
child = self.tbl[(i, j)]
self.tbl[(i + 1, j)] = child
# move actual widget down in GtkTable
w = child.get_widget()
self._remove(w)
row, col = i + 1, j
self.widget.attach(w, col, col + 1, row, row + 1,
xoptions=xoptions, yoptions=yoptions,
xpadding=0, ypadding=0)
key = (i, j)
if key in self.tbl:
child = self.tbl.pop(key)
self.tbl[(i + 1, j)] = child
# move actual widget down in GtkTable
w = child.get_widget()
self._remove(w)
row, col = i + 1, j
self.widget.attach(w, col, col + 1, row, row + 1,
xoptions=xoptions, yoptions=yoptions,
xpadding=0, ypadding=0)

for j in range(self.num_cols):
child = widgets[j]
Expand All @@ -1934,6 +1936,7 @@ def insert_row(self, index, widgets):
xoptions=xoptions, yoptions=yoptions,
xpadding=0, ypadding=0)

self.num_rows += 1
self.widget.show_all()

for child in widgets:
Expand All @@ -1952,26 +1955,27 @@ def delete_row(self, index):

# remove widgets in row from table
for j in range(self.num_cols):
child = self.tbl.pop((index, j))
self.remove(child)
key = (index, j)
if key in self.tbl:
child = self.tbl.pop(key)
self.remove(child)

if index < self.num_rows - 1:
# if not removing very last row,
# shift dict key, value pairs up
for i in range(index + 1, self.num_rows):
for j in range(self.num_cols):
child = self.tbl[(i, j)]
self.tbl[(i - 1, j)] = child
# move actual widget up in GtkTable
w = child.get_widget()
self._remove(w)
row, col = i - 1, j
self.widget.attach(w, col, col + 1, row, row + 1,
xoptions=xoptions, yoptions=yoptions,
xpadding=0, ypadding=0)
# delete items in last row to maintain self.tbl
for j in range(self.num_cols):
self.tbl.pop((self.num_rows - 1, j))
key = (i, j)
if key in self.tbl:
child = self.tbl.pop(key)
self.tbl[(i - 1, j)] = child
# move actual widget up in GtkTable
w = child.get_widget()
self._remove(w)
row, col = i - 1, j
self.widget.attach(w, col, col + 1, row, row + 1,
xoptions=xoptions, yoptions=yoptions,
xpadding=0, ypadding=0)

self.num_rows -= 1
self.resize_grid(self.num_rows, self.num_cols)
Expand Down
28 changes: 12 additions & 16 deletions ginga/qtw/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,14 +1631,15 @@ def add_widget(self, child, row, col, stretch=0):
self.make_callback('widget-added', child)

def remove(self, child, delete=False):
super().remove(child, delete=delete)

# need to delete the child from self.tbl
children = list(self.tbl.values())
keys = list(self.tbl.keys())
idx = children.index(child)
key = keys[idx]
del self.tbl[key]

super().remove(child, delete=delete)
if child in children:
keys = list(self.tbl.keys())
idx = children.index(child)
key = keys[idx]
del self.tbl[key]

def get_widget_at_cell(self, row, col):
return self.tbl[(row, col)]
Expand All @@ -1652,16 +1653,16 @@ def insert_row(self, index, widgets):
# handle case where user inserts row before the end of the gridbox
if index < num_rows:
# shift key/value pairs down to make the row empty at index
for i in range(num_rows, index, -1):
for i in range(num_rows - 1, index - 1, -1):
for j in range(num_cols):
key = (i - 1, j)
key = (i, j)
if key in self.tbl:
child = self.tbl.pop(key)
self.tbl[(i, j)] = child
self.tbl[(i + 1, j)] = child
# move actual widget down in QGridLayout
w = child.get_widget()
self._remove(w)
self.widget.layout().addWidget(w, i, j)
self.widget.layout().addWidget(w, i + 1, j)

for j in range(num_cols):
child = widgets[j]
Expand Down Expand Up @@ -1690,17 +1691,12 @@ def delete_row(self, index):
for j in range(num_cols):
key = (i, j)
if key in self.tbl:
child = self.tbl[key]
child = self.tbl.pop(key)
self.tbl[(i - 1, j)] = child
# move actual widget up in QGridLayout
w = child.get_widget()
self._remove(w)
self.widget.layout().addWidget(w, i - 1, j)
# delete items in last row to maintain self.tbl
for j in range(num_cols):
key = (num_rows - 1, j)
if key in self.tbl:
self.tbl.pop(key)


class ToolbarAction(WidgetBase):
Expand Down
78 changes: 57 additions & 21 deletions ginga/web/pgw/Widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2392,7 +2392,7 @@ class GridBox(ContainerBase):
%(content)s
</table>
<script type="text/javascript">
// see python method insert_row in this widget
// see python method insert_row() in this widget
ginga_app.add_widget_custom_method('%(id)s', 'insert_row',
function (elt, msg) {
let index = msg.value[0];
Expand All @@ -2403,7 +2403,18 @@ class GridBox(ContainerBase):
cell.innerHTML = msg.value[2][j];
}
});
// see python method update_cell in this widget
// see python method append_row() in this widget
ginga_app.add_widget_custom_method('%(id)s', 'append_row',
function (elt, msg) {
let index = -1;
let numColumns = msg.value[0];
let newRow = document.getElementById("%(id)s").insertRow(index);
for (let j = 0; j < numColumns; j++) {
let cell = newRow.insertCell(j);
cell.innerHTML = msg.value[1][j];
}
});
// see python method update_cell() in this widget
ginga_app.add_widget_custom_method('%(id)s', 'update_cell',
function (elt, msg) {
let i = msg.value[0];
Expand All @@ -2413,7 +2424,7 @@ class GridBox(ContainerBase):
let cell = row.cells[j];
cell.innerHTML = msg.value[2];
});
// see python method delete_row in this widget
// see python method delete_row() in this widget
ginga_app.add_widget_custom_method('%(id)s', 'delete_row',
function (elt, msg) {
document.getElementById("%(id)s").deleteRow(msg.value);
Expand Down Expand Up @@ -2480,14 +2491,15 @@ def add_widget(self, child, row, col, stretch=0):
self.make_callback('widget-added', child)

def remove(self, child, delete=False):
super().remove(child, delete=delete)

# need to delete the child from self.tbl
children = list(self.tbl.values())
keys = list(self.tbl.keys())
idx = children.index(child)
key = keys[idx]
del self.tbl[key]

super().remove(child, delete=delete)
if child in children:
keys = list(self.tbl.keys())
idx = children.index(child)
key = keys[idx]
del self.tbl[key]

def get_widget_at_cell(self, row, col):
return self.tbl[(row, col)]
Expand All @@ -2496,14 +2508,14 @@ def insert_row(self, index, widgets):
if len(widgets) != self.num_cols:
raise ValueError("Number of widgets ({}) != number of columns ({})".format(len(widgets), self.num_cols))

self.num_rows += 1

# handle case where user inserts row before the end of the gridbox
if index < self.num_rows - 1:
if index < self.num_rows:
# shift key/value pairs down to make the row empty at index
for i in range(self.num_rows - 2, index - 1, -1):
for i in range(self.num_rows - 1, index - 1, -1):
for j in range(self.num_cols):
self.tbl[(i + 1, j)] = self.tbl[(i, j)]
key = (i, j)
if key in self.tbl:
self.tbl[(i + 1, j)] = self.tbl.pop(key)

html = []
for j in range(self.num_cols):
Expand All @@ -2519,30 +2531,54 @@ def insert_row(self, index, widgets):
args = [index, self.num_cols, html]
app.do_operation("insert_row", id=self.id, value=args)

self.num_rows += 1

for child in widgets:
self.make_callback('widget-added', child)

def append_row(self, widgets):
return self.insert_row(self.num_rows, widgets)
if len(widgets) != self.num_cols:
raise ValueError("Number of widgets ({}) != number of columns ({})".format(len(widgets), self.num_cols))

index = self.num_rows
self.num_rows += 1

html = []
for j in range(self.num_cols):
child = widgets[j]
# populate inserted row with widgets for render_body()
self.tbl[(index, j)] = widgets[j]
self.add_ref(child)
if self._rendered:
html.append(child.render())

if self._rendered:
app = self.get_app()
args = [self.num_cols, html]
app.do_operation("append_row", id=self.id, value=args)

for child in widgets:
self.make_callback('widget-added', child)

def delete_row(self, index):
if index < 0 or index >= self.num_rows:
raise ValueError("Index ({}) out of bounds ({})".format(index, self.num_rows))

# remove widgets in row from table
for j in range(self.num_cols):
child = self.tbl.pop((index, j))
self.remove(child)
key = (index, j)
if key in self.tbl:
child = self.tbl.pop(key)
self.remove(child)

if index < self.num_rows - 1:
# if not removing very last row,
# shift dict key, value pairs up
for i in range(index + 1, self.num_rows):
for j in range(self.num_cols):
self.tbl[(i - 1, j)] = self.tbl[(i, j)]
# delete items in last row to maintain self.tbl
for j in range(self.num_cols):
self.tbl.pop((self.num_rows - 1, j))
key = (i, j)
if key in self.tbl:
self.tbl[(i - 1, j)] = self.tbl.pop(key)

self.num_rows -= 1
if self._rendered:
Expand Down

0 comments on commit cf22146

Please sign in to comment.