Newer
Older

benjamin.jakimow@geo.hu-berlin.de
committed
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
import os, sys, fnmatch, six, subprocess, re
from PyQt4.QtSvg import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *
from PyQt4.QtXmlPatterns import *
ROOT = os.path.dirname(os.path.dirname(__file__))
from timeseriesviewer import DIR_UI, file_search
jp = os.path.join
def getDOMAttributes(elem):
assert isinstance(elem, QDomElement)
values = dict()
attributes = elem.attributes()
for a in range(attributes.count()):
attr = attributes.item(a)
values[str(attr.nodeName())] = attr.nodeValue()
return values
def make():
#find ui files
ui_files = file_search(ROOT, '*.ui', recursive=True)
qrcs = set()
doc = QDomDocument()
for ui_file in ui_files:
pathDir = os.path.dirname(ui_file)
if doc.setContent(QFile(ui_file)):
items = doc.elementsByTagName('iconset')
for i in range(items.count()):
nodeQRC = items.item(i)
attr = getDOMAttributes(nodeQRC.toElement())
if 'resource' in attr.keys():
qrcs.add((pathDir, str(attr['resource'])))
s = ""
#compile Qt resource files
#resourcefiles = file_search(ROOT, '*.qrc', recursive=True)
resourcefiles = list(qrcs)
assert len(resourcefiles) > 0
for root_dir, f in resourcefiles:
#dn = os.path.dirname(f)
pathQrc = os.path.normpath(jp(root_dir, f))
assert os.path.exists(pathQrc)
bn = os.path.basename(f)
bn = os.path.splitext(bn)[0]
pathPy2 = os.path.join(DIR_UI, bn+'_py2.py' )
pathPy3 = os.path.join(DIR_UI, bn+'_py3.py' )
print('Make {}'.format(pathPy2))
subprocess.call(['pyrcc4','-py2','-o',pathPy2, pathQrc])
print('Make {}'.format(pathPy3))
subprocess.call(['pyrcc4','-py3','-o',pathPy3, pathQrc])
def svg2png(pathDir, overwrite=False, mode='INKSCAPE'):
assert mode in ['INKSCAPE', 'WEBKIT', 'SVG']
from PyQt4.QtWebKit import QWebPage
svgs = file_search(pathDir, '*.svg')
app = QApplication([], True)
for pathSvg in svgs:
dn = os.path.dirname(pathSvg)
bn, _ = os.path.splitext(os.path.basename(pathSvg))
pathPng = jp(dn, bn+'.png')
if mode == 'SVG':
renderer = QSvgRenderer(pathSvg)
doc_size = renderer.defaultSize() # size in px
img = QImage(doc_size, QImage.Format_ARGB32)
#img.fill(0xaaA08080)
painter = QPainter(img)
renderer.render(painter)
painter.end()
if overwrite or not os.path.exists(pathPng):
img.save(pathPng, quality=100)
del painter, renderer
elif mode == 'WEBKIT':
page = QWebPage()
frame = page.mainFrame()
f = QFile(pathSvg)
if f.open(QFile.ReadOnly | QFile.Text):
textStream = QTextStream(f)
svgData = textStream.readAll()
f.close()
qba = QByteArray(str(svgData))
frame.setContent(qba,"image/svg+xml")
page.setViewportSize(frame.contentsSize())
palette = page.palette()
background_color = QColor(50,0,0,50)
palette.setColor(QPalette.Window, background_color)
brush = QBrush(background_color)
palette.setBrush(QPalette.Window, brush)
page.setPalette(palette)
img = QImage(page.viewportSize(), QImage.Format_ARGB32)
img.fill(background_color) #set transparent background
painter = QPainter(img)
painter.setBackgroundMode(Qt.OpaqueMode)
#print(frame.renderTreeDump())
frame.render(painter)
painter.end()
if overwrite or not os.path.exists(pathPng):
print('Save {}...'.format(pathPng))
img.save(pathPng, quality=100)
del painter, frame, img, page
s =""
elif mode == 'INKSCAPE':
dirInkscape = r'C:\Program Files\Inkscape'
assert os.path.isdir(dirInkscape)
cmd = [jp(dirInkscape,'inkscape')]
cmd.append('--file={}'.format(pathSvg))
cmd.append('--export-png={}'.format(pathPng))
subprocess.call(cmd)
s = ""
def png2qrc(icondir, pathQrc):
pathQrc = os.path.abspath(pathQrc)
dirQrc = os.path.dirname(pathQrc)
app = QApplication([])
assert os.path.exists(pathQrc)
doc = QDomDocument()
doc.setContent(QFile(pathQrc))
query = QXmlQuery()
#query.setQuery("doc('{}')/RCC/qresource/file".format(pathQrc))
query.setQuery("doc('{}')/RCC/qresource[@prefix=\"enmapbox/png\"]/file".format(pathQrc))
query.setQuery("for $x in doc('{}')/RCC/qresource[@prefix=\"enmapbox/png\"] return data($x)".format(pathQrc))
assert query.isValid()
#elem = doc.elementsByTagName('qresource')print
pngFiles = [r.strip() for r in str(query.evaluateToString()).split('\n')]
pngFiles = set([f for f in pngFiles if os.path.isfile(jp(dirQrc,f))])
for f in file_search(icondir, '*.png'):
xmlPath = os.path.relpath(f, dirQrc).replace('\\','/')
pngFiles.add(xmlPath)
pngFiles = sorted(list(pngFiles))
resourceNodes = doc.elementsByTagName('qresource')
for i in range(resourceNodes.count()):
resourceNode = resourceNodes.item(i).toElement()
if resourceNode.hasAttribute('prefix') and resourceNode.attribute('prefix') == "enmapbox/png":
childs = resourceNode.childNodes()
while not childs.isEmpty():
node = childs.item(0)
node.parentNode().removeChild(node)
for pngFile in pngFiles:
node = doc.createElement('file')
node.appendChild(doc.createTextNode(pngFile))
resourceNode.appendChild(node)
f = open(pathQrc, "w")
f.write(doc.toString())
f.close()
s = ""
if __name__ == '__main__':
icondir = jp(DIR_UI, *['icons'])
pathQrc = jp(DIR_UI,'resources.qrc')
if False:
#convert SVG to PNG and link them into the resource file
svg2png(icondir, overwrite=True)
png2qrc(icondir, pathQrc)
if True: make()
print('Done')