-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
190 lines (172 loc) · 8.51 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace LaTeX_Homework_Spawner {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
private Boolean configLoaded;
private JObject config;
private string configFailReason;
private const string HeaderPrefix = "Homework Spawner 2.0 by Ethan Levine.";
private const string HeaderSuffix = "View README.txt for help.";
public MainWindow() {
InitializeComponent();
string configFile = "hwmk-spawner.conf";
if (Application.Current.Properties["CommandLineArgs"] != null) {
configFile = ((string[]) Application.Current.Properties["CommandLineArgs"])[0];
}
tryLoadConfig(configFile);
applyConfigMessage();
}
private void tryLoadConfig(String confFile) {
configLoaded = false;
try {
using (FileStream fs = new FileStream(confFile, FileMode.Open, FileAccess.Read, FileShare.Read)) {
using (StreamReader fr = new StreamReader(fs)) {
config = JObject.Parse(fr.ReadToEnd());
configLoaded = true;
applyConfig();
}
}
} catch (FileNotFoundException) {
configFailReason = "The configuration file, " + confFile + ", could not be found.";
} catch (IOException) {
configFailReason = "An IO failure occured while reading the configuration file, " + confFile;
} catch (JsonReaderException) {
configFailReason = "An error occured while parsing the configuration file, " + confFile;
}
}
private void applyConfigMessage() {
if (configLoaded) {
txbInfo.Text = HeaderPrefix + " Configuration loaded. " + HeaderSuffix;
} else {
txbInfo.Text = HeaderPrefix + " Configuration not loaded. " + HeaderSuffix + "\n" + configFailReason;
}
}
private void applyConfigToTextBox(string conf, TextBox box) {
if (configLoaded && config.SelectToken(conf) != null) {
box.Text = (string) config.SelectToken(conf);
}
}
private void applyConfigToCheckBox(string conf, CheckBox box) {
if (configLoaded && config.SelectToken(conf) != null) {
box.IsChecked = (bool) config.SelectToken(conf);
}
}
private void jumpIfNotEmpty(TextBox from, TextBox to) {
if (from.IsFocused && from.Text != "") {
to.Focus();
}
}
private void applyPackageSelection(string confPrefix) {
applyConfigToCheckBox(confPrefix + ".siunitx", chkPackageSiunitx);
applyConfigToCheckBox(confPrefix + ".tikz", chkPackageTikz);
applyConfigToCheckBox(confPrefix + ".circuitikz", chkPackageCircuitikz);
applyConfigToCheckBox(confPrefix + ".automata", chkPackageAutomata);
applyConfigToCheckBox(confPrefix + ".shapes", chkPackageShapes);
applyConfigToCheckBox(confPrefix + ".backgrounds", chkPackageBackgrounds);
applyConfigToCheckBox(confPrefix + ".listings", chkPackageListings);
applyConfigToCheckBox(confPrefix + ".matlab_listings", chkPackageMatlabListings);
applyConfigToCheckBox(confPrefix + ".graphicx", chkPackageGraphicx);
applyConfigToCheckBox(confPrefix + ".epstopdf", chkPackageEpstopdf);
applyConfigToCheckBox(confPrefix + ".scalefig", chkPackageScalefig);
applyConfigToCheckBox(confPrefix + ".float", chkPackageFloat);
applyConfigToCheckBox(confPrefix + ".wrapfig", chkPackageWrapfig);
}
private void applyConfig() {
applyConfigToTextBox("DefaultHomeworkDirectory", txtHomeworkDirectory);
applyConfigToTextBox("DefaultAuthorName", txtAuthorName);
applyConfigToCheckBox("CloseOnSpawn", chkCloseOnSpawn);
applyConfigToCheckBox("OpenEditorOnSpawn", chkOpenEditor);
applyConfigToCheckBox("StripComments", chkStripComments);
applyPackageSelection("DefaultPackages");
}
private void applyConfigForClass(string code) {
applyPackageSelection("ClassSpecs." + code + ".PackageOptions");
applyConfigToTextBox("ClassSpecs." + code + ".ClassName", txtClassName);
applyConfigToTextBox("ClassSpecs." + code + ".InstructorName", txtInstructorName);
}
private string extractClassCode(string code) {
if (code.Contains("-"))
return code.Substring(0, code.LastIndexOf('-'));
else
return code;
}
private string extractClassSection(string code) {
if (code.Contains("-"))
return code.Substring(code.LastIndexOf('-') + 1);
else
return "";
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
txtHomeworkDirectory.Focus();
jumpIfNotEmpty(txtHomeworkDirectory, txtAuthorName);
jumpIfNotEmpty(txtAuthorName, txtClassCode);
}
private void txtClassCode_LostFocus(object sender, RoutedEventArgs e) {
applyConfigForClass(extractClassCode(txtClassCode.Text));
// TODO - Figure out why this auto-jump isn't working.
jumpIfNotEmpty(txtClassName, txtInstructorName);
jumpIfNotEmpty(txtInstructorName, txtHomeworkTitle);
}
private void spawnHomework() {
string result = HomeworkSpawner.Spawn(txtHomeworkDirectory.Text, "template.v2.tex", new Dictionary<string, string> {
{"AuthorName", txtAuthorName.Text},
{"ClassCode", extractClassCode(txtClassCode.Text)},
{"ClassSection", extractClassSection(txtClassCode.Text)},
{"ClassName", txtClassName.Text},
{"InstructorName", txtInstructorName.Text},
{"HomeworkTitle", txtHomeworkTitle.Text},
{"AssignDate", txtAssignDate.Text},
{"DueDate", txtDueDate.Text},
{"Problems", txtProblems.Text}
}, new Dictionary<string, bool> {
{"siunitx", (bool) chkPackageSiunitx.IsChecked},
{"tikz", (bool) chkPackageTikz.IsChecked},
{"circuitikz", (bool) chkPackageTikz.IsChecked && (bool) chkPackageCircuitikz.IsChecked},
{"automata", (bool) chkPackageTikz.IsChecked && (bool) chkPackageAutomata.IsChecked},
{"shapes", (bool) chkPackageTikz.IsChecked && (bool) chkPackageShapes.IsChecked},
{"backgrounds", (bool) chkPackageTikz.IsChecked && (bool) chkPackageBackgrounds.IsChecked},
{"listings", (bool) chkPackageListings.IsChecked},
{"matlab_listings", (bool) chkPackageListings.IsChecked && (bool) chkPackageMatlabListings.IsChecked},
{"graphicx", (bool) chkPackageGraphicx.IsChecked},
{"epstopdf", (bool) chkPackageGraphicx.IsChecked && (bool) chkPackageEpstopdf.IsChecked},
{"scalefig", (bool) chkPackageGraphicx.IsChecked && (bool) chkPackageScalefig.IsChecked},
{"float", (bool) chkPackageGraphicx.IsChecked && (bool) chkPackageFloat.IsChecked},
{"wrapfig", (bool) chkPackageGraphicx.IsChecked && (bool) chkPackageWrapfig.IsChecked},
// Other options.
{"OpenEditor", (bool) chkOpenEditor.IsChecked},
{"StripComments", (bool) chkStripComments.IsChecked}
});
if ((bool) chkCloseOnSpawn.IsChecked && result == "") {
this.Close();
}
if (result != "") {
txbInfo.Text += "\n" + result;
}
}
private void btnSpawn_Click(object sender, RoutedEventArgs e) {
spawnHomework();
}
private void txt_PreviewKeyUp(object sender, KeyEventArgs e) {
if (e.Key == Key.Enter) {
spawnHomework();
}
}
}
}