Skip to content

Commit

Permalink
Merge pull request #50 from jbellister-slac/filter_segfault
Browse files Browse the repository at this point in the history
FIX: Prevent segfaults when filtering the alarm table
  • Loading branch information
jbellister-slac authored May 1, 2023
2 parents 2debb33 + 968e9fc commit 6f686ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 2 additions & 4 deletions slam/alarm_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,17 @@ def append(self, alarm_item: AlarmItem) -> None:
if alarm_item.name in self.alarm_items:
logger.warning(f'Attempting to append a row to the alarm table which is already there: {alarm_item.name}')
return
self.beginInsertRows(QModelIndex(), len(self.alarm_items), len(self.alarm_items))
self.layoutAboutToBeChanged.emit()
self.alarm_items[alarm_item.name] = alarm_item
self.endInsertRows()
self.layoutChanged.emit()

def remove_row(self, alarm_name: str):
""" Removes the row associated with the input name from this table """
if alarm_name not in self.alarm_items:
return
index_to_remove = list(self.alarm_items.keys()).index(alarm_name)
self.beginRemoveRows(QModelIndex(), index_to_remove, index_to_remove)
self.layoutAboutToBeChanged.emit()
del self.alarm_items[alarm_name]
self.endRemoveRows()
self.layoutChanged.emit()

def sort(self, col: int, order=Qt.AscendingOrder):
Expand Down
22 changes: 14 additions & 8 deletions slam/alarm_table_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import socket
from functools import partial
from kafka.producer import KafkaProducer
from qtpy.QtCore import QEvent, QSortFilterProxyModel, Qt, Signal
from qtpy.QtCore import QEvent, QModelIndex, QSortFilterProxyModel, Qt, Signal
from qtpy.QtGui import QCursor
from qtpy.QtWidgets import (QAbstractItemView, QAction, QApplication, QHBoxLayout, QHeaderView, QLabel,
QLineEdit, QMenu, QPushButton, QTableView, QVBoxLayout, QWidget)
from typing import Callable
from typing import Callable, List
from .alarm_table_model import AlarmItemsTableModel
from .alarm_tree_model import AlarmItemsTreeModel
from .permissions import UserAction, can_take_action
Expand Down Expand Up @@ -122,8 +122,7 @@ def __init__(self, tree_model: AlarmItemsTreeModel, kafka_producer: KafkaProduce
self.layout.addWidget(self.filter_active_label)
self.layout.addWidget(self.alarmView)

self.alarmModel.rowsInserted.connect(self.update_counter_label)
self.alarmModel.rowsRemoved.connect(self.update_counter_label)
self.alarmModel.layoutChanged.connect(self.update_counter_label)

def filter_table(self) -> None:
""" Filter the table based on the text typed into the filter bar """
Expand All @@ -150,17 +149,24 @@ def alarm_context_menu_event(self, ev: QEvent) -> None:
""" Display the right-click context menu for items in the active alarms table """
self.alarm_context_menu.popup(QCursor.pos())

def get_selected_indices(self) -> List[QModelIndex]:
""" Return the indices which have been selected by the user, applying a mapping if a filter has been applied """
indices = self.alarmView.selectionModel().selectedRows()
if self.filter_active_label.isVisible():
indices = [self.alarm_proxy_model.mapToSource(proxy_index) for proxy_index in indices]
return indices

def plot_pv(self) -> None:
""" Send off the signal for plotting a PV """
indices = self.alarmView.selectedIndexes()
indices = self.get_selected_indices()
if len(indices) > 0:
index = indices[0]
alarm_item = list(self.alarmModel.alarm_items.items())[index.row()][1]
self.plot_signal.emit(alarm_item.name)

def copy_alarm_name_to_clipboard(self) -> None:
""" Copy the selected PV to the user's clipboard """
indices = self.alarmView.selectionModel().selectedRows()
indices = self.get_selected_indices()
if len(indices) > 0:
copy_text = ''
for index in indices:
Expand All @@ -174,7 +180,7 @@ def send_acknowledge_action(self, acknowledged: bool) -> None:
if not can_take_action(UserAction.ACKNOWLEDGE, log_warning=True):
return

indices = self.alarmView.selectionModel().selectedRows()
indices = self.get_selected_indices()
if len(indices) > 0:
for index in indices:
alarm_item = list(self.alarmModel.alarm_items.items())[index.row()][1]
Expand All @@ -191,4 +197,4 @@ def send_acknowledge_action(self, acknowledged: bool) -> None:
self.kafka_producer.send(self.topic + 'Command',
key=f'command:{alarm_path}',
value={'user': username, 'host': hostname, 'command': command_to_send})

self.alarmView.selectionModel().reset()

0 comments on commit 6f686ab

Please sign in to comment.