def __init__(self, id, text="Input data", default=[], model=None): QWidget.__init__(self) self.setToolTip("<p>Select input dataset</p>") self.id = id if model is None: self.model = TreeModel() else: self.model = model self.comboBox = QComboBox() self.treeView = QListView(self.comboBox) self.connect(self.comboBox, SIGNAL("currentIndexChanged(int)"), self.changeSelectedText) self.proxyModel = SortFilterProxyModel() self.proxyModel.setDynamicSortFilter(True) self.proxyModel.setFilterKeyColumn(1) self.proxyModel.setSourceModel(self.model) regexp = QRegExp("|".join([r"%s" % i for i in default])) self.proxyModel.setFilterRegExp(regexp) # self.treeView.header().hide() self.currentText = QString() self.treeView.setModel(self.proxyModel) self.comboBox.setModel(self.proxyModel) self.comboBox.setView(self.treeView) # self.treeView.hideColumn(1) # self.treeView.hideColumn(2) # self.treeView.hideColumn(3) self.treeView.viewport().installEventFilter(self.comboBox) label = QLabel(text) hbox = HBoxLayout() hbox.addWidget(label) hbox.addWidget(self.comboBox) self.setLayout(hbox) self.changeSelectedText(None)
def __init__(self, parent=None): RWidget.__init__(self, parent) self.workspaceTree = self.TreeView(self) self.workspaceTree.setSortingEnabled(True) self.proxyModel = SortFilterProxyModel() self.proxyModel.setDynamicSortFilter(True) # self.proxyModel.setFilterKeyColumn(1) self.model = TreeModel() self.proxyModel.setSourceModel(self.model) # self.workspaceTree.setModel(self.model) self.workspaceTree.setModel(self.proxyModel) self.actions = [] self.refreshAction = QAction("Re&fresh variables", self) self.refreshAction.setToolTip("Refresh environment browser") self.refreshAction.setWhatsThis("Refresh environment browser") self.refreshAction.setIcon(QIcon(":view-refresh")) self.refreshAction.setEnabled(True) self.actions.append(self.refreshAction) self.loadAction = QAction("&Load data", self) self.loadAction.setToolTip("Load R variable(s) from file") self.loadAction.setWhatsThis("Load R variable(s) from file") self.loadAction.setIcon(QIcon(":custom-open-data")) self.loadAction.setEnabled(True) self.actions.append(self.loadAction) self.exportAction = QAction("&Export to file", self) self.exportAction.setToolTip("Export data to file") self.exportAction.setWhatsThis("Export data to file") self.exportAction.setIcon(QIcon(":custom-document-export")) self.exportAction.setEnabled(False) self.actions.append(self.exportAction) self.saveAction = QAction("&Save variable", self) self.saveAction.setToolTip("Save R variable to file") self.saveAction.setWhatsThis("Save R variable to file") self.saveAction.setIcon(QIcon(":custom-save-data")) self.saveAction.setEnabled(False) self.actions.append(self.saveAction) self.methodAction = QAction("&Print available methods", self) self.methodAction.setToolTip("Print available methods for object class") self.methodAction.setWhatsThis("Print available methods for object class") self.methodAction.setIcon(QIcon(":document-properties")) self.methodAction.setEnabled(False) self.actions.append(self.methodAction) self.attributeAction = QAction("Print object &attributes", self) self.attributeAction.setToolTip("Print available attributes for object class") self.attributeAction.setWhatsThis("Print available attributes for object class") self.attributeAction.setIcon(QIcon(":custom-tag")) self.attributeAction.setEnabled(False) self.actions.append(self.attributeAction) self.summaryAction = QAction("Print object Su&mmary", self) self.summaryAction.setToolTip("Print summary of object") self.summaryAction.setWhatsThis("Print summary of object") self.summaryAction.setIcon(QIcon(":custom-summary")) self.summaryAction.setEnabled(False) self.actions.append(self.summaryAction) self.plotAction = QAction("&Quick plot", self) self.plotAction.setToolTip("Create minimal plot for visualisation") self.plotAction.setWhatsThis("Create minimal plot for visualisation") self.plotAction.setIcon(QIcon(":gnome-fs-bookmark-missing")) self.plotAction.setEnabled(False) self.actions.append(self.plotAction) self.rmAction = QAction("&Remove", self) self.rmAction.setToolTip("Remove selected variable") self.rmAction.setWhatsThis("Removed selected variable") self.rmAction.setIcon(QIcon(":edit-delete")) self.rmAction.setEnabled(False) self.actions.append(self.rmAction) vbox = QVBoxLayout(self) vbox.addWidget(self.workspaceTree) self.variables = dict() self.connect(self.plotAction, SIGNAL("triggered()"), self.plotVariable) self.connect(self.summaryAction, SIGNAL("triggered()"), self.summariseVariable) self.connect(self.rmAction, SIGNAL("triggered()"), self.removeVariable) self.connect(self.exportAction, SIGNAL("triggered()"), self.exportVariable) self.connect(self.saveAction, SIGNAL("triggered()"), self.saveVariable) self.connect(self.loadAction, SIGNAL("triggered()"), self.loadRVariable) self.connect(self.methodAction, SIGNAL("triggered()"), self.printMethods) self.connect(self.refreshAction, SIGNAL("triggered()"), self.updateEnvironment) self.connect(self.attributeAction, SIGNAL("triggered()"), self.printAttributes) self.connect(self.workspaceTree, SIGNAL("itemSelectionChanged()"), self.selectionChanged) self.updateEnvironment()
class WorkspaceWidget(RWidget): class TreeView(QTreeView): def __init__(self, parent): QTreeView.__init__(self, parent) self.setAlternatingRowColors(True) self.setSelectionBehavior(QAbstractItemView.SelectRows) self.setSelectionMode(QAbstractItemView.SingleSelection) self.connect(self, SIGNAL("expanded(QModelIndex)"), self.expanded) def expanded(self, index): self.model().updateEntry(index) def mousePressEvent(self, event): item = self.childAt(event.globalPos()) if not item and event.button() == Qt.LeftButton: self.clearSelection() QTreeView.mousePressEvent(self, event) def selectionChanged(self, new, old): self.emit(SIGNAL("itemSelectionChanged()")) QTreeView.selectionChanged(self, new, old) def __init__(self, parent=None): RWidget.__init__(self, parent) self.workspaceTree = self.TreeView(self) self.workspaceTree.setSortingEnabled(True) self.proxyModel = SortFilterProxyModel() self.proxyModel.setDynamicSortFilter(True) # self.proxyModel.setFilterKeyColumn(1) self.model = TreeModel() self.proxyModel.setSourceModel(self.model) # self.workspaceTree.setModel(self.model) self.workspaceTree.setModel(self.proxyModel) self.actions = [] self.refreshAction = QAction("Re&fresh variables", self) self.refreshAction.setToolTip("Refresh environment browser") self.refreshAction.setWhatsThis("Refresh environment browser") self.refreshAction.setIcon(QIcon(":view-refresh")) self.refreshAction.setEnabled(True) self.actions.append(self.refreshAction) self.loadAction = QAction("&Load data", self) self.loadAction.setToolTip("Load R variable(s) from file") self.loadAction.setWhatsThis("Load R variable(s) from file") self.loadAction.setIcon(QIcon(":custom-open-data")) self.loadAction.setEnabled(True) self.actions.append(self.loadAction) self.exportAction = QAction("&Export to file", self) self.exportAction.setToolTip("Export data to file") self.exportAction.setWhatsThis("Export data to file") self.exportAction.setIcon(QIcon(":custom-document-export")) self.exportAction.setEnabled(False) self.actions.append(self.exportAction) self.saveAction = QAction("&Save variable", self) self.saveAction.setToolTip("Save R variable to file") self.saveAction.setWhatsThis("Save R variable to file") self.saveAction.setIcon(QIcon(":custom-save-data")) self.saveAction.setEnabled(False) self.actions.append(self.saveAction) self.methodAction = QAction("&Print available methods", self) self.methodAction.setToolTip("Print available methods for object class") self.methodAction.setWhatsThis("Print available methods for object class") self.methodAction.setIcon(QIcon(":document-properties")) self.methodAction.setEnabled(False) self.actions.append(self.methodAction) self.attributeAction = QAction("Print object &attributes", self) self.attributeAction.setToolTip("Print available attributes for object class") self.attributeAction.setWhatsThis("Print available attributes for object class") self.attributeAction.setIcon(QIcon(":custom-tag")) self.attributeAction.setEnabled(False) self.actions.append(self.attributeAction) self.summaryAction = QAction("Print object Su&mmary", self) self.summaryAction.setToolTip("Print summary of object") self.summaryAction.setWhatsThis("Print summary of object") self.summaryAction.setIcon(QIcon(":custom-summary")) self.summaryAction.setEnabled(False) self.actions.append(self.summaryAction) self.plotAction = QAction("&Quick plot", self) self.plotAction.setToolTip("Create minimal plot for visualisation") self.plotAction.setWhatsThis("Create minimal plot for visualisation") self.plotAction.setIcon(QIcon(":gnome-fs-bookmark-missing")) self.plotAction.setEnabled(False) self.actions.append(self.plotAction) self.rmAction = QAction("&Remove", self) self.rmAction.setToolTip("Remove selected variable") self.rmAction.setWhatsThis("Removed selected variable") self.rmAction.setIcon(QIcon(":edit-delete")) self.rmAction.setEnabled(False) self.actions.append(self.rmAction) vbox = QVBoxLayout(self) vbox.addWidget(self.workspaceTree) self.variables = dict() self.connect(self.plotAction, SIGNAL("triggered()"), self.plotVariable) self.connect(self.summaryAction, SIGNAL("triggered()"), self.summariseVariable) self.connect(self.rmAction, SIGNAL("triggered()"), self.removeVariable) self.connect(self.exportAction, SIGNAL("triggered()"), self.exportVariable) self.connect(self.saveAction, SIGNAL("triggered()"), self.saveVariable) self.connect(self.loadAction, SIGNAL("triggered()"), self.loadRVariable) self.connect(self.methodAction, SIGNAL("triggered()"), self.printMethods) self.connect(self.refreshAction, SIGNAL("triggered()"), self.updateEnvironment) self.connect(self.attributeAction, SIGNAL("triggered()"), self.printAttributes) self.connect(self.workspaceTree, SIGNAL("itemSelectionChanged()"), self.selectionChanged) self.updateEnvironment() def mousePressEvent(self, event): item = self.workspaceTree.indexAt(event.globalPos()) if not item and event.button() == Qt.LeftButton: self.workspaceTree.clearSelection() RWidget.mousePressEvent(self, event) def contextMenuEvent(self, event): menu = QMenu(self) menu.addAction(self.refreshAction) menu.addSeparator() for action in self.actions[1:-1]: menu.addAction(action) menu.addSeparator() menu.addAction(self.rmAction) menu.exec_(event.globalPos()) def selectionChanged(self): items = self.workspaceTree.selectedIndexes() if len(items) < 1: for action in self.actions[2:]: action.setEnabled(False) else: for action in self.actions[2:]: action.setEnabled(True) def printMethods(self): items = self.workspaceTree.selectedIndexes() if len(items) < 1: return False itemType = self.workspaceTree.model().getItem(items[0]).data(1) self.runCommand("methods(class='%s')" % (itemType,)) def printAttributes(self): items = self.workspaceTree.selectedIndexes() if len(items) < 1: return False tree = self.workspaceTree.model().parentTree(items[0]) self.runCommand('names(attributes(%s))' % tree) def summariseVariable(self): items = self.workspaceTree.selectedIndexes() if len(items) < 1: return False tree = self.workspaceTree.model().parentTree(items[0]) self.runCommand('summary(%s)' % tree) def plotVariable(self): items = self.workspaceTree.selectedIndexes() if len(items) < 1: return False index = items[0] tree = self.workspaceTree.model().parentTree(index) self.runCommand('plot(%s)' % tree) def removeVariable(self): items = self.workspaceTree.selectedIndexes() if len(items) < 1: return False item = items[0] tree = self.workspaceTree.model().parentTree(item) command = "rm(%s)" % tree if not item.parent() == QModelIndex(): command = "%s <- NULL" % tree waiter = SignalWaiter(self.parent, SIGNAL("errorOutput()")) self.runCommand(command) if not waiter.wait(50): self.workspaceTree.model().removeRows(item.row(),1, item.parent()) def exportVariable(self): items = self.workspaceTree.selectedIndexes() if len(items) < 1: return False tree = self.workspaceTree.model().parentTree(items[0]) fd = QFileDialog(self.parent, "Save data to file", str(robjects.r.getwd()), "Comma separated (*.csv);;Text file (*.txt);;All files (*.*)") fd.setAcceptMode(QFileDialog.AcceptSave) if not fd.exec_() == QDialog.Accepted: return False files = fd.selectedFiles() selectedFile = files.first() if selectedFile.length() == 0: return False suffix = QString(fd.selectedNameFilter()) index1 = suffix.lastIndexOf("(")+2 index2 = suffix.lastIndexOf(")") suffix = suffix.mid(index1, index2-index1) if not selectedFile.endsWith(suffix): selectedFile.append(suffix) command = QString('write.table(%s, file = "%s",' % (tree, selectedFile)) command.append(QString('append = FALSE, quote = TRUE, sep = ",", eol = "\\n", na = "NA"')) command.append(QString(', dec = ".", row.names = FALSE, col.names = TRUE, qmethod = "escape")')) self.runCommand(command) def saveVariable(self): items = self.workspaceTree.selectedIndexes() if len(items) < 1: return False item = self.workspaceTree.model().getItem(items[0]) name = item.data(0) parent = item.parent() names = [name] while not parent is None: names.append(QString(parent.data(0))) parent = parent.parent() if len(names) > 1: names.pop(-1) name = names[-1] fd = QFileDialog(self.parent, "Save data to file", os.path.join(str(robjects.r.getwd()[0]), unicode(name)+".Rdata"), "R data file (*.Rdata)") fd.setAcceptMode(QFileDialog.AcceptSave) if not fd.exec_() == QDialog.Accepted: return False files = fd.selectedFiles() selectedFile = files.first() if selectedFile.length() == 0: return False suffix = QString(fd.selectedNameFilter()) index1 = suffix.lastIndexOf("(")+2 index2 = suffix.lastIndexOf(")") suffix = suffix.mid(index1, index2-index1) if not selectedFile.endsWith(suffix): selectedFile.append(suffix) commands = QString('save(%s, file="%s")' % (name,selectedFile)) self.runCommand(commands) def loadRVariable(self): fd = QFileDialog(self.parent, "Load R variable(s) from file", str(robjects.r.getwd()[0]), "R data (*.Rdata);;All files (*.*)") fd.setAcceptMode(QFileDialog.AcceptOpen) fd.setFileMode(QFileDialog.ExistingFile) if fd.exec_() == QDialog.Rejected: return False files = fd.selectedFiles() selectedFile = files.first() if selectedFile.length() == 0: return False self.runCommand(QString('load("%s")' % (selectedFile))) self.updateEnvironment() def runCommand(self, command): if not command == "": self.emitCommands(command) def updateEnvironment(self): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) self.model = TreeModel() self.proxyModel.setSourceModel(self.model) # self.workspaceTree.setModel(model) QApplication.restoreOverrideCursor() return True