refactor: write shit and has bugs

This commit is contained in:
2025-09-05 13:32:42 +08:00
parent ff5dc9b22b
commit 7470bc58b3
15 changed files with 272 additions and 164 deletions

View File

@ -7,7 +7,8 @@
xmlns:userdata="clr-namespace:HFUTCourseSimulation.Kernel.UserData" xmlns:userdata="clr-namespace:HFUTCourseSimulation.Kernel.UserData"
mc:Ignorable="d" mc:Ignorable="d"
d:DataContext="{d:DesignInstance userdata:Course}" d:DataContext="{d:DesignInstance userdata:Course}"
Title="编辑课程" Height="500" Width="600" WindowStyle="ToolWindow"> x:Name="uiMainWindow"
Title="编辑课程" Height="500" Width="600" WindowStyle="ToolWindow" Loaded="uiMainWindow_Loaded" Closed="uiMainWindow_Closed">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="auto"/> <RowDefinition Height="auto"/>
@ -21,16 +22,16 @@
<ScrollViewer> <ScrollViewer>
<StackPanel Orientation="Vertical" Margin="10,0,10,0"> <StackPanel Orientation="Vertical" Margin="10,0,10,0">
<TextBlock Text="课程名称" FontWeight="Bold" Margin="0,10,0,0"/> <TextBlock Text="课程名称" FontWeight="Bold" Margin="0,10,0,0"/>
<TextBox Text="{Binding Name, Mode=TwoWay}" Width="200" HorizontalAlignment="Left" Margin="0,5,0,0"/> <TextBox Text="{Binding Name, Mode=TwoWay}" Width="200" HorizontalAlignment="Left" Padding="5" Margin="0,5,0,0"/>
<TextBlock Text="课程颜色" FontWeight="Bold" Padding="5" Margin="0,10,0,0"/> <TextBlock Text="课程颜色" FontWeight="Bold" Padding="5" Margin="0,10,0,0"/>
<Border ToolTip="单击改变颜色" Width="200" HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="1" Cursor="Hand" Margin="0,5,0,0"> <Border ToolTip="单击改变颜色" Width="200" HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="1" Cursor="Hand" Margin="0,5,0,0">
<Border.Background> <Border.Background>
<SolidColorBrush Color="{Binding Color.Background, Mode=TwoWay}"/> <SolidColorBrush Color="{Binding Background, Mode=TwoWay}"/>
</Border.Background> </Border.Background>
<TextBlock Text="示例颜色" Margin="5"> <TextBlock Text="示例颜色" Margin="5">
<TextBlock.Foreground> <TextBlock.Foreground>
<SolidColorBrush Color="{Binding Color.Foreground, Mode=TwoWay}"/> <SolidColorBrush Color="{Binding Foreground, Mode=TwoWay}"/>
</TextBlock.Foreground> </TextBlock.Foreground>
</TextBlock> </TextBlock>
</Border> </Border>
@ -41,7 +42,46 @@
</ScrollViewer> </ScrollViewer>
</TabItem> </TabItem>
<TabItem Header="课程安排" Padding="5"> <TabItem Header="课程安排" Padding="5">
<ListBox x:Name="uiSchedulesList" ItemsSource="{Binding Schedules, Mode=OneWay}" Margin="10" MouseDoubleClick="uiSchedulesList_MouseDoubleClick">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid d:DataContext="{d:DesignInstance userdata:Schedule}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="#607D8B" BorderBrush="Black" BorderThickness="1" CornerRadius="2" Margin="5" VerticalAlignment="Top">
<TextBlock Text="排" Foreground="White" Margin="5"/>
</Border>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="周次:" Grid.Column="0" Grid.Row="0" FontWeight="Bold"/>
<TextBlock Text="星期:" Grid.Column="0" Grid.Row="1" FontWeight="Bold"/>
<TextBlock Text="节次:" Grid.Column="0" Grid.Row="2" FontWeight="Bold"/>
<TextBlock Text="{Binding Week, Mode=OneWay}" Grid.Column="1" Grid.Row="0"/>
<TextBlock Text="{Binding Day, Mode=OneWay}" Grid.Column="1" Grid.Row="1"/>
<TextBlock Text="{Binding Index, Mode=OneWay}" Grid.Column="1" Grid.Row="2"/>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem x:Name="uiCtxMenuNewSchedule" Header="插入新安排" Click="uiCtxMenuNewSchedule_Click"/>
<MenuItem x:Name="uiCtxMenuEditSchedule" Header="编辑选中" Click="uiCtxMenuEditSchedule_Click"/>
<MenuItem x:Name="uiCtxMenuDeleteSchedule" Header="删除选中" Click="uiCtxMenuDeleteSchedule_Click"/>
<MenuItem x:Name="uiCtxMenuClearSchedule" Header="清空全部" Click="uiCtxMenuClearSchedule_Click"/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
</TabItem> </TabItem>
</TabControl> </TabControl>

