def setupForm(self):
+ self.current_record = QSqlRecord()
+ self.current_row = -1
# Text labels
self.netid_lab = QLabel("NetID:")
self.year_lab = QLabel("Year:")
self.displaypixmap = QPixmap()
self.photo.setAlignment(Qt.AlignCenter)
self.pb_changephoto = QPushButton("Select &photo")
- self.pb_changephoto.setEnabled(True)
+ self.pb_changephoto.setEnabled(False)
# Personal info
self.surname = QLineEdit()
self.firstname = QLineEdit()
self.dorm = QComboBox()
self.room = QLineEdit()
# Fill the comboboxes with values, set valid field types
- years = QStringList(["1987","1988","1989","1990","1991","1992","1993","1994"])
+ years = QStringList(["1983","1984","1985","1986","1987","1988","1989","1990","1991","1992","1993","1994"])
months = QStringList(["January","February","March","April","May","June","July","August","September","October","November","December"])
days = QStringList(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"])
dorms = QStringList(["Clements","Lechner","McFadden"])
def setupActions(self):
# Update filtered table
QObject.connect(self.search_bar, SIGNAL("textChanged(QString)"), self.updateTable )
+ # Selecting a record loads it for editing
+ QObject.connect(self.tableview.selectionModel(), SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), self.selectionChanged )
# Map buttons to actions
QObject.connect(self.pb_reset, SIGNAL("clicked()"), self.resetPressed )
QObject.connect(self.pb_cancel, SIGNAL("clicked()"), self.closePressed )
QObject.connect(self.pb_save, SIGNAL("clicked()"), self.savePressed )
QObject.connect(self.pb_changephoto, SIGNAL("clicked()"), self.changePhoto )
# Changes to the record enable buttons
- QObject.connect(self.netid, SIGNAL("textChanged(QString)"), self.formChanged )
- QObject.connect(self.firstname, SIGNAL("textChanged(QString)"), self.formChanged )
- QObject.connect(self.surname, SIGNAL("textChanged(QString)"), self.formChanged )
- QObject.connect(self.email, SIGNAL("textChanged(QString)"), self.formChanged )
- QObject.connect(self.phone, SIGNAL("textChanged(QString)"), self.formChanged )
- QObject.connect(self.major, SIGNAL("textChanged(QString)"), self.formChanged )
- QObject.connect(self.room, SIGNAL("textChanged(QString)"), self.formChanged )
+ QObject.connect(self.netid, SIGNAL("textEdited(QString)"), self.formChanged )
+ QObject.connect(self.firstname, SIGNAL("textEdited(QString)"), self.formChanged )
+ QObject.connect(self.surname, SIGNAL("textEdited(QString)"), self.formChanged )
+ QObject.connect(self.email, SIGNAL("textEdited(QString)"), self.formChanged )
+ QObject.connect(self.phone, SIGNAL("textEdited(QString)"), self.formChanged )
+ QObject.connect(self.major, SIGNAL("textEdited(QString)"), self.formChanged )
+ QObject.connect(self.room, SIGNAL("textEdited(QString)"), self.formChanged )
QObject.connect(self.year, SIGNAL("currentIndexChanged(int)"), self.formChanged )
QObject.connect(self.month, SIGNAL("currentIndexChanged(int)"), self.formChanged )
QObject.connect(self.day, SIGNAL("currentIndexChanged(int)"), self.formChanged )
def formChanged(self):
# Do this only if a record has been selected
- self.pb_reset.setEnabled(True)
- self.pb_save.setEnabled(True)
- self.pb_saveclose.setEnabled(True)
+ 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):
+ 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 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.surname.setText(self.current_record.field("surname").value().toString())
+ self.email.setText(self.current_record.field("email").value().toString())
+ self.phone.setText(self.current_record.field("phone").value().toString())
+ self.major.setText(self.current_record.field("major").value().toString())
+ self.room.setText(self.current_record.field("room").value().toString())
+ bday = str(self.current_record.field("birthday").value().toString()).split("-")
+ self.year.setCurrentIndex(self.year.findText(bday[0]))
+ self.month.setCurrentIndex(int(bday[1]) - 1)
+ self.day.setCurrentIndex(int(bday[2]) - 1)
+ self.dorm.setCurrentIndex(self.dorm.findText(self.current_record.field("dorm").value().toString()))
+ self.pb_reset.setEnabled(False)
+ self.pb_save.setEnabled(False)
+ self.pb_saveclose.setEnabled(False)
+ ba = self.current_record.field("photo").value().toByteArray()
+ if ba.size() > 0:
+ self.currentimage = QImage()
+ self.currentimage.loadFromData(ba)
+ self.displayimage = self.currentimage.scaled(self.photo.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
+ self.displaypixmap = QPixmap.fromImage(self.displayimage)
+ self.photo.setPixmap(self.displaypixmap)
+ else:
+ self.photo.setText("No photo available")
+ self.pb_changephoto.setEnabled(True)
+
def updateTable(self, text):
if text.isEmpty():
self.model.setFilter("")
if self.model.rowCount() == 1:
self.tableview.selectRow(0)
def resetPressed(self):
- self.fillForm([])
+ self.current_record = self.model.record(self.current_row)
+ self.fillForm()
self.pb_reset.setEnabled(False)
self.pb_save.setEnabled(False)
self.pb_saveclose.setEnabled(False)
print "Resetting form"
def closePressed(self):
print "Closing"
+ self.model.revertAll()
self.reject()
def savePressed(self):
print "Saving"
+ self.current_record.setValue("netid",QVariant(self.netid.text()))
+ self.current_record.setValue("forename",QVariant(self.firstname.text()))
+ self.current_record.setValue("surname",QVariant(self.surname.text()))
+ self.current_record.setValue("email",QVariant(self.email.text()))
+ self.current_record.setValue("phone",QVariant(self.phone.text()))
+ self.current_record.setValue("major",QVariant(self.major.text()))
+ self.current_record.setValue("room",QVariant(self.room.text()))
+ self.current_record.setValue("dorm",QVariant(self.dorm.currentText()))
+ y = self.year.currentText().toInt()[0]
+ m = self.month.currentIndex() + 1
+ d = self.day.currentText().toInt()[0] + 1
+ bday = QDate(y,m,d)
+ self.current_record.setValue("birthday", QVariant(bday.toString(Qt.ISODate)) )
+ self.current_record.setValue("mtime", QVariant(QDateTime.currentDateTime().toString(Qt.ISODate)) ) # update modification time
+
+ self.model.setRecord(self.current_row, self.current_record)
+ self.model.submitAll()
+ self.updateTable(self.search_bar.text()) # Reselect from the DB
self.pb_reset.setEnabled(False)
self.pb_save.setEnabled(False)
self.pb_saveclose.setEnabled(False)
self.displayimage = self.currentimage.scaled(self.photo.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.displaypixmap = QPixmap.fromImage(self.displayimage)
self.photo.setPixmap(self.displaypixmap)
-
- def fillForm(self, args):
- print "Filling form"
-
- def recordSelected(self):
- print "New record selected"
- self.fillForm([])
- self.pb_changephoto.setEnabled(True)
+ ba = QByteArray()
+ buffer = QBuffer(ba)
+ buffer.open(QIODevice.WriteOnly)
+ self.currentimage.save(buffer, "JPG")
+ buffer.close()
+ self.current_record.setValue("photo",QVariant(ba))
+ self.pb_reset.setEnabled(True)
+ self.pb_save.setEnabled(True)
+ self.pb_saveclose.setEnabled(True)
if __name__ == "__main__":
self.dorm = QComboBox()
self.room = QLineEdit()
# Fill the comboboxes with values, set valid field types
- years = QStringList(["1987","1988","1989","1990","1991","1992","1993","1994"])
+ years = QStringList(["1983","1984","1985","1986","1987","1988","1989","1990","1991","1992","1993","1994"])
months = QStringList(["January","February","March","April","May","June","July","August","September","October","November","December"])
days = QStringList(["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"])
dorms = QStringList(["Clements","Lechner","McFadden"])
self.year.addItems(years)
self.month.addItems(months)
self.day.addItems(days)
+ self.year.setCurrentIndex(6) # Start in 1988 or so
self.dorm.addItems(dorms)
self.phone.setInputMask("(999)-999-9999")
self.major.setMaxLength(4)