Skip to content

Commit

Permalink
🎨 DTMF fix and file-menu
Browse files Browse the repository at this point in the history
  • Loading branch information
SydneyOwl committed Apr 30, 2024
1 parent 3f612ed commit 225b5b6
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 14 deletions.
11 changes: 11 additions & 0 deletions Constants/Gt12/DTMF_CHOICE.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.ObjectModel;

namespace SenhaixFreqWriter.Constants.Gt12;

public class DTMF_CHOICE
{
public static ObservableCollection<string> time = new()
{
"50 ms", "100 ms", "200 ms", "300 ms", "500 ms"
};
}
43 changes: 42 additions & 1 deletion DataModels/Gt12/AppData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System.Xml.Serialization;
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using MsBox.Avalonia;
using SenhaixFreqWriter.DataModels.Shx8x00;

namespace SenhaixFreqWriter.DataModels.Gt12;

Expand Down Expand Up @@ -40,4 +45,40 @@ public AppData()
}
}
}

public static AppData forceNewInstance()
{
instance = new AppData();
return instance;
}
public void SaveToFile(Stream s)
{
var serializer = new XmlSerializer(typeof(AppData));
using (var streamWriter = new StreamWriter(s, Encoding.UTF8))
{
serializer.Serialize(streamWriter, instance);
}
}


public static void CreatObjFromFile(Stream s)
{
using (var streamReader = new StreamReader(s, Encoding.UTF8))
{
var xmls = streamReader.ReadToEnd();
AppData tmp;
try
{
var xmlSerializer = new XmlSerializer(typeof(AppData));
var stringReader = new StringReader(xmls);
tmp = (AppData)xmlSerializer.Deserialize(stringReader);
instance = tmp;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
MessageBoxManager.GetMessageBoxStandard("注意", "无效的文件").ShowAsync();
}
}
}
}
1 change: 1 addition & 0 deletions DataModels/Shx8x00/ClassTheRadioData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static void CreatObjFromFile(Stream s)
var xmlSerializer = new XmlSerializer(typeof(ClassTheRadioData));
var stringReader = new StringReader(xmls);
tmp = (ClassTheRadioData)xmlSerializer.Deserialize(stringReader);
instance = tmp;
instance.chanData.Clear();
foreach (var cd in tmp.channeldata)
{
Expand Down
11 changes: 10 additions & 1 deletion Views/Gt12/DTMFWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
x:Class="SenhaixFreqWriter.Views.Gt12.DTMFWindow"
xmlns:vm="using:SenhaixFreqWriter.Views.Gt12"
x:DataType="vm:DTMFWindow"
xmlns:constants="clr-namespace:SenhaixFreqWriter.Constants.Gt12"
Title="DTMF设置">
<Canvas>
<DataGrid Margin="20" x:Name="FMDataGrid"
Expand Down Expand Up @@ -42,5 +43,13 @@
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Label Content="本机id" Canvas.Bottom="90" Canvas.Left="20"></Label>
<TextBox Canvas.Bottom="85" Canvas.Left="70" Text="{Binding MyId}" LostFocus="GroupCodeInputElement_OnLostFocus"></TextBox>

<Label Content="DTMF码持续时间" Canvas.Bottom="90" Canvas.Left="160"></Label>
<ComboBox Canvas.Bottom="85" Canvas.Left="290" ItemsSource="{x:Static constants:DTMF_CHOICE.time}" SelectedIndex="{Binding WordTime}"></ComboBox>

<Label Content="DTMF码间断时间" Canvas.Bottom="50" Canvas.Left="160"></Label>
<ComboBox Canvas.Bottom="45" Canvas.Left="290" ItemsSource="{x:Static constants:DTMF_CHOICE.time}" SelectedIndex="{Binding IdleTime}"></ComboBox>
</Canvas>
</Window>
</Window>
36 changes: 36 additions & 0 deletions Views/Gt12/DTMFWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,44 @@ namespace SenhaixFreqWriter.Views.Gt12;

public partial class DTMFWindow : Window
{
public int WordTime
{
get => _wordTime;
set
{
_wordTime = value;
AppData.getInstance().dtmfs.WordTime = value;
}
}

public int IdleTime
{
get => _idleTime;
set
{
_idleTime = value;
AppData.getInstance().dtmfs.IdleTime = value;
}
}

private ObservableCollection<DTMPObject> _dtmfs = new();

private int _wordTime = AppData.getInstance().dtmfs.WordTime;

private int _idleTime = AppData.getInstance().dtmfs.IdleTime;

private string _myId = AppData.getInstance().dtmfs.LocalID;

public string MyId
{
get => _myId;
set
{
_myId = value ?? throw new ArgumentNullException(nameof(value));
AppData.getInstance().dtmfs.LocalID = value;
}
}

public ObservableCollection<DTMPObject> Dtmfs
{
get => _dtmfs;
Expand Down
10 changes: 5 additions & 5 deletions Views/Gt12/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@
<DockPanel LastChildFill="True">
<Menu DockPanel.Dock="Top">
<MenuItem Header="文件">
<MenuItem Header="新建">
<MenuItem Header="新建" Click="NewFileMenuItem_OnClick">
<MenuItem.Icon>
<PathIcon Data="{StaticResource new_file}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="打开">
<MenuItem Header="打开" Click="OpenFileMenuItem_OnClick">
<MenuItem.Icon>
<PathIcon Data="{StaticResource open_regular}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="保存">
<MenuItem Header="保存" Click="SaveFileMenuItem_OnClick">
<MenuItem.Icon>
<PathIcon Data="{StaticResource save_regular}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="另存为">
<MenuItem Header="另存为" Click="SaveAsMenuItem_OnClick">
<MenuItem.Icon>
<PathIcon Data="{StaticResource save_as_regular}" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Header="退出">
<MenuItem Header="退出" Click="ExitMenuItem_OnClick">
<MenuItem.Icon>
<PathIcon Data="{StaticResource error_circle_regular}" />
</MenuItem.Icon>
Expand Down
88 changes: 82 additions & 6 deletions Views/Gt12/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using Avalonia.Styling;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
using SenhaixFreqWriter.Constants.Gt12;
using SenhaixFreqWriter.DataModels.Gt12;
using SenhaixFreqWriter.DataModels.Shx8x00;
using SenhaixFreqWriter.Views.Common;

namespace SenhaixFreqWriter.Views.Gt12;
Expand All @@ -29,13 +33,15 @@ public ObservableCollection<Channel> listItems

private bool devSwitchFlag = false;

private string filePath = "";

private Channel copiedChannel;
public MainWindow()
{
InitializeComponent();
DataContext = this;
_listItems.CollectionChanged += CollectionChangedHandler;
setArea(0);
_listItems.CollectionChanged += CollectionChangedHandler;
Closed += OnWindowClosed;
}
private void About_OnClick(object? sender, RoutedEventArgs e)
Expand All @@ -53,12 +59,12 @@ private void OnWindowClosed(object? sender, EventArgs e)
}
private void CollectionChangedHandler(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action.Equals(NotifyCollectionChangedAction.Add) ||
e.Action.Equals(NotifyCollectionChangedAction.Remove))
{
// if (e.Action.Equals(NotifyCollectionChangedAction.Add) ||
// e.Action.Equals(NotifyCollectionChangedAction.Remove))
// {
calcSeq();
AppData.getInstance().channelList[currentArea] = listItems.ToArray();
}
// }
}

private void calcSeq()
Expand Down Expand Up @@ -99,8 +105,8 @@ private string calcNameSize(string name)
private void setArea(int area)
{
currentArea = area;
listItems.Clear();
var tmpChannel = AppData.getInstance().channelList[area];
listItems.Clear();
for (var i = 0; i < tmpChannel.Length; i++)
{
listItems.Add(tmpChannel[i]);
Expand Down Expand Up @@ -358,4 +364,74 @@ private void DTMFMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
new DTMFWindow().ShowDialog(this);
}

private async void NewFileMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
var box = MessageBoxManager
.GetMessageBoxStandard("注意", "该操作将清空编辑中的信道,确定继续?",
ButtonEnum.YesNo);

var result = await box.ShowWindowDialogAsync(this);
if (result == ButtonResult.No) return;
AppData.forceNewInstance();
setArea(0);
}

private void SaveFileMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(filePath))
{
Stream stream = new FileStream(filePath, FileMode.OpenOrCreate);
stream.Seek(0L, SeekOrigin.Begin);
stream.SetLength(0L);
AppData.getInstance().SaveToFile(stream);
stream.Close();
}
else
{
SaveAsMenuItem_OnClick(null, null);
}
}

