From 887f68dc8d771e579f14dc5940e15fa58277dbdc Mon Sep 17 00:00:00 2001
From: Markus Quaritsch <markus.quaritsch@tugraz.at>
Date: Tue, 7 Jun 2022 16:15:31 +0200
Subject: [PATCH] syncronize gear entries in ihpc dialog, implement autosendto
 functionality, sort entries before writing json files

---
 VECTO/GUI/IEPCForm.vb                         | 10 +++-
 VECTO/GUI/IHPCForm.vb                         | 23 +++++++-
 VECTO/GUI/IHPCPowerMapInputDialog.Designer.vb | 59 +++++++++++--------
 VECTO/GUI/VehicleForm.vb                      | 11 ++++
 .../OutputData/FileIO/JSONFileWriter.cs       | 12 ++--
 5 files changed, 81 insertions(+), 34 deletions(-)

diff --git a/VECTO/GUI/IEPCForm.vb b/VECTO/GUI/IEPCForm.vb
index 200e23663e..eedc20a6ea 100644
--- a/VECTO/GUI/IEPCForm.vb
+++ b/VECTO/GUI/IEPCForm.vb
@@ -14,6 +14,8 @@ Public Class IEPCForm
 	Private _flcFilePath1 as String
 	Private _flcFilePath2 as String
 	Private _changed as Boolean
+
+    Public AutoSendTo As action(of string, VehicleForm)
 	
 	Private _contextMenuFiles As String()
 
@@ -92,7 +94,7 @@ Public Class IEPCForm
 	End Sub
 
 	Private Function CreateListViewItem(axleNumber As Integer, filepath As String) As ListViewItem
-	    Dim basePath As String = Path.GetDirectoryName(_iepcFilePath)
+	    Dim basePath As String = GetPath(_iepcFilePath)
 	    Dim retVal As New ListViewItem
 		retVal.SubItems(0).Text = axleNumber.ToGUIFormat()
         retVal.SubItems.Add(GetRelativePath(filepath, basePath))
@@ -425,6 +427,12 @@ Public Class IEPCForm
 			Return False
 		End If
 
+	    If not AutoSendTo is nothing Then
+	        If VehicleForm.Visible Then
+	            AutoSendTo(file, VehicleForm)
+	        End If
+	    End If
+
 		_changed = False
 		LbStatus.Text = ""
 
diff --git a/VECTO/GUI/IHPCForm.vb b/VECTO/GUI/IHPCForm.vb
index 2807748e52..d9d8ccc1e8 100644
--- a/VECTO/GUI/IHPCForm.vb
+++ b/VECTO/GUI/IHPCForm.vb
@@ -15,6 +15,8 @@ Public Class IHPCForm
 
     Private _contextMenuFiles As String()
 
+    Public AutoSendTo As action(of string, VehicleForm)
+
 #Region "Set JSON Data"
 
     Public Sub ReadIHPCFile(file As String)
@@ -59,7 +61,7 @@ Public Class IHPCForm
     End Sub
 
     Private Sub SetPowerMapEntries(powerMapListView As ListView, entries As IList(Of IElectricMotorPowerMap))
-        For Each entry As IElectricMotorPowerMap In entries
+        For Each entry As IElectricMotorPowerMap In entries.OrderBy(Function(x) x.Gear)
             Dim listEntry = CreateListViewItem(entry.Gear, entry.PowerMap.Source)
             powerMapListView.Items.Add(listEntry)
         Next
@@ -68,7 +70,7 @@ Public Class IHPCForm
     Private Function CreateListViewItem(axleNumber As Integer, filepath As String) As ListViewItem
         Dim retVal As New ListViewItem
         retVal.SubItems(0).Text = axleNumber.ToGUIFormat()
-        retVal.SubItems.Add(GetRelativePath(filepath, Path.GetDirectoryName(_ihpcFilePath)))
+        retVal.SubItems.Add(GetRelativePath(filepath, GetPath(_ihpcFilePath)))
         Return retVal
     End Function
     
@@ -144,21 +146,30 @@ Public Class IHPCForm
     
     Private Sub AddListViewItem(listView As ListView)
         IHPCPowerMapInputDialog.Clear()
