diff --git a/HFUTCourseSimulation/Course.cs b/HFUTCourseSimulation/Course.cs index e654a44..4cedf03 100644 --- a/HFUTCourseSimulation/Course.cs +++ b/HFUTCourseSimulation/Course.cs @@ -23,7 +23,7 @@ namespace HFUTCourseSimulation { public class CourseItem { public CourseItem() { - BkColor = Colors.LightBlue.ColorToInt(); + //BkColor = Colors.LightBlue.ColorToInt(); this.Schedule = new List(); } diff --git a/HFUTCourseSimulation/Dialog/EditCourse.xaml b/HFUTCourseSimulation/Dialog/EditCourse.xaml index 742b885..e597c96 100644 --- a/HFUTCourseSimulation/Dialog/EditCourse.xaml +++ b/HFUTCourseSimulation/Dialog/EditCourse.xaml @@ -21,22 +21,22 @@ - + - + - + - + diff --git a/HFUTCourseSimulation/Dialog/EditSchedule.xaml b/HFUTCourseSimulation/Dialog/EditSchedule.xaml index b6d2a59..0b1e655 100644 --- a/HFUTCourseSimulation/Dialog/EditSchedule.xaml +++ b/HFUTCourseSimulation/Dialog/EditSchedule.xaml @@ -21,17 +21,17 @@ - + - + - + diff --git a/HFUTCourseSimulation/HFUTCourseSimulation.csproj b/HFUTCourseSimulation/HFUTCourseSimulation.csproj index 39b2f89..a7f0a9e 100644 --- a/HFUTCourseSimulation/HFUTCourseSimulation.csproj +++ b/HFUTCourseSimulation/HFUTCourseSimulation.csproj @@ -85,6 +85,7 @@ + @@ -92,7 +93,8 @@ - + + Designer diff --git a/HFUTCourseSimulation/ImageExport.cs b/HFUTCourseSimulation/ImageExport.cs index db807b0..8c3bf65 100644 --- a/HFUTCourseSimulation/ImageExport.cs +++ b/HFUTCourseSimulation/ImageExport.cs @@ -89,7 +89,7 @@ namespace HFUTCourseSimulation { var cellx = courses.Start.week - 1; var celly = courses.Start.index - 1; //background - cellBrush = new SolidBrush(courses.BkColor.ConvertWPFColor()); + cellBrush = new SolidBrush(Util.ColorTrans.ToWpfColor(courses.BkColor)); graphics.FillRectangle(cellBrush, cellx * BodyCellWidth, celly * BodyCellHeight, BodyCellWidth, BodyCellHeight * courses.Span); //draw text @@ -113,21 +113,4 @@ namespace HFUTCourseSimulation { } - public static class WPFColorConvert { - public static System.Drawing.Color ConvertWPFColor(this System.Windows.Media.Color c) { - return System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B); - } - public static System.Windows.Media.Color ConvertWinformColor(this System.Drawing.Color c) { - return System.Windows.Media.Color.FromArgb(c.A, c.R, c.G, c.B); - } - public static int ColorToInt(this System.Windows.Media.Color c) { - return (c.R << 16) + (c.G << 8) + c.B; - } - public static System.Windows.Media.Color IntToColor(this int num) { - var cache = (num >> 16) << 16; - var cache2 = (num >> 8) << 8; - return System.Windows.Media.Color.FromArgb(255, (byte)(num >> 16), (byte)((cache2 - cache) >> 8), (byte)(num - cache2)); - } - } - } diff --git a/HFUTCourseSimulation/Kernel/OldUserData/V1.cs b/HFUTCourseSimulation/Kernel/OldUserData/V1.cs new file mode 100644 index 0000000..879b4ed --- /dev/null +++ b/HFUTCourseSimulation/Kernel/OldUserData/V1.cs @@ -0,0 +1,126 @@ +using HFUTCourseSimulation.Kernel.UserData; +using HFUTCourseSimulation.Util; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media; + +namespace HFUTCourseSimulation.Kernel.OldUserData.V1 { + + public class Semester { + public Semester() { + this.Courses = new List(); + StartDate = DateTime.Today; + WeekCount = 1; + } + + [JsonProperty("StartDate")] + [JsonConverter(typeof(CustomDateTimeConverter))] + public DateTime StartDate { get; set; } + [JsonProperty("WeekCount")] + public int WeekCount { get; set; } + [JsonProperty("Courses")] + public List Courses { get; set; } + + public UserData.Semester ToLatest() { + return new UserData.Semester() { + StartDate = StartDate, + WeekCount = WeekCount.ToString(), + // Other properties keep original value. + Courses = Courses.Select((item) => item.ToLatest()).ToList() + }; + } + } + + public class CourseItem { + public CourseItem() { + BkColor = Colors.LightBlue; + this.Schedule = new List(); + } + + [JsonProperty("Name")] + public string Name { get; set; } + [JsonProperty("Description")] + public string Description { get; set; } + [JsonProperty("BkColor")] + [JsonConverter(typeof(CustomColorConverter))] + public Color BkColor { get; set; } + [JsonProperty("Schedule")] + public List Schedule { get; set; } + + public UserData.Course ToLatest() { + return new UserData.Course() { + Name = Name, + Description = Description, + Color = new UserData.ColorPair() { + Foreground = System.Drawing.Color.White, + Background = ColorTrans.ToWpfColor(BkColor) + }, + Schedules = Schedule.Select((item) => item.ToLatest()).ToList() + }; + } + } + + public class ScheduleItem { + [JsonProperty("Week")] + public string Week { get; set; } + [JsonProperty("Day")] + public string Day { get; set; } + [JsonProperty("Index")] + public string Index { get; set; } + + public UserData.Schedule ToLatest() { + return new UserData.Schedule() { + Week = Week, + Day = Day, + Index = Index + }; + } + } + + internal class CustomDateTimeConverter : JsonConverter { + public override DateTime ReadJson(JsonReader reader, Type objectType, DateTime existingValue, bool hasExistingValue, JsonSerializer serializer) { + if (reader.TokenType == JsonToken.String) { + var value = reader.Value as string; + if (DateTime.TryParse(value, out DateTime rv)) { + return rv; + } else { + throw new JsonSerializationException($"given string can not be parsed as DateTime: {value}"); + } + } else { + throw new JsonSerializationException($"expect a string but got {reader.TokenType}"); + } + } + + public override void WriteJson(JsonWriter writer, DateTime value, JsonSerializer serializer) { + writer.WriteValue(value.ToString()); + } + } + + internal class CustomColorConverter : JsonConverter { + public override Color ReadJson(JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue, JsonSerializer serializer) { + if (reader.TokenType == JsonToken.Integer) { + var argb = Convert.ToInt32(reader.Value); + // Read color and force set its Alpha to zero. + var rv = Util.ColorTrans.ToWinformColor(argb); + rv.A = 255; + return rv; + } else { + throw new JsonSerializationException($"expect a integer but got {reader.TokenType}"); + } + } + + public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer) { + // Reset its alpha to zero when saving. + var copied = value; + copied.A = 255; + writer.WriteValue(Util.ColorTrans.ToInt(copied)); + } + } + + +} diff --git a/HFUTCourseSimulation/Kernel/UserData.cs b/HFUTCourseSimulation/Kernel/UserData.cs index 147f780..80af443 100644 --- a/HFUTCourseSimulation/Kernel/UserData.cs +++ b/HFUTCourseSimulation/Kernel/UserData.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Drawing; @@ -7,6 +6,7 @@ using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; +using HFUTCourseSimulation.Util; namespace HFUTCourseSimulation.Kernel.UserData { @@ -15,13 +15,13 @@ namespace HFUTCourseSimulation.Kernel.UserData { /// public class Semester { public Semester() { - startDate = DateTime.Today; - weekCount = 20.ToString(); - indexCount = 11.ToString(); - breakfastAt = 0.ToString(); - lunchAt = 4.ToString(); - dinnerAt = 8.ToString(); - courses = new List(); + StartDate = DateTime.Today; + WeekCount = 20.ToString(); + IndexCount = 11.ToString(); + BreakfastAt = 0.ToString(); + LunchAt = 4.ToString(); + DinnerAt = 8.ToString(); + Courses = new List(); } /// @@ -29,100 +29,100 @@ namespace HFUTCourseSimulation.Kernel.UserData { /// [JsonProperty("start_date")] [JsonConverter(typeof(CustomDateTimeConverter))] - public DateTime startDate { get; set; } + public DateTime StartDate { get; set; } /// /// 教学周个数 /// [JsonProperty("week_count")] - public string weekCount { get; set; } + public string WeekCount { get; set; } /// /// 每天课程的节次数 /// [JsonProperty("index_count")] - public string indexCount { get; set; } + public string IndexCount { get; set; } /// /// 早餐插入在第几节次后 /// [JsonProperty("breakfast_at")] - public string breakfastAt { get; set; } + public string BreakfastAt { get; set; } /// /// 午餐插入在第几节次后 /// [JsonProperty("lunch_at")] - public string lunchAt { get; set; } + public string LunchAt { get; set; } /// /// 晚餐插入在第几节次后 /// [JsonProperty("dinner_at")] - public string dinnerAt { get; set; } + public string DinnerAt { get; set; } /// /// 课程列表 /// [JsonProperty("courses")] - public List courses { get; set; } + public List Courses { get; set; } } public class Course { public Course() { - name = ""; - description = ""; - color = Util.ColorPreset.MdColors.Indigo; - schedules = new List(); + Name = ""; + Description = ""; + Color = ColorTrans.ToStorageColor(Util.ColorPreset.MdColors.Indigo); + Schedules = new List(); } /// /// 课程的名称 /// [JsonProperty("name")] - public string name { get; set; } + public string Name { get; set; } /// /// 课程的说明,例如教室位置,教师姓名等。 /// [JsonProperty("description")] - public string description { get; set; } + public string Description { get; set; } /// /// 课程的颜色 /// [JsonProperty("color")] - public ColorPair color { get; set; } + public ColorPair Color { get; set; } /// /// 课程的所有安排 /// [JsonProperty("schedules")] - public List schedules { get; set; } + public List Schedules { get; set; } } public class Schedule { public Schedule() { - week = ""; - day = ""; - index = ""; + Week = ""; + Day = ""; + Index = ""; } /// /// 安排在哪些周 /// [JsonProperty("week")] - public string week { get; set; } + public string Week { get; set; } /// /// 安排在周的星期几 /// [JsonProperty("day")] - public string day { get; set; } + public string Day { get; set; } /// /// 安排在这些日子的哪些节次 /// [JsonProperty("index")] - public string index { get; set; } + public string Index { get; set; } } /// - /// 代表一个颜色对,包括前景色和背景色 + /// 存储时所用的颜色对,与辅助类里的颜色对有所区别。 /// - public struct ColorPair { - public ColorPair(Color foreground, Color background) { - this.foreground = foreground; - this.background = background; + public class ColorPair { + public ColorPair() { + Foreground = Color.Black; + Background = Color.White; } /// @@ -130,13 +130,13 @@ namespace HFUTCourseSimulation.Kernel.UserData { /// [JsonProperty("foreground")] [JsonConverter(typeof(CustomColorConverter))] - public Color foreground { get; set; } + public Color Foreground { get; set; } /// /// 背景色 /// [JsonProperty("background")] [JsonConverter(typeof(CustomColorConverter))] - public Color background { get; set; } + public Color Background { get; set; } } internal class CustomDateTimeConverter : JsonConverter { diff --git a/HFUTCourseSimulation/MainWindow.xaml b/HFUTCourseSimulation/MainWindow.xaml index d0fae36..8802353 100644 --- a/HFUTCourseSimulation/MainWindow.xaml +++ b/HFUTCourseSimulation/MainWindow.xaml @@ -45,28 +45,28 @@ - + - + - + - + - + - + - + - + diff --git a/HFUTCourseSimulation/MainWindow.xaml.cs b/HFUTCourseSimulation/MainWindow.xaml.cs index b4e0a1a..ff31443 100644 --- a/HFUTCourseSimulation/MainWindow.xaml.cs +++ b/HFUTCourseSimulation/MainWindow.xaml.cs @@ -128,6 +128,21 @@ namespace HFUTCourseSimulation { } private void uiMenuOpenV1_Click(object sender, RoutedEventArgs e) { + // Fetch file path. + var filepath = Util.Win32Dialog.OpenSemester(); + if (filepath is null) return; + + // Try to read file. + try { + using (var fs = new StreamReader(filepath, Encoding.UTF8)) { + var obj = JsonConvert.DeserializeObject(fs.ReadToEnd()); + context.currentSemester = obj.ToLatest(); + context.currentFilePath = filepath; + } + } catch (Exception ex) { + Util.Win32Dialog.Error(ex.ToString(), "打开文件时出错"); + return; + } // Update UI and data source UpdateUiLayout(); diff --git a/HFUTCourseSimulation/SimulationCore.cs b/HFUTCourseSimulation/SimulationCore.cs index 99a0956..e3b9247 100644 --- a/HFUTCourseSimulation/SimulationCore.cs +++ b/HFUTCourseSimulation/SimulationCore.cs @@ -37,7 +37,7 @@ namespace HFUTCourseSimulation { if (this.itemDict.Keys.Contains(vectorCache)) { ErrorList.Add($"课程冲突:无法将{item.Name}安排到 {weekItem}周,星期{dayItem},第{indexItem}节。因为此处已被{itemDict[vectorCache].Name}占据"); } else { - itemDict.Add(vectorCache, new SimulationItem() { Name = item.Name, Desc = item.Description, BkColor = item.BkColor.IntToColor() }); + itemDict.Add(vectorCache, new SimulationItem() { Name = item.Name, Desc = item.Description, BkColor = Util.ColorTrans.ToWinformColor(item.BkColor) }); } } } diff --git a/HFUTCourseSimulation/Util/ColorPair.cs b/HFUTCourseSimulation/Util/ColorPair.cs new file mode 100644 index 0000000..eab371a --- /dev/null +++ b/HFUTCourseSimulation/Util/ColorPair.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HFUTCourseSimulation.Util { + + /// + /// 代表一个颜色对,包括前景色和背景色 + /// + public struct ColorPair { + public ColorPair(Color foreground, Color background) { + this.Foreground = foreground; + this.Background = background; + } + + /// + /// 前景色(文本的颜色) + /// + public Color Foreground { get; set; } + /// + /// 背景色 + /// + public Color Background { get; set; } + } + + namespace ColorPreset { + + /// + /// Material Design颜色合集 + /// + public static class MdColors { + private static Color LightText => Color.White; + private static Color DarkText => Color.FromArgb(33, 33, 33); + + public static ColorPair Red => new ColorPair(LightText, Color.FromArgb(244, 67, 54)); + public static ColorPair Pink => new ColorPair(LightText, Color.FromArgb(233, 30, 99)); + public static ColorPair Purple => new ColorPair(LightText, Color.FromArgb(156, 39, 176)); + public static ColorPair DeepPurple => new ColorPair(LightText, Color.FromArgb(103, 58, 183)); + public static ColorPair Indigo => new ColorPair(LightText, Color.FromArgb(63, 81, 181)); + public static ColorPair Blue => new ColorPair(LightText, Color.FromArgb(33, 150, 243)); + public static ColorPair LightBlue => new ColorPair(LightText, Color.FromArgb(3, 169, 244)); + public static ColorPair Cyan => new ColorPair(LightText, Color.FromArgb(0, 188, 212)); + public static ColorPair Teal => new ColorPair(LightText, Color.FromArgb(0, 150, 136)); + public static ColorPair Green => new ColorPair(LightText, Color.FromArgb(76, 175, 80)); + public static ColorPair LightGreen => new ColorPair(DarkText, Color.FromArgb(139, 195, 74)); + public static ColorPair Lime => new ColorPair(DarkText, Color.FromArgb(205, 220, 57)); + public static ColorPair Yellow => new ColorPair(DarkText, Color.FromArgb(255, 235, 59)); + public static ColorPair Amber => new ColorPair(DarkText, Color.FromArgb(255, 193, 7)); + public static ColorPair Orange => new ColorPair(DarkText, Color.FromArgb(255, 152, 0)); + public static ColorPair DeepOrange => new ColorPair(LightText, Color.FromArgb(255, 87, 34)); + public static ColorPair Brown => new ColorPair(LightText, Color.FromArgb(121, 85, 72)); + public static ColorPair Grey => new ColorPair(DarkText, Color.FromArgb(158, 158, 158)); + public static ColorPair BlueGrey => new ColorPair(LightText, Color.FromArgb(96, 125, 139)); + } + + } + +} diff --git a/HFUTCourseSimulation/Util/ColorPreset.cs b/HFUTCourseSimulation/Util/ColorPreset.cs deleted file mode 100644 index 9fe3b5d..0000000 --- a/HFUTCourseSimulation/Util/ColorPreset.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Drawing; -using HFUTCourseSimulation.Kernel.UserData; - -namespace HFUTCourseSimulation.Util.ColorPreset { - - /// - /// Material Design颜色合集 - /// - public static class MdColors { - private static Color LightText => Color.White; - private static Color DarkText => Color.FromArgb(33, 33, 33); - - public static ColorPair Red => new ColorPair(LightText, Color.FromArgb(244, 67, 54)); - public static ColorPair Pink => new ColorPair(LightText, Color.FromArgb(233, 30, 99)); - public static ColorPair Purple => new ColorPair(LightText, Color.FromArgb(156, 39, 176)); - public static ColorPair DeepPurple => new ColorPair(LightText, Color.FromArgb(103, 58, 183)); - public static ColorPair Indigo => new ColorPair(LightText, Color.FromArgb(63, 81, 181)); - public static ColorPair Blue => new ColorPair(LightText, Color.FromArgb(33, 150, 243)); - public static ColorPair LightBlue => new ColorPair(LightText, Color.FromArgb(3, 169, 244)); - public static ColorPair Cyan => new ColorPair(LightText, Color.FromArgb(0, 188, 212)); - public static ColorPair Teal => new ColorPair(LightText, Color.FromArgb(0, 150, 136)); - public static ColorPair Green => new ColorPair(LightText, Color.FromArgb(76, 175, 80)); - public static ColorPair LightGreen => new ColorPair(DarkText, Color.FromArgb(139, 195, 74)); - public static ColorPair Lime => new ColorPair(DarkText, Color.FromArgb(205, 220, 57)); - public static ColorPair Yellow => new ColorPair(DarkText, Color.FromArgb(255, 235, 59)); - public static ColorPair Amber => new ColorPair(DarkText, Color.FromArgb(255, 193, 7)); - public static ColorPair Orange => new ColorPair(DarkText, Color.FromArgb(255, 152, 0)); - public static ColorPair DeepOrange => new ColorPair(LightText, Color.FromArgb(255, 87, 34)); - public static ColorPair Brown => new ColorPair(LightText, Color.FromArgb(121, 85, 72)); - public static ColorPair Grey => new ColorPair(DarkText, Color.FromArgb(158, 158, 158)); - public static ColorPair BlueGrey => new ColorPair(LightText, Color.FromArgb(96, 125, 139)); - } - -} diff --git a/HFUTCourseSimulation/Util/ColorTrans.cs b/HFUTCourseSimulation/Util/ColorTrans.cs new file mode 100644 index 0000000..4ade058 --- /dev/null +++ b/HFUTCourseSimulation/Util/ColorTrans.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using WinformColor = System.Windows.Media.Color; +using WpfColor = System.Drawing.Color; + +using StorageColorPair = HFUTCourseSimulation.Kernel.UserData.ColorPair; +using RuntimeColorPair = HFUTCourseSimulation.Util.ColorPair; + +namespace HFUTCourseSimulation.Util { + + public static class ColorTrans { + + #region Color Transform + + public static WpfColor ToWpfColor(int argb) { + return WpfColor.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) { + return WinformColor.FromArgb(c.A, c.R, c.G, c.B); + } + + public static int ToInt(WpfColor c) { + return c.ToArgb(); + } + + public static int ToInt(WinformColor c) { + uint argb = 0; + argb |= c.A; argb <<= 8; + argb |= c.R; argb <<= 8; + argb |= c.G; argb <<= 8; + argb |= c.B; + return (int)argb; + } + + #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 + + } +}