def initUi(self): self.setWindowTitle(self.tr('Remove Attributes')) self.setModal(True) self.resize(366, 274) sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Expanding) self.setSizePolicy(sizePolicy) self.gridLayout = QtGui.QGridLayout(self) self.dialogHeading = QtGui.QLabel( self.tr('Select the attribute column(s) which shall be removed'), self) self.listView = QtGui.QListView(self) model = QtGui.QStandardItemModel() for column in self.columns: item = QtGui.QStandardItem(column) model.appendRow(item) self.listView.setModel(model) self.listView.setSelectionMode(QtGui.QListView.MultiSelection) self.buttonBox = QtGui.QDialogButtonBox(self) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok) self.gridLayout.addWidget(self.dialogHeading, 0, 0, 1, 1) self.gridLayout.addWidget(self.listView, 1, 0, 1, 1) self.gridLayout.addWidget(self.buttonBox, 2, 0, 1, 1) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject)
def test_tableViewMissing(self, widgetClass, qtbot, model, exception, exceptionContains): widget = widgetClass() qtbot.addWidget(widget) with pytest.raises(exception) as excinfo: if model: widget.setModel(QtGui.QStandardItemModel()) createDelegate('foo', 'bar', widget) assert exceptionContains in str(excinfo.value)
def setDataFrame(self, dataFrame): self.df = dataFrame dataModel = DataFrameModel() dataModel.setDataFrame(self.df) self.dataModel = dataModel self.dataListView.setModel(dataModel) self.dataTableView.setViewModel(dataModel) self.dataComboBox.setModel(dataModel) # self.dataTableView.resizeColumnsToContents() # create a simple item model for our choosing combobox columnModel = QtGui.QStandardItemModel() for column in self.df.columns: columnModel.appendRow(QtGui.QStandardItem(column)) self.chooseColumnComboBox.setModel(columnModel) self.tableViewColumnDtypes.setModel(dataModel.columnDtypeModel()) self.tableViewColumnDtypes.horizontalHeader().setDefaultSectionSize(200) self.tableViewColumnDtypes.setItemDelegateForColumn(1, DtypeComboDelegate(self.tableViewColumnDtypes)) dataModel.changingDtypeFailed.connect(self.changeColumnValue)
class TestCustomDelegates(object): @pytest.fixture def emptyTableView(self, qtbot): widget = DemoTableView() qtbot.addWidget(widget) return widget @pytest.fixture def dataFrame(self): return pandas.DataFrame(['abc'], columns=['A']) @pytest.fixture def model(self, dataFrame): return DataFrameModel(dataFrame) @pytest.fixture def tableView(self, model, emptyTableView): emptyTableView.setModel(model) return emptyTableView @pytest.fixture def index(self, model): index = model.index(0, 0) assert index.isValid() return index @pytest.mark.parametrize( "widgetClass, model, exception, exceptionContains", [ (QtGui.QWidget, None, AttributeError, "has no attribute 'model'"), (DemoTableView, None, ValueError, "no model set for the current view"), (DemoTableView, QtGui.QStandardItemModel(), TypeError, 'model is not of type DataFrameModel'), ]) def test_tableViewMissing(self, widgetClass, qtbot, model, exception, exceptionContains): widget = widgetClass() qtbot.addWidget(widget) with pytest.raises(exception) as excinfo: if model: widget.setModel(QtGui.QStandardItemModel()) createDelegate('foo', 'bar', widget) assert exceptionContains in str(excinfo.value) @pytest.mark.parametrize( "value, singleStep", [ (numpy.int8(1), 1), (numpy.int16(1), 1), (numpy.int32(1), 1), (numpy.int64(1), 1), (numpy.uint8(1), 1), (numpy.uint16(1), 1), (numpy.uint32(1), 1), (numpy.uint64(1), 1), (numpy.float16(1.11111), 0.1), (numpy.float32(1.11111111), 0.1), (numpy.float64(1.1111111111111111), 0.1), #(numpy.float128(1.11111111111111111111), 0.1), ]) def test_setDelegates(self, qtbot, tableView, index, value, singleStep): dlg = createDelegate(numpy.dtype(value), 0, tableView) assert dlg is not None data = pandas.DataFrame([value], columns=['A']) data['A'] = data['A'].astype(value.dtype) model = tableView.model() model.setDataFrame(data) for i, delegate in enumerate([dlg]): assert tableView.itemDelegateForColumn(i) == delegate option = QtGui.QStyleOptionViewItem() option.rect = QtCore.QRect(0, 0, 100, 100) editor = delegate.createEditor(tableView, option, index) delegate.setEditorData(editor, index) assert editor.value() == index.data() delegate.setModelData(editor, model, index) delegate.updateEditorGeometry(editor, option, index) dtype = value.dtype if dtype in DataFrameModel._intDtypes: info = numpy.iinfo(dtype) assert isinstance(delegate, BigIntSpinboxDelegate) elif dtype in DataFrameModel._floatDtypes: info = numpy.finfo(dtype) assert isinstance(delegate, CustomDoubleSpinboxDelegate) assert delegate.decimals == DataFrameModel._float_precisions[ str(value.dtype)] assert delegate.maximum == info.max assert editor.maximum() == info.max assert delegate.minimum == info.min assert editor.minimum() == info.min assert delegate.singleStep == singleStep assert editor.singleStep() == singleStep def clickEvent(index): assert index.isValid() tableView.clicked.connect(clickEvent) with qtbot.waitSignal(tableView.clicked) as blocker: qtbot.mouseClick(tableView.viewport(), QtCore.Qt.LeftButton, pos=QtCore.QPoint(10, 10)) assert blocker.signal_triggered