+        dim gear = listView.Items.Count + 1
+        IHPCPowerMapInputDialog.tbGear.Text = gear.ToString()
 
         If IHPCPowerMapInputDialog.ShowDialog() = DialogResult.OK Then
-            Dim gear = Convert.ToInt32(IHPCPowerMapInputDialog.tbGear.Text)
             Dim filePath = IHPCPowerMapInputDialog.tbInputFile.Text
             listView.Items.Add(CreateListViewItem(gear, filePath))
+            if (listView.Equals(lvPowerMap1)) Then
+                lvPowerMap2.Items.Add(CreateListViewItem(gear, ""))
+            End If
+            if (listView.Equals(_lvPowerMap2)) Then
+                lvPowerMap1.Items.Add(CreateListViewItem(gear, ""))
+            End If
             Change()
         End If
     End Sub
 
     Private Sub btRemovePowerMap1_Click(sender As Object, e As EventArgs) Handles btRemovePowerMap1.Click
         RemoveListEntry(lvPowerMap1)
+        RemoveListEntry(lvPowerMap2)
     End Sub
     
     Private Sub btRemovePowerMap2_Click(sender As Object, e As EventArgs) Handles btRemovePowerMap2.Click
         RemoveListEntry(lvPowerMap2)
+        RemoveListEntry(lvPowerMap1)
     End Sub
 
     Private Sub RemoveListEntry(listView As ListView)
@@ -293,6 +304,12 @@ Public Class IHPCForm
             Return False
         End If
 
+        If not AutoSendTo is nothing Then
+            If VehicleForm.Visible Then
+                AutoSendTo(ihpcFilePath, VehicleForm)
+            End If
+        End If
+
         _changed = False
         LbStatus.Text = ""
 
diff --git a/VECTO/GUI/IHPCPowerMapInputDialog.Designer.vb b/VECTO/GUI/IHPCPowerMapInputDialog.Designer.vb
index ae9b58663d..64304f84cb 100644
--- a/VECTO/GUI/IHPCPowerMapInputDialog.Designer.vb
+++ b/VECTO/GUI/IHPCPowerMapInputDialog.Designer.vb
@@ -45,8 +45,9 @@ Partial Class IHPCPowerMapInputDialog
         Me.FlowLayoutPanel1.Controls.Add(Me.TableLayoutPanel5)
         Me.FlowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top
         Me.FlowLayoutPanel1.Location = New System.Drawing.Point(0, 0)
+        Me.FlowLayoutPanel1.Margin = New System.Windows.Forms.Padding(4, 5, 4, 5)
         Me.FlowLayoutPanel1.Name = "FlowLayoutPanel1"
-        Me.FlowLayoutPanel1.Size = New System.Drawing.Size(508, 45)
+        Me.FlowLayoutPanel1.Size = New System.Drawing.Size(762, 69)
         Me.FlowLayoutPanel1.TabIndex = 11
         '
         'TableLayoutPanel4
@@ -57,29 +58,32 @@ Partial Class IHPCPowerMapInputDialog
         Me.TableLayoutPanel4.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 67.2!))
         Me.TableLayoutPanel4.Controls.Add(Me.tbGear, 1, 0)
         Me.TableLayoutPanel4.Controls.Add(Me.Label3, 0, 0)
-        Me.TableLayoutPanel4.Location = New System.Drawing.Point(6, 8)
-        Me.TableLayoutPanel4.Margin = New System.Windows.Forms.Padding(6, 3, 3, 3)
+        Me.TableLayoutPanel4.Location = New System.Drawing.Point(9, 13)
+        Me.TableLayoutPanel4.Margin = New System.Windows.Forms.Padding(9, 5, 4, 5)
         Me.TableLayoutPanel4.Name = "TableLayoutPanel4"
         Me.TableLayoutPanel4.RowCount = 1
         Me.TableLayoutPanel4.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50!))
-        Me.TableLayoutPanel4.Size = New System.Drawing.Size(128, 28)
+        Me.TableLayoutPanel4.Size = New System.Drawing.Size(192, 43)
         Me.TableLayoutPanel4.TabIndex = 10
         '
         'tbGear
         '
         Me.tbGear.Anchor = System.Windows.Forms.AnchorStyles.None