private async void SaveAsMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
var ts = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
var topLevel = GetTopLevel(this);
var file = await topLevel.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = "保存配置文件",
SuggestedFileName = "Backup-GT12-" + ts + ".dat"
});
if (file is not null)
{
filePath = new Uri(file.Path.ToString()).LocalPath;
await using var stream = await file.OpenWriteAsync();
stream.Seek(0L, SeekOrigin.Begin);
stream.SetLength(0L);
AppData.getInstance().SaveToFile(stream);
stream.Close();
}
}

private void ExitMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
Close();
Environment.Exit(0);
}

private async void OpenFileMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
var topLevel = GetTopLevel(this);
var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = "打开备份",
AllowMultiple = false
});
if (files.Count > 0)
{
await using var stream = await files[0].OpenReadAsync();
AppData.CreatObjFromFile(stream);
setArea(0);
}
}
}
2 changes: 1 addition & 1 deletion Views/Shx8x00/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private async void saveAs_OnClick(object? sender, RoutedEventArgs e)
var file = await topLevel.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
{
Title = "保存配置文件",
SuggestedFileName = "Backup-" + ts + ".dat"
SuggestedFileName = "Backup-SHX8X00-" + ts + ".dat"
});
if (file is not null)
{
Expand Down

0 comments on commit 225b5b6

Please sign in to comment.