-
Notifications
You must be signed in to change notification settings - Fork 0
/
EWOBadges.py
274 lines (218 loc) · 9.55 KB
/
EWOBadges.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python3
# convert ui to py
# pyuic4 qtdesigner.ui -o qtdesigner.py
import csv
import os
import subprocess
import sys
from PyQt4 import QtCore, QtGui
from kuntze.oneBadge import oneBadge
from kuntze.resource import resource_path
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
from gui import Ui_MainWindow
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
__author__ = 'Mathias Kuntze'
class MyForm(QtGui.QMainWindow):
backgroundFileTypes = dict(defaultextension='.jpg', filetypes=[('.jpg', '*.jpg')])
textFileTypes = dict(defaultextension='.txt', filetypes=[('Text-Datei', '*.txt'), ('CSV-Datei', '*.csv')])
pdfFileTypes = dict(defaultextension='.pdf', filetypes=[('PDF', '*.pdf')])
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setWindowTitle(_translate("MainWindow", "EWOBadges v1.2", None))
self.backgroundPath = ""
self.backgroundPath_2 = ""
self.databasePath = ""
self.outputPath = ""
self.logoText = ""
self.ui.button_next.clicked.connect(self.nextTab)
self.ui.button_preview.clicked.connect(self.prevTab)
self.ui.button_load_data.clicked.connect(self.loadDatabase)
self.ui.button_start_generate.clicked.connect(self.generateBadges)
self.ui.button_background.clicked.connect(self.loadBackground)
self.ui.button_background_2.clicked.connect(self.loadBackground_2)
self.ui.Tab.currentChanged.connect(self.tabHandler)
self.ui.table_databases.setColumnCount(4)
self.ui.table_databases.setRowCount(8)
self.ui.table_databases.setHorizontalHeaderLabels(['Name', 'Spitzname', 'Studiengang', 'Jahr'])
def nextTab(self):
if self.ui.Tab.currentIndex() > 1:
return
self.ui.Tab.setCurrentIndex(self.ui.Tab.currentIndex() + 1)
def prevTab(self):
if self.ui.Tab.currentIndex() < 1:
return
self.ui.Tab.setCurrentIndex(self.ui.Tab.currentIndex() - 1)
def loadDatabase(self):
self.databasePath = QtGui.QFileDialog().getOpenFileName(caption="Namen", filter="CSV-Datei (*.txt *.csv)")
if not self.databasePath:
return
self.ui.table_databases.clear()
self.ui.table_databases.setColumnCount(4)
self.ui.table_databases.setRowCount(0)
self.ui.table_databases.setHorizontalHeaderLabels(['Name', 'Spitzname', 'Studiengang', 'Jahr'])
reader = csv.reader(open(self.databasePath), delimiter=';')
i = 0
for row in reader:
self.ui.table_databases.insertRow(i)
self.ui.table_databases.setItem(i, 0, QtGui.QTableWidgetItem(row[0]))
self.ui.table_databases.setItem(i, 1, QtGui.QTableWidgetItem(row[1]))
self.ui.table_databases.setItem(i, 2, QtGui.QTableWidgetItem(row[2]))
self.ui.table_databases.setItem(i, 3, QtGui.QTableWidgetItem(row[3]))
i += 1
self.ui.table_databases.resizeColumnsToContents()
self.ui.table_databases.resizeRowsToContents()
# front page
def loadBackground(self):
self.backgroundPath = QtGui.QFileDialog().getOpenFileName(caption="Vorderseite", filter="*.jpg")
pixmap = QtGui.QPixmap(self.backgroundPath)
pixmap = pixmap.scaled(self.ui.label_bg_prev.size(), QtCore.Qt.KeepAspectRatio)
self.ui.label_bg_prev.setPixmap(pixmap)
self.ui.label_bg_prev.show()
self.ui.edit_path_bg.setText(self.backgroundPath)
# back page
def loadBackground_2(self):
self.backgroundPath_2 = QtGui.QFileDialog().getOpenFileName(caption="Rückseite", filter="*.jpg")
pixmap = QtGui.QPixmap(self.backgroundPath_2)
pixmap = pixmap.scaled(self.ui.label_bg_prev_2.size(), QtCore.Qt.KeepAspectRatio)
self.ui.label_bg_prev_2.setPixmap(pixmap)
self.ui.label_bg_prev_2.show()
self.ui.edit_path_bg_2.setText(self.backgroundPath_2)
def generateBadges(self):
progress_max = self.ui.table_databases.rowCount()
self.ui.textarea_log.clear()
fontName = 'agency-fb'
fontPath = resource_path('misc/fonts/' + fontName + '.ttf')
pdfmetrics.registerFont(TTFont(fontName, fontPath))
imagePath = self.backgroundPath
imagePath_2 = self.backgroundPath_2
self.logoText = self.ui.edit_title.text()
if not imagePath:
msg = QtGui.QMessageBox()
msg.setText("Hintergrundbild muss angegeben werden!")
msg.setIcon(QtGui.QMessageBox.Warning)
msg.setWindowTitle("Hintergrundbild fehlt")
msg.setStandardButtons(QtGui.QMessageBox.Ok)
msg.exec_()
self.ui.Tab.setCurrentIndex(0)
self.ui.button_preview.setDisabled(True)
self.ui.button_next.setDisabled(False)
return
if not self.logoText:
msg = QtGui.QMessageBox()
msg.setText("Titel muss angegeben werden!")
msg.setIcon(QtGui.QMessageBox.Warning)
msg.setWindowTitle("Titel fehlt")
msg.setStandardButtons(QtGui.QMessageBox.Ok)
msg.exec_()
self.ui.Tab.setCurrentIndex(0)
self.ui.button_preview.setDisabled(True)
self.ui.button_next.setDisabled(False)
return
if not imagePath_2:
imagePath_2 = imagePath
self.outputPath = QtGui.QFileDialog().getSaveFileName(caption="Speicherort", filter="*.pdf")
if not self.outputPath:
return
badge_width = 9 * cm
badge_height = 6 * cm
c = canvas.Canvas(self.outputPath, pagesize=A4) # pagesize=[2*badge_width, badge_height])
title = self.ui.edit_title.text()
if not title:
title = 'Tutor'
offset_bottom = 25
offset_left = 40
for i in range(1, progress_max + 1):
name = self.ui.table_databases.item(i - 1, 0)
nick = self.ui.table_databases.item(i - 1, 1)
sg = self.ui.table_databases.item(i - 1, 2)
year = self.ui.table_databases.item(i - 1, 3)
rightText = list()
if sg is not None:
if sg.text() is not "":
rightText.append(sg.text())
if year is not None:
if year.text() is not "":
rightText.append(year.text())
if name is not None:
if name.text() is not "":
name = name.text()
else:
name = ""
else:
name = ""
if nick is not None:
if nick.text() is not "":
nick = nick.text()
else:
nick = ""
else:
nick = ""
flag = []
if name is "":
flag.append("Name")
if nick is "":
flag.append("Spitzname")
if len(flag) > 0:
self.ui.textarea_log.append(str(i) + " Es fehlen Informationen: " + ', '.join(flag))
if len(rightText) == 0:
a = ""
else:
a = "'".join(rightText)
oneBadge(c, imagePath, name, title, a, badge_width=badge_width, badge_height=badge_height,
font_name=fontName, offset=(offset_left, offset_bottom))
oneBadge(c, imagePath_2, nick, '', a, badge_width=badge_width, badge_height=badge_height,
font_name=fontName, offset=(offset_left + badge_width, offset_bottom))
if name is "" or name is None:
self.ui.textarea_log.append(str(i) + " leerer Badge wurde erzeugt.")
else:
self.ui.textarea_log.append(str(i) + " Badge fuer " + name + " wurde erzeugt.")
offset_bottom += badge_height + 25
pagebreak = False
if i % 4 == 0:
offset_bottom = 25
c.showPage()
percent = (100 / progress_max) * i
self.ui.progressbar.setValue(percent)
self.ui.textarea_log.append("")
self.ui.textarea_log.append("Alle Badges wurden erstellt und gespeichert (" + self.outputPath + ") !")
c.setAuthor('StuRa TU Ilmenau')
c.setTitle('Badges für die Erstiwoche TU Ilmenau')
c.setCreator('EWOBadges.exe @Mathias Kuntze')
c.setSubject('https://github.com/KuntzeM/EWOBadges')
c.save()
if sys.platform == 'linux':
subprocess.call(["xdg-open", self.outputPath])
else:
os.startfile(self.outputPath)
def tabHandler(self, i):
if i == 0:
self.ui.button_preview.setDisabled(True)
self.ui.button_next.setDisabled(False)
if i == 2:
self.ui.button_preview.setDisabled(False)
self.ui.button_next.setDisabled(True)
if i == 1:
self.ui.button_preview.setDisabled(False)
self.ui.button_next.setDisabled(False)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())