-        Me.tbGear.Location = New System.Drawing.Point(46, 4)
+        Me.tbGear.Location = New System.Drawing.Point(70, 8)
+        Me.tbGear.Margin = New System.Windows.Forms.Padding(4, 5, 4, 5)
         Me.tbGear.Name = "tbGear"
-        Me.tbGear.Size = New System.Drawing.Size(77, 20)
+        Me.tbGear.ReadOnly = true
+        Me.tbGear.Size = New System.Drawing.Size(114, 26)
         Me.tbGear.TabIndex = 1
         '
         'Label3
         '
         Me.Label3.Anchor = System.Windows.Forms.AnchorStyles.None
         Me.Label3.AutoSize = true
-        Me.Label3.Location = New System.Drawing.Point(5, 7)
+        Me.Label3.Location = New System.Drawing.Point(8, 11)
+        Me.Label3.Margin = New System.Windows.Forms.Padding(4, 0, 4, 0)
         Me.Label3.Name = "Label3"
-        Me.Label3.Size = New System.Drawing.Size(30, 13)
+        Me.Label3.Size = New System.Drawing.Size(45, 20)
         Me.Label3.TabIndex = 9
         Me.Label3.Text = "Gear"
         '
@@ -87,33 +91,36 @@ Partial Class IHPCPowerMapInputDialog
         '
         Me.TableLayoutPanel5.Anchor = System.Windows.Forms.AnchorStyles.None
         Me.TableLayoutPanel5.ColumnCount = 2
-        Me.TableLayoutPanel5.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 316!))
-        Me.TableLayoutPanel5.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 40!))
+        Me.TableLayoutPanel5.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 474!))
+        Me.TableLayoutPanel5.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 60!))
         Me.TableLayoutPanel5.Controls.Add(Me.btAddFilePath, 1, 0)
         Me.TableLayoutPanel5.Controls.Add(Me.tbInputFile, 0, 0)
-        Me.TableLayoutPanel5.Location = New System.Drawing.Point(140, 3)
+        Me.TableLayoutPanel5.Location = New System.Drawing.Point(209, 5)
+        Me.TableLayoutPanel5.Margin = New System.Windows.Forms.Padding(4, 5, 4, 5)
         Me.TableLayoutPanel5.Name = "TableLayoutPanel5"
         Me.TableLayoutPanel5.RowCount = 1
         Me.TableLayoutPanel5.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100!))
-        Me.TableLayoutPanel5.Size = New System.Drawing.Size(356, 39)
+        Me.TableLayoutPanel5.Size = New System.Drawing.Size(534, 60)
         Me.TableLayoutPanel5.TabIndex = 11
         '
         'btAddFilePath
         '
         Me.btAddFilePath.Anchor = System.Windows.Forms.AnchorStyles.None
         Me.btAddFilePath.Image = CType(resources.GetObject("btAddFilePath.Image"),System.Drawing.Image)
-        Me.btAddFilePath.Location = New System.Drawing.Point(324, 6)
+        Me.btAddFilePath.Location = New System.Drawing.Point(486, 10)
+        Me.btAddFilePath.Margin = New System.Windows.Forms.Padding(4, 5, 4, 5)
         Me.btAddFilePath.Name = "btAddFilePath"
-        Me.btAddFilePath.Size = New System.Drawing.Size(24, 26)
+        Me.btAddFilePath.Size = New System.Drawing.Size(36, 40)
         Me.btAddFilePath.TabIndex = 13
         Me.btAddFilePath.UseVisualStyleBackColor = true
         '
         'tbInputFile
         '
         Me.tbInputFile.Anchor = CType((System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right),System.Windows.Forms.AnchorStyles)
-        Me.tbInputFile.Location = New System.Drawing.Point(3, 9)
+        Me.tbInputFile.Location = New System.Drawing.Point(4, 17)
+        Me.tbInputFile.Margin = New System.Windows.Forms.Padding(4, 5, 4, 5)
         Me.tbInputFile.Name = "tbInputFile"
-        Me.tbInputFile.Size = New System.Drawing.Size(310, 20)
+        Me.tbInputFile.Size = New System.Drawing.Size(466, 26)
         Me.tbInputFile.TabIndex = 12
         '
         'TableLayoutPanel1
