Skip to content

Commit a924dca

Browse files
author
Shuge Lee
committed
Added Qt resource system usage demo and auto_create_qrc.py.
1 parent b17ce34 commit a924dca

File tree

5 files changed

+253
-30
lines changed

5 files changed

+253
-30
lines changed

resource_compiler/auto_create_qrc.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/usr/bin/env python
2+
"""
3+
auto scan resources file and create Qt resource(qrc) file for PySide/PyQt project
4+
5+
Usage:
6+
python auto_create_qrc.py your_pictures_path > bar.qrc
7+
pyrcc4-2.7 -no-compress bar.qrc -o bar.py
8+
9+
Author: Shuge Lee <[email protected]>
10+
License: MIT License
11+
"""
12+
import os
13+
import re
14+
import sys
15+
import web
16+
17+
PWD = os.path.dirname(os.path.realpath(__file__))
18+
19+
20+
def tree(top = '.',
21+
filters = None,
22+
output_prefix = None,
23+
max_level = 4,
24+
followlinks = False,
25+
top_info = False,
26+
report = True):
27+
# The Element of filters should be a callable object or
28+
# is a byte array object of regular expression pattern.
29+
topdown = True
30+
total_directories = 0
31+
total_files = 0
32+
33+
top_fullpath = os.path.realpath(top)
34+
top_par_fullpath_prefix = os.path.dirname(top_fullpath)
35+
36+
if top_info:
37+
lines = top_fullpath
38+
else:
39+
lines = ""
40+
41+
if filters is None:
42+
_default_filter = lambda x : not x.startswith(".")
43+
filters = [_default_filter]
44+
45+
for root, dirs, files in os.walk(top = top_fullpath, topdown = topdown, followlinks = followlinks):
46+
assert root != dirs
47+
48+
if max_level is not None:
49+
cur_dir = web.utils.strips(root, top_fullpath)
50+
path_levels = web.utils.strips(cur_dir, "/").count("/")
51+
if path_levels > max_level:
52+
continue
53+
54+
total_directories += len(dirs)
55+
total_files += len(files)
56+
57+
for filename in files:
58+
for _filter in filters:
59+
if callable(_filter):
60+
if not _filter(filename):
61+
total_files -= 1
62+
continue
63+
elif not re.search(_filter, filename, re.UNICODE):
64+
total_files -= 1
65+
continue
66+
67+
if output_prefix is None:
68+
cur_file_fullpath = os.path.join(top_par_fullpath_prefix, root, filename)
69+
else:
70+
buf = web.utils.strips(os.path.join(root, filename), top_fullpath)
71+
if output_prefix != "''":
72+
cur_file_fullpath = os.path.join(output_prefix, buf.strip('/'))
73+
else:
74+
cur_file_fullpath = buf
75+
76+
lines = "%s%s%s" % (lines, os.linesep, cur_file_fullpath)
77+
78+
lines = lines.lstrip(os.linesep)
79+
80+
if report:
81+
report = "%d directories, %d files" % (total_directories, total_files)
82+
lines = "%s%s%s" % (lines, os.linesep * 2, report)
83+
84+
return lines
85+
86+
87+
QRC_TPL = """<!DOCTYPE RCC><RCC version="1.0">
88+
<qresource>
89+
%s
90+
</qresource>
91+
</RCC>"""
92+
93+
94+
def scan_files(src_path = "."):
95+
filters = ['.(png|jpg|gif)$']
96+
output_prefix = './'
97+
report = False
98+
lines = tree(src_path, filters = filters, output_prefix = output_prefix, report = report)
99+
100+
lines = lines.split('\n')
101+
if "" in lines:
102+
lines.remove("")
103+
104+
return lines
105+
106+
def create_qrc_body(src_path):
107+
lines = scan_files(src_path)
108+
109+
buf = ["<file>%s</file>" % i for i in lines]
110+
buf = "\n".join(buf)
111+
buf = QRC_TPL % buf
112+
113+
return buf
114+
115+
def create_qrc(src_path, dst_file):
116+
buf = create_qrc_body(src_path)
117+
118+
if dst_file:
119+
parent = os.path.dirname(dst_file)
120+
if not os.path.exists(parent):
121+
os.makedirs(parent)
122+
123+
f = file(dst_file, "w")
124+
f.write(buf)
125+
f.close()
126+
else:
127+
sys.stdout.write(buf)
128+
129+
if __name__ == "__main__":
130+
args = sys.argv[1:]
131+
132+
if not args:
133+
msg = "python auto_create_qrc.py <src_path> [dst_qrc_file]"
134+
sys.stdout.write('\n' + msg + '\n')
135+
sys.exit(-1)
136+
137+
src_path = args[0]
138+
139+
if len(args) > 1:
140+
dst_file = args[1]
141+
else:
142+
dst_file = None
143+
144+
create_qrc(src_path, dst_file)

