class AbstractCalibrationTask(qt.QWidget): widgetShow = qt.Signal() widgetHide = qt.Signal() nextTaskRequested = qt.Signal() def __init__(self): super(AbstractCalibrationTask, self).__init__() self.__model = None self.installEventFilter(self) def initNextStep(self): if hasattr(self, "_nextStep"): self._nextStep.clicked.connect(self.nextTask) def eventFilter(self, widget, event): result = super(AbstractCalibrationTask, self).eventFilter(widget, event) if event.type() == qt.QEvent.Show: self.widgetShow.emit() elif event.type() == qt.QEvent.Hide: self.widgetHide.emit() return result def model(self): return self.__model def setModel(self, model): self.__model = model self._updateModel(model) def nextTask(self): self.nextTaskRequested.emit()
class AbstractModel(qt.QObject): changed = qt.Signal() def __init__(self, parent=None): qt.QObject.__init__(self, parent) self.__isLocked = 0 self.__wasChanged = False def isValid(self): return True def wasChanged(self): if self.__isLocked > 0: self.__wasChanged = True else: self.changed.emit() def lockSignals(self): self.__isLocked = self.__isLocked + 1 def unlockSignals(self): assert self.__isLocked > 0 self.__isLocked = self.__isLocked - 1 if self.__isLocked == 0 and self.__wasChanged: self.__wasChanged = False self.wasChanged()
class _PeakSelectionTableModel(qt.QAbstractTableModel): requestRingChange = qt.Signal(object, int) requestRemovePeak = qt.Signal(object) def __init__(self, parent, peakSelectionModel): assert isinstance(parent, PeakPickingTask) super(_PeakSelectionTableModel, self).__init__(parent=parent) self.__peakSelectionModel = peakSelectionModel peakSelectionModel.changed.connect(self.__invalidateModel) self.__callbacks = [] self.__invalidateModel() # QAbstractTableModel do not provide access to the parent self.__parent = parent def __invalidateModel(self): self.beginResetModel() for callback in self.__callbacks: target, method = callback target.changed.disconnect(method) self.__callbacks = [] for index, item in enumerate(self.__peakSelectionModel): callback = functools.partial(self.__invalidateItem, index) item.changed.connect(callback) self.__callbacks.append((item, callback)) self.endResetModel() def __invalidateItem(self, index): index1 = self.index(index, 0, qt.QModelIndex()) index2 = self.index(index, self.columnCount() - 1, qt.QModelIndex()) self.dataChanged.emit(index1, index2) def headerData(self, section, orientation, role=qt.Qt.DisplayRole): if orientation != qt.Qt.Horizontal: return None if role != qt.Qt.DisplayRole: return super(_PeakSelectionTableModel, self).headerData(section, orientation, role) if section == 0: return "Name" if section == 1: return "Peaks" if section == 2: return "Ring number" return None def flags(self, index): if index.column() == 2: return (qt.Qt.ItemIsEditable | qt.Qt.ItemIsEnabled | qt.Qt.ItemIsSelectable) return (qt.Qt.ItemIsEnabled | qt.Qt.ItemIsSelectable) def rowCount(self, parent=qt.QModelIndex()): return len(self.__peakSelectionModel) def columnCount(self, parent=qt.QModelIndex()): return 4 def data(self, index=qt.QModelIndex(), role=qt.Qt.DisplayRole): peakModel = self.__peakSelectionModel[index.row()] column = index.column() if role == qt.Qt.DecorationRole: if column == 0: color = peakModel.color() pixmap = qt.QPixmap(16, 16) pixmap.fill(color) icon = qt.QIcon(pixmap) return icon else: return None if role == qt.Qt.DisplayRole or role == qt.Qt.EditRole: if column == 0: return peakModel.name() if column == 1: return len(peakModel.coords()) if column == 2: return peakModel.ringNumber() return None def setData(self, index, value, role=qt.Qt.EditRole): if not index.isValid(): return peakModel = self.__peakSelectionModel[index.row()] column = index.column() if column == 2: self.requestRingChange.emit(peakModel, value) def removeRows(self, row, count, parent=qt.QModelIndex()): # while the tablempdel is already connected to the data model self.__peakSelectionModel.changed.disconnect(self.__invalidateModel) self.beginRemoveRows(parent, row, row + count - 1) for i in reversed(range(count)): peakModel = self.__peakSelectionModel[row + i] self.requestRemovePeak.emit(peakModel) self.endRemoveRows() # while the tablempdel is already connected to the data model self.__peakSelectionModel.changed.connect(self.__invalidateModel) return True