@@ -123,19 +130,21 @@ Partial Class IHPCPowerMapInputDialog
         Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50!))
         Me.TableLayoutPanel1.Controls.Add(Me.btAddInput, 0, 0)
         Me.TableLayoutPanel1.Controls.Add(Me.btCancel, 1, 0)
-        Me.TableLayoutPanel1.Location = New System.Drawing.Point(348, 51)
+        Me.TableLayoutPanel1.Location = New System.Drawing.Point(522, 78)
+        Me.TableLayoutPanel1.Margin = New System.Windows.Forms.Padding(4, 5, 4, 5)
         Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
         Me.TableLayoutPanel1.RowCount = 1
         Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50!))
-        Me.TableLayoutPanel1.Size = New System.Drawing.Size(148, 29)
+        Me.TableLayoutPanel1.Size = New System.Drawing.Size(222, 45)
         Me.TableLayoutPanel1.TabIndex = 12
         '
         'btAddInput
         '
         Me.btAddInput.Anchor = System.Windows.Forms.AnchorStyles.None
-        Me.btAddInput.Location = New System.Drawing.Point(3, 3)
+        Me.btAddInput.Location = New System.Drawing.Point(5, 5)
+        Me.btAddInput.Margin = New System.Windows.Forms.Padding(4, 5, 4, 5)
         Me.btAddInput.Name = "btAddInput"
-        Me.btAddInput.Size = New System.Drawing.Size(67, 23)
+        Me.btAddInput.Size = New System.Drawing.Size(100, 35)
         Me.btAddInput.TabIndex = 14
         Me.btAddInput.Text = "OK"
         '
@@ -143,20 +152,22 @@ Partial Class IHPCPowerMapInputDialog
         '
         Me.btCancel.Anchor = System.Windows.Forms.AnchorStyles.None
         Me.btCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
-        Me.btCancel.Location = New System.Drawing.Point(77, 3)
+        Me.btCancel.Location = New System.Drawing.Point(116, 5)
+        Me.btCancel.Margin = New System.Windows.Forms.Padding(4, 5, 4, 5)
         Me.btCancel.Name = "btCancel"
-        Me.btCancel.Size = New System.Drawing.Size(67, 23)
+        Me.btCancel.Size = New System.Drawing.Size(100, 35)
         Me.btCancel.TabIndex = 15
         Me.btCancel.Text = "Cancel"
         '
         'IHPCPowerMapInputDialog
         '
-        Me.AutoScaleDimensions = New System.Drawing.SizeF(6!, 13!)
+        Me.AutoScaleDimensions = New System.Drawing.SizeF(9!, 20!)
         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
-        Me.ClientSize = New System.Drawing.Size(508, 85)
+        Me.ClientSize = New System.Drawing.Size(762, 131)
         Me.Controls.Add(Me.TableLayoutPanel1)
         Me.Controls.Add(Me.FlowLayoutPanel1)
         Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
+        Me.Margin = New System.Windows.Forms.Padding(4, 5, 4, 5)
         Me.MaximizeBox = false
         Me.MinimizeBox = false
         Me.Name = "IHPCPowerMapInputDialog"
diff --git a/VECTO/GUI/VehicleForm.vb b/VECTO/GUI/VehicleForm.vb
index 8a61e13b6f..fca9c419a0 100644
--- a/VECTO/GUI/VehicleForm.vb
+++ b/VECTO/GUI/VehicleForm.vb
@@ -1605,6 +1605,11 @@ Public Class VehicleForm
 		Dim f = FileRepl(tbIEPCFilePath.Text, GetPath(_vehFile))
 
 		IEPCForm.JobDir = GetPath(_vehFile)
+	    IEPCForm.AutoSendTo = Sub(file, vehicleForm)
+	        If UCase(FileRepl(vehicleForm.tbIEPCFilePath.Text, JobDir)) <> UCase(file) Then _
+	            vehicleForm.tbIEPCFilePath.Text = GetFilenameWithoutDirectory(file, JobDir)
+	        VectoJobForm.UpdatePic()
+	    End Sub
 
 		If Not Trim(f) = "" Then
 			If Not File.Exists(f) Then
@@ -1640,6 +1645,12 @@ Public Class VehicleForm
 		Dim f = FileRepl(tbIHPCFilePath.Text, GetPath(_vehFile))
 
 		IHPCForm.JobDir = GetPath(_vehFile)
