]> git.zarvox.org Git - wp3.git/commitdiff
Make opening a new database work correctly in EditPerson.
authorDrew Fisher <drew.m.fisher@gmail.com>
Mon, 13 Jul 2009 01:01:32 +0000 (18:01 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Mon, 13 Jul 2009 01:01:32 +0000 (18:01 -0700)
This patch fixes the bug where you'd open a new database, edit some data
in it, hit save, and it would update that row in the originally-opened
database.

Also thrown in is some refactoring work in EditPerson to maximize code reuse.

editperson.py
mainapp.py

index 9422a85de1da1f8439b9ed1a20b5f0ceaf230865..959bce981753aef9a1cb464c400cca1ced9b7381 100644 (file)
@@ -7,12 +7,7 @@ class EditPerson(QWidget):
                QWidget.__init__(self,parent)
                self.db = db
 
-               self.model = QSqlTableModel(self, self.db)
-               self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
-               self.model.setTable("people")
-               self.model.select()
-               self.model.setHeaderData(self.model.fieldIndex("forename"), Qt.Horizontal, QVariant("First Name"))
-               self.model.setHeaderData(self.model.fieldIndex("surname"), Qt.Horizontal, QVariant("Last Name"))
+               self.setupModel()
 
                self.tableview = QTableView(self)
                self.tableview.setSelectionBehavior(QAbstractItemView.SelectRows)
@@ -56,6 +51,13 @@ class EditPerson(QWidget):
                self.show()
                
 
+       def setupModel(self):
+               self.model = QSqlTableModel(self, self.db)
+               self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
+               self.model.setTable("people")
+               self.model.select()
+               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):
                self.current_record = QSqlRecord()
                self.current_row = -1
@@ -239,21 +241,7 @@ class EditPerson(QWidget):
                print "Closing"
                self.model.revertAll()
                # Wipe contents of all lineedits, destroy current_record and current_index
-               self.search_bar.clear()
-               self.current_record = QSqlRecord()
-               self.current_index = -1
-               self.surname.clear()
-               self.firstname.clear()
-               self.phone.clear()
-               self.email.clear()
-               self.major.clear()
-               self.netid.clear()
-               self.year.setCurrentIndex(0)
-               self.month.setCurrentIndex(0)
-               self.day.setCurrentIndex(0)
-               self.dorm.setCurrentIndex(0)
-               self.room.clear()
-               self.photo.setText("No record selected")
+               self.wipeFields()
                self.model.select()
                self.emit(SIGNAL("done()"))
        def savePressed(self):
@@ -300,6 +288,34 @@ class EditPerson(QWidget):
                        self.pb_reset.setEnabled(True)
                        self.pb_save.setEnabled(True)
                        self.pb_saveclose.setEnabled(True)
+       def wipeFields(self):
+               self.search_bar.clear()
+               self.current_record = QSqlRecord()
+               self.current_index = -1
+               self.surname.clear()
+               self.firstname.clear()
+               self.phone.clear()
+               self.email.clear()
+               self.major.clear()
+               self.netid.clear()
+               self.year.setCurrentIndex(0)
+               self.month.setCurrentIndex(0)
+               self.day.setCurrentIndex(0)
+               self.dorm.setCurrentIndex(0)
+               self.room.clear()
+               self.photo.setText("No record selected")
+
+       def updateDB(self, newdbname):
+               print "updateDB called",newdbname
+               self.db.close()
+               self.db.setDatabaseName(newdbname)
+               self.db.open()
+               self.setupModel()
+               self.tableview.setModel(self.model)
+               QObject.connect(self.tableview.selectionModel(), SIGNAL("selectionChanged(QItemSelection, QItemSelection)"), self.selectionChanged )
+               self.wipeFields()
+               self.updateTable(QString())
+
 
 
 if __name__ == "__main__":
index 599cd3dbeb0519f82b5eb60fffaa61c2a67a17eb..14ee8658e04f5bcda32f2a1acdf0f5cfa0c91dca 100644 (file)
@@ -43,6 +43,7 @@ class MainApp (QMainWindow):
                QtCore.QObject.connect( self.fileQuitAction, QtCore.SIGNAL("triggered()"), self.quit)
                QtCore.QObject.connect( self.chooseaction, QtCore.SIGNAL("editPerson()"), self.editPersonSlot)
                QtCore.QObject.connect( self.editperson, QtCore.SIGNAL("done()"), self.returnToMainMenu)
+               QtCore.QObject.connect( self, QtCore.SIGNAL("databaseChanged(QString)"), self.editperson.updateDB)
                
        
        def createMenus(self):
@@ -65,6 +66,7 @@ class MainApp (QMainWindow):
                        q = QSqlQuery(self.db)
                        q.exec_(MainApp.schema)
                        self.statusBar().showMessage("Closed the existing database, created " + fileName)
+                       self.emit(QtCore.SIGNAL("databaseChanged(QString)"),self.db.databaseName())
                else:
                        self.statusBar().showMessage("Cancelled creating new database")
        def openFile(self):
@@ -74,6 +76,7 @@ class MainApp (QMainWindow):
                        self.db.setDatabaseName(fileName)
                        self.db.open()
                        self.statusBar().showMessage("Successfully loaded database " + fileName)
+                       self.emit(QtCore.SIGNAL("databaseChanged(QString)"),self.db.databaseName())
                else:
                        self.statusBar().showMessage("Canceled loading database")
        def mergeWizard(self):