Fixes and mapping

This commit is contained in:
MaddoScientisto 2026-02-04 23:16:06 +01:00
commit ba965e8266
10 changed files with 449 additions and 284 deletions

View file

@ -13,9 +13,11 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ImageCatalog_2;
using ImageCatalog_2.Commands;
using ImageCatalog_2.Services;
using MaddoShared;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
namespace ImageCatalog;
@ -25,8 +27,6 @@ public partial class MainForm
private readonly ILogger<MainForm> _logger;
private readonly ImageCreationStuff _imageCreationService;
private readonly ParametriSetup _parametriSetup;
private readonly PicSettings _picSettings;
@ -34,7 +34,6 @@ public partial class MainForm
ParametriSetup parametriSetup, ILogger<MainForm> logger)
{
Model = model;
_imageCreationService = imageCreationStuff;
_parametriSetup = parametriSetup;
_picSettings = picSettings;
_logger = logger;
@ -42,77 +41,37 @@ public partial class MainForm
_logger.LogDebug("Start");
InitializeComponent();
BindControls();
// Subscribe to DataModel events
// Set this form as the control for thread marshalling in the DataModel
Model.SetControl(this);
BindControls();
var version = Assembly.GetExecutingAssembly().GetName().Version;
_Label27.Text = $"Version: {version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
}
protected void BindControls()
{
// Bind buttons to ViewModel commands using command binding
_btnCreaCatalogoAsync.BindCommand(Model.ProcessImagesCommand);
button1.BindCommand(Model.ProcessImagesCommand);
_Button2.BindCommand(Model.SelectSourceFolderCommand);
_Button3.BindCommand(Model.SelectDestinationFolderCommand);
_Button4.BindCommand(Model.SelectLogoFileCommand);
_Button5.BindCommand(Model.SaveSettingsCommand);
_Button6.BindCommand(Model.LoadSettingsCommand);
_Button8.BindCommand(Model.SelectColorCommand);
// Subscribe to ViewModel events for UI dialogs (these need UI context)
Model.SelectSourceFolderRequested += OnSelectSourceFolderRequested;
Model.SelectDestinationFolderRequested += OnSelectDestinationFolderRequested;
Model.SelectLogoFileRequested += OnSelectLogoFileRequested;
Model.SaveSettingsRequested += OnSaveSettingsRequested;
Model.LoadSettingsRequested += OnLoadSettingsRequested;
Model.SelectColorRequested += OnSelectColorRequested;
var version = Assembly.GetExecutingAssembly().GetName().Version;
_Label27.Text = $"Version: {version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
_results = [];
UiUpdateEvent += OnUiUpdateEvent;
}
protected void BindControls()
{
// Wire up buttons to ViewModel commands
_Button2.Click += (s, e) => Model.SelectSourceFolderCommand.Execute(null);
_Button3.Click += (s, e) => Model.SelectDestinationFolderCommand.Execute(null);
_Button4.Click += (s, e) => Model.SelectLogoFileCommand.Execute(null);
_Button5.Click += (s, e) => Model.SaveSettingsCommand.Execute(null);
_Button6.Click += (s, e) => Model.LoadSettingsCommand.Execute(null);
_Button8.Click += (s, e) => Model.SelectColorCommand.Execute(null);
}
private event EventHandler<Tuple<string, int>> UiUpdateEvent;
delegate void SetTextCallback(Label target, string text);
private void SetText(Label target, string text)
{
if (InvokeRequired)
{
var d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { target, text });
}
else
{
target.Text = text;
}
}
private delegate void SetProgressCallback(ProgressBar target, int amount, int maximum);
private void SetProgress(ProgressBar target, int amount, int maximum)
{
if (InvokeRequired)
{
var d = new SetProgressCallback(SetProgress);
this.Invoke(d, new object[] { target, amount, maximum });
}
else
{
target.Maximum = maximum;
target.Value = amount;
}
}
private void OnUiUpdateEvent(object sender, Tuple<string, int> args)
{
SetProgress(ProgressBar1, _results.Count, args.Item2);
SetText(Label18, _results.Count.ToString());
SetText(Label10, args.Item1);
SetText(lblFotoTotaliNum, args.Item2.ToString());
}
private ConcurrentBag<string> _results;
private void SetDefaults()
{
// Bind ComboBoxes to Model using proper data binding
@ -135,6 +94,19 @@ public partial class MainForm
ComboBox5.DataSource = new List<string> { "Alto", "Centro", "Basso" };
ComboBox5.DataBindings.Add(new Binding("SelectedItem", bindingSource1, nameof(Model.LogoVerticalPosition),
false, DataSourceUpdateMode.OnPropertyChanged));
// Bind progress bar and status labels
ProgressBar1.DataBindings.Add(new Binding("Maximum", bindingSource1, nameof(Model.ProgressBarMaximum),
false, DataSourceUpdateMode.OnPropertyChanged));
ProgressBar1.DataBindings.Add(new Binding("Value", bindingSource1, nameof(Model.ProgressBarValue),
false, DataSourceUpdateMode.OnPropertyChanged));
Label18.DataBindings.Add(new Binding("Text", bindingSource1, nameof(Model.ProcessedImagesCount),
false, DataSourceUpdateMode.OnPropertyChanged));
lblFotoTotaliNum.DataBindings.Add(new Binding("Text", bindingSource1, nameof(Model.TotalImagesCount),
false, DataSourceUpdateMode.OnPropertyChanged));
Label10.DataBindings.Add(new Binding("Text", bindingSource1, nameof(Model.ProcessingStatus),
false, DataSourceUpdateMode.OnPropertyChanged));
}
@ -147,50 +119,6 @@ public partial class MainForm
_logger.LogInformation("Programma Avviato");
}
private void FixPaths()
{
Model.SourcePath = FixPath(Model.SourcePath);
Model.DestinationPath = FixPath(Model.DestinationPath);
}
private string FixPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return string.Empty;
}
// Trim leading/trailing quotes
path = path.Trim().Trim('"');
// Normalize directory separators
path = path.Replace('/', Path.DirectorySeparatorChar)
.Replace('\\', Path.DirectorySeparatorChar);
// Remove trailing separators then add one back
path = path.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
return path;
}
private void lockUI()
{
Model.UiEnabled = false;
//TabControl1.Enabled = false;
//Button5.Enabled = false;
//Button6.Enabled = false;
//btnCreaCatalogoAsync.Enabled = false;
}
private void unlockUI()
{
Model.UiEnabled = true;
//TabControl1.Enabled = true;
//Button5.Enabled = true;
//Button6.Enabled = true;
//btnCreaCatalogoAsync.Enabled = true;
}
private string CalcTime(DateTime timeStart, DateTime timeStop, int numFoto)
{
long timediffH, timediffS;
@ -224,6 +152,26 @@ public partial class MainForm
return FixPath(dialog.SelectedPath);
}
private string FixPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return string.Empty;
}
// Trim leading/trailing quotes
path = path.Trim().Trim('"');
// Normalize directory separators
path = path.Replace('/', Path.DirectorySeparatorChar)
.Replace('\\', Path.DirectorySeparatorChar);
// Remove trailing separators then add one back
path = path.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
return path;
}
private void OnSelectSourceFolderRequested(object sender, EventArgs e)
{
var dialogResult = SelectFolder(Model.SourcePath);
@ -356,76 +304,6 @@ public partial class MainForm
}
}
private void SetPicSettings(string SourcePath, string DestPath)
{
_picSettings.DirectorySorgente = SourcePath;
_picSettings.DirectoryDestinazione = DestPath;
// Font and text settings from Model
_picSettings.DimStandard = Model.FontSize;
_picSettings.DimStandardMiniatura = Model.FontSizeThumbnail;
_picSettings.IlFont = Model.FontName;
_picSettings.Grassetto = Model.FontBold;
_picSettings.Posizione = Model.VerticalPosition;
_picSettings.Allineamento = Model.HorizontalAlignment;
_picSettings.Trasparenza = Model.TextTransparency;
_picSettings.Margine = Model.TextMargin;
_picSettings.FontColoreRGB = ColorTranslator.FromHtml(Model.TextColorRGB);
// Thumbnail settings from Model
_picSettings.AltezzaSmall = Model.ThumbnailHeight;
_picSettings.LarghezzaSmall = Model.ThumbnailWidth;
_picSettings.Suffisso = Model.ThumbnailPrefix;
_picSettings.CreaMiniature = Model.CreateThumbnails;
_picSettings.JpegQualityMin = Model.JpegQualityThumbnail;
_picSettings.DimMin = Model.FontSizeThumbnail;
// Big photo settings from Model
_picSettings.AltezzaBig = Model.PhotoBigHeight;
_picSettings.LarghezzaBig = Model.PhotoBigWidth;
_picSettings.FotoGrandeDimOrigina = Model.KeepOriginalDimensions;
_picSettings.JpegQuality = Model.JpegQuality;
_picSettings.Codice = Model.BigPhotoSuffix;
// Logo settings from Model
_picSettings.LogoAggiungi = Model.AddLogo;
_picSettings.LogoNomeFile = Model.LogoFile;
_picSettings.LogoAltezza = Model.LogoHeight;
_picSettings.LogoLarghezza = Model.LogoWidth;
_picSettings.LogoMargine = Model.LogoMargin.ToString();
_picSettings.LogoTrasparenza = Model.LogoTransparency.ToString();
_picSettings.LogoPosizioneH = Model.LogoHorizontalPosition;
_picSettings.LogoPosizioneV = Model.LogoVerticalPosition;
// Text content from Model
_picSettings.TestoFirmaStart = Model.HorizontalText;
_picSettings.TestoFirmaStartV = Model.VerticalText;
// Vertical text settings from Model
_picSettings.DimVert = Model.VerticalTextSize;
_picSettings.MargVert = Model.VerticalTextMargin;
// Boolean flags from Model
_picSettings.UsaRotazioneAutomatica = Model.AutomaticRotation;
_picSettings.UsaForzaJpg = Model.ForceJpeg;
_picSettings.TestoNome = Model.ShowPhotoNumber;
_picSettings.NomeData = Model.ShowDate;
_picSettings.UsaOrarioTestoApplicare = Model.AddTime;
_picSettings.UsaTempoGaraTestoApplicare = Model.AddRaceTime;
_picSettings.OverwriteFiles = Model.OverwriteImages;
// Additional settings from Model
_picSettings.UsaOrarioMiniatura = Model.AddTimeToThumbnails;
_picSettings.DataPartenza = Model.RaceStartDate;
_picSettings.TestoOrario = Model.TimeLabel;
_picSettings.TestoMin = Model.ShowFileNameOnThumbnails;
// Thumbnail text options from Model
_picSettings.AggiungiScritteMiniature = Model.AddTextToThumbnails;
_picSettings.AggTempoGaraMin = Model.AddRaceTimeToThumbnails;
_picSettings.AggNumTempMin = Model.AddNumberAndTimeToThumbnails;
}
private void setLabel18Text(string text)
{
if (Label18.InvokeRequired)
@ -437,99 +315,6 @@ public partial class MainForm
Label18.Text = text;
}
}
private NumerazioneType GetNumerazioneEnum()
{
NumerazioneType numerazioneType;
if (rdbNumProgressiva.Checked)
{
numerazioneType = NumerazioneType.Progressiva;
}
else
{
numerazioneType = NumerazioneType.Files;
}
return numerazioneType;
}
private async void Button1_Click(object sender, EventArgs e)
{
_logger.LogInformation("Avvio elaborazione...");
lockUI();
Model.MainToken?.Dispose();
Model.MainToken = new CancellationTokenSource();
var token = Model.MainToken.Token;
// timeStart = TimeOfDay
FixPaths();
Label10.Text = "Elaborazione in corso...";
lblFotoTotaliNum.Text = "0";
Label18.Text = "0";
Model.SpeedCounter = "-f/m";
SetPicSettings(Model.SourcePath, Model.DestinationPath);
ProgressBar1.Minimum = 0;
ProgressBar1.Step = 1;
ProgressBar1.Value = 0;
var imageCreationOptions = new ImageCreationStuff.Options
{
AggiornaSottodirectory = chkAggiornaSottodirectory.Checked,
CreaSottocartelle = chkCreaSottocartelle.Checked,
FilePerCartella = int.Parse(txtFilePerCartella.Text),
SuffissoCartelle = txtSuffissoCartelle.Text,
CifreContatore = int.Parse(txtCifreContatore.Text),
NumerazioneType = GetNumerazioneEnum(),
SourcePath = Model.SourcePath,
DestinationPath = Model.DestinationPath,
MaxThreads = Model.ThreadsCount,
ChunksSize = Model.ChunkSize,
LinearExecution = rdbVecchioMetodo.Checked
};
try
{
_results = [];
_currentAmount = 0;
_previousAmount = 0;
timer1.Tick += Timer1OnTick;
timer1.Interval = 1000 * 60;
timer1.Enabled = true;
var time =
await _imageCreationService.CreaCatalogoParallel(imageCreationOptions, _results, UiUpdateEvent, token);
Model.SpeedCounter = time;
timer1.Enabled = false;
}
catch (OperationCanceledException operationCanceledException)
{
_logger.LogInformation("Operazione Cancellata");
}
finally
{
Model.MainToken?.Dispose();
Model.MainToken = null;
timer1.Tick -= Timer1OnTick;
}
Label10.Text = "Finito";
unlockUI();
}
private int _currentAmount = 0;
private int _previousAmount = 0;
private void Timer1OnTick(object sender, EventArgs e)
{
_previousAmount = _currentAmount;
_currentAmount = _results.Count;
int diff = _currentAmount - _previousAmount;
Model.SpeedCounter = $"{diff} f/m";
}
}
public class PicInfo