Files
UVtools/UVtools.Core/Operations/OperationProgress.cs
T
Tiago Conceição 1e9bbf9562 v2.7.2
* **Core:**
   * Fix some improper locks for progress counter and change to Interlocked instead
   * Fix a bug when chaging layer count by remove or add layers it will malform the file after save and crash the program with some tools and/or clipboard
   * Fix when a operation fails by other reason different from cancelation it was not restoring the backup
   * When manually delete/fix issues it will also backup the layers
* **LayerManager:**
   * LayerManager is now readonly and no longer used to transpose layers, each FileFormat have now a unique `LayerManager` instance which is set on the generic constructor
   * Implemented `List<Layer>` methods to easy modify the layers array
   * Changing the `Layers` instance will now recompute some properties, call the properties rebuild and forced sanitize of the structure
   * Better reallocation methods
* **Clipboard Manager:**
   * Add the hability to do full backups, they will be marked with an asterisk (*) at clipboard items list
   * When a partial backup is made and it backups all the layers it will be converted to full backup
   * Clipboard can now restore a snapshot directly with `RestoreSnapshot`
   * Prevent restore the initial backup upon file load and when clearing the clipboard
   * Clip's that change the layer count will perform a full backup and also do a fail-safe backup behind if previous clip is not a full backup
* **Pixel dimming:**
   * Allow to load an image file as a pattern (Do not use very large files or it will take much time to dump the data into the textbox)
   * Empty lines on patterns will be discarded and not trigger validation error
2021-03-28 04:26:01 +01:00

203 lines
6.4 KiB
C#

/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.Diagnostics;
using System.Threading;
using UVtools.Core.Extensions;
using UVtools.Core.Objects;
namespace UVtools.Core.Operations
{
public sealed class OperationProgress : BindableBase
{
public const string StatusDecodeThumbnails = "Decoded Thumbnails";
public const string StatusGatherLayers = "Gathered Layers";
public const string StatusDecodeLayers = "Decoded Layers";
public const string StatusEncodeLayers = "Encoded Layers";
public const string StatusWritingFile = "Writing File";
public const string StatusEncodeGcode = "Encoding GCode";
public const string StatusOptimizingBounds = "Gathering Bounds";
public const string StatusCalculatingBounds = "Calculating Bounds";
public const string StatusExtracting = "Extracting";
public const string StatusIslands = "Layers processed (Islands/Overhangs)";
public const string StatusResinTrapsOptimized = "Layers optimized (Resin traps)";
public const string StatusResinTraps = "Layers processed (Resin traps)";
public const string StatusRepairLayers = "Repaired Layers";
public readonly object Mutex = new();
public CancellationTokenSource TokenSource { get; } = new();
public CancellationToken Token => TokenSource.Token;
private bool _canCancel = true;
private string _title = "Operation";
private string _itemName = "Initializing";
private uint _processedItems;
private uint _itemCount;
public
OperationProgress()
{
}
public OperationProgress(string name, uint value = 0)
{
Reset(name, value);
}
public OperationProgress(bool canCancel)
{
_canCancel = canCancel;
}
public Stopwatch StopWatch { get; } = new ();
/// <summary>
/// Gets or sets if operation can be cancelled
/// </summary>
public bool CanCancel
{
get
{
if (!_canCancel) return _canCancel;
return !Token.IsCancellationRequested && Token.CanBeCanceled && _canCancel;
}
set => RaiseAndSetIfChanged(ref _canCancel, value);
}
/// <summary>
/// Gets or sets the item name for the operation
/// </summary>
public string Title
{
get => _title;
set => RaiseAndSetIfChanged(ref _title, value);
}
public string ElapsedTimeStr => $"{StopWatch.Elapsed.Minutes}m {StopWatch.Elapsed.Seconds}s";
//{StopWatch.Elapsed.Milliseconds} ms
/// <summary>
/// Gets or sets the item name for the operation
/// </summary>
public string ItemName
{
get => _itemName;
set => RaiseAndSetIfChanged(ref _itemName, value);
}
/// <summary>
/// Gets or sets the number of processed items
/// </summary>
public uint ProcessedItems
{
get => _processedItems;
set
{
//_processedItems = value;
if(!RaiseAndSetIfChanged(ref _processedItems, value)) return;
RaisePropertyChanged(nameof(ProgressPercent));
RaisePropertyChanged(nameof(Description));
}
}
/// <summary>
/// Gets or sets the total of item count on this operation
/// </summary>
public uint ItemCount
{
get => _itemCount;
set
{
RaiseAndSetIfChanged(ref _itemCount, value);
RaisePropertyChanged(nameof(IsIndeterminate));
RaisePropertyChanged(nameof(ProgressPercent));
RaisePropertyChanged(nameof(Description));
}
}
/// <summary>
/// Gets or sets an tag
/// </summary>
public object Tag { get; set; }
/// <summary>
/// Gets the remaining items to be processed
/// </summary>
public uint RemainingItems => _itemCount - _processedItems;
public int ProgressStep => (int)ProgressPercent;
public string Description => ToString();
public bool IsIndeterminate => _itemCount == 0;
/// <summary>
/// Gets the progress from 0 to 100%
/// </summary>
public double ProgressPercent => _itemCount == 0 ? 0 : Math.Round(_processedItems * 100.0 / _itemCount, 2).Clamp(0, 100);
public static OperationProgress operator +(OperationProgress progress, uint value)
{
progress.ProcessedItems += value;
return progress;
}
public static OperationProgress operator ++(OperationProgress progress)
{
progress.ProcessedItems++;
return progress;
}
public static OperationProgress operator --(OperationProgress progress)
{
progress.ProcessedItems--;
return progress;
}
public void Reset(string name = "", uint itemCount = 0, uint items = 0)
{
ItemName = name;
ItemCount = itemCount;
ProcessedItems = items;
}
public override string ToString()
{
return _itemCount == 0 ?
$"{_processedItems}/? {_itemName}" :
$"{_processedItems.ToString().PadLeft(_itemCount.ToString().Length, '0')}/{_itemCount} {_itemName} | {ProgressPercent:F2}%";
}
public void TriggerRefresh()
{
RaisePropertyChanged(nameof(ElapsedTimeStr));
RaisePropertyChanged(nameof(CanCancel));
//OnPropertyChanged(nameof(ProgressPercent));
//OnPropertyChanged(nameof(Description));
}
public void LockAndIncrement()
{
/*lock (Mutex)
{
ProcessedItems++;
}*/
Interlocked.Increment(ref _processedItems);
RaisePropertyChanged(nameof(ProcessedItems));
RaisePropertyChanged(nameof(ProgressPercent));
RaisePropertyChanged(nameof(Description));
}
}
}