]> git.zarvox.org Git - wp3.git/blob - odtwriter.py
Added Python docstrings for all classes.
[wp3.git] / odtwriter.py
1 from PyQt4.QtCore import *
2 from PyQt4.QtGui import *
3 from PyQt4.QtSql import *
4
5 class ODTWriter():
6         def __init__(self, db):
7                 self.db = db
8
9         def write(self, filename):
10                 """Write out data from current database to <filename> in ODT format."""
11                 doc = QTextDocument()
12                 cur = QTextCursor(doc)
13                 cur.movePosition(QTextCursor.Start)
14                 defaultFormat = QTextCharFormat()
15                 defaultFormat.setFontPointSize(8)
16                 defaultFormat.setFontFamily("Serif")
17                 nameFormat = QTextCharFormat()
18                 nameFormat.setFontPointSize(10)
19                 nameFormat.setFontWeight(QFont.Bold)
20                 q = QSqlQuery(self.db)
21                 q.exec_("SELECT id FROM people")
22                 rec = q.record()
23                 total = 0
24                 while q.next():
25                         total = total + 1
26                 q.exec_("SELECT * FROM people ORDER BY surname, forename")
27                 rec = q.record()
28                 col_id = rec.indexOf("id")
29                 col_forename = rec.indexOf("forename")
30                 col_surname = rec.indexOf("surname")
31                 col_netid = rec.indexOf("netid")
32                 col_email = rec.indexOf("email")
33                 col_birthday = rec.indexOf("birthday")
34                 col_phone = rec.indexOf("phone")
35                 col_major = rec.indexOf("major")
36                 col_dorm = rec.indexOf("dorm")
37                 col_room = rec.indexOf("room")
38                 col_photo = rec.indexOf("photo")
39                 col_createtime = rec.indexOf("createtime")
40                 col_mtime = rec.indexOf("mtime")
41                 topFrame = cur.currentFrame()
42                 n = 0
43
44                 progress = QProgressDialog("Preparing document...", "Cancel", 0, total)
45                 progress.setWindowModality(Qt.WindowModal)
46                 progress.setMinimumDuration(0)
47
48                 while q.next():
49                         table_row = []
50                         while len(table_row) < 2:
51                                 progress.setValue(n)
52                                 if progress.wasCanceled():
53                                         progress.setValue(total)
54                                         return False
55                                 progress.setLabelText(QString("Adding %1 %2").arg(q.value(col_forename).toString()).arg(q.value(col_surname).toString()))
56                                 n = n + 1
57                                 # Write this record to the document
58                                 id = q.value(col_id).toString()
59                                 phone = q.value(col_phone).toString()
60                                 if phone.size() == 4: # if they didn't put one in the DB
61                                         phone = QString("")
62                                 photo_bin = q.value(col_photo).toByteArray()
63                                 photo = QImage()
64                                 photo.loadFromData(photo_bin)
65                                 if photo.isNull(): # make a black photo for people without one
66                                         photo = QImage(1,1,QImage.Format_RGB32)
67                                         photo.fill(0)
68                                         print str(QString("No photo for %1 %2").arg(q.value(col_forename).toString()).arg(q.value(col_surname).toString()))
69                                 else: # Shrink what is probably a ~20MB pixel buffer into something slightly more usable
70                                         photo = photo.scaled( 400, 400, Qt.KeepAspectRatio, Qt.SmoothTransformation)
71                                 name = QString("mydata://") + id + QString(".jpg")
72                                 doc.addResource(QTextDocument.ImageResource, QUrl(name), photo)
73                                 imageFormat = QTextImageFormat()
74                                 imageFormat.setName(name)
75                                 imageFormat.setHeight(99.)
76                                 imageFormat.setWidth(132.)
77                                 imageFormat.setVerticalAlignment(QTextCharFormat.AlignMiddle)
78                                 table_row.append({ "imageformat": imageFormat,
79                                                 "forename": q.value(col_forename).toString(),
80                                                 "surname" : q.value(col_surname).toString(),
81                                                 "email" : q.value(col_email).toString(),
82                                                 "phone" : phone,
83                                                 "birthday" : q.value(col_birthday).toString(),
84                                                 "dorm" : q.value(col_dorm).toString(),
85                                                 "room" : q.value(col_room).toString() } )
86                                 if len(table_row) < 2:
87                                         if not q.next():
88                                                 break
89                         # Now table_row has either:
90                         #  - Two records in sequence
91                         #  - The lone final record
92                         tableformat = QTextTableFormat()
93                         tableformat.setAlignment(Qt.AlignCenter)
94                         tableformat.setPageBreakPolicy(QTextFormat.PageBreak_AlwaysAfter)
95                         table = cur.insertTable(5, 4, tableformat)
96                         table.mergeCells(0,0,5,1)
97                         table.mergeCells(0,2,5,1)
98                         for i in xrange(len(table_row)):
99                                 p = table_row[i]
100                                 cur.insertText(QString("   "))
101                                 cur.insertImage(p["imageformat"], QTextFrameFormat.FloatLeft)
102                                 cur.movePosition(QTextCursor.NextCell)
103                                 cur.insertText(QString("%1 %2").arg(p["forename"]).arg(p["surname"]), nameFormat )
104                                 cur.movePosition(QTextCursor.Down)
105                                 cur.insertText(QString("%1").arg(p["email"]), defaultFormat)
106                                 cur.movePosition(QTextCursor.Down)
107                                 cur.insertText(QString("%1").arg(p["phone"]), defaultFormat)
108                                 cur.movePosition(QTextCursor.Down)
109                                 cur.insertText(QString("%1").arg(p["birthday"]), defaultFormat)
110                                 cur.movePosition(QTextCursor.Down)
111                                 cur.insertText(QString("%1 %2").arg(p["dorm"]).arg(p["room"]), defaultFormat )
112                                 if i == 0 and len(table_row) == 2:
113                                         cur = table.cellAt(0,2).firstCursorPosition()
114                                 else:
115                                         cur.setPosition(topFrame.lastPosition())
116                 progress.setLabelText(QString("Writing output document (this might take a while)..."))
117                 progress.setValue(total-1)
118                 writer = QTextDocumentWriter(filename, QByteArray("odt"))
119                 success = writer.write(doc)
120                 progress.setValue(total)
121                 return success
122
123 # Desired output
124 #  ------
125 # |  --  | forename surname
126 # | |^^| | email
127 # |  --  | birthday
128 # | /||\ | phone
129 # |  ||  | dorm, room
130 #  ------