diff options
author | Jens Bache-Wiig <jbache@trolltech.com> | 2010-02-19 15:45:39 +0100 |
---|---|---|
committer | Jens Bache-Wiig <jbache@trolltech.com> | 2010-02-19 15:45:39 +0100 |
commit | c364038a0a55c541a434e17322bd0c4c72d65e96 (patch) | |
tree | a9a22600c0f7d2b7a9c80a2de09569824468bf70 | |
parent | 3b5bfffcbec6076fbe1d908d75703a5a59f45076 (diff) |
Added support for exporting text layers
-rwxr-xr-x | qmlexporter.py | 101 |
1 files changed, 75 insertions, 26 deletions
diff --git a/qmlexporter.py b/qmlexporter.py index fbfc1b5..3f867e6 100755 --- a/qmlexporter.py +++ b/qmlexporter.py @@ -10,7 +10,64 @@ import os gettext.install("gimp20-python", gimp.locale_directory, unicode = True) -def export_qml(image, qmlname, path): +imagedirectory = "" +imagepath = "" +path = "" + +def format_color(color) : + return "Qt.rgba(%i, %i, %i, %i)" % (color[0]/255.0, + color[1]/255.0, + color[2]/255.0, + color[3]/255.0) + +def dump_common_properties(layer, layername, f) : + f.write(' id:' + layername +'\n') + opacity = layer.opacity / 100.0 + if opacity < 1.0: # Only dump opacity if it is required + f.write(' opacity: %s' % opacity +'\n') + f.write(' x:%s ; y:%s' % layer.offsets + '\n') + f.write(' width:%s ' % layer.width + '\n') + f.write(' height:%s ' % layer.height + '\n') + +def dump_text_element(layer, layername, f): + f.write(' Text {\n') + f.write(' text:\'%s\' ' % pdb.gimp_text_layer_get_text(layer) + '\n') + f.write(' font.pixelSize:%i ' % pdb.gimp_text_layer_get_font_size(layer)[0] + '\n') + color = pdb.gimp_text_layer_get_color(layer) + f.write(' color:' + format_color(color) + '\n') + dump_common_properties(layer, layername, f) + f.write(' }\n') + +def dump_image_element(layer, layername, f, image): + global imagedirectory + global imagepath + global path + opacity = layer.opacity / 100.0 + name = imagepath + "/" + layername + ".png" + fullpath = os.path.join(path, name); + + # Write out the element + f.write(' Image {\n') + f.write(' source:\"' + imagedirectory + "/" + layername + '.png\"\n') + + # Dump common properties + dump_common_properties(layer, layername, f) + + # Store the layer as a .png + pdb.file_png_save(image, layer, fullpath, name, 0, 9, 1, 1, 1, 1, 1) + f.write(' }\n') + +# Removes non-usable id-characters from layer names +def fix_name(name) : + fixedname = name.lower() + fixedname = fixedname.replace(' ', '_') + fixedname = fixedname.replace('#', '__') + fixedname = fixedname.replace('!', '___') + return fixedname + +def export_qml(image, qmlname, path, flatten): + global imagepath + global imagedirectory qmlfilename = os.path.join(path, qmlname + '.qml') imagedirectory = qmlname + "_images" @@ -23,34 +80,24 @@ def export_qml(image, qmlname, path): f.write('import Qt 4.6\n') f.write('Rectangle {\n') copy = image.duplicate() - - i = len(copy.layers) - 1 + + layercount = len(copy.layers) + # We give the base element the same size as the background + bglayer = copy.layers[layercount-1] + f.write(' width:%s ' % bglayer.width + '\n') + f.write(' height:%s ' % bglayer.height + '\n') + + i = layercount - 1 while i >= 0 : layer = copy.layers[i] layer.visible = 1 - layername = layer.name.lower() - layername = layername.replace(' ', '_') - layername = layername.replace('#', '__') - - name = imagepath + "/" + layername + ".png" - fullpath = os.path.join(path, name); - opacity = layer.opacity / 100.0 - - # Write out the element - f.write(' Image {\n') - f.write(' id:' + layername +'\n') - - if opacity < 1.0: # Only dump opacity if it is required - f.write(' opacity: %s' % opacity +'\n') + layername = fix_name(layer.name) - f.write(' source:\"' + imagedirectory + "/" + layername + '.png\"\n') - f.write(' x:%s ; y:%s' % layer.offsets + '\n') - f.write(' width:%s ' % layer.width + '\n') - f.write(' height:%s ' % layer.height + '\n') + if pdb.gimp_drawable_is_text_layer(layer) and not flatten : + dump_text_element(layer, layername, f) + else : + dump_image_element(layer, layername, f, copy) - # Store the layer as a .png - pdb.file_png_save(copy, layer, fullpath, name, 0, 9, 1, 1, 1, 1, 1) - f.write(' }\n') i = i - 1 f.write('}\n') f.close() @@ -66,8 +113,10 @@ register( imagetypes=("*"), params=[ (PF_IMAGE, "image", "Image", None), - (PF_STRING, "qmlname", "QML Element", "MyElement"), - (PF_DIRNAME, "path", "Save QML to this directory", os.getcwd()),], + (PF_STRING, "qmlname", "QML Element Name:", "MyElement"), + (PF_DIRNAME, "path", "Save QML to this Directory", os.getcwd()), + (PF_BOOL, "flatten", "Convert text to image:", 0), + ], results=[], function=(export_qml), menu=("<Image>/File"), |