using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; using JD.Collections.Extensions; using JD.Timeline.Model.Clips; using JetBrains.Annotations; using TeaBox; using JD.Serialization; using ComboBox = System.Windows.Controls.ComboBox; using PropertyChangedEventArgs = System.ComponentModel.PropertyChangedEventArgs; using TextBox = System.Windows.Controls.TextBox; namespace JD.Timeline.View.Helpers { /// /// Interaction logic for PictogramClipEditionHelper.xaml /// public partial class PictogramClipEditionHelper : INotifyPropertyChanged { #region Variables private PictogramModel _currentModel; private PictogramClip _clip; private Tape _tape; private bool _showWatermarkText; private bool _isCurrentNameValid; private ObservableCollection _modelList = new ObservableCollection(); private bool _showNameWarning; #endregion #region Constructor public PictogramClipEditionHelper() { Owner = System.Windows.Application.Current.MainWindow; } public PictogramClipEditionHelper(Tape tape, bool isModal = true) : this() { IsModal = isModal; Tape = tape; CurrentModel = null; Initialize(); } public PictogramClipEditionHelper(PictogramClip clip, bool isModal = false) : this() { IsModal = isModal; Tape = clip.Tape; Clip = clip; CurrentModel = clip.Model; Initialize(); } #endregion #region Properties public bool IsModal { get; set; } public Tape Tape { get { return _tape; } set { if (_tape != value) { _tape = value; OnPropertyChanged("Tape"); } } } public new PictogramClip Clip { get { return _clip; } set { if (_clip != value) { _clip = value; OnPropertyChanged("Clip"); } } } public PictogramModel CurrentModel { get { return _currentModel; } set { if (_currentModel != value) { if (_currentModel != null) { _currentModel.PropertyChanged -= ModelOnPropertyChanged; } _currentModel = value; if (_currentModel != null) { _currentModel.PropertyChanged += ModelOnPropertyChanged; } OnPropertyChanged("CurrentModel"); } } } public ObservableCollection ModelList { get { return _modelList; } set { _modelList = value; } } public List PictoInformations { get; set; } public bool ShowWatermarkText { get { return _showWatermarkText; } set { if (value != _showWatermarkText) { _showWatermarkText = value; OnPropertyChanged("ShowWatermarkText"); } } } public bool IsCurrentNameValid { get { return _isCurrentNameValid; } set { if (value != _isCurrentNameValid) { _isCurrentNameValid = value; OnPropertyChanged("IsCurrentNameValid"); } } } public bool ShowNameWarning { get { return _showNameWarning; } set { if (value != _showNameWarning) { _showNameWarning = value; OnPropertyChanged("ShowNameWarning"); } } } #endregion #region Methods private void Initialize() { DataContext = this; InitializeComponent(); RefreshModelList(); } public void RefreshModelList() { _modelList.Clear(); _modelList.Add(new AddNewPictogramModelAction()); _modelList.AddRange(Tape.PictogramModels().AsSorted((x, y) => String.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase))); _modelComboBox.SelectedItem = CurrentModel ?? _modelComboBox.Items[0]; } private void Window_Deactivated(object sender, EventArgs e) { if (!IsModal) { Close(); } } private void Window_Activated(object sender, EventArgs e) { SetCaretAtEndOfTextBox(); } private void OkClick(object sender, RoutedEventArgs e) { if (IsCurrentNameValid) { DialogResult = true; } } private void CancelClick(object sender, RoutedEventArgs e) { DialogResult = false; } private void Models_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (_modelComboBox.SelectedItem is PictogramModel) { if (Clip != null) { Clip.Model = (PictogramModel)_modelComboBox.SelectedItem; } CurrentModel = _modelComboBox.SelectedItem as PictogramModel; ShowWatermarkText = CurrentModel != null && String.IsNullOrEmpty(CurrentModel.Name); _modelComboBox.Text = CurrentModel.Name; } else if (_modelComboBox.SelectedItem is AddNewPictogramModelAction) { CurrentModel = null; ShowWatermarkText = true; IsCurrentNameValid = false; _modelComboBox.Text = ""; } SetCaretAtEndOfTextBox(); RefreshPictoInformations(); } private void ModelOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) { _modelComboBox.Text = CurrentModel.Name; } private void Models_TextChanged(object sender, TextChangedEventArgs e) { var comboBox = sender as ComboBox; if (comboBox == null) return; // Checking forbidden chars if(!string.IsNullOrEmpty(comboBox.Text)) foreach (char key in System.IO.Path.GetInvalidFileNameChars()) if (comboBox.Text.Contains(key)) comboBox.Text = comboBox.Text.Replace(key.ToString(), ""); IsCurrentNameValid = Tape.PictogramModels() .Where(x => x != CurrentModel) .All(x => x.Name != comboBox.Text) && !String.IsNullOrEmpty(comboBox.Text) && PictogramModel.IsNameValid(comboBox.Text); if (IsCurrentNameValid && CurrentModel == null) { CurrentModel = Tape.GetOrCreatePictogramModel(comboBox.Text); RefreshModelList(); } else if (IsCurrentNameValid && CurrentModel != null) { CurrentModel.Name = comboBox.Text; } ShowWatermarkText = String.IsNullOrEmpty(comboBox.Text); ShowNameWarning = (CurrentModel != null && !IsCurrentNameValid) || (CurrentModel == null && !String.IsNullOrEmpty(comboBox.Text) && !IsCurrentNameValid); RefreshPictoInformations(); } private void RefreshPictoInformations() { _pictoFlags.Items.Clear(); if (CurrentModel != null) { foreach (var info in Enum.GetValues(typeof(PictogramFlag)).Cast().Where(x => x != PictogramFlag.None)) { _pictoFlags.Items.Add( new PictogramFlagViewModel { Model = CurrentModel, Flag = info, IconUri = new Uri($"pack://application:,,,/JD.Timeline;component/Resources/Icon_64x64_picto_{info.ToString()}.png") } ); } } } private void SetCaretAtEndOfTextBox() { var textBox = (_modelComboBox.Template.FindName("PART_EditableTextBox", _modelComboBox) as TextBox); if (textBox != null) { textBox.Focus(); textBox.Select(textBox.Text.Length, 0); } } public bool ShowSnapshotButton { get { return !IsModal; } } public bool ShowEditButton { get { return !IsModal && CurrentModel != null; } } #endregion #region Events public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } private void SnapshotButton_Click(object sender, RoutedEventArgs e) { if (CurrentModel != null) { CurrentModel.BitmapHandler().TakeSnapshotFromVideo(); } } private void EditButton_Click(object sender, RoutedEventArgs e) { if (CurrentModel != null) { CurrentModel.BitmapHandler().EditPictogram(); } } #endregion } #region WPF classes /// /// Represents a pictogram information item /// public class PictogramFlagViewModel { public PictogramFlag Flag { get; set; } public Uri IconUri { get; set; } public PictogramModel Model { get; set; } public bool IsChecked { get { return Model != null && (Model.Flags & Flag) == Flag; } set { if (Model != null) { if (value) { Model.Flags |= Flag; } else { Model.Flags &= ~Flag; } } } } } /// /// Represents the entry in combobox to add a new model /// public class AddNewPictogramModelAction { public override string ToString() { return ""; } } /// /// Used to select the correct DataTemplate regarding the type of entry in combobox /// public class PictogramModelHelperTemplateSelector : DataTemplateSelector { public DataTemplate PictogramModelDataTemplate { get; set; } public DataTemplate CreatePictogramModelDataTemplate { get; set; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { if (item is AddNewPictogramModelAction) return CreatePictogramModelDataTemplate; return PictogramModelDataTemplate; } } #endregion }