def test_removeColumns(self, qtbot, dataModel2): widget = DataTableWidget() qtbot.addWidget(widget) widget.show() widget.setViewModel(dataModel2) buttons = widget.findChildren(QtGui.QToolButton) for btn in buttons: if btn.isEnabled(): qtbot.mouseClick(btn, QtCore.Qt.LeftButton) break for btn in buttons: if str(btn.objectName()) == 'removecolumnbutton': qtbot.mouseClick(btn, QtCore.Qt.LeftButton) dlg = widget.findChildren(QtGui.QDialog)[-1] listview = dlg.findChildren(QtGui.QListView)[-1] listview.selectAll() dlg_buttons = dlg.findChildren(QtGui.QPushButton) for b in dlg_buttons: if str(b.text()) == 'OK': qtbot.mouseClick(b, QtCore.Qt.LeftButton) break break assert widget.view().model().columnCount() == 0
def test_click_each_button(self, qtbot, dataModel): widget = DataTableWidget() qtbot.addWidget(widget) widget.show() widget.setViewModel(dataModel) buttons = widget.findChildren(QtGui.QToolButton) for btn in buttons: if btn.isEnabled: qtbot.mouseClick(btn, QtCore.Qt.LeftButton) break for btn in buttons: if btn.objectName() == 'editbutton': continue if btn.objectName() in ['addcolumnbutton', 'removecolumnbutton']: qtbot.mouseClick(btn, QtCore.Qt.LeftButton) dlg = widget.findChildren(QtGui.QDialog)[-1] dlg_buttons = dlg.findChildren(QtGui.QPushButton) for b in dlg_buttons: if str(b.text()) == 'Cancel': qtbot.mouseClick(b, QtCore.Qt.LeftButton) break else: qtbot.mouseClick(btn, QtCore.Qt.LeftButton)
def test_setModel(self, qtbot, dataModel): widget = DataTableWidget() qtbot.addWidget(widget) widget.show() widget.setViewModel(dataModel) assert widget.view().model() is not None assert widget.view().model() == dataModel buttons = widget.findChildren(QtGui.QToolButton) for btn in buttons: if btn.isEnabled: qtbot.mouseClick(btn, QtCore.Qt.LeftButton) assert widget.view().model().editable qtbot.mouseClick(btn, QtCore.Qt.LeftButton) assert not widget.view().model().editable break
def test_enableToolBar(self, qtbot): widget = DataTableWidget() qtbot.addWidget(widget) widget.show() assert widget.view().model() is None buttons = widget.findChildren(QtGui.QToolButton) exclude_button = None for btn in buttons: if btn.isEnabled: qtbot.mouseClick(btn, QtCore.Qt.LeftButton) exclude_button = btn assert btn.isChecked() break for button in buttons: assert button.isEnabled() qtbot.mouseClick(btn, QtCore.Qt.LeftButton) for button in buttons: if button == exclude_button: continue assert not button.isEnabled() assert not button.isChecked()
def test_paginate(self, qtbot, dataModel3): widget = DataTableWidget() qtbot.addWidget(widget) widget.show() widget.setViewModel(dataModel3) model = widget.tableView.model() scroll_bar = widget.tableView.verticalScrollBar() scroll_bar_value = scroll_bar.value() scroll_bar.setValue(scroll_bar_value + 110) assert model.rowsLoaded == 200
def test_init(self, qtbot): widget = DataTableWidget() qtbot.addWidget(widget) widget.show() assert widget.view().model() is None buttons = widget.findChildren(QtGui.QToolButton) enabled_counter = 0 for btn in buttons: assert not btn.isChecked() if btn.isEnabled(): enabled_counter += 1 assert enabled_counter == 1
def test_addColumn(self, qtbot, dataModel): widget = DataTableWidget() qtbot.addWidget(widget) widget.show() widget.setViewModel(dataModel) buttons = widget.findChildren(QtGui.QToolButton) for btn in buttons: if btn.isEnabled: qtbot.mouseClick(btn, QtCore.Qt.LeftButton) break columns = [] addButton = None for btn in buttons: if btn.objectName == 'addcolumnbutton': addbutton = btn qtbot.mouseClick(btn, QtCore.Qt.LeftButton) dlg = widget.findChildren(QtGui.QDialog)[-1] dlg_buttons = dlg.findChildren(QtGui.QPushButton) comboBox = dlg.findChildren(QtGui.QComboBox)[-1] for i in xrange(comboBox.count()): columns.append(comboBox.itemText(i)) for b in dlg_buttons: if str(b.text()) == 'Cancel': qtbot.mouseClick(b, QtCore.Qt.LeftButton) break break columnCountBeforeInsert = widget.view().model().columnCount() for index, column in enumerate(columns): qtbot.mouseClick(addbutton, QtCore.Qt.LeftButton) dlg = widget.findChildren(QtGui.QDialog)[-1] textedits = dlg.findChildren(QtGui.QLineEdit) qtbot.keyClicks(textedits[0], column) comboBox = dlg.findChildren(QtGui.QComboBox)[-1] comboBox.setCurrentIndex(index) dlg_buttons = dlg.findChildren(QtGui.QPushButton) for b in dlg_buttons: if str(b.text()) == 'OK': qtbot.mouseClick(b, QtCore.Qt.LeftButton) break assert widget.view().model().columnCount() == \ columnCountBeforeInsert + 1 + index
def __init__(self, parent=None): super(TestWidget, self).__init__(parent) self.resize(1680, 756) self.move(0, 0) self.df = pandas.DataFrame() self.dataModel = None # init the data view's self.dataTableView = DataTableWidget(self) # self.dataTableView.setSortingEnabled(True) # self.dataTableView.setAlternatingRowColors(True) self.dataListView = QtGui.QListView(self) self.dataListView.setAlternatingRowColors(True) self.dataComboBox = QtGui.QComboBox(self) # make combobox to choose the model column for dataComboBox and dataListView self.chooseColumnComboBox = QtGui.QComboBox(self) self.buttonCsvData = QtGui.QPushButton("load csv data") self.buttonRandomData = QtGui.QPushButton("load random data") importDialog = CSVImportDialog(self) importDialog.load.connect(self.updateModel) self.buttonCsvData.clicked.connect(lambda: importDialog.show()) self.buttonRandomData.clicked.connect( lambda: self.setDataFrame(getRandomData(rows=100, columns=100))) self.exportDialog = CSVExportDialog(self) self.buttonCSVExport = QtGui.QPushButton("export to csv") self.buttonCSVExport.clicked.connect(self._exportModel) self.buttonLayout = QtGui.QHBoxLayout() self.buttonLayout.addWidget(self.buttonCsvData) self.buttonLayout.addWidget(self.buttonCSVExport) self.buttonLayout.addWidget(self.buttonRandomData) self.mainLayout = QtGui.QVBoxLayout() self.setLayout(self.mainLayout) self.mainLayout.addLayout(self.buttonLayout) self.mainLayout.addWidget(self.dataTableView) self.spinbox = QtGui.QSpinBox() self.mainLayout.addWidget(self.spinbox) self.spinbox.setMaximum(99999999999) self.spinbox.setValue(99999999999) self.rightLayout = QtGui.QVBoxLayout() self.chooseColumLayout = QtGui.QHBoxLayout() self.mainLayout.addLayout(self.rightLayout) self.rightLayout.addLayout(self.chooseColumLayout) self.chooseColumLayout.addWidget(QtGui.QLabel("Choose column:")) self.chooseColumLayout.addWidget(self.chooseColumnComboBox) self.rightLayout.addWidget(self.dataListView) self.rightLayout.addWidget(self.dataComboBox) self.tableViewColumnDtypes = QtGui.QTableView(self) self.rightLayout.addWidget(QtGui.QLabel('dtypes')) self.rightLayout.addWidget(self.tableViewColumnDtypes) self.buttonGoToColumn = QtGui.QPushButton("go to column") self.rightLayout.addWidget(self.buttonGoToColumn) self.buttonGoToColumn.clicked.connect(self.goToColumn) self.buttonSetFilter = QtGui.QPushButton("set filter") self.rightLayout.addWidget(self.buttonSetFilter) self.buttonSetFilter.clicked.connect(self.setFilter) self.buttonClearFilter = QtGui.QPushButton("clear filter") self.rightLayout.addWidget(self.buttonClearFilter) self.buttonClearFilter.clicked.connect(self.clearFilter) self.lineEditFilterCondition = QtGui.QLineEdit("freeSearch('am')") self.rightLayout.addWidget(self.lineEditFilterCondition) self.chooseColumnComboBox.currentIndexChanged.connect( self.setModelColumn) self.dataListView.mouseReleaseEvent = self.mouseReleaseEvent self.dropLineEdit = DropLineEdit("drop data from table here", self) self.rightLayout.addWidget(self.dropLineEdit) self.dropWidget = ComplexDropWidget(self) self.dropWidget.dropRecieved.connect(self.processDataDrops) self.rightLayout.addWidget(self.dropWidget)
class TestWidget(QtGui.QWidget): def __init__(self, parent=None): super(TestWidget, self).__init__(parent) self.resize(1680, 756) self.move(0, 0) self.df = pandas.DataFrame() self.dataModel = None # init the data view's self.dataTableView = DataTableWidget(self) # self.dataTableView.setSortingEnabled(True) # self.dataTableView.setAlternatingRowColors(True) self.dataListView = QtGui.QListView(self) self.dataListView.setAlternatingRowColors(True) self.dataComboBox = QtGui.QComboBox(self) # make combobox to choose the model column for dataComboBox and dataListView self.chooseColumnComboBox = QtGui.QComboBox(self) self.buttonCsvData = QtGui.QPushButton("load csv data") self.buttonRandomData = QtGui.QPushButton("load random data") importDialog = CSVImportDialog(self) importDialog.load.connect(self.updateModel) self.buttonCsvData.clicked.connect(lambda: importDialog.show()) self.buttonRandomData.clicked.connect( lambda: self.setDataFrame(getRandomData(rows=100, columns=100))) self.exportDialog = CSVExportDialog(self) self.buttonCSVExport = QtGui.QPushButton("export to csv") self.buttonCSVExport.clicked.connect(self._exportModel) self.buttonLayout = QtGui.QHBoxLayout() self.buttonLayout.addWidget(self.buttonCsvData) self.buttonLayout.addWidget(self.buttonCSVExport) self.buttonLayout.addWidget(self.buttonRandomData) self.mainLayout = QtGui.QVBoxLayout() self.setLayout(self.mainLayout) self.mainLayout.addLayout(self.buttonLayout) self.mainLayout.addWidget(self.dataTableView) self.spinbox = QtGui.QSpinBox() self.mainLayout.addWidget(self.spinbox) self.spinbox.setMaximum(99999999999) self.spinbox.setValue(99999999999) self.rightLayout = QtGui.QVBoxLayout() self.chooseColumLayout = QtGui.QHBoxLayout() self.mainLayout.addLayout(self.rightLayout) self.rightLayout.addLayout(self.chooseColumLayout) self.chooseColumLayout.addWidget(QtGui.QLabel("Choose column:")) self.chooseColumLayout.addWidget(self.chooseColumnComboBox) self.rightLayout.addWidget(self.dataListView) self.rightLayout.addWidget(self.dataComboBox) self.tableViewColumnDtypes = QtGui.QTableView(self) self.rightLayout.addWidget(QtGui.QLabel('dtypes')) self.rightLayout.addWidget(self.tableViewColumnDtypes) self.buttonGoToColumn = QtGui.QPushButton("go to column") self.rightLayout.addWidget(self.buttonGoToColumn) self.buttonGoToColumn.clicked.connect(self.goToColumn) self.buttonSetFilter = QtGui.QPushButton("set filter") self.rightLayout.addWidget(self.buttonSetFilter) self.buttonSetFilter.clicked.connect(self.setFilter) self.buttonClearFilter = QtGui.QPushButton("clear filter") self.rightLayout.addWidget(self.buttonClearFilter) self.buttonClearFilter.clicked.connect(self.clearFilter) self.lineEditFilterCondition = QtGui.QLineEdit("freeSearch('am')") self.rightLayout.addWidget(self.lineEditFilterCondition) self.chooseColumnComboBox.currentIndexChanged.connect( self.setModelColumn) self.dataListView.mouseReleaseEvent = self.mouseReleaseEvent self.dropLineEdit = DropLineEdit("drop data from table here", self) self.rightLayout.addWidget(self.dropLineEdit) self.dropWidget = ComplexDropWidget(self) self.dropWidget.dropRecieved.connect(self.processDataDrops) self.rightLayout.addWidget(self.dropWidget) @Slot('QMimeData') def processDataDrops(self, mimeData): """if you have more complicated stuff to do and you want to match some models, might be possible like that""" mimeDataPayload = mimeData.data() if isinstance(mimeDataPayload, PandasCellPayload): if self.dataModel is not None: if hex(id(self.dataModel)) == mimeDataPayload.parentId: self.dropWidget.setText( "complex stuff done after drop event. {0}".format( mimeDataPayload.column)) 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) @Slot() def _exportModel(self): model = self.dataTableView.view().model() self.exportDialog.setExportModel(model) self.exportDialog.show() @Slot('QAbstractItemModel') def updateModel(self, model): self.dataModel = model self.dataListView.setModel(model) self.dataTableView.setViewModel(model) self.dataComboBox.setModel(model) self.tableViewColumnDtypes.setModel(model.columnDtypeModel()) def setModelColumn(self, index): self.dataListView.setModelColumn(index) self.dataComboBox.setModelColumn(index) def goToColumn(self): print "go to column 7" index = self.dataTableView.view().model().index(7, 0) self.dataTableView.view().setCurrentIndex(index) def changeColumnValue(self, columnName, index, dtype): print "failed to change", columnName, "to", dtype print index.data(), index.isValid() self.dataTableView.view().setCurrentIndex(index) def setFilter(self): #filterIndex = eval(self.lineEditFilterCondition.text()) search = DataSearch("Test", self.lineEditFilterCondition.text()) self.dataTableView.view().model().setFilter(search) #raise NotImplementedError def clearFilter(self): self.dataTableView.view().model().clearFilter()
import sys from dtocean_qt.excepthook import excepthook sys.excepthook = excepthook # use QtGui from the compat module to take care if correct sip version, etc. from dtocean_qt.compat import QtGui from dtocean_qt.models.DataFrameModel import DataFrameModel from dtocean_qt.views.DataTableView import DataTableWidget from dtocean_qt.views._ui import icons_rc """setup a new empty model""" model = DataFrameModel() model.freeze_first = True """setup an application and create a table view widget""" app = QtGui.QApplication([]) widget = DataTableWidget() widget.resize(800, 600) widget.hideVerticalHeader(True) widget.show() """asign the created model""" widget.setViewModel(model) """create some test data""" data = { 'A': [10, 11, 12], 'B': [20, 21, 22], 'C': ['Peter Pan', 'Cpt. Hook', 'Tinkerbell'], 'D': [True, True, False] } df = pandas.DataFrame(data) """convert the column to the numpy.int8 datatype to test the delegates in the table int8 is limited to -128-127