View File

@ -19,7 +19,72 @@ namespace HFUTCourseSimulation.Dialog {
public partial class EditCourse : Window { public partial class EditCourse : Window {
public EditCourse() { public EditCourse() {
InitializeComponent(); InitializeComponent();
this.DataContext = Kernel.Context.Instance.currentCourse; }
public Kernel.UserData.Course CurrentCourse { get; set; }
private void uiMainWindow_Loaded(object sender, RoutedEventArgs e) {
this.DataContext = CurrentCourse;
}
private void uiMainWindow_Closed(object sender, EventArgs e) {
this.DataContext = null;
}
private void uiSchedulesList_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
// It just an alias to edit
uiCtxMenuEditSchedule_Click(sender, e);
}
private void uiCtxMenuNewSchedule_Click(object sender, RoutedEventArgs e) {
// Create new item and order user edit it
var item = new Kernel.UserData.Schedule();
var dialog = new EditSchedule();
dialog.CurrentSchedule = item;
dialog.ShowDialog();
// Then insert or append it into list
var idx = uiSchedulesList.SelectedIndex;
if (idx < 0) {
// No selection, append.
CurrentCourse.Schedules.Add(item);
} else {
// Has selection, insert
CurrentCourse.Schedules.Insert(idx, item);
} }
} }
private void uiCtxMenuEditSchedule_Click(object sender, RoutedEventArgs e) {
var idx = uiSchedulesList.SelectedIndex;
if (idx < 0) return;
var dialog = new EditSchedule();
dialog.CurrentSchedule = CurrentCourse.Schedules[idx];
dialog.ShowDialog();
}
private void uiCtxMenuDeleteSchedule_Click(object sender, RoutedEventArgs e) {
// Check whether there is item can be deleted
var idx = uiSchedulesList.SelectedIndex;
if (idx < 0) return;
// Order a confirm
var rv = Util.Win32Dialog.Confirm("确认删除选中的课程安排吗?该操作不可撤销!", "确认删除");
if (!rv) return;
// Remove it
CurrentCourse.Schedules.RemoveAt(idx);
}
private void uiCtxMenuClearSchedule_Click(object sender, RoutedEventArgs e) {
// Order a confirm
var rv = Util.Win32Dialog.Confirm("确认删除所有课程安排吗?该操作不可撤销!", "确认清空");
if (!rv) return;
// Clear all schedules
CurrentCourse.Schedules.Clear();
}
}
} }

View File

@ -7,7 +7,8 @@
xmlns:userdata="clr-namespace:HFUTCourseSimulation.Kernel.UserData" xmlns:userdata="clr-namespace:HFUTCourseSimulation.Kernel.UserData"
mc:Ignorable="d" mc:Ignorable="d"
d:DataContext="{d:DesignInstance userdata:Schedule}" d:DataContext="{d:DesignInstance userdata:Schedule}"
Title="编辑课程安排" Height="450" Width="600" WindowStyle="ToolWindow"> x:Name="uiMainWindow"
Title="编辑课程安排" Height="450" Width="600" WindowStyle="ToolWindow" Loaded="uiMainWindow_Loaded" Closed="uiMainWindow_Closed">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="auto"/> <RowDefinition Height="auto"/>

View File

@ -19,9 +19,17 @@ namespace HFUTCourseSimulation.Dialog {
public partial class EditSchedule : Window { public partial class EditSchedule : Window {
public EditSchedule() { public EditSchedule() {
InitializeComponent(); InitializeComponent();
this.DataContext = Kernel.Context.Instance.currentSchedule; }
}
public Kernel.UserData.Schedule CurrentSchedule { get; set; }
private void uiMainWindow_Loaded(object sender, RoutedEventArgs e) {
this.DataContext = CurrentSchedule;
}
private void uiMainWindow_Closed(object sender, EventArgs e) {
this.DataContext = null;
}
} }
} }

View File

@ -83,7 +83,6 @@
</Compile> </Compile>
<Compile Include="General.cs" /> <Compile Include="General.cs" />
<Compile Include="ImageExport.cs" /> <Compile Include="ImageExport.cs" />
<Compile Include="Kernel\Context.cs" />
<Compile Include="Kernel\MsgRecorder.cs" /> <Compile Include="Kernel\MsgRecorder.cs" />
<Compile Include="Kernel\OldUserData\V1.cs" /> <Compile Include="Kernel\OldUserData\V1.cs" />
<Compile Include="Kernel\SimData.cs" /> <Compile Include="Kernel\SimData.cs" />

