Skip to content

Commit

Permalink
Add progress bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
cfrpg committed May 31, 2022
1 parent dd6fb0a commit ba582c6
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 12 deletions.
51 changes: 51 additions & 0 deletions src/SharpBladeFlightAnalyzer/LoadViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace SharpBladeFlightAnalyzer
{
public class LoadViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

double maxProgress;
double currProgress;

Visibility visibility;


public double MaxProgress
{
get => maxProgress;
set
{
maxProgress = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MaxProgress"));
}
}
public double CurrProgress
{
get => currProgress;
set
{
currProgress = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CurrProgress"));
}
}

public Visibility Visibility
{
get => visibility;
set
{
visibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Visibility"));
}
}
}
}
6 changes: 3 additions & 3 deletions src/SharpBladeFlightAnalyzer/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:local="clr-namespace:SharpBladeFlightAnalyzer"
xmlns:d3="clr-namespace:InteractiveDataDisplay.WPF;assembly=InteractiveDataDisplay.WPF"
mc:Ignorable="d"
Title="SharpBladeFlightAnalyzer - cfrpg" Height="700" Width="1000" MinHeight="700" MinWidth="1000" Closing="Window_Closing" Drop="Window_Drop" AllowDrop="True">
Title="SharpBladeFlightAnalyzer - cfrpg" Height="700" Width="1000" MinHeight="700" MinWidth="1000" Closing="Window_Closing" Drop="Window_Drop" AllowDrop="True" Loaded="Window_Loaded">
<Window.Resources>
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
Expand Down Expand Up @@ -75,8 +75,8 @@
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="1" Grid.Row="1" Stroke="Gray" StrokeThickness="10" StrokeDashArray="3 1.55" RadiusX="20" RadiusY="20"/>
<Path Grid.Column="1" Grid.Row="1" Data="M100,60 100,140 M60,100 140,100" Stroke="Gray" StrokeThickness="10"/>
<Button x:Name="loadButton" Grid.Column="1" Grid.Row="1" Background="Transparent" BorderBrush="Transparent" Foreground="Transparent" Style="{DynamicResource ButtonStyle1}" Click="Button_Click"/>
<ProgressBar x:Name="loadProgressBar" HorizontalAlignment="Center" Height="20" VerticalAlignment="Center" Width="300" Grid.Row="1" Grid.ColumnSpan="3" Margin="0" Visibility="Hidden"/>
<Button Grid.Column="1" Grid.Row="1" Background="Transparent" BorderBrush="Transparent" Foreground="Transparent" Style="{DynamicResource ButtonStyle1}" Click="Button_Click"/>
<ProgressBar HorizontalAlignment="Center" Height="20" VerticalAlignment="Top" Width="300" Grid.Row="2" Grid.ColumnSpan="3" Margin="0,30,0,0" Visibility="{Binding Visibility}" Maximum="{Binding MaxProgress}" Value="{Binding CurrProgress}"/>

</Grid>
</local:TabPage>
Expand Down
85 changes: 78 additions & 7 deletions src/SharpBladeFlightAnalyzer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Windows.Shapes;
using System.Diagnostics;
using System.IO;
using System.Threading;


namespace SharpBladeFlightAnalyzer
Expand All @@ -29,6 +30,10 @@ public partial class MainWindow : Window

Dictionary<string, FieldConfig> fieldConfigs;

LoadViewModel lvm;

bool isloading;

public MainWindow()
{
InitializeComponent();
Expand All @@ -52,12 +57,12 @@ public MainWindow()
}
sr.Close();
}
string[] args = Environment.GetCommandLineArgs();
if(args.Length>1)
{
loadFile(args[1]);
}

isloading = false;
lvm = new LoadViewModel();

lvm.Visibility = Visibility.Hidden;
mainTabControl.DataContext = lvm;

//ULogFile f = new ULogFile();
//f.Load("D:\\temp\\log.ulg");
Expand Down Expand Up @@ -179,8 +184,10 @@ private void Button_Click(object sender, RoutedEventArgs e)
ofd.Filter = "ulog日志文件 (*.ulg)|*.ulg|All files (*.*)|*.*";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
loadFile(ofd.FileName);
//loadFile(ofd.FileName);
startLoadFile(ofd.FileName);
}

}

private void mainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
Expand All @@ -205,11 +212,13 @@ private void ShowMessageBox(string str)

private void Window_Drop(object sender, DragEventArgs e)
{
if (isloading)
return;
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

loadFile(files[0]);
startLoadFile(files[0]);

}
}
Expand All @@ -231,5 +240,67 @@ private void loadFile(string path)
currentPage = lpc;
}
}

