def set_break(legend, value_break, layer_alpha):
    """
    Create legend's breaks to represent categories or ranges

    :param value_break: range or category object
    """
    symbol = value_break.symbol()
    alpha_factor = symbol.alpha() * layer_alpha

    symbol_layer = symbol.symbolLayers()[0]
    layer_type = symbol_layer.layerType()
    properties = symbol_layer.properties()

    # Create a new break sub-element in legend
    legend_break = ET.SubElement(legend, 'Break')

    # setting default values
    color = ''
    line_color = ''
    if layer_type == 'SimpleMarker' or layer_type == 'SimpleFill':
        if properties['style'] != 'no':
            color = utils.rgba2argb(properties['color'],alpha_factor)
        if properties['outline_style'] != 'no':
            line_color = utils.rgba2argb(properties['outline_color'],alpha_factor)
        line_width = properties['outline_width']

    elif layer_type == 'SimpleLine':
        if properties['line_style'] != 'no':
            line_color = utils.rgba2argb(properties['line_color'],alpha_factor)
        line_width = properties['line_width']
    else:
        color = utils.random_color()
        line_color = '255,0,0,0'
        line_width = '1'
        print "Not possible to render the symbol properly a default was used instead"

    legend_break.set('EndColor', color)
    legend_break.set('StartColor', color)
    legend_break.set('OutlineEndColor', line_color)
    legend_break.set('OutlineStartColor', line_color)

    # Check if value_break has a single value or a lower and upper value
    # This is the only difference between graduated ranges and categorized categories
    try:
        # This will run for graduated layers
        legend_break.set('StartText', "{:.9f}".format(value_break.lowerValue()))
        legend_break.set('EndText', "{:.9f}".format(value_break.upperValue()))
    except AttributeError:
        # This will run for categorized layers
        value = value_break.value()
        # Convert to string in case of non string values
        if not isinstance(value, basestring):
            value = unicode(value)
        legend_break.set('StartText', value) # FIXME::Must check if values are always strings
        legend_break.set('EndText', value)

    legend_break.set('Rotulo', value_break.label())
    legend_break.set('Imagem', '')
    legend_break.set('Width', str(utils.mm2px(line_width)))
def set_vector_legend(legend, alpha_factor, symbol_layer=None):
    """
    Function to fill legend attributes with values from the symbolLayer
    """
    # Set default values for the case that layer_symbol arguments is not declared
    legend.set('BackColor', '')
    legend.set('LineColor', '')
    legend.set('Width', '2')
    legend.set('ImagePath', '')
    legend.set('ImageScale', '1')
    legend.set('EnableHatch', 'False')
    legend.set('Hatch', '0')
    legend.set('FieldName', '')
    legend.set('DashPattern', '')  # FIXME Need to understand how dash Pattern is read in gison3dmap
    legend.set('CampoRotacao', '')
    legend.set('CorSel', '255,255,255,0')

    # Check the type of symbolLayer and fill the legend attributes according
    if symbol_layer:
        layer_type = symbol_layer.layerType()
        properties = symbol_layer.properties()

        if layer_type == 'SimpleFill':
            if properties['style'] != 'no':
                legend.set('BackColor', utils.rgba2argb(properties['color'],alpha_factor))

            if properties['style'] not in ('solid','no'):
                legend.set('EnableHatch', 'True')
                legend.set('Hatch', '0') # FIXME:: Hatchs in QGIS are different from gison3dmap

            if properties['outline_style'] != 'no':
                legend.set('LineColor', utils.rgba2argb(properties['outline_color'],alpha_factor))

            legend.set('Width', utils.mm2px(properties['outline_width']))
            legend.set('DashPattern', '')  # ??

        elif layer_type == 'SimpleMarker':
            print properties
            legend.set('BackColor', utils.rgba2argb(properties['color'],alpha_factor))

            if properties['outline_style'] != 'no':
                legend.set('LineColor', utils.rgba2argb(properties['outline_color'],alpha_factor))

            legend.set('Width', utils.mm2px(properties['size']))
            legend.set('DashPattern', '')  # ??

        elif layer_type == 'SimpleLine':
            if properties['line_style'] != 'no':
                legend.set('LineColor', utils.rgba2argb(properties['line_color'],alpha_factor))
            legend.set('Width', utils.mm2px(properties['line_width']))
            legend.set('DashPattern', '')  # ??

        elif layer_type == 'ImageFill':
            legend.set('ImagePath', properties['imageFile'])
            legend.set('ImageScale', '1')
        else:
            legend.set('BackColor', utils.random_color())
            legend.set('LineColor', utils.random_color())