Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Added textbox validation.
Browse files Browse the repository at this point in the history
Improved bulk convert. Fixes #22
  • Loading branch information
KFreon committed Jun 16, 2017
1 parent 77e3235 commit ae195d5
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 44 deletions.
3 changes: 2 additions & 1 deletion CSharpImageLibrary/CSharpImageLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="UsefulThings, Version=1.7.2.1, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\UsefulCSharpThings.1.7.2.1\lib\net462\UsefulThings.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\UsefulThings\UsefulThings\bin\Debug\UsefulThings.dll</HintPath>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
Expand Down
14 changes: 9 additions & 5 deletions CSharpImageLibrary/ImageEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,12 @@ public static async Task<ConcurrentBag<string>> BulkConvert(IEnumerable<string>

// Test if can parallelise uncompressed saving
// Below says: Only formats that don't support mips or do but aren't block compressed - can be parallised. Also don't parallelise if using source formats.
bool supportsParallel = !useSourceFormat && !destFormatDetails.IsMippable;
supportsParallel |= !supportsParallel && !destFormatDetails.IsBlockCompressed;
bool supportsParallel = false;
if (!useSourceFormat)
{
supportsParallel = !useSourceFormat && !destFormatDetails.IsMippable;
supportsParallel |= !supportsParallel && !destFormatDetails.IsBlockCompressed;
}


if (EnableThreading && supportsParallel)
Expand All @@ -558,16 +562,16 @@ public static async Task<ConcurrentBag<string>> BulkConvert(IEnumerable<string>
using (ImageEngineImage img = new ImageEngineImage(file))
{
// Using source format can only come into this leg of the operation.
var saveFormatDetails = useSourceFormat ? img.FormatDetails : destFormatDetails;


string filename = useSourceFormat ? file : Path.GetFileNameWithoutExtension(file) + "." + destFormatDetails.Extension;
string filename = useSourceFormat ? Path.GetFileName(file) : Path.GetFileNameWithoutExtension(file) + "." + destFormatDetails.Extension; // This can stay destFormatDetails instead of saveFormatDetails as it shouldn't be able to get here if destFormatDetails not set.
string path = Path.Combine(useSourceAsDestination ? Path.GetDirectoryName(file) : saveFolder, filename);

path = UsefulThings.General.FindValidNewFileName(path);

try
{
await img.Save(path, useSourceFormat ? img.FormatDetails : destFormatDetails, saveMipType, removeAlpha: removeAlpha);
await img.Save(path, saveFormatDetails, saveMipType, removeAlpha: removeAlpha);
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion CSharpImageLibrary/ImageEngineImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ byte[] AttemptSaveUsingOriginalData(ImageFormats.ImageEngineFormatDetails destFo
return null;

// Get top mip size and create destination array
length = ImageFormats.GetCompressedSize(1, destFormatDetails, newWidth, newHeight);
length = ImageFormats.GetCompressedSize(0, destFormatDetails, newWidth, newHeight); // Should be the length of the top mipmap.
data = new byte[formattedMips.Length + length];

// Copy smaller mips to destination
Expand Down
112 changes: 76 additions & 36 deletions UI_Project/NewMainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
xmlns:UsefulThingsConverters="clr-namespace:UsefulThings.WPF.TypeConverters;assembly=UsefulThings"
xmlns:Library="clr-namespace:CSharpImageLibrary;assembly=CSharpImageLibrary"
xmlns:UsefulWPF="clr-namespace:UsefulThings.WPF;assembly=UsefulThings"
xmlns:UsefulValidation="clr-namespace:UsefulThings.WPF.ValidationRules;assembly=UsefulThings"
mc:Ignorable="d"
Title="NewMainWindow" Height="900" MinHeight="400" Width="1100" MinWidth="1010" AllowsTransparency="True" Background="{Binding WindowBackgroundColour}" WindowStyle="None" ResizeMode="CanResizeWithGrip"
Loaded="Window_Loaded" d:DataContext="{d:DesignInstance Type=local:Design_ViewModel, IsDesignTimeCreatable=True}"
Expand Down Expand Up @@ -134,12 +135,30 @@
</Setter>
<Setter Property="Background" Value="#B2008000"/>
</Trigger>

<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#B2808080"/>
<Setter Property="BorderBrush" Value="#B2808080"/>
</Trigger>
</Style.Triggers>
</Style>

<Style TargetType="TextBox">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="SelectionBrush" Value="LightGreen"/>

<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate x:Name="TextBoxInvalidPathTemplate">
<DockPanel LastChildFill="False">
<TextBlock Text="*" Foreground="Red" FontSize="20"/>
<AdornedElementPlaceholder>
<Border BorderBrush="Red" BorderThickness="2"/>
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style x:Key="ReadOnlyStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
Expand Down Expand Up @@ -1113,23 +1132,40 @@
</Button>

<Border x:Name="BulkConvertBox" Style="{StaticResource FadedBackgroundBorder}" Tag="{Binding BulkConvertOpen}">
<Border Style="{StaticResource FadedInnerBorder}">
<DockPanel LastChildFill="True">
<Label Content="Bulk Conversion" Style="{StaticResource TitleStyle}" FontSize="30" DockPanel.Dock="Top"/>
<Label Content="Enables many images to be converted at once. Use the settings below to set the target format and alpha settings for each format." DockPanel.Dock="Top"/>
<Border x:Name="BulkConvertInnerBorder" Style="{StaticResource FadedInnerBorder}">
<DockPanel x:Name="BulkConvertMainPanel" LastChildFill="True">
<DockPanel.Resources>
<Style TargetType="FrameworkElement" x:Key="InitialStyle">
<Setter Property="Visibility" Value="Collapsed"/>

<Style.Triggers>
<DataTrigger Binding="{Binding BulkConvertRunning}" Value="true">
<Setter Property="Opacity" Value="0.5"/>
<Setter Property="IsHitTestVisible" Value="False"/>
</DataTrigger>

<DataTrigger Binding="{Binding BulkConvertFailed.Count}" Value="0">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DockPanel.Resources>
<Label x:Name="BulkConvertBoxTitle" Content="Bulk Conversion" Style="{StaticResource TitleStyle}" FontSize="30" DockPanel.Dock="Top"/>
<Label x:Name="BulkConvertBoxDescription" Content="Enables many images to be converted at once. Use the settings below to set the target format and alpha settings for each format." DockPanel.Dock="Top"/>
<DockPanel x:Name="BottomStatus" DockPanel.Dock="Bottom" Margin="20,5">
<TextBlock Text="{Binding BulkStatus}" DockPanel.Dock="Right" HorizontalAlignment="Right" VerticalAlignment="Center" Foreground="White"/>
<ProgressBar DockPanel.Dock="Left" Width="300" HorizontalAlignment="Left" Height="30" Maximum="{Binding BulkProgressMax}" Value="{Binding BulkProgressValue}"/>
<TextBlock Text="{Binding BulkConvertElapsed}" DockPanel.Dock="Left" VerticalAlignment="Center" Foreground="White"/>
<ProgressBar MinWidth="300" Height="30" Maximum="{Binding BulkProgressMax}" Value="{Binding BulkProgressValue}" Margin="20,5"/>
</DockPanel>

<DockPanel x:Name="BottomButtons" DockPanel.Dock="Bottom" MinHeight="35" IsEnabled="{Binding BulkConvertRunning, Converter={StaticResource InverseBoolConverter}}">
<DockPanel x:Name="BottomButtons" DockPanel.Dock="Bottom" MinHeight="35" Style="{StaticResource InitialStyle}">
<Button x:Name="BulkBrowseButton" Content="Browse" DockPanel.Dock="Left" HorizontalAlignment="Left" Margin="30,0,0,0" Click="BulkBrowseButton_Click"/>
<Button x:Name="BulkFolderBrowseButton" Content="Add Folder" DockPanel.Dock="Left" HorizontalAlignment="Left" Margin="10,0,0,0" Click="BulkFolderBrowseButton_Click"/>
<CheckBox x:Name="BulkFolderBrowseRecurseChecker" Content="Recursive?" IsChecked="{Binding BulkFolderBrowseRecurse}" VerticalAlignment="Center" Foreground="White" Margin="5,0,0,0"/>
<Button x:Name="BulkCloseButton" Content="Close" DockPanel.Dock="Right" HorizontalAlignment="Left" Click="BulkCloseButton_Click" Margin="0,0,10,0"/>
<Button x:Name="BulkConvertButton" Content="Convert!" DockPanel.Dock="Bottom" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold" Click="BulkConvertButton_Click">
<Button x:Name="BulkConvertButton" Content="Convert!" DockPanel.Dock="Bottom" HorizontalAlignment="Center" Background="Transparent" FontSize="20" Padding="15,5" FontWeight="Bold" Click="BulkConvertButton_Click">
<Button.Style>
<Style TargetType="Button">
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<MultiDataTrigger>
Expand All @@ -1152,7 +1188,7 @@
</DockPanel>


<Grid DockPanel.Dock="Top" Margin="5,10" IsEnabled="{Binding BulkConvertRunning, Converter={StaticResource InverseBoolConverter}}">
<Grid x:Name="BulkConvertInputsBox" DockPanel.Dock="Top" Margin="5,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="600"/>
Expand All @@ -1168,31 +1204,11 @@
</DataTrigger>
</Style.Triggers>
</Style>

<Style TargetType="FrameworkElement" x:Key="InitialStyle">
<Setter Property="Visibility" Value="Visible"/>

<Style.Triggers>
<DataTrigger Binding="{Binding BulkConvertFinished}" Value="true">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>

<Style x:Key="ResultsStyle_Label" TargetType="Label" BasedOn="{StaticResource TitleStyle}">
<Setter Property="Visibility" Value="Collapsed"/>

<Style.Triggers>
<DataTrigger Binding="{Binding BulkConvertFinished}" Value="true">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Resources>

<ListBox x:Name="BulkConvertListBox" Background="Transparent" Foreground="White" ItemsSource="{Binding BulkConvertFiles}" AllowDrop="True" DragOver="BulkConvertListBox_DragOver" Drop="BulkConvertListBox_Drop"
Style="{StaticResource InitialStyle}" KeyDown="BulkConvertListBox_KeyDown" SelectionMode="Extended"/>
<DockPanel Grid.Column="1" Style="{StaticResource InitialStyle}" LastChildFill="False">
Style="{StaticResource InitialStyle}" KeyDown="BulkConvertListBox_KeyDown" SelectionMode="Extended"/>
<DockPanel x:Name="BulkConvertSettingsBox" Grid.Column="1" LastChildFill="False" Style="{StaticResource InitialStyle}">
<DockPanel.Resources>
<Style TargetType="ContentControl">
<Setter Property="DockPanel.Dock" Value="Top"/>
Expand Down Expand Up @@ -1225,16 +1241,40 @@

<Label Content="Destination Folder" Style="{StaticResource TitleStyle}" FontSize="20" DockPanel.Dock="Top"/>
<CheckBox DockPanel.Dock="Top" Content="Same as Original (no overwriting)" Foreground="White" IsChecked="{Binding BulkUseSourceDestination}" Margin="5,0,0,15"/>
<DockPanel DockPanel.Dock="Top" IsEnabled="{Binding BulkUseSourceDestination, Converter={StaticResource InverseBoolConverter}}">
<DockPanel x:Name="BulkConvertSaveBox" DockPanel.Dock="Top" IsEnabled="{Binding BulkUseSourceDestination, Converter={StaticResource InverseBoolConverter}}">
<Button x:Name="BulkSaveBrowse" Content="Browse" DockPanel.Dock="Right" Click="BulkSaveBrowse_Click"/>
<TextBox Margin="5,0" Text="{Binding BulkSaveFolder}"/>
<TextBox Margin="5,0">
<TextBox.Text>
<Binding Path="BulkSaveFolder"
UpdateSourceTrigger="PropertyChanged"
ValidatesOnDataErrors="True"
ValidatesOnNotifyDataErrors="True"
NotifyOnValidationError="True">
<Binding.ValidationRules>
<UsefulValidation:TextBoxValidation RequireExistence="True" ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DockPanel>
</DockPanel>

<Label Content="Failed" Style="{StaticResource ResultsStyle_Label}" Grid.ColumnSpan="2"/>
<ListBox x:Name="BulkFailedListBox" ItemsSource="{Binding BulkConvertFailed}" Margin="0,30,0,0" Background="Transparent" Foreground="White" Grid.ColumnSpan="2" Style="{StaticResource ResultsStyle}"/>
</Grid>
<DockPanel x:Name="BulkConvertResultsBox" Grid.ColumnSpan="2" Style="{StaticResource ResultsStyle}">
<Label x:Name="BulkConvertFailureLabel" Content="{Binding ElementName=BulkFailedListBox, Path=Items.Count, StringFormat=Finished with {0} failures.}" DockPanel.Dock="Top">
<Label.Style>
<Style TargetType="Label" BasedOn="{StaticResource TitleStyle}">
<Style.Triggers>
<DataTrigger Binding="{Binding BulkConvertFailed.Count}" Value="0">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>

<ListBox x:Name="BulkFailedListBox" ItemsSource="{Binding BulkConvertFailed}" Margin="0,30,0,0" Background="Transparent" Foreground="White" DockPanel.Dock="Top"/>
</DockPanel>
</Grid>
</DockPanel>
</Border>

Expand Down
25 changes: 25 additions & 0 deletions UI_Project/NewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,21 @@ public uint BMask
#region Bulk Convert Properties
public MTRangedObservableCollection<string> BulkConvertFiles { get; set; } = new MTRangedObservableCollection<string>();
public MTRangedObservableCollection<string> BulkConvertFailed { get; set; } = new MTRangedObservableCollection<string>();
Stopwatch bulkConvertTimer = new Stopwatch();


string bulkConvertElaspsed = null;
public string BulkConvertElapsed
{
get
{
return bulkConvertElaspsed;
}
set
{
SetProperty(ref bulkConvertElaspsed, value);
}
}

bool bulkConvertOpen = false;
public bool BulkConvertOpen
Expand Down Expand Up @@ -1388,6 +1403,7 @@ void CloseImage(bool updateUI)
BulkConvertRunning = false;
BulkConvertFiles.Clear();
BulkConvertFailed.Clear();
bulkConvertTimer = new Stopwatch();
MergeChannelsImages.Clear();
LoadFailed = false;
LoadFailError = null;
Expand Down Expand Up @@ -1500,22 +1516,31 @@ internal async Task DoBulkConvert()
BulkStatus = $"Converting {BulkProgressValue}/{BulkProgressMax} images.";
BulkConvertFinished = false;
BulkConvertRunning = true;
bulkConvertTimer.Start();

Progress<int> progressReporter = new Progress<int>(index =>
{
BulkProgressValue++;
BulkStatus = $"Converting {BulkProgressValue}/{BulkProgressMax} images.";
});

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(0.5);
timer.Tick += (soruce, args) => BulkConvertElapsed = bulkConvertTimer.Elapsed.ToString(@"hh\.mm\.ss\.f");
timer.Start();

BulkConvertFailed.AddRange(await Task.Run(() => ImageEngine.BulkConvert(BulkConvertFiles, SaveFormatDetails, UseSourceFormatForSaving, BulkSaveFolder, SaveMipType, BulkUseSourceDestination, GeneralRemovingAlpha, progressReporter)));

timer.Stop();
BulkStatus = "Conversion complete! ";
if (BulkConvertFailed.Count > 0)
BulkStatus += $"{BulkConvertFailed.Count} failed to convert.";

BulkProgressValue = BulkProgressMax;
BulkConvertFinished = true;
BulkConvertRunning = false;
bulkConvertTimer.Stop();
BulkConvertElapsed = bulkConvertTimer.Elapsed.ToString(@"hh\.mm\.ss\.f");
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion UI_Project/UI_Project.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="UsefulThings, Version=1.7.2.1, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\UsefulCSharpThings.1.7.2.1\lib\net462\UsefulThings.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\UsefulThings\UsefulThings\bin\Debug\UsefulThings.dll</HintPath>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
Expand Down

0 comments on commit ae195d5

Please sign in to comment.