private void startLoadFile(string path)
{
if (isloading)
return;
isloading = true;
Thread uiThread = new Thread(new ParameterizedThreadStart(updateUI));
uiThread.IsBackground = true;
uiThread.Start(path);

}

private void updateUI(object o)
{
string path = (string)o;
Thread loadThread= new Thread(new ParameterizedThreadStart(loadingFile));
loadThread.IsBackground = true;
lvm.CurrProgress = 0;
lvm.Visibility = Visibility.Visible;
ULogFile f = new ULogFile();
loadThread.Start(new Tuple<ULogFile, string>(f, path));
Thread.Sleep(20);
lvm.MaxProgress = f.TotalSize;
while (true)
{
lvm.CurrProgress = f.CurrPos;
Thread.Sleep(20);
if (f.ReadCompleted)
break;
}
lvm.Visibility = Visibility.Hidden;
Dispatcher.Invoke(new Action(delegate ()
{
LogPageControl lpc = new LogPageControl(f, this);
lpc.addFieldBtn.Click += AddFieldBtn_Click;
TabPage page = new TabPage();
page.Header = f.File.Name;

page.Content = lpc;
page.DisposableContent = f;
mainTabControl.Items.Insert(mainTabControl.Items.Count - 1, page);
mainTabControl.SelectedIndex = mainTabControl.Items.Count - 2;
currentPage = lpc;
}));
isloading = false;
}

private void loadingFile(object o)
{
Tuple<ULogFile, string> args = (Tuple<ULogFile, string>)o;
ULogFile f = args.Item1;
bool res = f.Load(args.Item2, fieldConfigs);
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
startLoadFile(args[1]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<DependentUpon>FieldListWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Graph.cs" />
<Compile Include="LoadViewModel.cs" />
<Compile Include="LoggedMessage.cs" />
<Compile Include="MessageFormat.cs" />
<Compile Include="MessageViewModel.cs" />
Expand Down
27 changes: 25 additions & 2 deletions src/SharpBladeFlightAnalyzer/ULogFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public class ULogFile :IDisposable

bool defEndFlag = false;

bool readCompleted;

long currLoadPos;

bool[] ignoreErrorFlags;

public ulong Timestamp
{
get { return timestamp; }
Expand Down Expand Up @@ -97,6 +103,10 @@ public FileInfo File
public List<MessageViewModel> MessageList { get => messageList; set => messageList = value; }
public Dictionary<string, MessageFormat> FormatList { get => formatList; set => formatList = value; }
public Dictionary<int, Message> MessageDict { get => messageDict; set => messageDict = value; }
public long TotalSize { get => reader.BaseStream.Length; }

public long CurrPos { get => currLoadPos; }
public bool ReadCompleted { get => readCompleted; set => readCompleted = value; }

public ULogFile()
{
Expand All @@ -105,7 +115,9 @@ public ULogFile()
MessageDict = new Dictionary<int, Message>();
infomations=new List<Tuple<string, string>>();
parameters=new List<Parameter>();
Messages = new List<LoggedMessage>();
Messages = new List<LoggedMessage>();
readCompleted = false;
ignoreErrorFlags = new bool[256];
}

public bool Load(string path, Dictionary<string, FieldConfig> fieldConfigs)
Expand All @@ -129,7 +141,10 @@ public bool Load(string path, Dictionary<string, FieldConfig> fieldConfigs)
return false;
version = buff[7];
timestamp = reader.ReadUInt64();
while (readMessage()) ;
while (readMessage())
{
currLoadPos = reader.BaseStream.Position;
}
reader.Close();

FileInfo fi = new FileInfo(System.AppDomain.CurrentDomain.BaseDirectory + "config\\Quaternions.txt");
Expand Down Expand Up @@ -185,6 +200,7 @@ public bool Load(string path, Dictionary<string, FieldConfig> fieldConfigs)
messageList.TrimExcess();

GC.Collect();
readCompleted = true;
return true;
}

Expand Down Expand Up @@ -269,9 +285,16 @@ private bool readMessage()

if (!res)
{
if(ignoreErrorFlags[msgtype])
{
return true;
}
errmsg = "读取文件时发生错误:" + System.Environment.NewLine + errmsg + System.Environment.NewLine + "是否继续?";
if (MessageBox.Show(errmsg, "SharpBladeFlightAnalyzer", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
ignoreErrorFlags[msgtype] = true;
return true;
}
else
return false;
}
Expand Down

0 comments on commit ba582c6

Please sign in to comment.