-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineTemplateEditorWindow.xaml.cs
More file actions
304 lines (276 loc) · 11.3 KB
/
Copy pathMachineTemplateEditorWindow.xaml.cs
File metadata and controls
304 lines (276 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using DanteConfigEditor.Services;
using Microsoft.Win32;
namespace DanteConfigEditor;
public sealed record MachineTemplateFormResult(
string TemplateName,
string Manufacturer,
string Model,
string Description,
string Category,
IReadOnlyList<string> Tags,
IReadOnlyList<string> TxLabels,
IReadOnlyList<string> RxLabels,
string? ImageSourcePath,
bool RemoveImage);
public partial class MachineTemplateEditorWindow : Window
{
private readonly UiLanguage _language;
public MachineTemplateEditorWindow(
UiLanguage language,
bool useLightTheme,
string title,
string intro,
string templateName,
string manufacturer,
string model,
string description,
string category,
IEnumerable<string> tags,
IEnumerable<string> txLabels,
IEnumerable<string> rxLabels,
string? imagePath = null,
bool allowImageRemoval = false)
{
InitializeComponent();
_language = language;
DialogThemeService.Apply(this, useLightTheme);
Title = title;
HeadingTextBlock.Text = title;
IntroTextBlock.Text = intro;
TemplateNameTextBox.Text = templateName;
ManufacturerTextBox.Text = manufacturer;
ModelTextBox.Text = model;
DescriptionTextBox.Text = description;
CategoryTextBox.Text = category;
TagsTextBox.Text = string.Join(", ", tags);
TxLabels = BuildRows(txLabels);
RxLabels = BuildRows(rxLabels);
TxLabelsGrid.ItemsSource = TxLabels;
RxLabelsGrid.ItemsSource = RxLabels;
ImagePathTextBox.Text = imagePath ?? string.Empty;
RemoveImageCheckBox.Visibility = allowImageRemoval
? Visibility.Visible
: Visibility.Collapsed;
LoadImagePreview(imagePath);
ApplyLanguage();
}
public ObservableCollection<MachineTemplateLabelRow> TxLabels { get; }
public ObservableCollection<MachineTemplateLabelRow> RxLabels { get; }
public MachineTemplateFormResult? Result { get; private set; }
private void ApplyLanguage()
{
TemplateNameLabel.Content = L("Nom dans la banque", "Name in the bank");
ManufacturerLabel.Content = L("Fabricant", "Manufacturer");
ModelLabel.Content = L("Modèle matériel", "Hardware model");
CategoryLabel.Content = L("Catégorie", "Category");
TagsLabel.Content = L("Mots-clés", "Keywords");
TagsHintTextBlock.Text = L(
"Utilisés pour la recherche, séparés par une virgule (ex. processeur, scène).",
"Used for search, separated with commas (for example processor, stage).");
DescriptionLabel.Content = L("Description / notes", "Description / notes");
ImageLabel.Content = L("Image du modèle (facultative)", "Template image (optional)");
BrowseImageButton.Content = L("Choisir", "Browse");
RemoveImageCheckBox.Content = L(
"Retirer l'image existante",
"Remove existing image");
LabelsHeadingTextBlock.Text = L("Labels proposés par défaut", "Default labels");
LabelsIntroTextBlock.Text = L(
"Ces labels restent modifiables avant chaque insertion dans un projet.",
"These labels can still be changed before each insertion into a project.");
TxLabelColumn.Header = L("Label TX", "Tx label");
RxLabelColumn.Header = L("Label RX", "Rx label");
GenerateTxButton.Content = L("Générer TX 1, TX 2...", "Generate TX 1, TX 2...");
GenerateRxButton.Content = L("Générer RX 1, RX 2...", "Generate RX 1, RX 2...");
SaveButton.Content = L("Enregistrer", "Save");
CancelButton.Content = L("Annuler", "Cancel");
TemplateNameTextBox.ToolTip = L(
"Nom affiché dans la liste de la banque. Il peut être différent du nom de la machine dans le projet.",
"Name displayed in the bank list. It can differ from the device name in the project.");
ManufacturerTextBox.ToolTip = L(
"Fabricant du matériel, par exemple Lake, Yamaha ou Allen & Heath.",
"Hardware manufacturer, for example Lake, Yamaha or Allen & Heath.");
ModelTextBox.ToolTip = L(
"Référence du matériel, par exemple LM 44.",
"Hardware reference, for example LM 44.");
CategoryTextBox.ToolTip = L(
"Famille utilisée pour filtrer la banque, par exemple processeur, console ou interface.",
"Family used to filter the bank, for example processor, console or interface.");
TagsTextBox.ToolTip = TagsHintTextBlock.Text;
DescriptionTextBox.ToolTip = L(
"Informations libres permettant d'identifier l'usage ou les particularités du modèle.",
"Free-form information describing the template's use or characteristics.");
ImagePathTextBox.ToolTip = L(
"Copie facultative d'une image PNG, JPEG ou WebP dans le dossier du modèle.",
"Optional copy of a PNG, JPEG or WebP image into the template folder.");
BrowseImageButton.ToolTip = L(
"Choisir l'image qui sera copiée dans le dossier du modèle.",
"Choose the image that will be copied into the template folder.");
TxLabelsGrid.ToolTip = L(
"Labels TX proposés par défaut. Double-cliquez une cellule pour la modifier.",
"Default Tx labels. Double-click a cell to edit it.");
RxLabelsGrid.ToolTip = L(
"Labels RX proposés par défaut. Double-cliquez une cellule pour la modifier.",
"Default Rx labels. Double-click a cell to edit it.");
GenerateTxButton.ToolTip = L(
"Remplace tous les labels TX par TX 1, TX 2, etc.",
"Replaces all Tx labels with TX 1, TX 2, and so on.");
GenerateRxButton.ToolTip = L(
"Remplace tous les labels RX par RX 1, RX 2, etc.",
"Replaces all Rx labels with RX 1, RX 2, and so on.");
SaveButton.ToolTip = L(
"Enregistre ce modèle dans la banque sélectionnée.",
"Saves this template in the selected bank.");
CancelButton.ToolTip = L(
"Ferme sans enregistrer les modifications du modèle.",
"Closes without saving template changes.");
}
private void BrowseImageButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new()
{
Filter = L(
"Images|*.png;*.jpg;*.jpeg;*.webp",
"Images|*.png;*.jpg;*.jpeg;*.webp"),
Title = L("Choisir l'image du modèle", "Choose template image")
};
if (dialog.ShowDialog(this) != true)
{
return;
}
ImagePathTextBox.Text = dialog.FileName;
RemoveImageCheckBox.IsChecked = false;
LoadImagePreview(dialog.FileName);
}
private void GenerateTxButton_Click(object sender, RoutedEventArgs e)
{
GenerateLabels(TxLabels, "TX");
TxLabelsGrid.Items.Refresh();
}
private void GenerateRxButton_Click(object sender, RoutedEventArgs e)
{
GenerateLabels(RxLabels, "RX");
RxLabelsGrid.Items.Refresh();
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
TxLabelsGrid.CommitEdit(DataGridEditingUnit.Cell, exitEditingMode: true);
RxLabelsGrid.CommitEdit(DataGridEditingUnit.Cell, exitEditingMode: true);
string templateName = TemplateNameTextBox.Text.Trim();
if (string.IsNullOrWhiteSpace(templateName))
{
ShowValidation(L("Le nom du modèle est obligatoire.", "The template name is required."));
return;
}
string? invalidTx = FirstInvalidLabel(TxLabels);
if (invalidTx is not null)
{
ShowValidation(L(
$"Label TX invalide : {invalidTx}",
$"Invalid Tx label: {invalidTx}"));
return;
}
string? invalidRx = FirstInvalidLabel(RxLabels);
if (invalidRx is not null)
{
ShowValidation(L(
$"Label RX invalide : {invalidRx}",
$"Invalid Rx label: {invalidRx}"));
return;
}
Result = new MachineTemplateFormResult(
templateName,
ManufacturerTextBox.Text.Trim(),
ModelTextBox.Text.Trim(),
DescriptionTextBox.Text.Trim(),
CategoryTextBox.Text.Trim(),
TagsTextBox.Text
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries),
TxLabels.Select(row => row.Label.Trim()).ToArray(),
RxLabels.Select(row => row.Label.Trim()).ToArray(),
string.IsNullOrWhiteSpace(ImagePathTextBox.Text)
? null
: Path.GetFullPath(ImagePathTextBox.Text),
RemoveImageCheckBox.IsChecked == true);
DialogResult = true;
}
private void LoadImagePreview(string? path)
{
ImagePreview.Source = null;
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
{
return;
}
try
{
BitmapImage image = new();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.DecodePixelWidth = 420;
image.UriSource = new Uri(Path.GetFullPath(path), UriKind.Absolute);
image.EndInit();
image.Freeze();
ImagePreview.Source = image;
}
catch
{
// La validation détaillée du format est effectuée par le dépôt au
// moment de l'enregistrement. L'aperçu reste simplement vide.
}
}
private static ObservableCollection<MachineTemplateLabelRow> BuildRows(
IEnumerable<string> labels)
{
return new ObservableCollection<MachineTemplateLabelRow>(
labels.Select((label, index) => new MachineTemplateLabelRow(index + 1, label)));
}
private static void GenerateLabels(
IEnumerable<MachineTemplateLabelRow> rows,
string prefix)
{
foreach (MachineTemplateLabelRow row in rows)
{
row.Label = $"{prefix} {row.Number}";
}
}
private static string? FirstInvalidLabel(
IEnumerable<MachineTemplateLabelRow> rows)
{
foreach (MachineTemplateLabelRow row in rows)
{
string? error = DanteNameRules.ValidateChannelName(row.Label);
if (error is not null)
{
return $"#{row.Number} « {row.Label} » - {error}";
}
}
return null;
}
private void ShowValidation(string message)
{
MessageBox.Show(
this,
message,
L("Valeur invalide", "Invalid value"),
MessageBoxButton.OK,
MessageBoxImage.Error);
}
private string L(string french, string english)
{
return _language == UiLanguage.English ? english : french;
}
}
public sealed class MachineTemplateLabelRow
{
public MachineTemplateLabelRow(int number, string label)
{
Number = number;
Label = label;
}
public int Number { get; }
public string Label { get; set; }
}