+	    IHPCForm.AutoSendTo = Sub(file, vehicleForm)
+	        If UCase(FileRepl(vehicleForm.tbIHPCFilePath.Text, JobDir)) <> UCase(file) Then _
+	            vehicleForm.tbIHPCFilePath.Text = GetFilenameWithoutDirectory(file, JobDir)
+	        VectoJobForm.UpdatePic()
+	    End Sub
+
 	    If Not Trim(f) = "" Then
 	        If Not File.Exists(f) Then
 	            MsgBox("File not found!")
diff --git a/VectoCore/VectoCore/OutputData/FileIO/JSONFileWriter.cs b/VectoCore/VectoCore/OutputData/FileIO/JSONFileWriter.cs
index e58dd595b2..6dc185bc8e 100644
--- a/VectoCore/VectoCore/OutputData/FileIO/JSONFileWriter.cs
+++ b/VectoCore/VectoCore/OutputData/FileIO/JSONFileWriter.cs
@@ -109,7 +109,7 @@ public class JSONFileWriter : IOutputFileWriter
 	private List<Dictionary<string, object>> GetVoltageLevelEntries(IList<IElectricMotorVoltageLevel> voltageLevels, string filename)
 	{
 		var vlevels = new List<Dictionary<string, object>>();
-		foreach (var entry in voltageLevels)
+		foreach (var entry in voltageLevels.OrderBy(x => x.VoltageLevel))
 		{
 			var vlevel = new Dictionary<string, object> {
 				{ JsonKeys.EM_Voltage, entry.VoltageLevel.Value() },
@@ -121,7 +121,7 @@ public class JSONFileWriter : IOutputFileWriter
 				{ JsonKeys.EM_FullLoadCurve, GetRelativePath(entry.FullLoadCurve.Source, Path.GetDirectoryName(filename)) }
 			};
 			var powerMaps = new Dictionary<int, object>();
-			foreach (var pMap in entry.PowerMap)
+			foreach (var pMap in entry.PowerMap.OrderBy(x => x.Gear))
 			{
 				powerMaps.Add(pMap.Gear, GetRelativePath(pMap.PowerMap.Source, Path.GetDirectoryName(filename)));
 			}
@@ -150,7 +150,7 @@ public class JSONFileWriter : IOutputFileWriter
 
 		var gears = new List<Dictionary<string, object>>();
 
-		foreach (var gear in iepc.Gears) {
+		foreach (var gear in iepc.Gears.OrderBy(x => x.GearNumber)) {
 			var currentGear = new Dictionary<string, object> {
 				{ JsonKeys.Gearbox_Gear_Ratio, gear.Ratio }
 			};
@@ -162,7 +162,7 @@ public class JSONFileWriter : IOutputFileWriter
 		}
 
 		var voltageLevels = new List<Dictionary<string, object>>();
-		foreach (var voltageLevel in iepc.VoltageLevels) {
+		foreach (var voltageLevel in iepc.VoltageLevels.OrderBy(x => x.VoltageLevel)) {
 			var currentLevel = new Dictionary<string, object>
 			{
 				{JsonKeys.IEPC_Voltage, voltageLevel.VoltageLevel.Value()},
@@ -175,7 +175,7 @@ public class JSONFileWriter : IOutputFileWriter
 
 			};
 			var powerMaps = new Dictionary<string, object>();
-			foreach (var pMap in voltageLevel.PowerMap)
+			foreach (var pMap in voltageLevel.PowerMap.OrderBy(x => x.Gear))
 			{
 				powerMaps.Add(pMap.Gear.ToString(), GetRelativePath(pMap.PowerMap.Source, Path.GetDirectoryName(filename)));
 			}
@@ -184,7 +184,7 @@ public class JSONFileWriter : IOutputFileWriter
 		}
 
 		var dragCurves = new Dictionary<string, object>();
-		foreach (var dragCurveEntry in iepc.DragCurves) {
+		foreach (var dragCurveEntry in iepc.DragCurves.OrderBy(x => x.Gear)) {
 			dragCurves.Add(dragCurveEntry.Gear.ToString(), 
 				GetRelativePath(dragCurveEntry.DragCurve.Source, Path.GetDirectoryName(filename)));
 		}
-- 
GitLab