1 from PyQt4.QtCore import *
2 from PyQt4.QtGui import *
3 from PyQt4.QtSql import *
5 from chooseaction import ChooseAction
6 from editperson import EditPerson
7 from mergephotos import MergePhotos
8 from odtwriter import ODTWriter
10 class MainApp (QMainWindow):
11 schema = "CREATE TABLE people ( id INTEGER PRIMARY KEY AUTOINCREMENT, forename TEXT NOT NULL, surname TEXT NOT NULL, netid 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);"
12 def __init__(self, Parent=None):
13 QMainWindow.__init__(self, Parent)
14 self.setWindowTitle("Whitepages V3")
16 self.db = QSqlDatabase.addDatabase("QSQLITE")
17 self.db.setDatabaseName("families.db")
19 q = QSqlQuery(self.db)
20 q.exec_(MainApp.schema)
22 self.center = QStackedWidget()
23 self.chooseaction = ChooseAction(self,self.db)
24 self.editperson = EditPerson(self,self.db)
25 self.mergephotos = MergePhotos(self,self.db)
26 self.center.addWidget(self.chooseaction)
27 self.center.addWidget(self.editperson)
28 self.center.addWidget(self.mergephotos)
29 self.setCentralWidget(self.center)
34 self.statusBar().showMessage("Ready")
35 self.setUnifiedTitleAndToolBarOnMac(True);
39 def createActions(self):
40 self.fileNewAction = QAction("&New Database",self)
41 QObject.connect( self.fileNewAction, SIGNAL("triggered()"), self.newFile)
42 self.fileOpenAction = QAction("&Open Database",self)
43 QObject.connect( self.fileOpenAction, SIGNAL("triggered()"), self.openFile)
44 self.fileImportAction = QAction("&Import (Merge)", self)
45 QObject.connect( self.fileImportAction, SIGNAL("triggered()"), self.mergeWizard)
46 self.fileQuitAction = QAction("&Quit",self)
47 QObject.connect( self.fileQuitAction, SIGNAL("triggered()"), self.quit)
48 QObject.connect( self.chooseaction, SIGNAL("editPerson()"), self.editPersonSlot)
49 QObject.connect( self.chooseaction, SIGNAL("mergePhotos()"), self.mergePhotos)
50 QObject.connect( self.chooseaction, SIGNAL("exportDocument(QString)"), self.exportDocumentSlot)
51 QObject.connect( self.editperson, SIGNAL("done()"), self.returnToMainMenu)
52 QObject.connect( self, SIGNAL("databaseChanged(QString)"), self.editperson.updateDB)
53 QObject.connect( self, SIGNAL("mergeDone()"), self.editperson.reselect )
56 def createMenus(self):
57 self.fileMenu = self.menuBar().addMenu("&File")
58 self.fileMenu.addAction(self.fileNewAction)
59 self.fileMenu.addAction(self.fileOpenAction)
60 self.fileMenu.addAction(self.fileImportAction)
61 self.fileMenu.addSeparator()
62 self.fileMenu.addAction(self.fileQuitAction)
66 fileName = QFileDialog.getSaveFileName(self, "Save new file as:", ".", "Databases (*.db)")
67 if not fileName.isEmpty(): # If they cancelled, do nothing
68 if not fileName.endsWith(".db"): # if they leave off the extension, add it
69 fileName = fileName.append(".db")
71 self.db.setDatabaseName(fileName)
73 q = QSqlQuery(self.db)
74 q.exec_(MainApp.schema)
75 self.statusBar().showMessage("Closed the existing database, created " + fileName)
76 self.emit(SIGNAL("databaseChanged(QString)"),self.db.databaseName())
78 self.statusBar().showMessage("Cancelled creating new database")
80 fileName = QFileDialog.getOpenFileName(self, "Open dataset", ".", "Databases (*.db)")
81 if not fileName.isEmpty():
83 self.db.setDatabaseName(fileName)
85 self.statusBar().showMessage("Successfully loaded database " + fileName)
86 self.emit(SIGNAL("databaseChanged(QString)"),self.db.databaseName())
88 self.statusBar().showMessage("Canceled loading database")
89 def mergeWizard(self):
90 fileName = QFileDialog.getOpenFileName(self, "Import which dataset?", ".", "Databases (*.db)" )
91 if not fileName.isEmpty():
92 tempdb = QSqlDatabase.addDatabase("QSQLITE","tempdb")
93 tempdb.setDatabaseName(fileName)
95 q = QSqlQuery(self.db)
96 import_q = QSqlQuery(tempdb)
97 import_q.exec_("SELECT * FROM people;")
98 rec = import_q.record()
99 col_id = rec.indexOf("id")
100 col_forename = rec.indexOf("forename")
101 col_surname = rec.indexOf("surname")
102 col_netid = rec.indexOf("netid")
103 col_email = rec.indexOf("email")
104 col_birthday = rec.indexOf("birthday")
105 col_phone = rec.indexOf("phone")
106 col_major = rec.indexOf("major")
107 col_dorm = rec.indexOf("dorm")
108 col_room = rec.indexOf("room")
109 col_photo = rec.indexOf("photo")
110 col_createtime = rec.indexOf("createtime")
111 col_mtime = rec.indexOf("mtime")
112 #print "Number of columns:",rec.count()
114 while import_q.next():
115 qtestnewnetid = QSqlQuery(self.db)
116 qtestnewnetid.prepare("SELECT * FROM people WHERE netid = :netid")
117 qtestnewnetid.bindValue(":netid", import_q.value(col_netid) )
118 qtestnewnetid.exec_()
119 if qtestnewnetid.next(): # if the person's already in the DB, keep the old data
121 q.prepare("INSERT INTO people (netid, forename, surname, email, birthday, phone, major, dorm, room, createtime, mtime, photo )"
122 "VALUES (:netid, :forename, :surname, :email, :birthday, :phone, :major, :dorm, :room, :createtime, :mtime, :photo )" )
123 q.bindValue(":netid", import_q.value(col_netid) )
124 q.bindValue(":forename", import_q.value(col_forename) )
125 q.bindValue(":surname", import_q.value(col_surname) )
126 q.bindValue(":email", import_q.value(col_email) )
127 q.bindValue(":birthday", import_q.value(col_birthday) )
128 q.bindValue(":phone", import_q.value(col_phone) )
129 q.bindValue(":major", import_q.value(col_major) )
130 q.bindValue(":dorm", import_q.value(col_dorm) )
131 q.bindValue(":room", import_q.value(col_room) )
132 q.bindValue(":createtime", import_q.value(col_createtime) )
133 q.bindValue(":mtime", import_q.value(col_mtime) )
134 q.bindValue(":photo", import_q.value(col_photo) )
136 print "Inserted", import_q.value(col_forename).toString(), import_q.value(col_surname).toString(), "into DB"
137 records_merged = records_merged + 1
138 self.statusBar().showMessage(QString("Successfully merged %1 records from %2").arg(QString(str(records_merged))).arg(fileName))
140 self.emit(SIGNAL("mergeDone()"))
142 self.statusBar().showMessage("Canceled merging databases")
144 # You could do cleanup, like closing/flushing the database, an "Are you sure you want to quit?"
145 # modal dialog, or saving the window layout/state.
147 def returnToMainMenu(self):
148 self.center.setCurrentWidget(self.chooseaction)
149 def editPersonSlot(self):
150 self.editperson.model.select() # update the table
151 self.center.setCurrentWidget(self.editperson)
152 def mergePhotos(self):
153 print "beginning photo merge"
154 self.mergephotos.beginMerge()
155 def exportDocumentSlot(self, filename):
156 writer = ODTWriter(self.db)
157 writer.write(filename)
160 if __name__ == "__main__" :
161 app = QApplication([""])