Skip to content

Commit

Permalink
Add changing photo (ChildDetails)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwerty committed Nov 20, 2016
1 parent 2016c98 commit 81bb7c0
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 46 deletions.
7 changes: 6 additions & 1 deletion WpfApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Configuration;
using System.Windows;
using NLog;
using WpfApp.Framework;
Expand All @@ -10,7 +11,11 @@ namespace WpfApp
/// </summary>
public partial class App : Application
{
/// <summary>Path to Camera.exe</summary>
public static readonly string WebcamPath = ConfigurationManager.AppSettings["WebcamPath"];
public const string WebcamArguments = "photo 1";
public static readonly Logger Logger = LogManager.GetLogger(string.Empty);

public App()
{
Startup += App_Startup;
Expand All @@ -25,7 +30,7 @@ private void App_Startup(object sender, StartupEventArgs e)
Resources.MergedDictionaries.Add(res);
}

private void App_Exit(object sender, ExitEventArgs e)
private static void App_Exit(object sender, ExitEventArgs e)
{
WindowStateSaver.SaveAllSettings();
Logger.Trace("On Exit");
Expand Down
8 changes: 4 additions & 4 deletions WpfApp/Framework/WindowStateSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static InitialisationResult InitializeSettings()
return InitialisationResult.FileNotFound;
}

string json = File.ReadAllText(Path);
var json = File.ReadAllText(Path);
try
{
_settings = JsonConvert.DeserializeObject<IDictionary<string, Settings>>(json);
Expand All @@ -92,12 +92,12 @@ public static void SaveAllSettings()
{
try
{
string json = JsonConvert.SerializeObject(_settings);
var json = JsonConvert.SerializeObject(_settings);
File.WriteAllText(Path, json);
}
catch (Exception e)
catch (IOException e)
{
App.Logger.Fatal(e, "Cannot save settings");
App.Logger.Error(e, "Cannot save settings");
}
}

Expand Down
3 changes: 2 additions & 1 deletion WpfApp/Settings/AppFilePaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ namespace WpfApp.Settings
{
public static class AppFilePaths
{
private static string Resources { get; } = "resources";
private const string Resources = "resources";
private static string Images { get; } = Path.Combine(Resources, "images");
public static string PersonImages { get; } = Path.Combine(Images, "people");
public static string ChildImages { get; } = PersonImages;
public static string ParentImages { get; } = PersonImages;
public static readonly string NoImage = Resources + Path.DirectorySeparatorChar + "no_picture.jpg";

public static void CreateAllDirectories()
{
Expand Down
4 changes: 2 additions & 2 deletions WpfApp/Util/ImageUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace WpfApp.Util
{
public static class ImageUtil
{
private const int WidthRequired = 1280;
private const int HeightRequired = 720;
private const int WidthRequired = 600;
private const int HeightRequired = 800;

public static BitmapEncoder GetEncoderWithCompressedImage(Uri source)
{
Expand Down
32 changes: 24 additions & 8 deletions WpfApp/View/AddChildWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:pp="clr-namespace:System.Windows.Controls.Primitives;assembly=PresentationFramework"
xmlns:util="clr-namespace:WpfApp.Util"
xmlns:converter="clr-namespace:WpfApp.View.Converter"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="Добавление ребёнка" Height="551.333" Width="1317.417"
Name="ThisWindow">
Expand Down Expand Up @@ -349,24 +350,39 @@

<DockPanel Grid.Column="1">
<Grid DockPanel.Dock="Bottom" DataContext="{Binding ElementName=ThisWindow, Path=DataContext}">
<Grid.Resources>
<converter:ToNegateBoolConverter x:Key="NegateBoolConverter"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Command="{Binding OpenDialogLoadImageCommand}" Content="Выбрать изображение"/>
<Button Grid.Column="1" Content="С камеры" Command="{Binding CaptureFromCameraCommand}"/>
<Button Grid.Column="0" Command="{Binding LoadImageFromFileCommand}" Content="Выбрать изображение"/>
<Button Grid.Column="1" Content="С камеры" Command="{Binding CaptureImageFromCameraCommand}"/>
<Button Grid.Column="2" Content="Убрать" Command="{Binding RemoveImageCommand}" IsEnabled="{Binding ChildImageSource, Converter={StaticResource NegateBoolConverter}, ConverterParameter={x:Null}}"/>
</Grid>
<Button DockPanel.Dock="Top" Command="{Binding OpenImageChosenCommand}" Cursor="Hand" DataContext="{Binding ElementName=ThisWindow, Path=DataContext}">
<Button DockPanel.Dock="Top" Command="{Binding OpenImageChoosingCommand}" DataContext="{Binding ElementName=ThisWindow, Path=DataContext}">
<Button.Template>
<ControlTemplate>
<ControlTemplate.Resources>
<converter:NullConverter x:Key="NullConverter">
<converter:NullConverter.Null>
<ImageSource>../resources/no_picture.jpg</ImageSource>
</converter:NullConverter.Null>
</converter:NullConverter>
<ImageSource x:Key="NoPicture">../resources/no_picture.jpg</ImageSource>
<converter:NullConverter x:Key="NullConverter" Null="{StaticResource NoPicture}"/>
<converter:ToBoolConverter x:Key="BoolConverter"/>
</ControlTemplate.Resources>
<Image Source="{Binding ChildImageSource, Converter={StaticResource NullConverter}}">
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Source, Converter={StaticResource BoolConverter}, ConverterParameter={StaticResource NoPicture}}">
<DataTrigger.Value>
<system:Boolean>False</system:Boolean>
</DataTrigger.Value>
<Setter Property="Cursor" Value="Hand"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ControlTemplate>
</Button.Template>
Expand Down
36 changes: 32 additions & 4 deletions WpfApp/View/ChildDetailsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,35 @@
<TabItem Header="Осн. информация">
<DockPanel Margin="5">
<StackPanel Margin="5 0 0 0" Width="250" DockPanel.Dock="Right">
<Image MaxHeight="400" Source="{Binding CurrentChild.Person.PhotoPath, Converter={StaticResource ChildImageConverter}}"/>
<Button Command="{Binding OpenImageCommand}">
<Button.Template>
<ControlTemplate>
<ControlTemplate.Resources>
<ImageSource x:Key="NoPicture">../resources/no_picture.jpg</ImageSource>
<converter:NullConverter x:Key="NullConverter" Null="{StaticResource NoPicture}"/>
<converter:ToBoolConverter x:Key="BoolConverter"/>
</ControlTemplate.Resources>
<Image MaxHeight="400" Source="{Binding ChildImageSource, Converter={StaticResource NullConverter}}">
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Source, Converter={StaticResource BoolConverter}, ConverterParameter={StaticResource NoPicture}}">
<DataTrigger.Value>
<system:Boolean>False</system:Boolean>
</DataTrigger.Value>
<Setter Property="Cursor" Value="Hand"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ControlTemplate>
</Button.Template>
</Button>
<StackPanel>
<StackPanel.Resources>
<converter:FromBoolConverter x:Key="EnableConverter">
<converter:FromBoolConverter.True>sdf</converter:FromBoolConverter.True>
</converter:FromBoolConverter>
<converter:FromBoolConverter x:Key="EnableConverter"/>
<converter:ToNegateBoolConverter x:Key="NegateBoolConverter"/>
</StackPanel.Resources>
<StackPanel Orientation="Vertical" Visibility="{Binding Path=CurrentChildIsArchived, Converter={StaticResource VisibleCollapsedVisibilityConverter}, ConverterParameter={x:Static converter:FromBoolConverter.Reverce}}">
<TextBox IsEnabled="{Binding CurrentChildIsArchived, Converter={StaticResource EnableConverter}, ConverterParameter={x:Static converter:FromBoolConverter.Reverce}}"
Expand All @@ -65,6 +88,11 @@
Command="{Binding RemoveChildFromArchiveCommand}" Content="Убрать из архива"/>
</StackPanel>
<Button Command="{Binding ChangeGroupCommand}" Content="Перевести в другую группу..."/>
<Separator Margin="10"/>
<Label HorizontalAlignment="Center" Content="Изображение"/>
<Button Command="{Binding ReloadImageFromFileCommand}" Content="Выбрать изображение..."/>
<Button Command="{Binding RecaptureImageFromCameraCommand}" Content="С камеры..."/>
<Button Command="{Binding RemoveImageCommand}" Content="Удалить изображение" IsEnabled="{Binding ChildImageSource, Converter={StaticResource NegateBoolConverter}, ConverterParameter={x:Null}}"/>
</StackPanel>
</StackPanel>
<DockPanel DockPanel.Dock="Left">
Expand Down
2 changes: 1 addition & 1 deletion WpfApp/View/Converter/FromBoolConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace WpfApp.View.Converter
{
public class FromBoolConverter : IValueConverter
{
public static object Reverce = new object();
public static readonly object Reverce = new object();

public object True { get; set; } = true;
public object False { get; set; } = false;
Expand Down
17 changes: 14 additions & 3 deletions WpfApp/View/Converter/ImageConverter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WpfApp.Settings;
using WpfApp.Util;

namespace WpfApp.View.Converter
{
Expand Down Expand Up @@ -40,8 +40,19 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
if (value == null)
return DefaultImage;

var uri = new Uri(_path + (string)value, UriKind.Absolute);
return new BitmapImage(uri);
var path = _path + (string)value;
if (!File.Exists(path))
return DependencyProperty.UnsetValue;

// copy image (to delete in ChildDetails)
var uri = new Uri(path, UriKind.Absolute);
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = uri;
image.EndInit();
return image;
}

public ImageSource DefaultImage { get; set; }
Expand Down
35 changes: 21 additions & 14 deletions WpfApp/ViewModel/AddChildViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,31 @@ namespace WpfApp.ViewModel
{
internal class AddChildViewModel : ViewModelBase
{
private static readonly string WebcamPath = ConfigurationManager.AppSettings["WebcamPath"];
private const string WebcamArguments = "photo 1";

public IRelayCommand OpenDialogLoadImageCommand { get; }
public IRelayCommand LoadImageFromFileCommand { get; }
public IRelayCommand AddChildCommand { get; }
public IRelayCommand CaptureFromCameraCommand { get; }
public IRelayCommand OpenImageChosenCommand { get; }
public IRelayCommand CaptureImageFromCameraCommand { get; }
public IRelayCommand OpenImageChoosingCommand { get; }
public IRelayCommand RemoveImageCommand { get; }
public IRelayCommand ChooseParentCommand { get; }
public IRelayCommand DetachParentCommand { get; }

public AddChildViewModel()
{
AddChildCommand = new RelayCommand<Child>(AddChild);
OpenDialogLoadImageCommand = new RelayCommand(OpenDialogLoadImage);
CaptureFromCameraCommand = new RelayCommand(CaptureFromCamera);
OpenImageChosenCommand = new RelayCommand(OpenChosenImage);
LoadImageFromFileCommand = new RelayCommand(LoadImageFromFile);
CaptureImageFromCameraCommand = new RelayCommand(CaptureImageFromCamera);
OpenImageChoosingCommand = new RelayCommand(OpenImageChoosing);
RemoveImageCommand = new RelayCommand(RemoveImage);
ChooseParentCommand = new RelayCommand<Parents>(ChooseParent);
DetachParentCommand = new RelayCommand<Parents>(DetachParent);
}

private void RemoveImage()
{
_imageUri = null;
ChildImageSource = null;
}

private void DetachParent(Parents parents)
{
switch (parents)
Expand Down Expand Up @@ -93,19 +98,19 @@ private void ChooseParent(Parents parentType)
}
}

private void OpenChosenImage()
private void OpenImageChoosing()
{
if (_imageUri != null)
Process.Start(_imageUri.AbsolutePath);
Process.Start("explorer.exe", $"/select, \"{Path.GetFullPath(_imageUri.AbsolutePath)}\"");
}

private void CaptureFromCamera()
private void CaptureImageFromCamera()
{
var process = new Process
{
StartInfo =
{
FileName = WebcamPath, RedirectStandardOutput = true, UseShellExecute = false, Arguments = WebcamArguments,
FileName = App.WebcamPath, RedirectStandardOutput = true, UseShellExecute = false, Arguments = App.WebcamArguments,
},
};
process.Start();
Expand Down Expand Up @@ -320,7 +325,8 @@ public DateTime ChildAdditionDate
}


private void OpenDialogLoadImage()
// copypaste in ChildDetailsViewModel.cs
private void LoadImageFromFile()
{
if (_openFileDialog.ShowDialog() == false) return;

Expand All @@ -336,6 +342,7 @@ private void SetChildImage(string path)
var b = new BitmapImage();
b.BeginInit();
b.CacheOption = BitmapCacheOption.OnLoad;
b.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
b.UriSource = _imageUri;
b.EndInit();
ChildImageSource = b;
Expand Down
Loading

0 comments on commit 81bb7c0

Please sign in to comment.