resource_compiler/foo.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
#-*- coding:utf-8 -*-
3+
"""
4+
Qt resource system usage
5+
6+
Test environment:
7+
Mac OS X 10.6.8
8+
9+
http://doc.qt.nokia.com/latest/resources.html
10+
http://doc.qt.nokia.com/latest/rcc.html
11+
"""
12+
import sys
13+
14+
try:
15+
from PySide import QtCore
16+
from PySide import QtGui
17+
except ImportError:
18+
from PyQt4 import QtCore
19+
from PyQt4 import QtGui
20+
21+
22+
import bar
23+
24+
25+
class Demo(QtGui.QWidget):
26+
def __init__(self):
27+
super(Demo, self).__init__()
28+
29+
x, y, w, h = 500, 200, 300, 400
30+
self.setGeometry(x, y, w, h)
31+
32+
pix = QtGui.QPixmap(":/resources/icons/camera.png")
33+
label = QtGui.QLabel(self)
34+
label.setPixmap(pix)
35+
label.move(10, 10)
36+
37+
38+
def show_and_raise(self):
39+
self.show()
40+
self.raise_()
41+
42+
43+
if __name__ == "__main__":
44+
app = QtGui.QApplication(sys.argv)
45+
46+
demo = Demo()
47+
demo.show_and_raise()
48+
49+
sys.exit(app.exec_())
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Notes On Resources
2+
3+
Qt resource system 用于在可执行应用程序里面保存二进制文件(icons, translation files 等等)。
4+
5+
- [The Qt Resource System](http://doc.qt.nokia.com/latest/resources.html)
6+
- [Resource Compiler (rcc)](http://doc.qt.nokia.com/latest/rcc.html)
7+
8+
rcc means Qt's **r**esour**c**e **c**ompiler .
9+
10+
11+
## Example
12+
13+
find . | grep -v DS_Store
14+
15+
.
16+
./foo.py
17+
./resources
18+
./resources/icons
19+
./resources/icons/camera.png
20+
./resources/icons/iblah.png
21+
./resources/icons/mic.png
22+
./resources/icons/send_files.png
23+
24+
25+
rcc -project -o bar.qrc
26+
pyrcc4-2.7 -no-compress bar.qrc -o bar.py
27+
28+
29+
in foo.py
30+
31+
import bar
32+
33+
...
34+
pix = QtGui.QPixmap(":/resources/icons/camera.png")
2.37 KB
Loading

scripts/detect_env.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/python
12
"""
23
Detect platform name and SIP/Python/PyQt/PySide version.
34
@@ -9,51 +10,46 @@
910
References:
1011
- http://www.pyside.org/docs/pyside/pysideversion.html
1112
"""
12-
import platform
13+
1314
import sys
14-
import time
15+
import platform
1516

1617
try:
1718
import PySide
1819
from PySide import QtCore
20+
print "Using PySide ..."
21+
print
1922
except ImportError:
20-
from PyQt4 import QtCore
2123
PySide = None
24+
from PyQt4 import QtCore
25+
2226

2327
try:
2428
import sip
2529
except ImportError:
2630
sip = None
2731

28-
def get_platform():
29-
pl = None
30-
while not pl:
31-
try:
32-
pl = platform.system()
33-
except:
34-
# sometime system call failed on MacOS 10.6,
35-
# fix it in a quick and dirty way
36-
time.sleep(0.1)
37-
continue
38-
return pl
39-
40-
def get_python_version():
41-
return sys.version[:3]
42-
43-
print 'Platform:', get_platform()
44-
45-
print 'Python:', get_python_version()
46-
47-
if sip:
48-
print 'SIP:', sip.SIP_VERSION_STR
4932

50-
if not PySide:
51-
print "Qt:", QtCore.QT_VERSION_STR
52-
print 'PyQt:', QtCore.PYQT_VERSION_STR
53-
else:
54-
print 'Qt:', QtCore.__version__
55-
print "PySide:", PySide.__version__
33+
def get_qt_version():
34+
if not PySide:
35+
return QtCore.QT_VERSION_STR
36+
else:
37+
return QtCore.__version__
5638

39+
def get_pyside_version():
40+
return PySide and PySide.__version__
5741

42+
def main():
43+
print 'Platform: %s' % sys.platform
44+
print "Python version: %s" % platform.python_version()
45+
print "Qt version: %s" % get_qt_version()
5846

47+
if sip:
48+
print "SIP version: %s" % sip.SIP_VERSION_STR
49+
if PySide:
50+
print "PySide version: %s" % PySide.__version__
51+
else:
52+
print "PyQt version: %s" % QtCore.PYQT_VERSION_STR
5953

54+
if __name__ == "__main__":
55+
main()

0 commit comments

Comments
 (0)