Implement moving of elements.

- NEU: Elemente können verschoben werden.
This commit is contained in:
Daniel Kraus 2015-08-12 22:03:25 +02:00
parent 12c2412e38
commit 2bb0452a87
7 changed files with 257 additions and 17 deletions

BIN
gimp/down.xcf Normal file

Binary file not shown.

BIN
gimp/up.xcf Normal file

Binary file not shown.

BIN
zaaReloaded2/Icons/down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 545 B

BIN
zaaReloaded2/Icons/up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 537 B

View File

@ -280,6 +280,34 @@ namespace zaaReloaded2.ViewModels
}
}
public DelegatingCommand MoveElementUpCommand
{
get
{
if (_moveElementUpCommand == null)
{
_moveElementUpCommand = new DelegatingCommand(
param => DoMoveElementUp(),
param => CanMoveElementUp());
}
return _moveElementUpCommand;
}
}
public DelegatingCommand MoveElementDownCommand
{
get
{
if (_moveElementDownCommand == null)
{
_moveElementDownCommand = new DelegatingCommand(
param => DoMoveElementDown(),
param => CanMoveElementDown());
}
return _moveElementDownCommand;
}
}
#endregion
#region Public methods
@ -436,6 +464,201 @@ namespace zaaReloaded2.ViewModels
bool CanCopyElement() { return LastSelectedElement != null && LastSelectedElement.IsSelected; }
void DoMoveElementUp()
{
if (CanMoveElementUp())
{
// We need to get a hold of the LastSelectedElement because a TreeView
// might reset the selection when we move elements around.
ElementViewModel lastSelectedElement = LastSelectedElement;
// Top-level elements are either control elements or format elements;
// child elements on the second level however are always format elements
// and must be treated differently.
if (IsTopLevelElement())
{
int index = Elements.IndexOf(lastSelectedElement);
if (lastSelectedElement is ControlElementViewModel ||
Elements[index - 1] is FormatElementViewModel ||
!((ControlElementViewModel)Elements[index - 1]).CanHaveChildren
)
{
// Simple case: top-level control element -- just move it up;
// if the selected element is a format element and the element
// above it is a format element too, just move it up as well.
// If the element above the selected element is a control element,
// but cannot have children, move the selected element up as well.
Elements.RemoveAt(index);
Elements.Insert(index - 1, lastSelectedElement);
_settings.Elements.RemoveAt(index);
_settings.Elements.Insert(
index - 1,
LastSelectedElement.RevealModelObject() as ElementBase);
}
else
{
// If we get here, the selected element is a format element
// and the element above it is a control element that can
// have child elements, i.e. the selected element is demoted
// to a child element of the control element above it.
ControlElementViewModel controlElementAbove =
Elements[index - 1] as ControlElementViewModel;
Elements.RemoveAt(index);
controlElementAbove.AddChildElement(
lastSelectedElement as FormatElementViewModel);
FormatElementBase model = lastSelectedElement.RevealModelObject() as FormatElementBase;
ControlElementBase modelAbove = _settings.Elements[index - 1] as ControlElementBase;
_settings.Elements.RemoveAt(index);
modelAbove.Children.Add(model);
}
}
else
{
// The selected element is a child element.
// If it is at the top of the child elements list, promote it
// to a top-level element; if not, just move it up in the
// child elements list.
FormatElementViewModel selected = lastSelectedElement as FormatElementViewModel;
int index = selected.Parent.Elements.IndexOf(selected);
if (index == 0)
{
// Promote the element from the top of the children list
// to a top-level element above its parent.
int parentIndex = Elements.IndexOf(selected.Parent);
selected.Parent.Elements.RemoveAt(0);
Elements.Insert(parentIndex, selected);
FormatElementBase model = selected.RevealModelObject() as FormatElementBase;
ControlElementBase parentModel = selected.Parent.RevealModelObject() as ControlElementBase;
parentModel.Children.RemoveAt(0);
_settings.Elements.Insert(parentIndex, model);
selected.Parent = null;
}
else
{
selected.Parent.Elements.Move(index, index - 1);
ControlElementBase parentModel = selected.Parent.RevealModelObject() as ControlElementBase;
FormatElementBase selectedModel = parentModel.Children[index];
parentModel.Children.RemoveAt(index);
parentModel.Children.Insert(index - 1, selectedModel);
}
}
// Select the last selected element again.
lastSelectedElement.IsSelected = true;
}
}
bool CanMoveElementUp()
{
if (IsTopLevelElement())
{
return Elements.IndexOf(LastSelectedElement) > 0;
}
else
{
// If the selected element is a child element, it can always be moved
// up before the parent element.
return LastSelectedElement != null && LastSelectedElement.IsSelected;
}
}
void DoMoveElementDown()
{
if (CanMoveElementDown())
{
// We need to get a hold of the LastSelectedElement because a TreeView
// might reset the selection when we move elements around.
ElementViewModel lastSelectedElement = LastSelectedElement;
// Top-level elements are either control elements or format elements;
// child elements on the second level however are always format elements
// and must be treated differently.
if (IsTopLevelElement())
{
int index = Elements.IndexOf(lastSelectedElement);
if (lastSelectedElement is ControlElementViewModel ||
Elements[index + 1 ] is FormatElementViewModel ||
!((ControlElementViewModel)Elements[index + 1]).CanHaveChildren
)
{
// Simple case: top-level control element -- just move it down;
// if the selected element is a format element and the element
// below it is a format element too, just move it down as well.
// If the element below the selected element is a control element,
// but cannot have children, move the selected element down as well.
Elements.RemoveAt(index);
Elements.Insert(index + 1, lastSelectedElement);
_settings.Elements.RemoveAt(index);
_settings.Elements.Insert(
index + 1,
LastSelectedElement.RevealModelObject() as ElementBase);
}
else
{
// If we get here, the selected element is a format element
// and the element below it is a control element that can
// have child elements, i.e. the selected element is demoted
// to a child element of the control element below it.
ControlElementViewModel controlElementBelow =
Elements[index + 1] as ControlElementViewModel;
Elements.RemoveAt(index);
controlElementBelow.Elements.Insert(
0,
lastSelectedElement as FormatElementViewModel);
((FormatElementViewModel)lastSelectedElement).Parent = controlElementBelow;
FormatElementBase model = lastSelectedElement.RevealModelObject() as FormatElementBase;
ControlElementBase modelBelow = _settings.Elements[index + 1] as ControlElementBase;
_settings.Elements.RemoveAt(index);
modelBelow.Children.Insert(0, model);
}
}
else
{
// The selected element is a child element.
// If it is at the bottom of the child elements list, promote it
// to a top-level element; if not, just move it down in the
// child elements list.
FormatElementViewModel selected = lastSelectedElement as FormatElementViewModel;
int index = selected.Parent.Elements.IndexOf(selected);
if (index == selected.Parent.Elements.Count - 1)
{
// Promote the element from the bottom of the children list
// to a top-level element below its parent.
int parentIndex = Elements.IndexOf(selected.Parent);
selected.Parent.Elements.RemoveAt(selected.Parent.Elements.Count - 1);
Elements.Insert(parentIndex + 1, selected);
FormatElementBase model = selected.RevealModelObject() as FormatElementBase;
ControlElementBase parentModel = selected.Parent.RevealModelObject() as ControlElementBase;
parentModel.Children.RemoveAt(parentModel.Children.Count - 1);
_settings.Elements.Insert(parentIndex + 1, model);
selected.Parent = null;
}
else
{
selected.Parent.Elements.Move(index, index + 1);
ControlElementBase parentModel = selected.Parent.RevealModelObject() as ControlElementBase;
FormatElementBase selectedModel = parentModel.Children[index];
parentModel.Children.RemoveAt(index);
parentModel.Children.Insert(index + 1, selectedModel);
}
}
// Select the last selected element again.
lastSelectedElement.IsSelected = true;
}
}
bool CanMoveElementDown()
{
if (IsTopLevelElement())
{
return Elements.IndexOf(LastSelectedElement) < Elements.Count - 1;
}
else
{
// If the selected element is a child element, it can always be moved
// down after the parent element.
return LastSelectedElement != null && LastSelectedElement.IsSelected;
}
}
/// <summary>
/// Sets LastSelectedElement property whenever the IsSelected
/// property of an ElementViewModel changes
@ -458,7 +681,8 @@ namespace zaaReloaded2.ViewModels
/// </summary>
bool IsTopLevelElement()
{
return (LastSelectedElement is ControlElementViewModel ||
return LastSelectedElement != null &&
(LastSelectedElement is ControlElementViewModel ||
((FormatElementViewModel)LastSelectedElement).Parent == null);
}
@ -490,6 +714,8 @@ namespace zaaReloaded2.ViewModels
DelegatingCommand _editElementCommand;
DelegatingCommand _deleteElementCommand;
DelegatingCommand _copyElementCommand;
DelegatingCommand _moveElementUpCommand;
DelegatingCommand _moveElementDownCommand;
Message<ViewModelMessageContent> _addElementMessage;
Message<ViewModelMessageContent> _addChildElementMessage;
Message<ViewModelMessageContent> _editElementMessage;

View File

@ -23,7 +23,8 @@
xmlns:b="clr-namespace:Bovender.Mvvm.Views.Settings;assembly=Bovender"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:action="clr-namespace:Bovender.Mvvm.Actions;assembly=Bovender"
Width="500" Height="440" ResizeMode="CanResizeWithGrip" ShowInTaskbar="False"
Width="460" Height="400" ResizeMode="CanResizeWithGrip" ShowInTaskbar="False"
MinWidth="460" MinHeight="400"
WindowStyle="ToolWindow" Topmost="True"
b:WindowState.CenterScreen="True" b:WindowState.Save="True"
Title="Stil bearbeiten"
@ -66,21 +67,30 @@
<GroupBox Header="Elemente" Margin="0 10 0 0">
<DockPanel LastChildFill="True">
<StackPanel DockPanel.Dock="Right" Margin="10 0 0 0">
<Button Command="{Binding AddElementCommand}" ToolTip="Neues Element" Margin="0 0 0 10">
<Image Source="/zaaReloaded2;component/Icons/plus.png" Width="24" />
</Button>
<Button Command="{Binding AddChildElementCommand}" ToolTip="Neues Kindelement" Margin="0 0 0 10">
<Image Source="/zaaReloaded2;component/Icons/plus-child.png" Width="24" />
</Button>
<Button Command="{Binding DeleteElementCommand}" ToolTip="Entfernen" Margin="0 0 0 10">
<Image Source="/zaaReloaded2;component/Icons/minus.png" Width="24" />
</Button>
<Button Command="{Binding EditElementCommand}" IsDefault="True" ToolTip="Bearbeiten" Margin="0 0 0 10">
<Image Source="/zaaReloaded2;component/Icons/pen.png" Width="24" />
</Button>
<Button Command="{Binding CopyElementCommand}" ToolTip="Kopieren" Margin="0 0 0 10">
<Image Source="/zaaReloaded2;component/Icons/duplicate.png" Width="24" />
</Button>
<!-- need the stack panel to prevent stretching of the uniform grid -->
<UniformGrid Columns="2">
<Button Command="{Binding AddElementCommand}" ToolTip="Neues Element" Margin="0 0 5 10">
<Image Source="/zaaReloaded2;component/Icons/plus.png" Width="24" />
</Button>
<Button Command="{Binding EditElementCommand}" IsDefault="True" ToolTip="Bearbeiten" Margin="5 0 0 10">
<Image Source="/zaaReloaded2;component/Icons/pen.png" Width="24" />
</Button>
<Button Command="{Binding AddChildElementCommand}" ToolTip="Neues Kindelement" Margin="0 0 5 10">
<Image Source="/zaaReloaded2;component/Icons/plus-child.png" Width="24" />
</Button>
<Button Command="{Binding MoveElementUpCommand}" ToolTip="Nach oben" Margin="5 0 0 10">
<Image Source="/zaaReloaded2;component/Icons/up.png" Width="24" />
</Button>
<Button Command="{Binding DeleteElementCommand}" ToolTip="Entfernen" Margin="0 0 5 10">
<Image Source="/zaaReloaded2;component/Icons/minus.png" Width="24" />
</Button>
<Button Command="{Binding MoveElementDownCommand}" ToolTip="Nach unten" Margin="5 0 0 10">
<Image Source="/zaaReloaded2;component/Icons/down.png" Width="24" />
</Button>
<Button Command="{Binding CopyElementCommand}" ToolTip="Kopieren" Margin="0 0 5 10">
<Image Source="/zaaReloaded2;component/Icons/duplicate.png" Width="24" />
</Button>
</UniformGrid>
</StackPanel>
<TreeView ItemsSource="{Binding Elements}">
<TreeView.ItemContainerStyle>

View File

@ -401,6 +401,10 @@
<ItemGroup>
<Resource Include="Icons\plus-child.png" />
</ItemGroup>
<ItemGroup>
<Resource Include="Icons\down.png" />
<Resource Include="Icons\up.png" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>