MIERUNE / qgis-plugx-plugin

QGIS to Illustrator with PlugX
GNU General Public License v2.0
3 stars 0 forks source link

Fix svg/raster for subsymbol and subsubsu... ones #102

Closed bordoray closed 11 months ago

bordoray commented 11 months ago

Issue

close #98

変更内容:Description

テスト手順:Test

image

その他:Notes

bordoray commented 11 months ago
def export_assets_from(symbol: QgsSymbol, output_dir: str):
    asset_dir = get_asset_dir(output_dir)
    if not os.path.exists(asset_dir):
        os.makedirs(asset_dir)

    for symbol_layer in symbol:
        if symbol_layer.type() ....

Make makedir out of for symbol_layer in symbol: loop means assets folder will be produced even if there is no asset.

I couldn't understand why export_assets_from() is called twice inside itself

Agree that it is difficult to understand...

            if symbol_layer.layerType() not in ["RasterMarker", "SvgMarker"]:
                if symbol_layer.subSymbol():
                    export_assets_from(symbol_layer.subSymbol(), output_dir)
                continue  # skip: extract only raster or svg marker

A given symbol layer may be not RasterMarker and SvgMarker, but its subsymbol or its subsub symbol may be RasterMarker or SvgMarker. If we not call its subsymbol before continue , loop may skip its subsubsymbol

In below example which have symbol and subsymbol order:

I commit a mix of your refactor and what I suggest imo

Kanahiro commented 11 months ago

Make makedir out of for symbol_layer in symbol: loop means assets folder will be produced even if there is no asset.

I think this in not problem. Actually it is somewhat redundant but it assures the directory always exists when assets are exported, and the logic become simpler.

Kanahiro commented 11 months ago

How is this? I refactored

def export_assets_from(symbol: QgsSymbol, output_dir: str):
    asset_dir = get_asset_dir(output_dir)
    if not os.path.exists(asset_dir):
        os.makedirs(asset_dir)

    for symbol_layer in symbol:
        if symbol_layer.subSymbol():
            # recursive: if the symbol layer has sub symbol, extract from it
            export_assets_from(symbol_layer.subSymbol(), output_dir)

        if symbol_layer.type() == Qgis.SymbolType.Marker:
            # extract only raster or svg marker
            if symbol_layer.layerType() not in ["RasterMarker", "SvgMarker"]:
                continue  # if not, skip

            asset_path = os.path.join(asset_dir, get_asset_name(symbol_layer))
            shutil.copy(
                symbol_layer.path(),
                asset_path,
            )
bordoray commented 11 months ago

Implemented, thanks!