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.
(cherry picked from commit 9d5e2370f86223555e7055c6b116d99593f24da4)