View File

@ -89,7 +89,7 @@ namespace HFUTCourseSimulation {
var cellx = courses.Start.week - 1; var cellx = courses.Start.week - 1;
var celly = courses.Start.index - 1; var celly = courses.Start.index - 1;
//background //background
cellBrush = new SolidBrush(Util.ColorTrans.ToWpfColor(courses.BkColor)); cellBrush = new SolidBrush(Util.ColorTrans.ToWinformColor(courses.BkColor));
graphics.FillRectangle(cellBrush, cellx * BodyCellWidth, celly * BodyCellHeight, BodyCellWidth, BodyCellHeight * courses.Span); graphics.FillRectangle(cellBrush, cellx * BodyCellWidth, celly * BodyCellHeight, BodyCellWidth, BodyCellHeight * courses.Span);
//draw text //draw text

View File

@ -1,24 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HFUTCourseSimulation.Kernel {
public class Context {
public static readonly Context Instance = new Context();
private Context() {
currentSemester = null;
currentCourse = null;
currentSchedule = null;
}
public UserData.Semester currentSemester;
public UserData.Course currentCourse;
public UserData.Schedule currentSchedule;
public UserData.ColorPair currentColor;
public string currentFilePath;
}
}

View File

@ -1,6 +1,4 @@
using HFUTCourseSimulation.Kernel.UserData; using Newtonsoft.Json;
using HFUTCourseSimulation.Util;
using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
@ -56,10 +54,8 @@ namespace HFUTCourseSimulation.Kernel.OldUserData.V1 {
return new UserData.Course() { return new UserData.Course() {
Name = Name, Name = Name,
Description = Description, Description = Description,
Color = new UserData.ColorPair() { Foreground = Colors.Black,
Foreground = System.Drawing.Color.White, Background = BkColor,
Background = ColorTrans.ToWpfColor(BkColor)
},
Schedules = Schedule.Select((item) => item.ToLatest()).ToList() Schedules = Schedule.Select((item) => item.ToLatest()).ToList()
}; };
} }
@ -106,7 +102,7 @@ namespace HFUTCourseSimulation.Kernel.OldUserData.V1 {
if (reader.TokenType == JsonToken.Integer) { if (reader.TokenType == JsonToken.Integer) {
var argb = Convert.ToInt32(reader.Value); var argb = Convert.ToInt32(reader.Value);
// Read color and force set its Alpha to zero. // Read color and force set its Alpha to zero.
var rv = Util.ColorTrans.ToWinformColor(argb); var rv = Util.ColorTrans.ToWpfColor(argb);
rv.A = 255; rv.A = 255;
return rv; return rv;
} else { } else {

View File

@ -105,7 +105,7 @@ namespace HFUTCourseSimulation.Kernel.SimData {
/// <summary> /// <summary>
/// 课程的颜色 /// 课程的颜色
/// </summary> /// </summary>
public readonly UserData.ColorPair color; public readonly Util.ColorPair color;
/// <summary> /// <summary>
/// 课程的起始节次。 /// 课程的起始节次。
/// 该类保证该值位于1到indexCount之间含首尾 /// 该类保证该值位于1到indexCount之间含首尾

View File

@ -1,11 +1,11 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Media;
using HFUTCourseSimulation.Util; using HFUTCourseSimulation.Util;
namespace HFUTCourseSimulation.Kernel.UserData { namespace HFUTCourseSimulation.Kernel.UserData {
@ -66,7 +66,9 @@ namespace HFUTCourseSimulation.Kernel.UserData {
public Course() { public Course() {
Name = ""; Name = "";
Description = ""; Description = "";
Color = ColorTrans.ToStorageColor(Util.ColorPreset.MdColors.Indigo); var color = Util.ColorPreset.MdColors.Indigo;
Foreground = color.Foreground;
Background = color.Background;
Schedules = new List<Schedule>(); Schedules = new List<Schedule>();
} }
@ -81,10 +83,17 @@ namespace HFUTCourseSimulation.Kernel.UserData {
[JsonProperty("description")] [JsonProperty("description")]
public string Description { get; set; } public string Description { get; set; }
/// <summary> /// <summary>
/// 课程的颜色 /// 课程的前景色(文本)
/// </summary> /// </summary>
[JsonProperty("color")] [JsonProperty("foreground")]
public ColorPair Color { get; set; } [JsonConverter(typeof(CustomColorConverter))]
public Color Foreground { get; set; }
/// <summary>
/// 课程的背景色(背景)
/// </summary>
[JsonProperty("background")]
[JsonConverter(typeof(CustomColorConverter))]
public Color Background { get; set; }
/// <summary> /// <summary>
/// 课程的所有安排 /// 课程的所有安排
/// </summary> /// </summary>
@ -116,29 +125,6 @@ namespace HFUTCourseSimulation.Kernel.UserData {
public string Index { get; set; } public string Index { get; set; }
} }
/// <summary>
/// 存储时所用的颜色对,与辅助类里的颜色对有所区别。
/// </summary>
public class ColorPair {
public ColorPair() {
Foreground = Color.Black;
Background = Color.White;
}
/// <summary>
/// 前景色(文本的颜色)
/// </summary>
[JsonProperty("foreground")]
[JsonConverter(typeof(CustomColorConverter))]
public Color Foreground { get; set; }
/// <summary>
/// 背景色
/// </summary>
[JsonProperty("background")]
[JsonConverter(typeof(CustomColorConverter))]
public Color Background { get; set; }
}
internal class CustomDateTimeConverter : JsonConverter<DateTime> { internal class CustomDateTimeConverter : JsonConverter<DateTime> {
private static readonly string DatetimeFormat = "yyyy-MM-dd"; private static readonly string DatetimeFormat = "yyyy-MM-dd";
@ -164,14 +150,14 @@ namespace HFUTCourseSimulation.Kernel.UserData {
public override Color ReadJson(JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue, JsonSerializer serializer) { public override Color ReadJson(JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue, JsonSerializer serializer) {
if (reader.TokenType == JsonToken.Integer) { if (reader.TokenType == JsonToken.Integer) {
var argb = Convert.ToInt32(reader.Value); var argb = Convert.ToInt32(reader.Value);
return Color.FromArgb(argb); return ColorTrans.ToWpfColor(argb);
} else { } else {
throw new JsonSerializationException($"expect a integer but got {reader.TokenType}"); throw new JsonSerializationException($"expect a integer but got {reader.TokenType}");
} }
} }
public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer) { public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer) {
writer.WriteValue(value.ToArgb()); writer.WriteValue(ColorTrans.ToInt(value));
} }
} }

View File

@ -62,11 +62,29 @@
</ScrollViewer> </ScrollViewer>
</TabItem> </TabItem>
<TabItem Header="课程设置" Padding="5"> <TabItem Header="课程设置" Padding="5">
<ListBox x:Name="uiCoursesList" ItemsSource="{Binding Courses}" Margin="10" MouseDoubleClick="uiCoursesList_MouseDoubleClick"> <ListBox x:Name="uiCoursesList" ItemsSource="{Binding Courses, Mode=OneWay}" Margin="10" MouseDoubleClick="uiCoursesList_MouseDoubleClick">
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate> <DataTemplate>
<Grid d:DataContext="{d:DesignInstance userdata:Course}"> <Grid d:DataContext="{d:DesignInstance userdata:Course}">
<TextBlock Text="{Binding Name, Mode=OneWay}"/> <Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="Black" BorderThickness="1" CornerRadius="2" Margin="5" VerticalAlignment="Top">
<Border.Background>
<SolidColorBrush Color="{Binding Background, Mode=OneWay}"/>
</Border.Background>
<TextBlock Text="课" Margin="5">
<TextBlock.Foreground>
<SolidColorBrush Color="{Binding Foreground, Mode=OneWay}"/>
</TextBlock.Foreground>
</TextBlock>
</Border>
<StackPanel Orientation="Vertical" Grid.Column="1">
<TextBlock Text="{Binding Name, Mode=OneWay}" FontWeight="Bold"/>
<TextBlock Text="这是一段注释"/>
<TextBlock Text="共计N个课程安排"/>
</StackPanel>
</Grid> </Grid>
</DataTemplate> </DataTemplate>
</ListBox.ItemTemplate> </ListBox.ItemTemplate>
@ -75,6 +93,7 @@
<MenuItem x:Name="uiCtxMenuNewCourse" Header="插入新课程" Click="uiCtxMenuNewCourse_Click"/> <MenuItem x:Name="uiCtxMenuNewCourse" Header="插入新课程" Click="uiCtxMenuNewCourse_Click"/>
<MenuItem x:Name="uiCtxMenuEditCourse" Header="编辑选中" Click="uiCtxMenuEditCourse_Click"/> <MenuItem x:Name="uiCtxMenuEditCourse" Header="编辑选中" Click="uiCtxMenuEditCourse_Click"/>
<MenuItem x:Name="uiCtxMenuDeleteCourse" Header="删除选中" Click="uiCtxMenuDeleteCourse_Click"/> <MenuItem x:Name="uiCtxMenuDeleteCourse" Header="删除选中" Click="uiCtxMenuDeleteCourse_Click"/>
<MenuItem x:Name="uiCtxMenuClearCourse" Header="清空全部" Click="uiCtxMenuClearCourse_Click"/>
</ContextMenu> </ContextMenu>
</ListBox.ContextMenu> </ListBox.ContextMenu>
</ListBox> </ListBox>

View File

@ -1,7 +1,9 @@
using System; using HFUTCourseSimulation.Dialog;
using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
@ -13,7 +15,6 @@ using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using Newtonsoft.Json;
namespace HFUTCourseSimulation { namespace HFUTCourseSimulation {
/// <summary> /// <summary>
@ -22,20 +23,20 @@ namespace HFUTCourseSimulation {
public partial class MainWindow : Window { public partial class MainWindow : Window {
public MainWindow() { public MainWindow() {
InitializeComponent(); InitializeComponent();
this.context = Kernel.Context.Instance;
UpdateUiLayout(); UpdateUiLayout();
} }
#region Context and Assistant Functions #region Context and Assistant Functions
private readonly Kernel.Context context; private Kernel.UserData.Semester CurrentSemester { get; set; }
private string CurrentFilePath { get; set; }
/// <summary> /// <summary>
/// 返回当前是否有文件被加载。 /// 返回当前是否有文件被加载。
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private bool IsFileLoaded() { private bool IsFileLoaded() {
return !(context.currentSemester is null); return !(CurrentSemester is null);
} }
/// <summary> /// <summary>
@ -43,7 +44,7 @@ namespace HFUTCourseSimulation {
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private bool HasAssocFile() { private bool HasAssocFile() {
return !(context.currentFilePath is null); return !(CurrentFilePath is null);
} }
/// <summary> /// <summary>
@ -75,7 +76,7 @@ namespace HFUTCourseSimulation {
// Window Caption // Window Caption
if (isFileLoaded) { if (isFileLoaded) {
if (hasAssocFile) { if (hasAssocFile) {
this.Title = $"HFUT课表模拟 - {context.currentFilePath}"; this.Title = $"HFUT课表模拟 - {CurrentFilePath}";
} else { } else {
this.Title = "HFUT课表模拟 - [未命名]"; this.Title = "HFUT课表模拟 - [未命名]";
} }
@ -89,7 +90,7 @@ namespace HFUTCourseSimulation {
/// </summary> /// </summary>
private void UpdateDataSource() { private void UpdateDataSource() {
if (this.IsFileLoaded()) { if (this.IsFileLoaded()) {
this.DataContext = context.currentSemester; this.DataContext = CurrentSemester;
} else { } else {
this.DataContext = null; this.DataContext = null;
} }
@ -100,8 +101,8 @@ namespace HFUTCourseSimulation {
#region Menu Handlers #region Menu Handlers
private void uiMenuNew_Click(object sender, RoutedEventArgs e) { private void uiMenuNew_Click(object sender, RoutedEventArgs e) {
context.currentSemester = new Kernel.UserData.Semester(); CurrentSemester = new Kernel.UserData.Semester();
context.currentFilePath = null; CurrentFilePath = null;
UpdateUiLayout(); UpdateUiLayout();
UpdateDataSource(); UpdateDataSource();
} }
@ -114,8 +115,8 @@ namespace HFUTCourseSimulation {
// Try to read file. // Try to read file.
try { try {
using (var fs = new StreamReader(filepath, Encoding.UTF8)) { using (var fs = new StreamReader(filepath, Encoding.UTF8)) {
context.currentSemester = JsonConvert.DeserializeObject<Kernel.UserData.Semester>(fs.ReadToEnd()); CurrentSemester = JsonConvert.DeserializeObject<Kernel.UserData.Semester>(fs.ReadToEnd());
context.currentFilePath = filepath; CurrentFilePath = filepath;
} }
} catch (Exception ex) { } catch (Exception ex) {
Util.Win32Dialog.Error(ex.ToString(), "打开文件时出错"); Util.Win32Dialog.Error(ex.ToString(), "打开文件时出错");
@ -136,8 +137,8 @@ namespace HFUTCourseSimulation {
try { try {
using (var fs = new StreamReader(filepath, Encoding.UTF8)) { using (var fs = new StreamReader(filepath, Encoding.UTF8)) {
var obj = JsonConvert.DeserializeObject<Kernel.OldUserData.V1.Semester>(fs.ReadToEnd()); var obj = JsonConvert.DeserializeObject<Kernel.OldUserData.V1.Semester>(fs.ReadToEnd());
context.currentSemester = obj.ToLatest(); CurrentSemester = obj.ToLatest();
context.currentFilePath = filepath; CurrentFilePath = filepath;
} }
} catch (Exception ex) { } catch (Exception ex) {
Util.Win32Dialog.Error(ex.ToString(), "打开文件时出错"); Util.Win32Dialog.Error(ex.ToString(), "打开文件时出错");
@ -152,16 +153,16 @@ namespace HFUTCourseSimulation {
private void uiMenuSave_Click(object sender, RoutedEventArgs e) { private void uiMenuSave_Click(object sender, RoutedEventArgs e) {
// Check whether there is associated file. // Check whether there is associated file.
// If it not, order user select one. // If it not, order user select one.
if (context.currentFilePath is null) { if (CurrentFilePath is null) {
var filepath = Util.Win32Dialog.SaveSemester(); var filepath = Util.Win32Dialog.SaveSemester();
if (filepath is null) return; if (filepath is null) return;
context.currentFilePath = filepath; CurrentFilePath = filepath;
} }
// Try to save file // Try to save file
try { try {
using (var fs = new StreamWriter(context.currentFilePath, false, Encoding.UTF8)) { using (var fs = new StreamWriter(CurrentFilePath, false, Encoding.UTF8)) {
fs.Write(JsonConvert.SerializeObject(context.currentSemester)); fs.Write(JsonConvert.SerializeObject(CurrentSemester));
} }
} catch (Exception ex) { } catch (Exception ex) {
Util.Win32Dialog.Error(ex.ToString(), "保存文件时出错"); Util.Win32Dialog.Error(ex.ToString(), "保存文件时出错");
@ -178,12 +179,12 @@ namespace HFUTCourseSimulation {
if (filepath is null) return; if (filepath is null) return;
// Update it to current path for following editing. // Update it to current path for following editing.
context.currentFilePath = filepath; CurrentFilePath = filepath;
// Try to save file // Try to save file
try { try {
using (var fs = new StreamWriter(context.currentFilePath, false, Encoding.UTF8)) { using (var fs = new StreamWriter(CurrentFilePath, false, Encoding.UTF8)) {
fs.Write(JsonConvert.SerializeObject(context.currentSemester)); fs.Write(JsonConvert.SerializeObject(CurrentSemester));
} }
} catch (Exception ex) { } catch (Exception ex) {
Util.Win32Dialog.Error(ex.ToString(), "保存文件时出错"); Util.Win32Dialog.Error(ex.ToString(), "保存文件时出错");
@ -200,8 +201,8 @@ namespace HFUTCourseSimulation {
if (!rv) return; if (!rv) return;
// Clear semester and assoc file // Clear semester and assoc file
context.currentSemester = null; CurrentSemester = null;
context.currentFilePath = null; CurrentFilePath = null;
// Update UI and data source // Update UI and data source
UpdateUiLayout(); UpdateUiLayout();
@ -260,25 +261,57 @@ namespace HFUTCourseSimulation {
#region Main Area Handlers #region Main Area Handlers
private void uiCoursesList_MouseDoubleClick(object sender, MouseButtonEventArgs e) { private void uiCoursesList_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
//if (this.uiCoursesList.SelectedIndex < 0) { // It just an alias to edit
// MessageBox.Show("未选择任何项", "错误", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK); uiCtxMenuEditCourse_Click(sender, e);
// return;
//}
//General.CourseCache = General.GeneralSheet.Courses[uiCoursesList.SelectedIndex].Clone();
//SyncCourseData();
} }
private void uiCtxMenuNewCourse_Click(object sender, RoutedEventArgs e) { private void uiCtxMenuNewCourse_Click(object sender, RoutedEventArgs e) {
// Create new item and order user edit it
var item = new Kernel.UserData.Course();
var dialog = new Dialog.EditCourse();
dialog.CurrentCourse = item;
dialog.ShowDialog();
// Then insert or append it into list
var idx = uiCoursesList.SelectedIndex;
if (idx < 0) {
// No selection, append.
CurrentSemester.Courses.Add(item);
} else {
// Has selection, insert
CurrentSemester.Courses.Insert(idx, item);
}
} }
private void uiCtxMenuEditCourse_Click(object sender, RoutedEventArgs e) { private void uiCtxMenuEditCourse_Click(object sender, RoutedEventArgs e) {
var idx = uiCoursesList.SelectedIndex;
if (idx < 0) return;
var dialog = new Dialog.EditCourse();
dialog.CurrentCourse = CurrentSemester.Courses[idx];
dialog.ShowDialog();
} }
private void uiCtxMenuDeleteCourse_Click(object sender, RoutedEventArgs e) { private void uiCtxMenuDeleteCourse_Click(object sender, RoutedEventArgs e) {
// Check whether there is item can be deleted
var idx = uiCoursesList.SelectedIndex;
if (idx < 0) return;
// Order a confirm
var rv = Util.Win32Dialog.Confirm("确认删除选中的课程吗?该操作不可撤销!", "确认删除");
if (!rv) return;
// Remove it
CurrentSemester.Courses.RemoveAt(idx);
}
private void uiCtxMenuClearCourse_Click(object sender, RoutedEventArgs e) {
// Order a confirm
var rv = Util.Win32Dialog.Confirm("确认删除所有课程吗?该操作不可撤销!", "确认清空");
if (!rv) return;
// Clear all schedules
CurrentSemester.Courses.Clear();
} }
#endregion #endregion

View File

@ -37,7 +37,7 @@ namespace HFUTCourseSimulation {
if (this.itemDict.Keys.Contains(vectorCache)) { if (this.itemDict.Keys.Contains(vectorCache)) {
ErrorList.Add($"课程冲突:无法将{item.Name}安排到 {weekItem}周,星期{dayItem},第{indexItem}节。因为此处已被{itemDict[vectorCache].Name}占据"); ErrorList.Add($"课程冲突:无法将{item.Name}安排到 {weekItem}周,星期{dayItem},第{indexItem}节。因为此处已被{itemDict[vectorCache].Name}占据");
} else { } else {
itemDict.Add(vectorCache, new SimulationItem() { Name = item.Name, Desc = item.Description, BkColor = Util.ColorTrans.ToWinformColor(item.BkColor) }); itemDict.Add(vectorCache, new SimulationItem() { Name = item.Name, Desc = item.Description, BkColor = Util.ColorTrans.ToWpfColor(item.BkColor) });
} }
} }
} }

View File

@ -1,9 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Media;
namespace HFUTCourseSimulation.Util { namespace HFUTCourseSimulation.Util {
@ -32,28 +32,28 @@ namespace HFUTCourseSimulation.Util {
/// Material Design颜色合集 /// Material Design颜色合集
/// </summary> /// </summary>
public static class MdColors { public static class MdColors {
private static Color LightText => Color.White; private static Color LightText => Colors.White;
private static Color DarkText => Color.FromArgb(33, 33, 33); private static Color DarkText => Color.FromRgb(33, 33, 33);
public static ColorPair Red => new ColorPair(LightText, Color.FromArgb(244, 67, 54)); public static ColorPair Red => new ColorPair(LightText, Color.FromRgb(244, 67, 54));
public static ColorPair Pink => new ColorPair(LightText, Color.FromArgb(233, 30, 99)); public static ColorPair Pink => new ColorPair(LightText, Color.FromRgb(233, 30, 99));
public static ColorPair Purple => new ColorPair(LightText, Color.FromArgb(156, 39, 176)); public static ColorPair Purple => new ColorPair(LightText, Color.FromRgb(156, 39, 176));
public static ColorPair DeepPurple => new ColorPair(LightText, Color.FromArgb(103, 58, 183)); public static ColorPair DeepPurple => new ColorPair(LightText, Color.FromRgb(103, 58, 183));
public static ColorPair Indigo => new ColorPair(LightText, Color.FromArgb(63, 81, 181)); public static ColorPair Indigo => new ColorPair(LightText, Color.FromRgb(63, 81, 181));
public static ColorPair Blue => new ColorPair(LightText, Color.FromArgb(33, 150, 243)); public static ColorPair Blue => new ColorPair(LightText, Color.FromRgb(33, 150, 243));
public static ColorPair LightBlue => new ColorPair(LightText, Color.FromArgb(3, 169, 244)); public static ColorPair LightBlue => new ColorPair(LightText, Color.FromRgb(3, 169, 244));
public static ColorPair Cyan => new ColorPair(LightText, Color.FromArgb(0, 188, 212)); public static ColorPair Cyan => new ColorPair(LightText, Color.FromRgb(0, 188, 212));
public static ColorPair Teal => new ColorPair(LightText, Color.FromArgb(0, 150, 136)); public static ColorPair Teal => new ColorPair(LightText, Color.FromRgb(0, 150, 136));
public static ColorPair Green => new ColorPair(LightText, Color.FromArgb(76, 175, 80)); public static ColorPair Green => new ColorPair(LightText, Color.FromRgb(76, 175, 80));
public static ColorPair LightGreen => new ColorPair(DarkText, Color.FromArgb(139, 195, 74)); public static ColorPair LightGreen => new ColorPair(DarkText, Color.FromRgb(139, 195, 74));
public static ColorPair Lime => new ColorPair(DarkText, Color.FromArgb(205, 220, 57)); public static ColorPair Lime => new ColorPair(DarkText, Color.FromRgb(205, 220, 57));
public static ColorPair Yellow => new ColorPair(DarkText, Color.FromArgb(255, 235, 59)); public static ColorPair Yellow => new ColorPair(DarkText, Color.FromRgb(255, 235, 59));
public static ColorPair Amber => new ColorPair(DarkText, Color.FromArgb(255, 193, 7)); public static ColorPair Amber => new ColorPair(DarkText, Color.FromRgb(255, 193, 7));
public static ColorPair Orange => new ColorPair(DarkText, Color.FromArgb(255, 152, 0)); public static ColorPair Orange => new ColorPair(DarkText, Color.FromRgb(255, 152, 0));
public static ColorPair DeepOrange => new ColorPair(LightText, Color.FromArgb(255, 87, 34)); public static ColorPair DeepOrange => new ColorPair(LightText, Color.FromRgb(255, 87, 34));
public static ColorPair Brown => new ColorPair(LightText, Color.FromArgb(121, 85, 72)); public static ColorPair Brown => new ColorPair(LightText, Color.FromRgb(121, 85, 72));
public static ColorPair Grey => new ColorPair(DarkText, Color.FromArgb(158, 158, 158)); public static ColorPair Grey => new ColorPair(DarkText, Color.FromRgb(158, 158, 158));
public static ColorPair BlueGrey => new ColorPair(LightText, Color.FromArgb(96, 125, 139)); public static ColorPair BlueGrey => new ColorPair(LightText, Color.FromRgb(96, 125, 139));
} }
} }

View File

@ -4,11 +4,8 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using WinformColor = System.Windows.Media.Color; using WpfColor = System.Windows.Media.Color;
using WpfColor = System.Drawing.Color; using WinformColor = System.Drawing.Color;
using StorageColorPair = HFUTCourseSimulation.Kernel.UserData.ColorPair;
using RuntimeColorPair = HFUTCourseSimulation.Util.ColorPair;
namespace HFUTCourseSimulation.Util { namespace HFUTCourseSimulation.Util {
@ -16,32 +13,32 @@ namespace HFUTCourseSimulation.Util {
#region Color Transform #region Color Transform
public static WpfColor ToWpfColor(int argb) { public static WinformColor ToWinformColor(int argb) {
return WpfColor.FromArgb(argb); return WinformColor.FromArgb(argb);
}
public static WpfColor ToWpfColor(WinformColor c) {
return WpfColor.FromArgb(c.A, c.R, c.G, c.B);
}
public static WinformColor ToWinformColor(int _argb) {
var argb = (uint)_argb;
var a = (argb & 0xff000000) >> 24;
var r = (argb & 0x00ff0000) >> 16;
var g = (argb & 0x0000ff00) >> 8;
var b = (argb & 0x000000ff);
return WinformColor.FromArgb((byte)a, (byte)r, (byte)g, (byte)b);
} }
public static WinformColor ToWinformColor(WpfColor c) { public static WinformColor ToWinformColor(WpfColor c) {
return WinformColor.FromArgb(c.A, c.R, c.G, c.B); return WinformColor.FromArgb(c.A, c.R, c.G, c.B);
} }
public static int ToInt(WpfColor c) { public static WpfColor ToWpfColor(int _argb) {
return c.ToArgb(); var argb = (uint)_argb;
var a = (argb & 0xff000000) >> 24;
var r = (argb & 0x00ff0000) >> 16;
var g = (argb & 0x0000ff00) >> 8;
var b = (argb & 0x000000ff);
return WpfColor.FromArgb((byte)a, (byte)r, (byte)g, (byte)b);
}
public static WpfColor ToWpfColor(WinformColor c) {
return WpfColor.FromArgb(c.A, c.R, c.G, c.B);
} }
public static int ToInt(WinformColor c) { public static int ToInt(WinformColor c) {
return c.ToArgb();
}
public static int ToInt(WpfColor c) {
uint argb = 0; uint argb = 0;
argb |= c.A; argb <<= 8; argb |= c.A; argb <<= 8;
argb |= c.R; argb <<= 8; argb |= c.R; argb <<= 8;
@ -52,17 +49,5 @@ namespace HFUTCourseSimulation.Util {
#endregion #endregion
#region Color Pair Transform
public static StorageColorPair ToStorageColor(RuntimeColorPair cp) {
return new StorageColorPair() { Foreground = cp.Foreground, Background = cp.Background };
}
public static RuntimeColorPair ToRuntimeColor(StorageColorPair cp) {
return new RuntimeColorPair(cp.Foreground, cp.Background);
}
#endregion
} }
} }