mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-19 17:12:22 +01:00
Fixe add/remove preset button.
This commit is contained in:
1 parent
9400f742d8
commit
fbc080f935
4 files changed
+40
-17
No files matched your search
@@ -4,6 +4,7 @@ namespace FileConverter
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
@@ -20,7 +21,7 @@ namespace FileConverter
|
||||
{
|
||||
private const char PresetSeparator = ';';
|
||||
|
||||
private List<ConversionPreset> conversionPresets = new List<ConversionPreset>();
|
||||
private ObservableCollection<ConversionPreset> conversionPresets = new ObservableCollection<ConversionPreset>();
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
@@ -32,7 +33,7 @@ namespace FileConverter
|
||||
}
|
||||
}
|
||||
|
||||
public List<ConversionPreset> ConversionPresets
|
||||
public ObservableCollection<ConversionPreset> ConversionPresets
|
||||
{
|
||||
get { return this.conversionPresets; }
|
||||
set { this.conversionPresets = value; }
|
||||
@@ -47,13 +48,13 @@ namespace FileConverter
|
||||
|
||||
public void Load()
|
||||
{
|
||||
List<ConversionPreset> conversionPresets = this.ConversionPresets;
|
||||
conversionPresets.Clear();
|
||||
ICollection<ConversionPreset> presets = this.ConversionPresets;
|
||||
presets.Clear();
|
||||
|
||||
string userFilePath = Settings.GetUserSettingsFilePath();
|
||||
if (File.Exists(userFilePath))
|
||||
{
|
||||
XmlHelpers.LoadFromFile<ConversionPreset>("Settings", userFilePath, ref conversionPresets);
|
||||
XmlHelpers.LoadFromFile<ConversionPreset>("Settings", userFilePath, ref presets);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -61,7 +62,7 @@ namespace FileConverter
|
||||
string defaultFilePath = Settings.GetDefaultSettingsFilePath();
|
||||
if (File.Exists(defaultFilePath))
|
||||
{
|
||||
XmlHelpers.LoadFromFile<ConversionPreset>("Settings", defaultFilePath, ref conversionPresets);
|
||||
XmlHelpers.LoadFromFile<ConversionPreset>("Settings", defaultFilePath, ref presets);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -75,7 +76,7 @@ namespace FileConverter
|
||||
// Save the settings in a temporary files (we'll write the settings file when we'll succeed to write the registry keys).
|
||||
string temporaryFilePath = Settings.GetUserSettingsTemporaryFilePath();
|
||||
XmlHelpers.SaveToFile("Settings", temporaryFilePath, this.ConversionPresets);
|
||||
|
||||
|
||||
// Compute the registry entries data from settings.
|
||||
Dictionary<string, List<string>> registryEntries = Settings.ComputeRegistryEntriesFromConvertionPresets(this.ConversionPresets);
|
||||
|
||||
@@ -121,7 +122,7 @@ namespace FileConverter
|
||||
return;
|
||||
}
|
||||
|
||||
List<ConversionPreset> conversionPresets = new List<ConversionPreset>();
|
||||
ICollection<ConversionPreset> conversionPresets = new ObservableCollection<ConversionPreset>();
|
||||
XmlHelpers.LoadFromFile("Settings", temporaryFilePath, ref conversionPresets);
|
||||
|
||||
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter");
|
||||
@@ -211,12 +212,12 @@ namespace FileConverter
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Dictionary<string, List<string>> ComputeRegistryEntriesFromConvertionPresets(List<ConversionPreset> conversionPresets)
|
||||
private static Dictionary<string, List<string>> ComputeRegistryEntriesFromConvertionPresets(ICollection<ConversionPreset> conversionPresets)
|
||||
{
|
||||
Dictionary<string, List<string>> registryEntries = new Dictionary<string, List<string>>();
|
||||
for (int index = 0; index < conversionPresets.Count; index++)
|
||||
|
||||
foreach (ConversionPreset conversionPreset in conversionPresets)
|
||||
{
|
||||
ConversionPreset conversionPreset = conversionPresets[index];
|
||||
List<string> inputTypes = conversionPreset.InputTypes;
|
||||
for (int inputIndex = 0; inputIndex < inputTypes.Count; inputIndex++)
|
||||
{
|
||||
|
||||
@@ -64,7 +64,7 @@
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Label Content="Preset name" Grid.Row="0" HorizontalContentAlignment="Right" Margin="0,0,5,0"/>
|
||||
<TextBox Grid.Column="1" Margin="5,2,5,2" Text="{Binding SelectedPreset.Name, Mode=TwoWay, ElementName=window}" TextWrapping="Wrap" />
|
||||
<TextBox Name="PresetNameTextBox" Grid.Row="0" Grid.Column="1" Margin="5,2,5,2" Text="{Binding SelectedPreset.Name, Mode=TwoWay, ElementName=window}" TextWrapping="Wrap" />
|
||||
|
||||
<GroupBox Grid.Row="1" Grid.Column="0" Header="Input formats" Margin="5">
|
||||
<StackPanel>
|
||||
|
||||
@@ -62,6 +62,11 @@ namespace FileConverter
|
||||
|
||||
private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (this.selectedPreset == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CheckBox checkBox = sender as System.Windows.Controls.CheckBox;
|
||||
string inputFormat = checkBox.Content as string;
|
||||
|
||||
@@ -73,6 +78,11 @@ namespace FileConverter
|
||||
|
||||
private void ToggleButton_OnUnchecked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (this.selectedPreset == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CheckBox checkBox = sender as System.Windows.Controls.CheckBox;
|
||||
string inputFormat = checkBox.Content as string;
|
||||
|
||||
@@ -100,7 +110,11 @@ namespace FileConverter
|
||||
private void AddPresetButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Application application = Application.Current as Application;
|
||||
application.Settings.ConversionPresets.Add(new ConversionPreset("New preset", OutputType.None, new string[0]));
|
||||
ConversionPreset newPreset = new ConversionPreset("New preset", OutputType.None, new string[0]);
|
||||
application.Settings.ConversionPresets.Add(newPreset);
|
||||
this.SelectedPreset = newPreset;
|
||||
this.PresetNameTextBox.Focus();
|
||||
this.PresetNameTextBox.SelectAll();
|
||||
}
|
||||
|
||||
private void RemovePresetButton_Click(object sender, RoutedEventArgs e)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Windows.Documents;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
@@ -9,7 +10,7 @@ using FileConverter;
|
||||
|
||||
public class XmlHelpers
|
||||
{
|
||||
public static void LoadFromFile<T>(string root, string path, ref List<T> list)
|
||||
public static void LoadFromFile<T>(string root, string path, ref ICollection<T> collection)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
@@ -28,7 +29,11 @@ public class XmlHelpers
|
||||
using (StreamReader reader = new StreamReader(path))
|
||||
using (XmlReader xmlReader = XmlReader.Create(reader, new XmlReaderSettings { IgnoreWhitespace = true, IgnoreComments = true }))
|
||||
{
|
||||
list.AddRange((List<T>)serializer.Deserialize(xmlReader));
|
||||
List<T> elements = (List<T>)serializer.Deserialize(xmlReader);
|
||||
foreach (T element in elements)
|
||||
{
|
||||
collection.Add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception exception)
|
||||
@@ -37,13 +42,16 @@ public class XmlHelpers
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveToFile<T>(string root, string path, List<T> objectsToSerialize)
|
||||
public static void SaveToFile<T>(string root, string path, ICollection<T> objectsToSerialize)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
List<T> list = new List<T>();
|
||||
list.AddRange(objectsToSerialize);
|
||||
|
||||
XmlRootAttribute xmlRoot = new XmlRootAttribute
|
||||
{
|
||||
ElementName = root
|
||||
@@ -56,7 +64,7 @@ public class XmlHelpers
|
||||
using (StreamWriter writer = new StreamWriter(path))
|
||||
using (XmlWriter xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = true, IndentChars = " " }))
|
||||
{
|
||||
serializer.Serialize(xmlWriter, objectsToSerialize);
|
||||
serializer.Serialize(xmlWriter, list);
|
||||
}
|
||||
}
|
||||
catch (System.Exception exception)
|
||||
|
||||
Reference in new issue
Block a user