Refactor handling of metadata a bit
Use QScopedPointer and always copy the metadata; otherwise we might be deleting the layer's own metadata object as passed through the visitor.