// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// The Static Preview View Model
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.ViewModels
{
using System;
using System.Runtime.ExceptionServices;
using System.Windows;
using System.Windows.Media.Imaging;
using HandBrake.ApplicationServices.Model;
using HandBrake.ApplicationServices.Services.Encode.Model;
using HandBrake.ApplicationServices.Services.Interfaces;
using HandBrake.ApplicationServices.Services.Scan.Interfaces;
using HandBrake.Interop.Model.Encoding;
using HandBrakeWPF.ViewModels.Interfaces;
///
/// The Static Preview View Model
///
public class StaticPreviewViewModel : ViewModelBase, IStaticPreviewViewModel
{
/*
* TODO
* - Integrate Video Preview panel.
*/
#region Fields
///
/// The scan service.
///
private readonly IScan scanService;
///
/// The height.
///
private int height;
///
/// The preview image.
///
private BitmapImage previewImage;
///
/// The selected preview image.
///
private int selectedPreviewImage;
///
/// The width.
///
private int width;
///
/// The preview not available.
///
private bool previewNotAvailable;
#endregion
#region Constructors and Destructors
///
/// Initializes a new instance of the class.
///
///
/// The scan service.
///
public StaticPreviewViewModel(IScan scanService)
{
this.scanService = scanService;
this.selectedPreviewImage = 1;
this.Title = Properties.Resources.Preview;
this.PreviewNotAvailable = true;
}
#endregion
#region Public Properties
///
/// Gets or sets the height.
///
public int Height
{
get
{
return this.height;
}
set
{
if (value == this.height)
{
return;
}
this.height = value;
this.NotifyOfPropertyChange(() => this.Height);
}
}
///
/// Gets or sets the preview image.
///
public BitmapImage PreviewImage
{
get
{
return this.previewImage;
}
set
{
if (Equals(value, this.previewImage))
{
return;
}
this.previewImage = value;
this.NotifyOfPropertyChange(() => this.PreviewImage);
}
}
///
/// Gets or sets the selected preview image.
///
public int SelectedPreviewImage
{
get
{
return this.selectedPreviewImage;
}
set
{
if (value == this.selectedPreviewImage)
{
return;
}
this.selectedPreviewImage = value;
this.NotifyOfPropertyChange(() => this.SelectedPreviewImage);
this.UpdatePreviewFrame();
}
}
///
/// Gets or sets the task.
///
public EncodeTask Task { get; set; }
///
/// Gets the total previews.
///
public int TotalPreviews
{
get
{
return this.UserSettingService.GetUserSetting(UserSettingConstants.PreviewScanCount) - 1;
}
}
///
/// Gets or sets the width.
///
public int Width
{
get
{
return this.width;
}
set
{
if (value == this.width)
{
return;
}
this.width = value;
this.NotifyOfPropertyChange(() => this.Width);
}
}
///
/// Gets or sets a value indicating whether preview not available.
///
public bool PreviewNotAvailable
{
get
{
return this.previewNotAvailable;
}
set
{
if (value.Equals(this.previewNotAvailable))
{
return;
}
this.previewNotAvailable = value;
this.NotifyOfPropertyChange(() => this.PreviewNotAvailable);
}
}
#endregion
#region Public Methods and Operators
///
/// The update preview frame.
///
///
/// The task.
///
public void UpdatePreviewFrame(EncodeTask task)
{
this.Task = task;
this.UpdatePreviewFrame();
this.DisplayName = "Picture Preview";
this.Title = Properties.Resources.Preview;
}
///
/// Gets or sets a value indicating whether is open.
///
public bool IsOpen { get; set; }
///
/// The update preview frame.
///
[HandleProcessCorruptedStateExceptions]
public void UpdatePreviewFrame()
{
// Don't preview for small images.
if (this.Task.Anamorphic == Anamorphic.Loose && this.Task.Width < 32)
{
PreviewNotAvailable = true;
return;
}
if ((this.Task.Anamorphic == Anamorphic.None || this.Task.Anamorphic == Anamorphic.Custom) && (this.Task.Width < 32 || this.Task.Height < 32))
{
PreviewNotAvailable = true;
return;
}
BitmapImage image = null;
try
{
image = this.scanService.GetPreview(this.Task, this.SelectedPreviewImage);
}
catch (Exception exc)
{
PreviewNotAvailable = true;
}
if (image != null)
{
PreviewNotAvailable = false;
this.Width = (int)Math.Ceiling(image.Width);
this.Height = (int)Math.Ceiling(image.Height);
this.PreviewImage = image;
}
}
///
/// The preview size changed.
///
///
/// The ea.
///
public void PreviewSizeChanged(SizeChangedEventArgs ea)
{
Rect workArea = SystemParameters.WorkArea;
if (ea.NewSize.Width > workArea.Width)
{
this.Width = (int)Math.Round(workArea.Width, 0) - 20;
this.Title = Properties.Resources.Preview_Scaled;
}
if (ea.NewSize.Height > workArea.Height)
{
this.Height = (int)Math.Round(workArea.Height, 0) - 20;
this.Title = Properties.Resources.Preview_Scaled;
}
}
#endregion
}
}