Added Number-Operation + UI

This commit is contained in:
adroslice 2019-11-21 19:46:33 +01:00
parent ccd9a0efe2
commit 52ddcdddc3
4 changed files with 141 additions and 1 deletions

View File

@ -230,5 +230,35 @@
</Grid>
</Expander>
</DataTemplate>
<DataTemplate DataType="{x:Type ops:Number}">
<Expander Classes="OperationExpander">
<Grid ColumnDefinitions="auto,*" RowDefinitions="auto,auto,auto,auto,auto,auto,auto,auto,auto">
<TextBlock Grid.Row="0" Text="Mode:"/>
<ComboBox Grid.Row="0" Items="{Binding Modes}" Name="ModeSelector" SelectedIndex="{Binding Mode}" PropertyChanged="PreviewChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Grid.Row="1" Text="Pattern:" IsVisible="{Binding #ModeSelector.SelectedItem.IsReplace}"/>
<TextBox Grid.Row="1" Text="{Binding Pattern}" IsVisible="{Binding #ModeSelector.SelectedItem.IsReplace}" PropertyChanged="PreviewChanged"/>
<TextBlock Grid.Row="2" Text="Position:" IsVisible="{Binding #ModeSelector.SelectedItem.IsInsert}"/>
<NumericUpDown Grid.Row="2" Value="{Binding InsertPosition}" IsVisible="{Binding #ModeSelector.SelectedItem.IsInsert}" PropertyChanged="PreviewChanged"/>
<TextBlock Grid.Row="3" Text="Start Index:"/>
<NumericUpDown Grid.Row="3" Value="{Binding StartIndex}" PropertyChanged="PreviewChanged"/>
<TextBlock Grid.Row="4" Text="Increment:"/>
<NumericUpDown Grid.Row="4" Value="{Binding Increment}" PropertyChanged="PreviewChanged"/>
<TextBlock Grid.Row="5" Text="Padding:"/>
<NumericUpDown Grid.Row="5" Value="{Binding Padding}" PropertyChanged="PreviewChanged"/>
<TextBlock Grid.Row="6" Text="Auto Padding:"/>
<CheckBox Grid.Row="6" IsChecked="{Binding AutoPadding}" PropertyChanged="PreviewChanged"/>
<TextBlock Grid.Row="7" Text="Regex:" IsVisible="{Binding #ModeSelector.SelectedItem.IsReplace}"/>
<CheckBox Grid.Row="7" IsChecked="{Binding UseRegex}" IsVisible="{Binding #ModeSelector.SelectedItem.IsReplace}" PropertyChanged="PreviewChanged"/>
<TextBlock Grid.Row="8" Text="Full Name:" IsVisible="{Binding #ModeSelector.SelectedItem.IsReplace}"/>
<CheckBox Grid.Row="8" IsChecked="{Binding FullName}" IsVisible="{Binding #ModeSelector.SelectedItem.IsReplace}" PropertyChanged="PreviewChanged"/>
</Grid>
</Expander>
</DataTemplate>
</Window.DataTemplates>
</Window>

View File

@ -1,8 +1,8 @@
using System.Collections.Generic;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Collections;
using BFR.DataModels;
using BFR.Operations;
@ -26,6 +26,7 @@ namespace BFR
OperationType.Make<Overwrite>(),
OperationType.Make<Replace>(),
OperationType.Make<Remove>(),
OperationType.Make<Number>(),
};
public static readonly AvaloniaProperty<bool> IsCommitButtonEnabledProperty =

69
BFR/Operations/Number.cs Normal file
View File

@ -0,0 +1,69 @@
using System.Xml.Serialization;
using System.Collections.Generic;
using Avalonia;
using BFR.Helpers;
using BFR.DataModels;
namespace BFR.Operations
{
public class Number : Operation
{
public override string Name => nameof(Number);
public override string Description => "Numbers all file names according to how they are sorted.";
public static readonly AvaloniaProperty<NumberMode> ModeProperty =
AvaloniaProperty.Register<MainWindow, NumberMode>(nameof(Mode), NumberMode.Prefix);
public NumberMode Mode { get => GetValue(ModeProperty); set => SetValue(ModeProperty, value); }
[XmlIgnore]
public NumberOperationMode[] Modes => NumberOperationMode.Modes;
public int InsertPosition { get; set; } = 0;
public int Increment { get; set; } = 1;
public int StartIndex { get; set; } = 1;
public int Padding { get; set; } = 0;
public bool AutoPadding { get; set; } = false;
public bool FullName { get; set; } = false;
public bool UseRegex { get; set; } = false;
public string Pattern { get; set; } = "";
protected override void ApplyToInternal(IList<FileModel> files)
{
var index = StartIndex;
var padding = AutoPadding
? (StartIndex + Increment * (files.Count - 1)).ToString().Length
: Padding;
foreach (var file in files)
{
var newName = FullName ? file.FullName : file.Name;
var text = PadInteger(index, padding);
newName = Mode switch
{
NumberMode.Prefix => $"{text}{newName}",
NumberMode.Suffix => $"{newName}{text}",
NumberMode.Insert => newName.Insert(InsertPosition, text),
NumberMode.Replace => newName.Replace(Pattern, text, UseRegex, false),
_ => newName
};
if (FullName) file.FullName = newName;
else file.Name = newName;
index += Increment;
}
}
private static string PadInteger(int value, int padding)
{
var valueString = value.ToString();
while (valueString.Length < padding)
valueString = $"0{valueString}";
return valueString;
}
}
}

View File

@ -0,0 +1,40 @@
using System;
using System.Linq;
using System.Collections.Generic;
using BFR.Helpers;
namespace BFR.Operations
{
public class NumberOperationMode : OperationMode<NumberMode>
{
public bool IsPrefix => Index == NumberMode.Prefix;
public bool IsSuffix => Index == NumberMode.Suffix;
public bool IsInsert => Index == NumberMode.Insert;
public bool IsReplace => Index == NumberMode.Replace;
public readonly static NumberOperationMode[] Modes = All();
public NumberOperationMode(NumberMode index, string name, string description) :
base(index, name, description) { }
protected static NumberOperationMode[] All() => ((IEnumerable<NumberMode>)Enum.GetValues(typeof(NumberMode))).Select(x =>
new NumberOperationMode(
x,
x.GetAttribute<OperationModeAttribute>().DisplayName,
x.GetAttribute<OperationModeAttribute>().Description
)).ToArray();
}
public enum NumberMode
{
[OperationMode(nameof(Prefix), "Inserts the numbering before the file name.")]
Prefix,
[OperationMode(nameof(Suffix), "Inserts the numbering after the file name.")]
Suffix,
[OperationMode(nameof(Insert), "Inserts the numbering at the specified index.")]
Insert,
[OperationMode(nameof(Replace), "Replaces the given pattern with the numbering.")]
Replace,
}
}