Initial support for layer groups

Layers can now be grouped using the new group action. For this purpose the
flat layers list in the document was replaced by the new LayerStack class,
which is subclassed from list in order to provide a similar interface. A
LayerStack can contain layers and other LayerStack s, creating a recursive
structure. Layers also record their parent LayerStack.
The documents layer_idx has been replaced by a reference to the layer
itself. Wherever indices are needed, they are used together with the parent
stack.

This commit is only the initial part of the conversion and very buggy.