]> git.zarvox.org Git - wp3.git/commitdiff
Export to ODT.
authorDrew Fisher <drew.m.fisher@gmail.com>
Sat, 22 Aug 2009 03:12:54 +0000 (22:12 -0500)
committerDrew Fisher <drew.m.fisher@gmail.com>
Sat, 22 Aug 2009 03:12:54 +0000 (22:12 -0500)
A limited ODT exporter.  It's not pretty, nor suitable for final
printing, but illustrates the concept and mechanism.

mainapp.py
odtwriter.py [new file with mode: 0644]

index e57fe55d1671a1d7b71e8d61376e6df8990e0bc4..1c96c10f2fd456f1e171d18358538ae790e03953 100644 (file)
@@ -5,6 +5,7 @@ from PyQt4.QtSql import *
 from chooseaction import ChooseAction
 from editperson import EditPerson
 from mergephotos import MergePhotos
+from odtwriter import ODTWriter
 
 class MainApp (QMainWindow):
        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);"
@@ -150,6 +151,9 @@ class MainApp (QMainWindow):
        def mergePhotos(self):
                print "beginning photo merge"
                self.mergephotos.beginMerge()
+       def exportDocumentSlot(self, filename):
+               writer = ODTWriter(self.db)
+               writer.write(filename)
 
 
 if __name__ == "__main__" :
diff --git a/odtwriter.py b/odtwriter.py
new file mode 100644 (file)
index 0000000..ddaf83b
--- /dev/null
@@ -0,0 +1,63 @@
+from PyQt4.QtCore import *
+from PyQt4.QtGui import *
+from PyQt4.QtSql import *
+
+class ODTWriter():
+       def __init__(self, db):
+               self.db = db
+
+       def write(self, filename):
+               doc = QTextDocument()
+               cur = QTextCursor(doc)
+               defaultFormat = QTextCharFormat()
+               defaultFormat.setFontPointSize(12)
+               nameFormat = QTextCharFormat()
+               nameFormat.setFontPointSize(12)
+               nameFormat.setFontWeight(QFont.Bold)
+               q = QSqlQuery(self.db)
+               q.exec_("SELECT * FROM people")
+               rec = q.record()
+               col_id = rec.indexOf("id")
+               col_forename = rec.indexOf("forename")
+               col_surname = rec.indexOf("surname")
+               col_netid = rec.indexOf("netid")
+               col_email = rec.indexOf("email")
+               col_birthday = rec.indexOf("birthday")
+               col_phone = rec.indexOf("phone")
+               col_major = rec.indexOf("major")
+               col_dorm = rec.indexOf("dorm")
+               col_room = rec.indexOf("room")
+               col_photo = rec.indexOf("photo")
+               col_createtime = rec.indexOf("createtime")
+               col_mtime = rec.indexOf("mtime")
+               while q.next():
+                       # Write this record to the document
+                       id = q.value(col_id).toString()
+                       forename = q.value(col_forename).toString()
+                       surname = q.value(col_surname).toString()
+                       email = q.value(col_email).toString()
+                       birthday = q.value(col_birthday).toString()
+                       phone = q.value(col_phone).toString()
+                       if phone.localeAwareCompare(QString("()==")) == 0: # if they didn't put one in the DB
+                               phone = QString("")
+                       photo_bin = q.value(col_photo).toByteArray()
+                       photo = QImage()
+                       photo.loadFromData(photo_bin)
+                       # We probably need to normalize photo size here, or set up a QTextFrameFormat
+                       if photo.isNull(): # make a black photo for people without one
+                               photo = QImage(100,100,QImage.Format_RGB32)
+                               photo.fill(0)
+                               print "No photo for",forename,surname
+                       name = QString("mydata://") + id + QString(".jpg")
+                       doc.addResource(QTextDocument.ImageResource, QUrl(name), photo)
+                       imageFormat = QTextImageFormat()
+                       imageFormat.setName(name)
+                       imageFormat.setHeight(100.)
+                       imageFormat.setWidth(100.)
+#                      cur.insertImage(photo)
+                       cur.insertText(QString("%1 %2\n").arg(forename).arg(surname), nameFormat )
+                       cur.insertText(QString("%1").arg(q.value(col_email).toString()), defaultFormat)
+                       cur.insertImage(imageFormat, QTextFrameFormat.FloatLeft)
+                       cur.insertText(QString("\n") , defaultFormat)
+               writer = QTextDocumentWriter(filename, QByteArray("odt"))
+               return writer.write(doc)