import csv
import sys
from PyQt4 import QtGui, QtCore
global xLoc
global yLoc
global itemIdxs
def __init__(self, fileName, horizHeaders, vertHeaders, parent=None):
super(CSVeditor, self).__init__(parent)
self.horizHeaders = horizHeaders
self.fileName = fileName
self.model.setHorizontalHeaderLabels(horizHeaders)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(False)
self.tableView.setAlternatingRowColors(True)
self.tableView.hideColumn(0)
self.tableView.hideColumn(1)
self.tableView.hideColumn(2)
selectionModel = self.tableView.selectionModel()
selectionModel.selectionChanged.connect(self.selRange)
shortcut.setKey("Enter")
self.connect(shortcut, QtCore.SIGNAL("activated()"), self.shareChanges)
self.tableView.clicked.connect(self.Release)
# autoLoad CSV from workspace [to be edited for dropdown CSV upload selection(s)]
with open(fileName, "rb") as fileInput:
for idx, row in enumerate(csv.reader(fileInput)):
header = 0
if idx is header:
pass
elif idx>0:
self.model.appendRow(items)
self.model.setVerticalHeaderLabels(vertHeaders)
self.show()
# index of selected range, and parent item (last index) for batch application of user-value changes
def selRange(self,selected,deselected):
selectedIdx = self.tableView.selectedIndexes()
idxCol = []
idxRow = []
for row in selectedIdx:
idxRow.append(row.row())
for column in selectedIdx:
idxCol.append(column.column())
global itemIdxs
itemIdxs = zip(idxRow,idxCol)
def shareChanges(self):
self.tableView.clicked.connect(self.Release)
try:
updatedVal = self.model.data(self.model.index(xLoc,yLoc+3))
print xLoc,yLoc,updatedVal
except AttributeError:
pass
readOnlyCols = [0]
try:
for i in itemIdxs:
row = i[0]
col = i[1]
if col in readOnlyCols:
pass
else:
self.model.setData(self.model.index(row,col), updatedVal, Qt.DisplayRole)
except AttributeError:
print ":("
pass
# button induced save-change to CSV (NO UNDO YET!)
def writeCsv(self, fileName):
with open(fileName, "wb") as fileOutput:
writer = csv.writer(fileOutput)
writer.writerow(self.horizHeaders)
for rowNumber in range(self.model.rowCount()):
fields = [self.model.data(self.model.index(rowNumber,columnNumber),QtCore.Qt.DisplayRole) for columnNumber in range(self.model.columnCount())]
writer.writerow(fields)
print ':)'
@QtCore.pyqtSlot()
def on_pushButtonWrite_clicked(self):
self.writeCsv(self.fileName)
def Cancel(self):
self.close()
def closeEvent(self,event):
self.deleteLater()
def Release(self,index):
global xLoc
global yLoc
xLoc,yLoc = index.row(),(index.column()-3)
import csv
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QVariant
global xLoc
global yLoc
global itemIdxs
class CSVeditor(QtGui.QDialog, QtGui.QWidget):
def __init__(self, fileName, horizHeaders, vertHeaders, parent=None):
super(CSVeditor, self).__init__(parent)
self.horizHeaders = horizHeaders
self.fileName = fileName
self.model = QtGui.QStandardItemModel(self)
self.model.setHorizontalHeaderLabels(horizHeaders)
self.tableView = QtGui.QTableView(self)
self.tableView.setModel(self.model)
self.tableView.horizontalHeader().setStretchLastSection(False)
self.tableView.setAlternatingRowColors(True)
self.tableView.hideColumn(0)
self.tableView.hideColumn(1)
self.tableView.hideColumn(2)
self.tableView.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
selectionModel = self.tableView.selectionModel()
selectionModel.selectionChanged.connect(self.selRange)
shortcut = QtGui.QShortcut(self)
shortcut.setKey("Enter")
self.connect(shortcut, QtCore.SIGNAL("activated()"), self.shareChanges)
self.tableView.clicked.connect(self.Release)
# autoLoad CSV from workspace [to be edited for dropdown CSV upload selection(s)]
with open(fileName, "rb") as fileInput:
for idx, row in enumerate(csv.reader(fileInput)):
header = 0
if idx is header:
pass
elif idx>0:
items = [QtGui.QStandardItem(field) for field in row]
self.model.appendRow(items)
self.model.setVerticalHeaderLabels(vertHeaders)
self.show()
# index of selected range, and parent item (last index) for batch application of user-value changes
def selRange(self,selected,deselected):
selectedIdx = self.tableView.selectedIndexes()
idxCol = []
idxRow = []
for row in selectedIdx:
idxRow.append(row.row())
for column in selectedIdx:
idxCol.append(column.column())
global itemIdxs
itemIdxs = zip(idxRow,idxCol)
def shareChanges(self):
self.tableView.clicked.connect(self.Release)
try:
updatedVal = self.model.data(self.model.index(xLoc,yLoc+3))
print xLoc,yLoc,updatedVal
except AttributeError:
pass
readOnlyCols = [0]
try:
for i in itemIdxs:
row = i[0]
col = i[1]
if col in readOnlyCols:
pass
else:
self.model.setData(self.model.index(row,col), updatedVal, Qt.DisplayRole)
except AttributeError:
print ":("
pass
# button induced save-change to CSV (NO UNDO YET!)
def writeCsv(self, fileName):
with open(fileName, "wb") as fileOutput:
writer = csv.writer(fileOutput)
writer.writerow(self.horizHeaders)
for rowNumber in range(self.model.rowCount()):
fields = [self.model.data(self.model.index(rowNumber,columnNumber),QtCore.Qt.DisplayRole) for columnNumber in range(self.model.columnCount())]
writer.writerow(fields)
print ':)'
@QtCore.pyqtSlot()
def on_pushButtonWrite_clicked(self):
self.writeCsv(self.fileName)
def Cancel(self):
self.close()
def closeEvent(self,event):
self.deleteLater()
def Release(self,index):
global xLoc
global yLoc
xLoc,yLoc = index.row(),(index.column()-3)
To copy to clipboard, switch view to plain text mode
Bookmarks