Support new databases and opening existing databases.
New person wizard actually adds data to the database.
Other classes have access to the database when they want it.
from editperson import EditPerson
class ChooseAction (QWidget):
- def __init__(self, parent=None):
+ def __init__(self, parent=None, db=None):
QWidget.__init__(self,parent)
- self.l1 = QLabel("<center><h1>Select a task:<h1></center>")
- self.pb1 = QCommandLinkButton("Add a person to the directory","If you haven't seen this program before, hit this button.")
- self.pb2 = QCommandLinkButton("Edit a directory record","If you've already added your data, but want to edit it, hit this button.")
- self.pb3 = QCommandLinkButton("Import photos","To match picture from a camera to entered data, hit this button.")
- self.pb4 = QCommandLinkButton("Export as document","To export the directory as a document for printing, hit this button.")
+ self.db = db
+ self.l1 = QLabel("<h1>Select a task:<h1>")
+ self.l1.setAlignment(QtCore.Qt.AlignCenter)
+ self.pb1 = QCommandLinkButton("&Add a person to the directory","If you haven't seen this program before, hit this button.")
+ self.pb2 = QCommandLinkButton("&Edit a directory record","If you've already added your data, but want to edit it, hit this button.")
+ self.pb3 = QCommandLinkButton("&Import photos","To match picture from a camera to entered data, hit this button.")
+ self.pb4 = QCommandLinkButton("E&xport as document","To export the directory as a document for printing, hit this button.")
self.layout = QGridLayout()
self.layout.addWidget(self.l1,0,0,1,3)
self.layout.addWidget(self.pb1,1,1,1,1)
QtCore.QObject.connect(self.pb4, QtCore.SIGNAL("clicked()"), self.exportDocument)
self.setLayout(self.layout)
def addPerson(self):
- wiz = NewPersonWizard()
+ wiz = NewPersonWizard(self,self.db)
wiz.exec_()
wiz = None
def editPerson(self):
- ep = EditPerson()
+ ep = EditPerson(self,self.db)
ep.exec_()
ep = None
def importPhotos(self):
pass
def exportDocument(self):
- pass
+ fileName = QFileDialog.getSaveFileName(self, "Save new file as:", ".", "OpenDocument Text Documents (*.odt)")
+ print "Will export db as",fileName
from PyQt4.QtGui import *
class EditPerson(QDialog):
- def __init__(self, parent=None):
+ def __init__(self, parent=None, db=None):
QDialog.__init__(self,parent)
+ self.db = db
self.setWindowTitle("Edit records")
self.toplayout = QHBoxLayout() # Master layout
self.room_lab = QLabel("Room number:")
self.bday_lab = QLabel("Birthday:")
self.dorm_lab = QLabel("Dorm:")
- self.photo = QLabel("<center>Photo goes here</center>")
+ self.photo = QLabel("Photo goes here")
self.photo.setScaledContents(True)
+ self.currentimage = QImage()
+ self.displayimage = QImage()
+ self.displaypixmap = QPixmap()
+ self.photo.setAlignment(Qt.AlignCenter)
self.pb_changephoto = QPushButton("Select &photo")
+ self.pb_changephoto.setEnabled(True)
# Personal info
self.surname = QLineEdit()
self.firstname = QLineEdit()
self.buttonlayout.addWidget(self.pb_cancel)
self.buttonlayout.addWidget(self.pb_save)
self.buttonlayout.addWidget(self.pb_saveclose)
- self.pb_changephoto.setEnabled(False)
def setupActions(self):
# Update filtered table
QObject.connect(self.search_bar, SIGNAL("textChanged(QString)"), self.updateTable )
from PyQt4 import QtCore
from PyQt4.QtGui import *
+from PyQt4.QtSql import *
from chooseaction import ChooseAction
class MainApp (QMainWindow):
+ schema = "CREATE TABLE people ( id INTEGER PRIMARY KEY AUTOINCREMENT, netid TEXT NOT NULL, surname TEXT NOT NULL, forename TEXT NOT NULL, email TEXT, birthday TEXT, phone TEXT, major TEXT, dorm TEXT, room INTEGER, photo BLOB, createtime TEXT NOT NULL, mtime TEXT NOT NULL);"
def __init__(self, Parent=None):
QMainWindow.__init__(self, Parent)
self.setWindowTitle("Whitepages V3")
- self.center = ChooseAction(self)
+ self.db = QSqlDatabase.addDatabase("QSQLITE")
+ self.db.setDatabaseName("families.db")
+ self.db.open()
+ q = QSqlQuery(self.db)
+ q.exec_(MainApp.schema)
+
+ self.center = ChooseAction(self,self.db)
self.setCentralWidget(self.center)
self.createActions()
def newFile(self):
- print "Close the existing database, create a new one."
+ self.db.close()
+ fileName = QFileDialog.getSaveFileName(self, "Save new file as:", ".", "Databases (*.db)")
+ self.db.setDatabaseName(fileName)
+ self.db.open()
+ q = QSqlQuery(self.db)
+ q.exec_(MainApp.schema)
+ print "Closed the existing database, created",fileName
def openFile(self):
fileName = QFileDialog.getOpenFileName(self, "Open dataset", ".", "Databases (*.db)")
- print "and then we'd open the db",fileName
+ if not fileName.isEmpty():
+ self.db.close()
+ self.db.setDatabaseName(fileName)
+ self.db.open()
+ print "Loaded database",fileName
+ else:
+ print "Canceled loading database"
def mergeWizard(self):
fileName = QFileDialog.getOpenFileName(self, "Import which dataset?", ".", "Databases (*.db)" )
print "and then we'd import data from",fileName
from pagecommit import PageCommit
class NewPersonWizard(QWizard):
- def __init__(self, parent=None):
+ def __init__(self, parent=None,db=None):
QWizard.__init__(self, parent)
+ self.db = db
self.addPage(PageNetID())
self.addPage(PageNewUserData())
- self.addPage(PageCommit())
+ self.addPage(PageCommit(self,self.db))
self.resize(400,300)
self.show()
from PyQt4.QtCore import *
from PyQt4.QtGui import *
+from PyQt4.QtSql import *
class PageCommit(QWizardPage):
- def __init__(self, parent=None):
+ def __init__(self, parent=None, db=None):
QWizardPage.__init__(self,parent)
+ self.db = db
self.instructions = QLabel("You're done with the computer. Go have your picture taken.\n\nThe SAs/PAs will add the photo to your data.")
self.instructions.setWordWrap(True)
self.grid = QGridLayout()
self.setLayout(self.grid)
self.setTitle("Move-in Wizard")
self.setSubTitle("Photo")
+
def initializePage(self):
- pass # handle committing the data from the previous pages to the DB
+ q = QSqlQuery(self.db)
+ q.prepare("INSERT INTO people (netid, forename, surname, email, birthday, phone, major, dorm, room, createtime, mtime )"
+ "VALUES (:netid, :forename, :surname, :email, :birthday, :phone, :major, :dorm, :room, :createtime, :mtime )" );
+ q.bindValue(":netid", self.field("netid") )
+ q.bindValue(":forename", self.field("forename") )
+ q.bindValue(":surname", self.field("surname") )
+ q.bindValue(":email", self.field("email") )
+ y = self.field("year").toInt()[0] + 1987
+ m = self.field("month").toInt()[0] + 1
+ d = self.field("day").toInt()[0] + 1
+ bday = QDate(y,m,d)
+ q.bindValue(":birthday", QVariant(bday.toString(Qt.ISODate)) )
+ q.bindValue(":phone", self.field("phone") )
+ q.bindValue(":major", self.field("major") )
+ q.bindValue(":dorm", self.field("dorm") )
+ q.bindValue(":room", self.field("room") )
+ q.bindValue(":createtime", QVariant(QDateTime.currentDateTime().toString(Qt.ISODate)) )
+ q.bindValue(":mtime", QVariant(QDateTime.currentDateTime().toString(Qt.ISODate)) )
+ q.exec_()
+ print "Inserted into database"
if __name__ == "__main__":
a = QApplication([""])
self.instructions = QLabel("&Enter your NetID:")
self.netid = QLineEdit()
self.instructions.setBuddy(self.netid)
- self.registerField("netid*", self.netid, "text", SIGNAL("textChanged(QString)"))
+ self.registerField("netid*", self.netid)
self.grid = QGridLayout()
self.grid.addWidget(self.instructions,0,0)
self.grid.addWidget(self.netid,0,1)
class PageNewUserData(QWizardPage):
def __init__(self, parent=None):
QWizardPage.__init__(self,parent)
- self.completed = False # Is the page done being filled out?
+ self.setCommitPage(True)
# Create all the subwidgets
self.year_lab = QLabel("Year:")
self.month_lab = QLabel("Month:")
self.day_lab = QLabel("Day:")
- self.firstname_lab = QLabel("Preferred name:")
+ self.forename_lab = QLabel("Preferred name:")
self.surname_lab = QLabel("Last name:")
self.email_lab = QLabel("Email:")
self.phone_lab = QLabel("Phone:")
self.dorm_lab = QLabel("Dorm:")
# Personal info
self.surname = QLineEdit()
- self.firstname = QLineEdit()
+ self.forename = QLineEdit()
self.phone = QLineEdit()
self.email = QLineEdit()
self.major = QLineEdit()
# Register fields with QWizard
self.registerField("surname*", self.surname)
- self.registerField("firstname*", self.firstname)
+ self.registerField("forename*", self.forename)
self.registerField("phone", self.phone)
self.registerField("email*", self.email)
self.registerField("major", self.major)
self.bday_layout.addWidget(self.year)
self.bday_layout.addWidget(self.month)
self.bday_layout.addWidget(self.day)
- self.layout.addWidget(self.firstname_lab, 1, 0)
- self.layout.addWidget(self.firstname, 1, 1)
+ self.layout.addWidget(self.forename_lab, 1, 0)
+ self.layout.addWidget(self.forename, 1, 1)
self.layout.addWidget(self.surname_lab, 2, 0)
self.layout.addWidget(self.surname, 2, 1)
self.layout.addWidget(self.email_lab, 3, 0)
if(key == "sn"):
self.surname.setText(value)
elif(key == "givenName"):
- self.firstname.setText(value)
+ self.forename.setText(value)
elif(key == "mail"):
self.email.setText(value)
elif(key == "major"):