def initUI(self): """ Set up the model and the view This sets up the layout as follows: 1. self.treeView widget 2. searchbox_layout layout 2.1 self.treeLineEdit (QtGui.QLineEdit) 2.2 self.treeComboBox (QtGui.QComboBox) """ self._precursor_model = PeptideTree( [], firstColumnName=self.first_column_name_) self.treeView = PeptidesTreeView() self.treeView.setModel(self._precursor_model) ## -> this would be how to sort but it seems to be buggy ## self.pProxyModel = QtGui.QSortFilterProxyModel() ## self.pProxyModel.setSourceModel(self._precursor_model) ## self.treeView.setModel(self.pProxyModel) ## self.treeView.setSortingEnabled(True) searchbox_layout = QtGui.QHBoxLayout() self.treeLineEdit = QtGui.QLineEdit() self.treeComboBox = QtGui.QComboBox() # Populate the ComboBox for i in range(self._precursor_model.columnCount(self)): self.treeComboBox.addItem( self._precursor_model.headerData(i, Qt.Horizontal, Qt.DisplayRole)) searchbox_layout.addWidget(self.treeLineEdit) searchbox_layout.addWidget(self.treeComboBox) # Combine the tree and the searchbox final_layout = QtGui.QVBoxLayout() final_layout.addWidget(self.treeView) final_layout.addLayout(searchbox_layout) self.setLayout(final_layout) # ## Connect the Signals # self.treeLineEdit.textChanged.connect(self.changedText) self.treeLineEdit.returnPressed.connect(self.changeReturnPressedSlot) # QItemSelectionModel -> connect the tree to here self.treeView.selectionModel().selectionChanged.connect( self.treeViewSelectionChangedSlot)
def initUI(self): """ Set up the model and the view This sets up the layout as follows: 1. self.treeView widget 2. searchbox_layout layout 2.1 self.treeLineEdit (QtGui.QLineEdit) 2.2 self.treeComboBox (QtGui.QComboBox) """ self._precursor_model = PeptideTree([], firstColumnName=self.first_column_name_) self.treeView = PeptidesTreeView() self.treeView.setModel(self._precursor_model) ## -> this would be how to sort but it seems to be buggy ## self.pProxyModel = QtGui.QSortFilterProxyModel() ## self.pProxyModel.setSourceModel(self._precursor_model) ## self.treeView.setModel(self.pProxyModel) ## self.treeView.setSortingEnabled(True) searchbox_layout = QtGui.QHBoxLayout() self.treeLineEdit = QtGui.QLineEdit() self.treeComboBox = QtGui.QComboBox() # Populate the ComboBox for i in range(self._precursor_model.columnCount(self)): self.treeComboBox.addItem(self._precursor_model.headerData(i, Qt.Horizontal, Qt.DisplayRole)) searchbox_layout.addWidget(self.treeLineEdit) searchbox_layout.addWidget(self.treeComboBox) # Combine the tree and the searchbox final_layout = QtGui.QVBoxLayout() final_layout.addWidget(self.treeView) final_layout.addLayout(searchbox_layout) self.setLayout(final_layout) # ## Connect the Signals # self.treeLineEdit.textChanged.connect(self.changedText) self.treeLineEdit.returnPressed.connect(self.changeReturnPressedSlot) # QItemSelectionModel -> connect the tree to here self.treeView.selectionModel().selectionChanged.connect(self.treeViewSelectionChangedSlot)
class PeptideTreeWidget(QtGui.QWidget): """ The Peptide Tree Widget is displayed on the left side of the main area (see :class:`.ApplicationView`). It consists of a :class:`.PeptidesTreeView` widget and a search box below. Attributes: - self._precursor_model: The underlying peptide tree model (of type :class:`.PeptideTree`) - self.treeView: The underlying peptide tree view widget (of type :class:`.PeptidesTreeView`) - self.treeLineEdit: A place for input below the tree - self.treeComboBox: A combo box relating to the column for search Emits the following signals: - selectionChanged : when the peptide selection is changed """ # Signals selectionChanged = QtCore.pyqtSignal(QModelIndex) """ Qt signal emitted when the peptide selection changes """ def __init__(self, firstColumnName): super(PeptideTreeWidget, self).__init__() self._precursor_model = None self.treeView = None self.first_column_name_ = firstColumnName self.initUI() def initUI(self): """ Set up the model and the view This sets up the layout as follows: 1. self.treeView widget 2. searchbox_layout layout 2.1 self.treeLineEdit (QtGui.QLineEdit) 2.2 self.treeComboBox (QtGui.QComboBox) """ self._precursor_model = PeptideTree( [], firstColumnName=self.first_column_name_) self.treeView = PeptidesTreeView() self.treeView.setModel(self._precursor_model) ## -> this would be how to sort but it seems to be buggy ## self.pProxyModel = QtGui.QSortFilterProxyModel() ## self.pProxyModel.setSourceModel(self._precursor_model) ## self.treeView.setModel(self.pProxyModel) ## self.treeView.setSortingEnabled(True) searchbox_layout = QtGui.QHBoxLayout() self.treeLineEdit = QtGui.QLineEdit() self.treeComboBox = QtGui.QComboBox() # Populate the ComboBox for i in range(self._precursor_model.columnCount(self)): self.treeComboBox.addItem( self._precursor_model.headerData(i, Qt.Horizontal, Qt.DisplayRole)) searchbox_layout.addWidget(self.treeLineEdit) searchbox_layout.addWidget(self.treeComboBox) # Combine the tree and the searchbox final_layout = QtGui.QVBoxLayout() final_layout.addWidget(self.treeView) final_layout.addLayout(searchbox_layout) self.setLayout(final_layout) # ## Connect the Signals # self.treeLineEdit.textChanged.connect(self.changedText) self.treeLineEdit.returnPressed.connect(self.changeReturnPressedSlot) # QItemSelectionModel -> connect the tree to here self.treeView.selectionModel().selectionChanged.connect( self.treeViewSelectionChangedSlot) def changeReturnPressedSlot(self): """ Slot connected to the signal generated by pressing return in the text field """ if self.treeiter is None: return column = self.treeComboBox.currentIndex() try: model_idx = self.treeiter.next() except StopIteration: # If we have reached the bottom, wrap around try: self.treeiter = self._iterIndices(self.treeView, column, self.treeLineEdit.text()) model_idx = self.treeiter.next() except StopIteration: # No match found return self.treeView.selectAndScrollTo(model_idx) def _iterIndices(self, treeView, column_, text_): """ Iterator generator function to go through all indexes """ s = re.compile(str(text_), re.IGNORECASE) m = treeView.model() for model_idx in treeView.iterAllLevelElements(column_): display_data = m.data(model_idx, Qt.DisplayRole) if s.search(display_data): yield model_idx def changedText(self, text): """ Slot connected to the signal generated by changing the text in the text field """ column = self.treeComboBox.currentIndex() try: self.treeiter = self._iterIndices(self.treeView, column, text) model_idx = self.treeiter.next() except StopIteration: # No match found return self.treeView.selectAndScrollTo(model_idx) @QtCore.pyqtSlot(QtGui.QItemSelectionModel, QtGui.QItemSelectionModel) def treeViewSelectionChangedSlot(self, newvalue, oldvalue): if len(newvalue.indexes()) == 0: return # assert that the the underlying selected element is always the same. assert isinstance(newvalue, QtGui.QItemSelection) assert all( x.internalPointer() == newvalue.indexes()[0].internalPointer() for x in newvalue.indexes()) self.selectionChanged.emit(newvalue.indexes()[0]) def expandLevel(self, level): if level == "Peptides": self.treeView.expandToDepth(0) elif level == "Precursors": self.treeView.expandToDepth(1) elif level == "smart": self.treeView.expandMultiElementItems() else: self.treeView.collapseAll() def get_precursor_model(self): """ Access to the underlying precursor model Returns ------- precursor_model : :class:`.PeptideTree` The underlying precursor model """ return self._precursor_model
class PeptideTreeWidget(QtGui.QWidget): # Signals selectionChanged = QtCore.pyqtSignal(QModelIndex) def __init__(self, firstColumnName): super(PeptideTreeWidget, self).__init__() self.first_column_name_ = firstColumnName self.initUI() def initUI(self): # Set up the model and the view self._precursor_model = PeptideTree([], firstColumnName=self.first_column_name_) self.treeView = PeptidesTreeView() self.treeView.setModel(self._precursor_model) ## -> this would be how to sort but it seems to be buggy ## self.pProxyModel = QtGui.QSortFilterProxyModel() ## self.pProxyModel.setSourceModel(self._precursor_model) ## self.treeView.setModel(self.pProxyModel) ## self.treeView.setSortingEnabled(True) searchbox_layout = QtGui.QHBoxLayout() self.treeLineEdit = QtGui.QLineEdit() self.treeComboBox = QtGui.QComboBox() # Populate the ComboBox for i in range(self._precursor_model.columnCount(self)): self.treeComboBox.addItem(self._precursor_model.headerData(i, Qt.Horizontal, Qt.DisplayRole)) searchbox_layout.addWidget(self.treeLineEdit) searchbox_layout.addWidget(self.treeComboBox) # Combine the tree and the searchbox self.leftside_layout = QtGui.QVBoxLayout() self.leftside_layout.addWidget(self.treeView) self.leftside_layout.addLayout(searchbox_layout) self.setLayout(self.leftside_layout) # ## Connect the Signals # self.treeLineEdit.textChanged.connect(self.changedTextTest) self.treeLineEdit.returnPressed.connect(self.changeReturnPressedTest) # QItemSelectionModel -> connect the tree to here self.treeView.selectionModel().selectionChanged.connect(self.treeViewSelectionChanged) def changeReturnPressedTest(self): if self.treeiter is None: return column = self.treeComboBox.currentIndex() try: model_idx = self.treeiter.next() except StopIteration: # If we have reached the bottom, wrap around try: self.treeiter = self.generate_it(self.treeView, column, self.treeLineEdit.text()) model_idx = self.treeiter.next() except StopIteration: # No match found return self.treeView.selectAndScrollTo(model_idx) # Iterator generator function to go through all indexes def generate_it(self, treeView, column_, text_): s = re.compile(str(text_), re.IGNORECASE) m = treeView.model() for model_idx in treeView.iterAllLevelElements(column_): display_data = m.data(model_idx, Qt.DisplayRole).toPyObject() if s.search(display_data): yield model_idx def changedTextTest(self, text): column = self.treeComboBox.currentIndex() try: self.treeiter = self.generate_it(self.treeView, column, text) model_idx = self.treeiter.next() except StopIteration: # No match found return self.treeView.selectAndScrollTo(model_idx) @QtCore.pyqtSlot(QtGui.QItemSelectionModel, QtGui.QItemSelectionModel) def treeViewSelectionChanged(self, newvalue, oldvalue): if len(newvalue.indexes()) == 0 : return # assert that the the underlying selected element is always the same. assert isinstance(newvalue, QtGui.QItemSelection) assert all(x.internalPointer() == newvalue.indexes()[0].internalPointer() for x in newvalue.indexes()) self.selectionChanged.emit(newvalue.indexes()[0]) def expandLevel(self, level): if level == "Peptides": self.treeView.expandToDepth(0) elif level == "Precursors": self.treeView.expandToDepth(1) elif level == "smart": self.treeView.expandMultiElementItems() else: self.treeView.collapseAll() def get_precursor_model(self): return self._precursor_model
class PeptideTreeWidget(QtGui.QWidget): """ The Peptide Tree Widget is displayed on the left side of the main area (see :class:`.ApplicationView`). It consists of a :class:`.PeptidesTreeView` widget and a search box below. Slots: :meth:`~TAPIR.PeptideTreeWidget.treeViewSelectionChangedSlot` : Handle a change in the selection in the tree (and pass forward through `selectionChanged` signal) Signals: selectionChanged : when the peptide selection is changed Attributes: - self._precursor_model: The underlying peptide tree model (of type :class:`.PeptideTree`) - self.treeView: The underlying peptide tree view widget (of type :class:`.PeptidesTreeView`) - self.treeLineEdit: A place for input below the tree - self.treeComboBox: A combo box relating to the column for search """ # Signals selectionChanged = QtCore.pyqtSignal(QModelIndex) """ Qt signal emitted when the peptide selection changes """ def __init__(self, firstColumnName): super(PeptideTreeWidget, self).__init__() self._precursor_model = None self.treeView = None self.first_column_name_ = firstColumnName self.initUI() def initUI(self): """ Set up the model and the view This sets up the layout as follows: 1. self.treeView widget 2. searchbox_layout layout 2.1 self.treeLineEdit (QtGui.QLineEdit) 2.2 self.treeComboBox (QtGui.QComboBox) """ self._precursor_model = PeptideTree([], firstColumnName=self.first_column_name_) self.treeView = PeptidesTreeView() self.treeView.setModel(self._precursor_model) ## -> this would be how to sort but it seems to be buggy ## self.pProxyModel = QtGui.QSortFilterProxyModel() ## self.pProxyModel.setSourceModel(self._precursor_model) ## self.treeView.setModel(self.pProxyModel) ## self.treeView.setSortingEnabled(True) searchbox_layout = QtGui.QHBoxLayout() self.treeLineEdit = QtGui.QLineEdit() self.treeComboBox = QtGui.QComboBox() # Populate the ComboBox for i in range(self._precursor_model.columnCount(self)): self.treeComboBox.addItem(self._precursor_model.headerData(i, Qt.Horizontal, Qt.DisplayRole)) searchbox_layout.addWidget(self.treeLineEdit) searchbox_layout.addWidget(self.treeComboBox) # Combine the tree and the searchbox final_layout = QtGui.QVBoxLayout() final_layout.addWidget(self.treeView) final_layout.addLayout(searchbox_layout) self.setLayout(final_layout) # ## Connect the Signals # self.treeLineEdit.textChanged.connect(self.changedText) self.treeLineEdit.returnPressed.connect(self.changeReturnPressedSlot) # QItemSelectionModel -> connect the tree to here self.treeView.selectionModel().selectionChanged.connect(self.treeViewSelectionChangedSlot) def changeReturnPressedSlot(self): """ Slot connected to the signal generated by pressing return in the text field """ if self.treeiter is None: return column = self.treeComboBox.currentIndex() try: model_idx = self.treeiter.next() except StopIteration: # If we have reached the bottom, wrap around try: self.treeiter = self._iterIndices(self.treeView, column, self.treeLineEdit.text()) model_idx = self.treeiter.next() except StopIteration: # No match found return self.treeView.selectAndScrollTo(model_idx) def _iterIndices(self, treeView, column_, text_): """ Iterator generator function to go through all indexes """ s = re.compile(str(text_), re.IGNORECASE) m = treeView.model() for model_idx in treeView.iterAllLevelElements(column_): display_data = m.data(model_idx, Qt.DisplayRole) if s.search(display_data): yield model_idx def changedText(self, text): """ Slot connected to the signal generated by changing the text in the text field """ column = self.treeComboBox.currentIndex() try: self.treeiter = self._iterIndices(self.treeView, column, text) model_idx = self.treeiter.next() except StopIteration: # No match found return self.treeView.selectAndScrollTo(model_idx) @QtCore.pyqtSlot(QtGui.QItemSelectionModel, QtGui.QItemSelectionModel) def treeViewSelectionChangedSlot(self, newvalue, oldvalue): if len(newvalue.indexes()) == 0 : return # assert that the the underlying selected element is always the same. assert isinstance(newvalue, QtGui.QItemSelection) assert all(x.internalPointer() == newvalue.indexes()[0].internalPointer() for x in newvalue.indexes()) self.selectionChanged.emit(newvalue.indexes()[0]) def expandLevel(self, level): if level == "Peptides": self.treeView.expandToDepth(0) elif level == "Precursors": self.treeView.expandToDepth(1) elif level == "smart": self.treeView.expandMultiElementItems() else: self.treeView.collapseAll() def get_precursor_model(self): """ Access to the underlying precursor model Returns: :class:`.PeptideTree` : The underlying precursor model """ """ Returns ------- precursor_model : :class:`.PeptideTree` The underlying precursor model """ return self._precursor_model