Files
UVtools/UVtools.Core/Operations/OperationProgress.cs
T
Tiago Conceição 6d661821b5 v1.3.0
* (Add) Changelog description to the new version update dialog
* (Add) Tool - Infill: Proper configurable infills
* (Add) Pixel area as "px²" to the layer bounds and ROI at layer bottom information bar
* (Add) Pixel dimming: Alternate pattern every x layers
* (Add) Pixel dimming: Lattice infill
* (Add) Solidify: Required minimum/maximum area to solidify found areas (Default values will produce the old behaviour)
* (Add) Issues: Allow to hide and ignore selected issues
* (Add) Issue - Touch boundary: Allow to configure Left, Top, Right, Bottom margins in pixels, defaults to 5px (#94)
* (Add) UVJ: Allow convert to another formats (#96)
* (Add) Setters to some internal Core properties for more abstraction
* (Improvement) Issue - Touch boundary: Only check boundary pixels if layer bounds overlap the set margins, otherwise, it will not waste cycles on check individual rows of pixels when not need to
* (Change) Place .ctb extension show first than .cbddlp due more popular this days
* (Change) Pixel dimming: Text "Borders" to "Walls"
* (Change) Issues: Remove "Remove" text from button, keep only the icon to free up space
* (Change) Ungroup extensions on "covert to" menu (#97)
* (Fix) Issues: Detect button has a incorrect "save" icon
* (Fix) SL1: Increase NumSlow property limit
* (Fix) UVJ: not decoding nor showing preview images
* (Fix) "Convert to" menu shows same options than previous loaded file when current file dont support convertions (#96)
* (Fix) Hides "Convert to" menu when unable to convert to another format (#96)
* (Fix) Program crash when demo file is disabled and tries to load a file in
* (Fix) Rare crash on startup when mouse dont move in startup period and user types a key in meanwhile
* (Fix) On a slow startup on progress window it will show "Decoded layers" as default text, changed to "Initializing"
2020-11-14 05:26:49 +00:00

184 lines
5.8 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 StatusResinTraps = "Layers processed (Resin traps)";
public const string StatusRepairLayers = "Repaired Layers";
public object Mutex = new object();
public CancellationTokenSource TokenSource { get; } = new CancellationTokenSource();
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 Stopwatch();
/// <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;
RaiseAndSetIfChanged(ref _processedItems, value);
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 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}/{ItemCount} {ItemName} | {ProgressPercent:0.00}%";
}
public void TriggerRefresh()
{
RaisePropertyChanged(nameof(ElapsedTimeStr));
RaisePropertyChanged(nameof(CanCancel));
//OnPropertyChanged(nameof(ProgressPercent));
//OnPropertyChanged(nameof(Description));
}
}
}