/* * GNU AFFERO GENERAL PUBLIC LICENSE * Version 3, 19 November 2007 * Copyright (C) 2007 Free Software Foundation, Inc. * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. */ using System.ComponentModel; using System.Drawing.Design; using System.Runtime.CompilerServices; using System.Windows.Forms; using UVtools.Core.Extensions; using UVtools.Core.Objects; using UVtools.Core.Operations; using UVtools.GUI.Annotations; using UVtools.GUI.Forms; namespace UVtools.GUI.Controls { public partial class CtrlToolWindowContent : UserControl, INotifyPropertyChanged { #region BindableBase /// /// Multicast event for property change notifications. /// public event PropertyChangedEventHandler PropertyChanged; /// /// Checks if a property already matches a desired value. Sets the property and /// notifies listeners only when necessary. /// /// Type of the property. /// Reference to a property with both getter and setter. /// Desired value for the property. /// /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers that /// support CallerMemberName. /// /// /// True if the value was changed, false if the existing value matched the /// desired value. /// [NotifyPropertyChangedInvocator] protected bool SetProperty(ref T storage, T value, [CallerMemberName] string propertyName = null) { if (Equals(storage, value)) { return false; } storage = value; OnPropertyChanged(propertyName); return true; } /// /// Notifies listeners that a property value has changed. /// /// /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers /// that support . /// [NotifyPropertyChangedInvocator] protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { var eventHandler = PropertyChanged; if (!ReferenceEquals(eventHandler, null)) { eventHandler(this, new PropertyChangedEventArgs(propertyName)); } } #endregion #region Properties [ReadOnly(true)] [Browsable(false)] public Operation BaseOperation { get; private set; } [ReadOnly(true)] [Browsable(false)] public FrmToolWindow ParentToolWindow => ParentForm as FrmToolWindow; [SettingsBindable(true)] public bool CanRun { get; set; } = true; /// /// Gets or sets if this control can make use of ROI /// [SettingsBindable(true)] public bool CanROI { get; set; } = false; [Editor("System.ComponentModel.Design.MultilineStringEditor", typeof(UITypeEditor))] [SettingsBindable(true)] public string Description { get; set; } [SettingsBindable(true)] public bool ExtraButtonVisible { get; set; } = false; [Editor("System.ComponentModel.Design.MultilineStringEditor", typeof(UITypeEditor))] [SettingsBindable(true)] public string ExtraButtonText { get; set; } = "&Reset to defaults"; [SettingsBindable(true)] public bool ExtraCheckboxVisible { get; set; } = false; [Editor("System.ComponentModel.Design.MultilineStringEditor", typeof(UITypeEditor))] [SettingsBindable(true)] public string ExtraCheckboxText { get; set; } = "Show advanced options"; private bool _buttonOkEnabled = true; private bool _layerRangeVisible = true; [SettingsBindable(true)] public bool ButtonOkEnabled { get => _buttonOkEnabled; set => SetProperty(ref _buttonOkEnabled, value); } [Editor("System.ComponentModel.Design.MultilineStringEditor", typeof(UITypeEditor))] [SettingsBindable(true)] public string ButtonOkText { get; set; } = "Ok"; [Editor("System.ComponentModel.Design.MultilineStringEditor", typeof(UITypeEditor))] [SettingsBindable(true)] public string ButtonCancelText { get; set; } = "Cancel"; [SettingsBindable(true)] public bool LayerRangeVisible { get => _layerRangeVisible; set => SetProperty(ref _layerRangeVisible, value); } [SettingsBindable(true)] public bool LayerRangeEndVisible { get; set; } = true; /*[SettingsBindable(true)] public uint LayerRangeStart { get; set; } [SettingsBindable(true)] public uint LayerRangeEnd { get; set; }*/ [ReadOnly(true)] [Browsable(false)] public virtual string ConfirmationText => BaseOperation is null ? $"{Text}?" : BaseOperation.ConfirmationText; #endregion #region Constructor public CtrlToolWindowContent() { InitializeComponent(); } #endregion #region Methods public void SetOperation(Operation operation) { BaseOperation = operation; if(!string.IsNullOrEmpty(operation.Title)) Text = operation.Title; if (!string.IsNullOrEmpty(operation.Description)) Description = operation.Description; if (!string.IsNullOrEmpty(operation.ButtonOkText)) ButtonOkText = operation.ButtonOkText; } /// /// Updates operation object with items retrieved from form fields /// public virtual bool UpdateOperation() { if (ParentToolWindow is null) return true; BaseOperation.LayerIndexStart = ParentToolWindow.LayerRangeStart; BaseOperation.LayerIndexEnd = ParentToolWindow.LayerRangeEnd; if (CanROI && BaseOperation.ROI.IsEmpty) { BaseOperation.ROI = Program.FrmMain.ROI; } return true; } /// /// Validates if is safe to continue with operation /// /// public virtual bool ValidateForm() { if (BaseOperation is null) return true; if(!UpdateOperation()) return false; return ValidateFormFromString(BaseOperation.Validate()); } /// /// Validates if is safe to continue with operation, if not shows a message box with the error /// /// /// public bool ValidateFormFromString(string text) { if (string.IsNullOrEmpty(text)) return true; MessageBoxError(text); return false; } /// /// Validates if is safe to continue with operation, if not shows a message box with the error /// /// /// public bool ValidateFormFromString(StringTag text) { if (text is null) return true; if (string.IsNullOrEmpty(text.ToString())) return true; MessageBoxError(text.ToString()); return false; } public DialogResult MessageBoxError(string message, MessageBoxButtons buttons = MessageBoxButtons.OK) => GUIExtensions.MessageBoxError($"{Text} Error", message, buttons); public DialogResult MessageQuestionBox(string message, string title = null, MessageBoxButtons buttons = MessageBoxButtons.YesNo) => GUIExtensions.MessageBoxQuestion($"{title ?? Text}", message, buttons); #endregion #region Events public virtual void ExtraActionCall(object sender) { } #endregion } }