ShareReadability
SettingsWindow.xaml.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using System.IO;
3 using System.Windows;
4 using Microsoft.Win32;
7 
8 namespace ShareReadability
9 {
10  /// @file SettingsWindow.xaml.cs
11  /// <inheritdoc cref="Window" />
12  /// <summary>
13  /// Interaction logic for SettingsWindow.xaml
14  /// </summary>
15  public partial class SettingsWindow : Window
16  {
17  public Settings Settings { get; set; }
18  private readonly List<CustomSlider> _sliders;
19  private string _dictionaryPath;
20  private double _sum;
21 
22  /// <inheritdoc />
23  /// <summary>
24  /// Constructor of the settings window.
25  /// </summary>
26  public SettingsWindow()
27  {
28  InitializeComponent();
29  _sliders = new List<CustomSlider> {SliderAwl, SliderSl, SliderVc, SliderNf, SliderSsc};
30  }
31 
32  /// <summary>
33  /// Initializes the sliders.
34  /// </summary>
35  public void InitializeSlider()
36  {
38  SliderAwl.Value = Settings.AverageWordLengthWeight;
39  SliderSl.Value = Settings.SentenceLengthWeight;
40  SliderVc.Value = Settings.VocabularyComplexityWeight;
41  SliderNf.Value = Settings.NominalFormsWeight;
42  SliderSsc.Value = Settings.SentenceStructureComplexityWeight;
44  _dictionaryPath = Settings.DictionaryPath;
45  TextBlockDictionaryPath.Text = _dictionaryPath;
46  }
47 
48  /// <summary>
49  /// Closes the settings window and sets the values.
50  /// </summary>
51  /// <param name="sender">The sender.</param>
52  /// <param name="e">The event.</param>
53  private void ButtonOk_Click(object sender, RoutedEventArgs e)
54  {
55  // Set values from GUI
56  Settings.DictionaryPath = _dictionaryPath;
57  Settings.Dictionary = new List<string>(File.ReadAllLines(Settings.DictionaryPath));
58 
59  Settings.AverageWordLengthWeight = (float)SliderAwl.Value;
60  Settings.SentenceLengthWeight = (float)SliderSl.Value;
61  Settings.VocabularyComplexityWeight = (float)SliderVc.Value;
62  Settings.NominalFormsWeight = (float)SliderNf.Value;
63  Settings.SentenceStructureComplexityWeight = (float)SliderSsc.Value;
64 
65  Close();
66  }
67 
68  /// <summary>
69  /// Closes the settings window without passing the set values.
70  /// </summary>
71  /// <param name="sender">The sender.</param>
72  /// <param name="e">The event.</param>
73  private void ButtonCancel_Click(object sender, RoutedEventArgs e)
74  {
75  Close();
76  }
77 
78  /// <summary>
79  /// Sets all sliders active.
80  /// </summary>
81  private void SetSliderActive()
82  {
83  foreach (var slider in _sliders)
84  {
85  slider.DontRunHandler = false;
86  }
87  }
88 
89  /// <summary>
90  /// Sets all sliders inactive.
91  /// </summary>
92  private void SetSliderInActive()
93  {
94  foreach (var slider in _sliders)
95  {
96  slider.DontRunHandler = true;
97  }
98  }
99 
100  /// <summary>
101  /// Normalizes all slider values with their total sum, so they sum up to 1.0.
102  /// </summary>
103  /// <param name="sender">The sender.</param>
104  /// <param name="e">The event.</param>
105  private void Slider_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
106  {
107  var currentSlider = sender as CustomSlider;
108  if (currentSlider != null && currentSlider.DontRunHandler) return;
110 
111  _sum = 0.0;
112  foreach (var customSlider in _sliders)
113  {
114  _sum += customSlider.Value;
115  }
116 
117  foreach (var customSlider in _sliders)
118  {
119  customSlider.Value /= _sum;
120  }
121 
122  SetSliderActive();
123  }
124 
125  /// <summary>
126  /// Loads a new dictionary, if the actions is not canceled.
127  /// </summary>
128  /// <param name="sender">The sender.</param>
129  /// <param name="e">The event.</param>
130  private void ButtonDictionary_OnClick(object sender, RoutedEventArgs e)
131  {
132  var openFileDialog = new OpenFileDialog
133  {
134  Multiselect = false,
135  Filter = @"Text files(*.txt)|*.txt|All files(*.*)|*.*"
136  };
137 
138  var combinedPath = Path.Combine(Directory.GetCurrentDirectory(), "data/dictionary");
139  openFileDialog.InitialDirectory = Path.GetFullPath(combinedPath);
140 
141  if (openFileDialog.ShowDialog() == true)
142  {
143  var filename = Path.GetFileName(openFileDialog.FileName);
144  TextBlockDictionaryPath.Text = filename;
145  _dictionaryPath = openFileDialog.FileName;
146  }
147  }
148  }
149 }
void InitializeSlider()
Initializes the sliders.
void ButtonDictionary_OnClick(object sender, RoutedEventArgs e)
Loads a new dictionary, if the actions is not canceled.
SettingsWindow()
Constructor of the settings window.
void ButtonCancel_Click(object sender, RoutedEventArgs e)
Closes the settings window without passing the set values.
void SetSliderInActive()
Sets all sliders inactive.
void SetSliderActive()
Sets all sliders active.
void Slider_OnValueChanged(object sender, RoutedPropertyChangedEventArgs< double > e)
Normalizes all slider values with their total sum, so they sum up to 1.0.
void ButtonOk_Click(object sender, RoutedEventArgs e)
Closes the settings window and sets the values.