class EditPerson(QWidget):
def __init__(self, parent=None, db=None):
+ """Set up EditPerson with parent widget and database from which to pull data"""
QWidget.__init__(self,parent)
self.db = db
self.show()
def setupModel(self):
+ """Prepare data model for the table on the left and add header fields"""
self.model = QSqlTableModel(self, self.db)
self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
self.model.setTable("people")
self.model.setHeaderData(self.model.fieldIndex("forename"), Qt.Horizontal, QVariant("First Name"))
self.model.setHeaderData(self.model.fieldIndex("surname"), Qt.Horizontal, QVariant("Last Name"))
def setupForm(self):
+ """Initialize data breakout widgets on right"""
self.current_record = QSqlRecord()
self.current_row = -1
# Text labels
self.buttonlayout.addWidget(self.pb_save)
self.buttonlayout.addWidget(self.pb_saveclose)
def setupActions(self):
+ """Set up signal/slot connections"""
# Update filtered table
QObject.connect(self.search_bar, SIGNAL("textChanged(QString)"), self.updateTable )
# Selecting a record loads it for editing
QObject.connect(self.dorm, SIGNAL("currentIndexChanged(int)"), self.formChanged )
def formChanged(self):
+ """Enable Save/Reset buttons when a field has been changed in the editor"""
# Do this only if a record has been selected
if not self.current_record.isEmpty():
self.pb_reset.setEnabled(True)
self.pb_save.setEnabled(True)
self.pb_saveclose.setEnabled(True)
def selectionChanged(self, selected, deselected):
+ """Handle changes in table row selection"""
items = selected.indexes()
if len(items) > 0:
self.current_row = items[0].row() # this is the row of the current selected item
self.current_record = self.model.record(items[0].row())
self.fillForm()
def fillForm(self):
+ """Fill form with fields from the selected row of the database"""
# Fill fields with data from selected record
self.netid.setText(self.current_record.field("netid").value().toString())
self.firstname.setText(self.current_record.field("forename").value().toString())
self.pb_changephoto.setEnabled(True)
def reselect(self):
+ """Refreshes the table view from the data source. Called when databases merged - need to reload rows from database."""
self.updateTable(self.search_bar.text())
def updateTable(self, text):
+ """Filter table view with text in the search bar. Table view shows only entries that contain each of the space-separated strings in the bar in either the entries forename or surname."""
if text.isEmpty():
self.model.setFilter("")
self.model.select()
if self.model.rowCount() == 1:
self.tableview.selectRow(0)
def resetPressed(self):
+ """Revert fields in editor to their source from the database. Disable Save/Reset buttons."""
self.current_record = self.model.record(self.current_row)
self.fillForm()
self.pb_reset.setEnabled(False)
self.pb_saveclose.setEnabled(False)
print "Resetting form"
def closePressed(self):
+ """Revert all unsaved changes, clear all entries, and emit done() for parent to swap widgets shown."""
print "Closing"
self.model.revertAll()
# Wipe contents of all lineedits, destroy current_record and current_index
self.model.select()
self.emit(SIGNAL("done()"))
def savePressed(self):
+ """Commit changes to currently-selected record to the database."""
print "Saving"
self.current_record.setValue("netid",QVariant(self.netid.text()))
self.current_record.setValue("forename",QVariant(self.firstname.text()))
self.pb_save.setEnabled(False)
self.pb_saveclose.setEnabled(False)
def saveClosePressed(self):
+ """Act as if Save were pressed, then Close."""
self.savePressed()
self.closePressed()
def changePhoto(self):
+ """Allow user to pick a JPEG or PNG file to set as the current record's photo."""
fileName = QFileDialog.getOpenFileName(self, "Select Photo", ".", "Images (*.jpg *.JPG *.png)" )
if not fileName.isEmpty():
print "opening", fileName
self.pb_save.setEnabled(True)
self.pb_saveclose.setEnabled(True)
def wipeFields(self):
+ """Clear all editable fields, including the search bar."""
self.search_bar.clear()
self.current_record = QSqlRecord()
self.current_index = -1
self.photo.setText("No record selected")
def updateDB(self, newdbname):
+ """Force database reload with new database name."""
print "updateDB called",newdbname
self.db.close()
self.db.setDatabaseName(newdbname)